diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 573e29ff6..120f76063 100755 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,13 +1,11 @@ #!/usr/bin/env node -import { readFileSync } from 'fs'; +import { findAndRequirePackageJson } from 'find-and-require-package-json'; import { CLI, CLIOptions } from 'inquirerer'; -import { join } from 'path'; import { commands } from './commands'; if (process.argv.includes('--version') || process.argv.includes('-v')) { - const pkgPath = join(__dirname, 'package.json'); - const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')); + const pkg = findAndRequirePackageJson(__dirname); console.log(pkg.version); process.exit(0); } diff --git a/packages/cli/src/utils/display.ts b/packages/cli/src/utils/display.ts index 57c4f2dac..f24b8a984 100644 --- a/packages/cli/src/utils/display.ts +++ b/packages/cli/src/utils/display.ts @@ -1,13 +1,3 @@ -import { findAndRequirePackageJson } from 'find-and-require-package-json'; -import yanse from 'yanse'; - -// Function to display the version information -export function displayVersion() { - const pkg = findAndRequirePackageJson(__dirname); - console.log(yanse.green(`Name: ${pkg.name}`)); - console.log(yanse.blue(`Version: ${pkg.version}`)); -} - export const usageText = ` Usage: cnc [options] constructive [options] @@ -58,7 +48,3 @@ export const usageText = ` cnc server --port 8080 Start server on port 8080 cnc init workspace Initialize new workspace `; - -export function displayUsage() { - console.log(usageText); -} diff --git a/pgpm/cli/src/index.ts b/pgpm/cli/src/index.ts index c9096674a..1dc50fb83 100644 --- a/pgpm/cli/src/index.ts +++ b/pgpm/cli/src/index.ts @@ -1,7 +1,6 @@ #!/usr/bin/env node -import { readFileSync } from 'fs'; +import { findAndRequirePackageJson } from 'find-and-require-package-json'; import { CLI, CLIOptions } from 'inquirerer'; -import { join } from 'path'; import { commands, createPgpmCommandMap } from './commands'; export { createInitUsageText } from './commands/init'; @@ -43,8 +42,7 @@ export const options: Partial = { if (require.main === module) { if (process.argv.includes('--version') || process.argv.includes('-v')) { - const pkgPath = join(__dirname, 'package.json'); - const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')); + const pkg = findAndRequirePackageJson(__dirname); console.log(pkg.version); process.exit(0); } diff --git a/pgpm/cli/src/utils/argv.ts b/pgpm/cli/src/utils/argv.ts index 985e90aec..2a2fb40a2 100644 --- a/pgpm/cli/src/utils/argv.ts +++ b/pgpm/cli/src/utils/argv.ts @@ -1,8 +1,5 @@ -import { Logger } from '@pgpmjs/logger'; import { ParsedArgs } from 'minimist'; -const log = new Logger('argv-utils'); - export const extractFirst = (argv: Partial) => { const first = argv._?.[0]; const newArgv = { @@ -11,120 +8,3 @@ export const extractFirst = (argv: Partial) => { }; return { first, newArgv }; }; - -/** - * Common CLI argument validation and processing utilities - */ - -export interface ValidatedArgv extends ParsedArgs { - cwd: string; - database?: string; - package?: string; - to?: string; - recursive?: boolean; - yes?: boolean; - tx?: boolean; - fast?: boolean; - logOnly?: boolean; - createdb?: boolean; - usePlan?: boolean; - cache?: boolean; - drop?: boolean; - all?: boolean; - summary?: boolean; - help?: boolean; - h?: boolean; -} - -/** - * Validates and normalizes common CLI arguments - */ -export function validateCommonArgs(argv: Partial): ValidatedArgv { - const validated: ValidatedArgv = { - ...argv, - cwd: argv.cwd || process.cwd(), - _: argv._ || [] - }; - - const booleanFlags = ['recursive', 'yes', 'tx', 'fast', 'logOnly', 'createdb', 'usePlan', 'cache', 'drop', 'all', 'summary', 'help', 'h']; - - for (const flag of booleanFlags) { - if (argv[flag] !== undefined && typeof argv[flag] !== 'boolean') { - log.warn(`--${flag} flag should be boolean, converting to true`); - validated[flag] = true; - } - } - - const stringFlags = ['package', 'to', 'database']; - - for (const flag of stringFlags) { - if (argv[flag] !== undefined && typeof argv[flag] !== 'string') { - log.warn(`--${flag} should be a string, converting`); - validated[flag] = String(argv[flag]); - } - } - - return validated; -} - -/** - * Checks if required flags are provided when certain conditions are met - */ -export function validateFlagDependencies(argv: ValidatedArgv): void { - if (argv.to && !argv.package && !argv.recursive) { - log.warn('--to flag provided without --package or --recursive. Target may not work as expected.'); - } - - if (argv.package && argv.recursive) { - if (argv.package.includes(':')) { - log.warn('--package should not contain ":" when using --recursive. Use --to for target specification.'); - } - } -} - -/** - * Logs the effective CLI arguments for debugging - */ -export function logEffectiveArgs(argv: ValidatedArgv, commandName: string): void { - const relevantArgs = { - cwd: argv.cwd, - database: argv.database, - package: argv.package, - to: argv.to, - recursive: argv.recursive, - yes: argv.yes, - tx: argv.tx, - fast: argv.fast, - logOnly: argv.logOnly, - createdb: argv.createdb, - usePlan: argv.usePlan, - cache: argv.cache, - drop: argv.drop, - all: argv.all, - summary: argv.summary - }; - - const definedArgs = Object.fromEntries( - Object.entries(relevantArgs).filter(([_, value]) => value !== undefined) - ); - - if (Object.keys(definedArgs).length > 1) { // More than just cwd - log.debug(`${commandName} effective arguments:`, definedArgs); - } -} - -/** - * Constructs a deployment target string from package and to arguments - */ -export function constructTarget(argv: ValidatedArgv, packageName?: string): string | undefined { - if (packageName && argv.to) { - return `${packageName}:${argv.to}`; - } else if (packageName) { - return packageName; - } else if (argv.package && argv.to) { - return `${argv.package}:${argv.to}`; - } else if (argv.package) { - return argv.package; - } - return undefined; -} diff --git a/pgpm/cli/src/utils/display.ts b/pgpm/cli/src/utils/display.ts index ed71cecc0..261df4c69 100644 --- a/pgpm/cli/src/utils/display.ts +++ b/pgpm/cli/src/utils/display.ts @@ -1,13 +1,3 @@ -import { findAndRequirePackageJson } from 'find-and-require-package-json'; -import yanse from 'yanse'; - -// Function to display the version information -export function displayVersion() { - const pkg = findAndRequirePackageJson(__dirname); - console.log(yanse.green(`Name: ${pkg.name}`)); - console.log(yanse.blue(`Version: ${pkg.version}`)); -} - export const usageText = ` Usage: pgpm [options] @@ -57,7 +47,3 @@ export const usageText = ` pgpm init workspace Initialize new workspace pgpm install @pgpm/base32 Install a database module `; - -export function displayUsage() { - console.log(usageText); -}