feat(cli): add --help and --version options to main command (#35)

This commit is contained in:
Joakim Carlstein 2023-12-14 10:49:54 +01:00 committed by GitHub
parent dac43ce95d
commit 960ce08674
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 27 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/cli': minor
---
Add --help and --version options to main command

View file

@ -129,14 +129,14 @@ Arguments:
Options: Options:
-h, --help Show this help message and exit -h, --help Show this help message and exit
-d, --directory The directory where the migration files are located (required) -d, --directory The directory where the migration files are located (required)
-r, --reporter The reporter to use for reporting the migration file creation progress -r, --reporter The reporter to use for reporting the migration file creation progress
-p, --plugin The plugin(s) to use (can be specified multiple times) -p, --plugin The plugin(s) to use (can be specified multiple times)
-t, --template A template file to use as contents for the new migration file -t, --template A template file to use as contents for the new migration file
(if the extension option is not provided the template file's extension will be used) (if the extension option is not provided the template file's extension will be used)
-e, --extension The extension to use for the new migration file -e, --extension The extension to use for the new migration file
(if no template or plugin is provided an empty migration file will be created with the given extension) (if no template or plugin is provided an empty migration file will be created with the given extension)
One of the --template, --extension or the --plugin options must be specified One of the --template, --extension or the --plugin options must be specified
@ -283,12 +283,12 @@ Arguments:
Options: Options:
-h, --help Show this help message and exit -h, --help Show this help message and exit
-d, --directory The directory where the migration files are located (required) -d, --directory The directory where the migration files are located (required)
-r, --reporter The reporter to use for reporting the removal process -r, --reporter The reporter to use for reporting the removal process
-s, --storage The storage to use to get the migration history (required) -s, --storage The storage to use to get the migration history (required)
-f, --force Force removal of the migration history entry even if the migration file does not exist -f, --force Force removal of the migration history entry even if the migration file does not exist
or it's in a non-failed state or it's in a non-failed state
Examples: Examples:
@ -326,17 +326,28 @@ const commands: Record<string, Action> = {
new: newMigration, new: newMigration,
}; };
const command = process.argv[2]?.toLowerCase(); const main: Action = async (args) => {
const action = command ? commands[command] : undefined; const { values, positionals } = parseArgs({
args,
options: {
help: {
type: 'boolean',
short: 'h',
},
version: {
type: 'boolean',
short: 'v',
},
},
allowPositionals: true,
});
if (!action) { const usage = `Usage: emigrate <options>/<command>
if (command) {
console.error(`Unknown command: ${command}\n`);
} else {
console.error('No command specified\n');
}
console.log(`Usage: emigrate <command> Options:
-h, --help Show this help message and exit
-v, --version Print version number and exit
Commands: Commands:
@ -344,12 +355,34 @@ Commands:
new Create a new migration file new Create a new migration file
list List all migrations and their status list List all migrations and their status
remove Remove entries from the migration history remove Remove entries from the migration history
`); `;
process.exit(1);
} const command = positionals[0]?.toLowerCase();
const action = command ? commands[command] : undefined;
if (!action) {
if (command) {
console.error(`Unknown command: ${command}\n`);
} else if (values.version) {
const { getPackageInfo } = await import('./get-package-info.js');
const { version } = await getPackageInfo();
console.log(version);
process.exitCode = 0;
return;
} else if (!values.help) {
console.error('No command specified\n');
}
console.log(usage);
process.exitCode = 1;
return;
}
await action(process.argv.slice(3));
};
try { try {
await action(process.argv.slice(3)); await main(process.argv.slice(2));
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
console.error(error.message); console.error(error.message);

View file

@ -0,0 +1,28 @@
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
type PackageInfo = {
version: string;
};
export const getPackageInfo = async () => {
const packageInfoPath = fileURLToPath(new URL('../package.json', import.meta.url));
try {
const content = await fs.readFile(packageInfoPath, 'utf8');
const packageJson: unknown = JSON.parse(content);
if (
typeof packageJson === 'object' &&
packageJson &&
'version' in packageJson &&
typeof packageJson.version === 'string'
) {
return packageJson as PackageInfo;
}
} catch {
// ignore
}
throw new Error(`Could not read package info from: ${packageInfoPath}`);
};