Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/infrastructure/application/cli/program.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Argument, Command, InvalidOptionArgumentError, Option} from '@commander-js/extra-typings';
import type {JsonPrimitive, JsonValue} from '@croct/json';
import {realpathSync} from 'fs';
import {realpathSync, statSync} from 'fs';
import {ApiKey} from '@croct/sdk/apiKey';
import {Token} from '@croct/sdk/token';
import {Cli} from '@/infrastructure/application/cli/cli';
Expand All @@ -21,11 +21,34 @@ function createProgram(config: Configuration): typeof program {
.description('Manage your Croct projects')
.enablePositionalOptions()
.option('--cwd <path>', 'The working directory.', path => {
let directory: string;
let isDirectory: boolean;

try {
return realpathSync(path);
} catch {
throw new InvalidOptionArgumentError('The path does not exist.');
directory = realpathSync(path);
isDirectory = statSync(directory).isDirectory();
} catch (error) {
const code = error instanceof Error && 'code' in error ? error.code : null;

switch (code) {
case 'ENOENT':
case 'ENOTDIR':
throw new InvalidOptionArgumentError('The path does not exist.');

case 'EACCES':
case 'EPERM':
throw new InvalidOptionArgumentError('The path is not accessible.');

default:
throw new InvalidOptionArgumentError('The path cannot be resolved.');
}
}
Comment on lines 27 to 45

if (!isDirectory) {
throw new InvalidOptionArgumentError('The path is not a directory.');
}

return directory;
})
.addOption(
new Option('--api-key <key>', 'The API key to use for authentication.')
Expand Down Expand Up @@ -457,6 +480,9 @@ export async function run(args: string[] = process.argv, welcome = true): Promis
? undefined
: new URL(options.registry),
configurationFile: options.config,
directories: {
current: options.cwd,
},
});

const template = getTemplate(invocation.args);
Expand Down
Loading