11import { Argument , Command , InvalidOptionArgumentError , Option } from '@commander-js/extra-typings' ;
22import type { JsonPrimitive , JsonValue } from '@croct/json' ;
3- import { realpathSync } from 'fs' ;
3+ import { realpathSync , statSync } from 'fs' ;
44import { ApiKey } from '@croct/sdk/apiKey' ;
55import { Token } from '@croct/sdk/token' ;
66import { Cli } from '@/infrastructure/application/cli/cli' ;
@@ -21,11 +21,34 @@ function createProgram(config: Configuration): typeof program {
2121 . description ( 'Manage your Croct projects' )
2222 . enablePositionalOptions ( )
2323 . option ( '--cwd <path>' , 'The working directory.' , path => {
24+ let directory : string ;
25+ let isDirectory : boolean ;
26+
2427 try {
25- return realpathSync ( path ) ;
26- } catch {
27- throw new InvalidOptionArgumentError ( 'The path does not exist.' ) ;
28+ directory = realpathSync ( path ) ;
29+ isDirectory = statSync ( directory ) . isDirectory ( ) ;
30+ } catch ( error ) {
31+ const code = error instanceof Error && 'code' in error ? error . code : null ;
32+
33+ switch ( code ) {
34+ case 'ENOENT' :
35+ case 'ENOTDIR' :
36+ throw new InvalidOptionArgumentError ( 'The path does not exist.' ) ;
37+
38+ case 'EACCES' :
39+ case 'EPERM' :
40+ throw new InvalidOptionArgumentError ( 'The path is not accessible.' ) ;
41+
42+ default :
43+ throw new InvalidOptionArgumentError ( 'The path cannot be resolved.' ) ;
44+ }
2845 }
46+
47+ if ( ! isDirectory ) {
48+ throw new InvalidOptionArgumentError ( 'The path is not a directory.' ) ;
49+ }
50+
51+ return directory ;
2952 } )
3053 . addOption (
3154 new Option ( '--api-key <key>' , 'The API key to use for authentication.' )
@@ -457,6 +480,9 @@ export async function run(args: string[] = process.argv, welcome = true): Promis
457480 ? undefined
458481 : new URL ( options . registry ) ,
459482 configurationFile : options . config ,
483+ directories : {
484+ current : options . cwd ,
485+ } ,
460486 } ) ;
461487
462488 const template = getTemplate ( invocation . args ) ;
0 commit comments