Skip to content

Commit e202c69

Browse files
authored
AAE-39314 Break dependencies on the "commander" lib in the CLI (#11299)
1 parent 1c8af04 commit e202c69

File tree

11 files changed

+507
-137
lines changed

11 files changed

+507
-137
lines changed

lib/cli/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@ adf-cli <command> --help
1818

1919
## Developing
2020

21-
Link the project as a global tool
21+
### Quick Setup
22+
23+
Link the project as a global tool (builds and links in one command):
2224

2325
```bash
24-
npm link
26+
npm run link
2527
```
2628

29+
This will build the CLI and make the `adf-cli` command available globally on your system.
30+
31+
When you're done developing:
32+
33+
```bash
34+
npm run unlink
35+
```
36+
37+
### Development Mode
38+
2739
Build the tool in the **develop** mode (automatically watches for changes and rebuilds the commands):
2840

2941
```bash
@@ -38,6 +50,21 @@ DEVELOP=true adf-cli <command>
3850

3951
In develop mode, the CLI takes the prebuilt scripts from the dist folder.
4052

53+
### Manual Workflow
54+
55+
If you need more control, you can manually build and link:
56+
57+
```bash
58+
# Build the CLI
59+
npm run build
60+
61+
# Or build with full distribution (includes copying resources)
62+
npm run dist
63+
64+
# Link from the dist directory
65+
cd ../../dist/libs/cli && npm link
66+
```
67+
4168
## Commands
4269

4370
| **Commands** | **Description** |

lib/cli/bin/adf-cli

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
const minimist = require('minimist');
2+
const { parseArgs } = require('node:util');
33
const { resolve, join } = require('node:path');
44
const { readFileSync, existsSync } = require('node:fs');
55
const { argv, exit, env, cwd } = require('node:process');
@@ -10,10 +10,22 @@ function printHelp() {
1010
console.log(`${name} v${version}`);
1111
}
1212

13-
const args = minimist(argv.slice(2), {
14-
boolean: ['verbose']
13+
const { values, positionals } = parseArgs({
14+
args: argv.slice(2),
15+
options: {
16+
verbose: {
17+
type: 'boolean'
18+
}
19+
},
20+
allowPositionals: true,
21+
strict: false
1522
});
1623

24+
const args = {
25+
...values,
26+
_: positionals
27+
};
28+
1729
if (args._.length === 0) {
1830
printHelp();
1931
exit(1);

lib/cli/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
"scripts": {
1818
"build": "tsc -p tsconfig.json",
1919
"develop": "tsc -p tsconfig.json --watch",
20-
"dist": "rm -rf ../../dist/libs/cli && npm run build && cp -R ./bin ../../dist/libs/cli && cp -R ./resources ../../dist/libs/cli && cp -R ./templates ../../dist/libs/cli && cp ./package.json ../../dist/libs/cli"
20+
"dist": "rm -rf ../../dist/libs/cli && npm run build && cp -R ./bin ../../dist/libs/cli && cp -R ./resources ../../dist/libs/cli && cp -R ./templates ../../dist/libs/cli && cp ./package.json ../../dist/libs/cli",
21+
"link": "npm run dist && cd ../../dist/libs/cli && npm link",
22+
"unlink": "cd ../../dist/libs/cli && npm unlink"
2123
},
2224
"dependencies": {
2325
"@alfresco/js-api": ">=8.4.0-0",
24-
"commander": "^6.2.1",
2526
"ejs": "^3.1.10",
2627
"license-checker": "^25.0.1",
2728
"node-fetch": "^2.7.0",
28-
"rxjs": "7.8.2",
2929
"shelljs": "^0.10.0",
3030
"spdx-license-list": "^5.0.0"
3131
},

lib/cli/scripts/audit.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import * as ejs from 'ejs';
2222
import * as path from 'path';
2323
import * as fs from 'fs';
2424
import { argv, exit } from 'node:process';
25-
import { Command } from 'commander';
26-
27-
const program = new Command();
25+
import { parseArgs } from 'node:util';
2826

2927
interface AuditCommandArgs {
3028
package?: string;
@@ -39,19 +37,39 @@ interface AuditCommandArgs {
3937
* @returns void
4038
*/
4139
export default function main(_args: string[], workingDir: string) {
42-
program
43-
.description('Generate an audit report')
44-
.usage('audit [options]')
45-
.option('-p, --package <path>', 'Path to package file (default: package.json in working directory)')
46-
.option('-d, --outDir <dir>', 'Ouput directory (default: working directory)')
47-
.parse(argv);
48-
4940
if (argv.includes('-h') || argv.includes('--help')) {
50-
program.outputHelp();
41+
console.log(`
42+
Usage: audit [options]
43+
44+
Generate an audit report
45+
46+
Options:
47+
-p, --package <path> Path to package file (default: package.json in working directory)
48+
-d, --outDir <dir> Output directory (default: working directory)
49+
-h, --help Display help for command
50+
`);
5151
exit(0);
5252
}
5353

54-
const options: AuditCommandArgs = program.opts();
54+
const { values } = parseArgs({
55+
args: argv.slice(2),
56+
options: {
57+
package: {
58+
type: 'string',
59+
short: 'p'
60+
},
61+
outDir: {
62+
type: 'string',
63+
short: 'd'
64+
}
65+
},
66+
allowPositionals: true
67+
});
68+
69+
const options: AuditCommandArgs = {
70+
package: values.package as string | undefined,
71+
outDir: values.outDir as string | undefined
72+
};
5573

5674
let packagePath = path.resolve(workingDir, 'package.json');
5775

lib/cli/scripts/changelog.ts

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
/* eslint-disable @typescript-eslint/naming-convention */
2121

2222
import { argv, exit } from 'node:process';
23+
import { parseArgs } from 'node:util';
2324
import * as shell from 'shelljs';
2425
import * as path from 'path';
25-
import { Command } from 'commander';
2626
import { logger } from './logger';
2727
import * as fs from 'fs';
2828
import * as ejs from 'ejs';
2929

30-
const program = new Command();
31-
3230
interface Commit {
3331
hash: string;
3432
author: string;
@@ -151,28 +149,74 @@ function commitAuthorAllowed(commit: Commit, authorFilter: string): boolean {
151149
* @returns void
152150
*/
153151
export default function main(_args: string[], workingDir: string) {
154-
program
155-
.description('Generate changelog report for two branches of git repository')
156-
.version('0.0.1', '-v, --version')
157-
.usage('changelog [options]')
158-
.option('-r, --range <range>', 'Commit range, e.g. origin/master..develop', 'origin/master..develop')
159-
.option('-d, --dir <dir>', 'Working directory (default: working directory)')
160-
.option('-m, --max <number>', 'Limit the number of commits to output')
161-
.option('-o, --output <dir>', 'Output directory, will use console output if not defined')
162-
.option('--skip <number>', 'Skip number commits before starting to show the commit output')
163-
.option('-f, --format <format>', 'Output format (md, html)', 'md')
164-
.option('-e --exclude <string>', 'Exclude authors from the output, comma-delimited list')
165-
.parse(argv);
166-
167152
if (argv.includes('-h') || argv.includes('--help')) {
168-
program.outputHelp();
153+
console.log(`
154+
Usage: changelog [options]
155+
156+
Generate changelog report for two branches of git repository
157+
158+
Options:
159+
-v, --version Output the version number
160+
-r, --range <range> Commit range, e.g. origin/master..develop (default: "origin/master..develop")
161+
-d, --dir <dir> Working directory (default: working directory)
162+
-m, --max <number> Limit the number of commits to output
163+
-o, --output <dir> Output directory, will use console output if not defined
164+
--skip <number> Skip number commits before starting to show the commit output
165+
-f, --format <format> Output format (md, html) (default: "md")
166+
-e, --exclude <string> Exclude authors from the output, comma-delimited list
167+
-h, --help Display help for command
168+
`);
169169
exit(0);
170170
}
171171

172-
const options = program.opts();
172+
if (argv.includes('-v') || argv.includes('--version')) {
173+
console.log('0.0.1');
174+
exit(0);
175+
}
176+
177+
const { values } = parseArgs({
178+
args: argv.slice(2),
179+
options: {
180+
range: {
181+
type: 'string',
182+
short: 'r',
183+
default: 'origin/master..develop'
184+
},
185+
dir: {
186+
type: 'string',
187+
short: 'd'
188+
},
189+
max: {
190+
type: 'string',
191+
short: 'm'
192+
},
193+
output: {
194+
type: 'string',
195+
short: 'o'
196+
},
197+
skip: {
198+
type: 'string'
199+
},
200+
format: {
201+
type: 'string',
202+
short: 'f',
203+
default: 'md'
204+
},
205+
exclude: {
206+
type: 'string',
207+
short: 'e'
208+
}
209+
},
210+
allowPositionals: true
211+
});
173212

174-
const dir = path.resolve(options.dir || workingDir);
175-
const { range, skip, max, format, output, exclude } = options;
213+
const dir = path.resolve((values.dir as string) || workingDir);
214+
const range = values.range as string;
215+
const skip = values.skip ? parseInt(values.skip as string, 10) : undefined;
216+
const max = values.max ? parseInt(values.max as string, 10) : undefined;
217+
const format = values.format as string;
218+
const output = values.output as string | undefined;
219+
const exclude = values.exclude as string | undefined;
176220

177221
const remote = getRemote(dir);
178222

lib/cli/scripts/check-cs-env.ts

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { AlfrescoApi /*, NodesApi, UploadApi*/ } from '@alfresco/js-api';
1919
import { argv, exit } from 'node:process';
20-
import { Command } from 'commander';
20+
import { parseArgs } from 'node:util';
2121
import { logger } from './logger';
2222

2323
interface CheckCsEnvArgs {
@@ -27,8 +27,6 @@ interface CheckCsEnvArgs {
2727
time?: number;
2828
retry?: number;
2929
}
30-
31-
const program = new Command();
3230
const MAX_RETRY = 3;
3331
const TIMEOUT = 20000;
3432

@@ -39,18 +37,63 @@ let alfrescoJsApi: AlfrescoApi;
3937
* Check CS environment command
4038
*/
4139
export default async function main() {
42-
program
43-
.version('0.1.0')
44-
.description('Check Content service is up ')
45-
.usage('check-cs-env [options]')
46-
.option('--host [type]', 'Remote environment host adf.lab.com ')
47-
.option('-p, --password [type]', 'password ')
48-
.option('-u, --username [type]', 'username ')
49-
.option('-t, --time [type]', 'time ')
50-
.option('-r, --retry [type]', 'retry ')
51-
.parse(argv);
52-
53-
const opts = program.opts();
40+
if (argv.includes('-h') || argv.includes('--help')) {
41+
console.log(`
42+
Usage: check-cs-env [options]
43+
44+
Check Content service is up
45+
46+
Options:
47+
-v, --version Output the version number
48+
--host <host> Remote environment host adf.lab.com
49+
-p, --password <pass> Password
50+
-u, --username <user> Username
51+
-t, --time <ms> Time in milliseconds
52+
-r, --retry <num> Retry count
53+
-h, --help Display help for command
54+
`);
55+
exit(0);
56+
}
57+
58+
if (argv.includes('-v') || argv.includes('--version')) {
59+
console.log('0.1.0');
60+
exit(0);
61+
}
62+
63+
const { values } = parseArgs({
64+
args: argv.slice(2),
65+
options: {
66+
host: {
67+
type: 'string'
68+
},
69+
password: {
70+
type: 'string',
71+
short: 'p'
72+
},
73+
username: {
74+
type: 'string',
75+
short: 'u'
76+
},
77+
time: {
78+
type: 'string',
79+
short: 't'
80+
},
81+
retry: {
82+
type: 'string',
83+
short: 'r'
84+
}
85+
},
86+
allowPositionals: true
87+
});
88+
89+
const opts: CheckCsEnvArgs = {
90+
host: values.host as string | undefined,
91+
username: values.username as string | undefined,
92+
password: values.password as string | undefined,
93+
time: values.time ? parseInt(values.time as string, 10) : undefined,
94+
retry: values.retry ? parseInt(values.retry as string, 10) : undefined
95+
};
96+
5497
await checkEnv(opts);
5598
// TODO: https://alfresco.atlassian.net/browse/ACS-5873
5699
// await checkDiskSpaceFullEnv();

0 commit comments

Comments
 (0)