Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
14 changes: 0 additions & 14 deletions packages/cli/src/utils/display.ts
Original file line number Diff line number Diff line change
@@ -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 <command> [options]
constructive <command> [options]
Expand Down Expand Up @@ -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);
}
6 changes: 2 additions & 4 deletions pgpm/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -43,8 +42,7 @@ export const options: Partial<CLIOptions> = {

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);
}
Expand Down
120 changes: 0 additions & 120 deletions pgpm/cli/src/utils/argv.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Logger } from '@pgpmjs/logger';
import { ParsedArgs } from 'minimist';

const log = new Logger('argv-utils');

export const extractFirst = (argv: Partial<ParsedArgs>) => {
const first = argv._?.[0];
const newArgv = {
Expand All @@ -11,120 +8,3 @@ export const extractFirst = (argv: Partial<ParsedArgs>) => {
};
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<ParsedArgs>): 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;
}
14 changes: 0 additions & 14 deletions pgpm/cli/src/utils/display.ts
Original file line number Diff line number Diff line change
@@ -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 <command> [options]

Expand Down Expand Up @@ -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);
}