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,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}`);
};