Skip to content

Commit fbb016c

Browse files
authored
Honor the --cwd option (#154)
1 parent 288c5ef commit fbb016c

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

‎src/infrastructure/application/cli/program.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Argument, Command, InvalidOptionArgumentError, Option} from '@commander-js/extra-typings';
22
import type {JsonPrimitive, JsonValue} from '@croct/json';
3-
import {realpathSync} from 'fs';
3+
import {realpathSync, statSync} from 'fs';
44
import {ApiKey} from '@croct/sdk/apiKey';
55
import {Token} from '@croct/sdk/token';
66
import {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

Comments
 (0)