feat(cli): add --help and --version options to main command (#35)
This commit is contained in:
parent
dac43ce95d
commit
960ce08674
3 changed files with 93 additions and 27 deletions
5
.changeset/wild-jeans-guess.md
Normal file
5
.changeset/wild-jeans-guess.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@emigrate/cli': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add --help and --version options to main command
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
28
packages/cli/src/get-package-info.ts
Normal file
28
packages/cli/src/get-package-info.ts
Normal 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}`);
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue