Skip to content

Commit 3fb7ca1

Browse files
authored
Merge pull request #466 from constructive-io/cli/cleanup-unused-code
Cli/cleanup unused code
2 parents 8eaef53 + e08a9ec commit 3fb7ca1

File tree

5 files changed

+4
-156
lines changed

5 files changed

+4
-156
lines changed

packages/cli/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#!/usr/bin/env node
2-
import { readFileSync } from 'fs';
2+
import { findAndRequirePackageJson } from 'find-and-require-package-json';
33
import { CLI, CLIOptions } from 'inquirerer';
4-
import { join } from 'path';
54

65
import { commands } from './commands';
76

87
if (process.argv.includes('--version') || process.argv.includes('-v')) {
9-
const pkgPath = join(__dirname, 'package.json');
10-
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
8+
const pkg = findAndRequirePackageJson(__dirname);
119
console.log(pkg.version);
1210
process.exit(0);
1311
}

packages/cli/src/utils/display.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
import { findAndRequirePackageJson } from 'find-and-require-package-json';
2-
import yanse from 'yanse';
3-
4-
// Function to display the version information
5-
export function displayVersion() {
6-
const pkg = findAndRequirePackageJson(__dirname);
7-
console.log(yanse.green(`Name: ${pkg.name}`));
8-
console.log(yanse.blue(`Version: ${pkg.version}`));
9-
}
10-
111
export const usageText = `
122
Usage: cnc <command> [options]
133
constructive <command> [options]
@@ -58,7 +48,3 @@ export const usageText = `
5848
cnc server --port 8080 Start server on port 8080
5949
cnc init workspace Initialize new workspace
6050
`;
61-
62-
export function displayUsage() {
63-
console.log(usageText);
64-
}

pgpm/cli/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
2-
import { readFileSync } from 'fs';
2+
import { findAndRequirePackageJson } from 'find-and-require-package-json';
33
import { CLI, CLIOptions } from 'inquirerer';
4-
import { join } from 'path';
54

65
import { commands, createPgpmCommandMap } from './commands';
76
export { createInitUsageText } from './commands/init';
@@ -43,8 +42,7 @@ export const options: Partial<CLIOptions> = {
4342

4443
if (require.main === module) {
4544
if (process.argv.includes('--version') || process.argv.includes('-v')) {
46-
const pkgPath = join(__dirname, 'package.json');
47-
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
45+
const pkg = findAndRequirePackageJson(__dirname);
4846
console.log(pkg.version);
4947
process.exit(0);
5048
}

pgpm/cli/src/utils/argv.ts

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { Logger } from '@pgpmjs/logger';
21
import { ParsedArgs } from 'minimist';
32

4-
const log = new Logger('argv-utils');
5-
63
export const extractFirst = (argv: Partial<ParsedArgs>) => {
74
const first = argv._?.[0];
85
const newArgv = {
@@ -11,120 +8,3 @@ export const extractFirst = (argv: Partial<ParsedArgs>) => {
118
};
129
return { first, newArgv };
1310
};
14-
15-
/**
16-
* Common CLI argument validation and processing utilities
17-
*/
18-
19-
export interface ValidatedArgv extends ParsedArgs {
20-
cwd: string;
21-
database?: string;
22-
package?: string;
23-
to?: string;
24-
recursive?: boolean;
25-
yes?: boolean;
26-
tx?: boolean;
27-
fast?: boolean;
28-
logOnly?: boolean;
29-
createdb?: boolean;
30-
usePlan?: boolean;
31-
cache?: boolean;
32-
drop?: boolean;
33-
all?: boolean;
34-
summary?: boolean;
35-
help?: boolean;
36-
h?: boolean;
37-
}
38-
39-
/**
40-
* Validates and normalizes common CLI arguments
41-
*/
42-
export function validateCommonArgs(argv: Partial<ParsedArgs>): ValidatedArgv {
43-
const validated: ValidatedArgv = {
44-
...argv,
45-
cwd: argv.cwd || process.cwd(),
46-
_: argv._ || []
47-
};
48-
49-
const booleanFlags = ['recursive', 'yes', 'tx', 'fast', 'logOnly', 'createdb', 'usePlan', 'cache', 'drop', 'all', 'summary', 'help', 'h'];
50-
51-
for (const flag of booleanFlags) {
52-
if (argv[flag] !== undefined && typeof argv[flag] !== 'boolean') {
53-
log.warn(`--${flag} flag should be boolean, converting to true`);
54-
validated[flag] = true;
55-
}
56-
}
57-
58-
const stringFlags = ['package', 'to', 'database'];
59-
60-
for (const flag of stringFlags) {
61-
if (argv[flag] !== undefined && typeof argv[flag] !== 'string') {
62-
log.warn(`--${flag} should be a string, converting`);
63-
validated[flag] = String(argv[flag]);
64-
}
65-
}
66-
67-
return validated;
68-
}
69-
70-
/**
71-
* Checks if required flags are provided when certain conditions are met
72-
*/
73-
export function validateFlagDependencies(argv: ValidatedArgv): void {
74-
if (argv.to && !argv.package && !argv.recursive) {
75-
log.warn('--to flag provided without --package or --recursive. Target may not work as expected.');
76-
}
77-
78-
if (argv.package && argv.recursive) {
79-
if (argv.package.includes(':')) {
80-
log.warn('--package should not contain ":" when using --recursive. Use --to for target specification.');
81-
}
82-
}
83-
}
84-
85-
/**
86-
* Logs the effective CLI arguments for debugging
87-
*/
88-
export function logEffectiveArgs(argv: ValidatedArgv, commandName: string): void {
89-
const relevantArgs = {
90-
cwd: argv.cwd,
91-
database: argv.database,
92-
package: argv.package,
93-
to: argv.to,
94-
recursive: argv.recursive,
95-
yes: argv.yes,
96-
tx: argv.tx,
97-
fast: argv.fast,
98-
logOnly: argv.logOnly,
99-
createdb: argv.createdb,
100-
usePlan: argv.usePlan,
101-
cache: argv.cache,
102-
drop: argv.drop,
103-
all: argv.all,
104-
summary: argv.summary
105-
};
106-
107-
const definedArgs = Object.fromEntries(
108-
Object.entries(relevantArgs).filter(([_, value]) => value !== undefined)
109-
);
110-
111-
if (Object.keys(definedArgs).length > 1) { // More than just cwd
112-
log.debug(`${commandName} effective arguments:`, definedArgs);
113-
}
114-
}
115-
116-
/**
117-
* Constructs a deployment target string from package and to arguments
118-
*/
119-
export function constructTarget(argv: ValidatedArgv, packageName?: string): string | undefined {
120-
if (packageName && argv.to) {
121-
return `${packageName}:${argv.to}`;
122-
} else if (packageName) {
123-
return packageName;
124-
} else if (argv.package && argv.to) {
125-
return `${argv.package}:${argv.to}`;
126-
} else if (argv.package) {
127-
return argv.package;
128-
}
129-
return undefined;
130-
}

pgpm/cli/src/utils/display.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
import { findAndRequirePackageJson } from 'find-and-require-package-json';
2-
import yanse from 'yanse';
3-
4-
// Function to display the version information
5-
export function displayVersion() {
6-
const pkg = findAndRequirePackageJson(__dirname);
7-
console.log(yanse.green(`Name: ${pkg.name}`));
8-
console.log(yanse.blue(`Version: ${pkg.version}`));
9-
}
10-
111
export const usageText = `
122
Usage: pgpm <command> [options]
133
@@ -57,7 +47,3 @@ export const usageText = `
5747
pgpm init workspace Initialize new workspace
5848
pgpm install @pgpm/base32 Install a database module
5949
`;
60-
61-
export function displayUsage() {
62-
console.log(usageText);
63-
}

0 commit comments

Comments
 (0)