@nexical/cli-core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/deploy.yml +34 -0
- package/LICENSE +201 -0
- package/index.ts +9 -0
- package/package.json +33 -0
- package/src/BaseCommand.ts +73 -0
- package/src/CLI.ts +316 -0
- package/src/CommandInterface.ts +22 -0
- package/src/CommandLoader.ts +83 -0
- package/src/commands/help.ts +159 -0
- package/src/utils/config.ts +43 -0
- package/src/utils/logger.ts +12 -0
- package/src/utils/shell.ts +21 -0
- package/test/e2e/basic.e2e.test.ts +16 -0
- package/test/integration/help.integration.test.ts +102 -0
- package/test/unit/commands/help.test.ts +345 -0
- package/test/unit/core/BaseCommand.test.ts +131 -0
- package/test/unit/core/CLI.config.test.ts +87 -0
- package/test/unit/core/CLI.test.ts +828 -0
- package/test/unit/core/CommandLoader.test.ts +200 -0
- package/test/unit/utils/config.test.ts +63 -0
- package/test/unit/utils/logger.test.ts +27 -0
- package/test/unit/utils/shell.test.ts +81 -0
- package/test/utils/integration-helpers.ts +22 -0
- package/tsconfig.json +25 -0
- package/tsup.config.ts +18 -0
- package/vitest.config.ts +15 -0
- package/vitest.e2e.config.ts +10 -0
- package/vitest.integration.config.ts +17 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Deploy Nexical Core CLI Framework
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
deploy:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
name: Build & Publish
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout Repo
|
|
14
|
+
uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Setup Node.js
|
|
17
|
+
uses: actions/setup-node@v4
|
|
18
|
+
with:
|
|
19
|
+
node-version: 22
|
|
20
|
+
registry-url: 'https://registry.npmjs.org'
|
|
21
|
+
|
|
22
|
+
- name: Install Dependencies
|
|
23
|
+
run: npm ci
|
|
24
|
+
|
|
25
|
+
- name: Build
|
|
26
|
+
run: npm run build
|
|
27
|
+
|
|
28
|
+
- name: Test
|
|
29
|
+
run: npm run test
|
|
30
|
+
|
|
31
|
+
- name: Publish to NPM
|
|
32
|
+
run: npm publish --access public
|
|
33
|
+
env:
|
|
34
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Cellebris LTD
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nexical/cli-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"dev": "tsup --watch",
|
|
8
|
+
"cli": "node dist/index.js",
|
|
9
|
+
"test": "npm run test:unit && npm run test:integration && npm run test:e2e",
|
|
10
|
+
"test:unit": "vitest run --config vitest.config.ts --coverage",
|
|
11
|
+
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
12
|
+
"test:e2e": "npm run build && vitest run --config vitest.e2e.config.ts",
|
|
13
|
+
"test:watch": "vitest"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"cac": "^6.7.14",
|
|
17
|
+
"consola": "^3.4.2",
|
|
18
|
+
"lilconfig": "^3.1.3",
|
|
19
|
+
"picocolors": "^1.1.1",
|
|
20
|
+
"yaml": "^2.8.2",
|
|
21
|
+
"zod": "^3.22.4"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/fs-extra": "^11.0.4",
|
|
25
|
+
"@types/node": "^20.10.0",
|
|
26
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
27
|
+
"execa": "^9.6.1",
|
|
28
|
+
"fs-extra": "^11.3.2",
|
|
29
|
+
"tsup": "^8.0.1",
|
|
30
|
+
"typescript": "^5.3.3",
|
|
31
|
+
"vitest": "^4.0.15"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { logger } from './utils/logger.js';
|
|
2
|
+
import { CommandDefinition, CommandInterface } from './CommandInterface.js';
|
|
3
|
+
import { findProjectRoot, loadConfig } from './utils/config.js';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
|
|
6
|
+
export abstract class BaseCommand implements CommandInterface {
|
|
7
|
+
static usage = '';
|
|
8
|
+
static description = '';
|
|
9
|
+
static args: CommandDefinition = {};
|
|
10
|
+
|
|
11
|
+
// Configurable flags
|
|
12
|
+
static requiresProject = false;
|
|
13
|
+
|
|
14
|
+
protected projectRoot: string | null = null;
|
|
15
|
+
protected config: any = {};
|
|
16
|
+
protected globalOptions: any = {};
|
|
17
|
+
protected cli: any = null;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
constructor(cli: any, globalOptions: any = {}) {
|
|
21
|
+
this.globalOptions = globalOptions;
|
|
22
|
+
this.cli = cli;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async init() {
|
|
26
|
+
// 1. Root detection strategy
|
|
27
|
+
if (this.globalOptions.rootDir) {
|
|
28
|
+
this.projectRoot = this.globalOptions.rootDir;
|
|
29
|
+
} else {
|
|
30
|
+
this.projectRoot = await findProjectRoot(this.cli.name, process.cwd());
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const requiresProject = (this.constructor as any).requiresProject;
|
|
34
|
+
|
|
35
|
+
if (requiresProject && !this.projectRoot) {
|
|
36
|
+
this.error('This command requires to be run within an app project (app.yml not found).', 1);
|
|
37
|
+
return; // TS doesn't know error exits
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (this.projectRoot) {
|
|
41
|
+
this.config = await loadConfig(this.cli.name, this.projectRoot);
|
|
42
|
+
// logger.debug(`Loaded config from ${this.projectRoot}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
abstract run(options: any): Promise<void>;
|
|
47
|
+
|
|
48
|
+
// Helpers
|
|
49
|
+
success(msg: string) {
|
|
50
|
+
logger.success(msg);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
info(msg: string) {
|
|
55
|
+
logger.info(msg);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
warn(msg: string) {
|
|
59
|
+
logger.warn(msg);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
error(msg: string | Error, code = 1) {
|
|
63
|
+
if (msg instanceof Error) {
|
|
64
|
+
logger.error(msg.message);
|
|
65
|
+
if (this.globalOptions.debug) {
|
|
66
|
+
logger.error(msg.stack);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
logger.error(msg);
|
|
70
|
+
}
|
|
71
|
+
process.exit(code);
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/CLI.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { cac } from 'cac';
|
|
2
|
+
import { CommandLoader } from './CommandLoader.js';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import pc from 'picocolors';
|
|
7
|
+
import pkg from '../package.json';
|
|
8
|
+
import { logger, setDebugMode } from './utils/logger.js';
|
|
9
|
+
|
|
10
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
|
|
12
|
+
export interface CLIConfig {
|
|
13
|
+
commandName?: string;
|
|
14
|
+
searchDirectories?: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class CLI {
|
|
18
|
+
public name: string;
|
|
19
|
+
private cli: ReturnType<typeof cac>;
|
|
20
|
+
private loader: CommandLoader;
|
|
21
|
+
private HelpCommandClass: any;
|
|
22
|
+
private config: CLIConfig;
|
|
23
|
+
|
|
24
|
+
constructor(config: CLIConfig = {}) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.name = this.config.commandName || 'app';
|
|
27
|
+
this.cli = cac(this.name);
|
|
28
|
+
this.loader = new CommandLoader(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private loadedCommands: any[] = [];
|
|
32
|
+
|
|
33
|
+
getCommands() {
|
|
34
|
+
return this.loadedCommands;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getRawCLI() {
|
|
38
|
+
return this.cli;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async start() {
|
|
42
|
+
// In built version, we are in dist/index.js (from cli/index.ts) -> core bundled? or just imported.
|
|
43
|
+
// The core logic is now in src/cli/core/src/CLI.ts or dist/core/src/CLI.js
|
|
44
|
+
|
|
45
|
+
// Check for debug flag early
|
|
46
|
+
if (process.argv.includes('--debug')) {
|
|
47
|
+
setDebugMode(true);
|
|
48
|
+
logger.debug('Debug mode enabled via --debug flag');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let commandsDirs: string[] = [];
|
|
52
|
+
|
|
53
|
+
if (this.config.searchDirectories && this.config.searchDirectories.length > 0) {
|
|
54
|
+
commandsDirs = this.config.searchDirectories;
|
|
55
|
+
} else {
|
|
56
|
+
// We assume the standard structure:
|
|
57
|
+
// cli/
|
|
58
|
+
// index.js
|
|
59
|
+
// commands/
|
|
60
|
+
// core/
|
|
61
|
+
// src/
|
|
62
|
+
// CLI.ts
|
|
63
|
+
|
|
64
|
+
// When running from source (ts-node src/cli/index.ts), specific commands are in src/cli/commands.
|
|
65
|
+
// core is in src/cli/core/src.
|
|
66
|
+
// Relative path from CLI.ts to commands: ../../../commands
|
|
67
|
+
|
|
68
|
+
const possibleDirs = [
|
|
69
|
+
path.resolve(__dirname, './src/commands'),
|
|
70
|
+
path.resolve(process.cwd(), 'commands') // Fallback relative to cwd?
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
for (const dir of possibleDirs) {
|
|
74
|
+
if (fs.existsSync(dir)) {
|
|
75
|
+
commandsDirs.push(dir);
|
|
76
|
+
break; // Keep existing behavior: verify if we want multiple or just first found
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Fallback or error
|
|
82
|
+
if (commandsDirs.length === 0) {
|
|
83
|
+
logger.debug("No commands directory found.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for (const dir of commandsDirs) {
|
|
87
|
+
// Loader accumulates commands
|
|
88
|
+
await this.loader.load(dir);
|
|
89
|
+
}
|
|
90
|
+
this.loadedCommands = this.loader.getCommands();
|
|
91
|
+
|
|
92
|
+
// Group commands by root command name
|
|
93
|
+
const commandGroups: Record<string, any[]> = {};
|
|
94
|
+
// Locate HelpCommand for fallback usage
|
|
95
|
+
const helpCmd = this.loadedCommands.find(c => c.command === 'help');
|
|
96
|
+
if (helpCmd) {
|
|
97
|
+
this.HelpCommandClass = helpCmd.class;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const cmd of this.loadedCommands) {
|
|
101
|
+
const root = cmd.command.split(' ')[0];
|
|
102
|
+
if (!commandGroups[root]) commandGroups[root] = [];
|
|
103
|
+
commandGroups[root].push(cmd);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const [root, cmds] of Object.entries(commandGroups)) {
|
|
107
|
+
// Case 1: Single command, no subcommands (e.g. 'init')
|
|
108
|
+
if (cmds.length === 1 && cmds[0].command === root) {
|
|
109
|
+
const cmd = cmds[0];
|
|
110
|
+
const CommandClass = cmd.class;
|
|
111
|
+
// Original logic for single command
|
|
112
|
+
let commandName = cmd.command;
|
|
113
|
+
const argsDef = CommandClass.args || {};
|
|
114
|
+
if (argsDef.args) {
|
|
115
|
+
argsDef.args.forEach((arg: any) => {
|
|
116
|
+
const isVariadic = arg.name.endsWith('...');
|
|
117
|
+
const cleanName = isVariadic ? arg.name.slice(0, -3) : arg.name;
|
|
118
|
+
// Always use optional brackets [] for CAC to allow --help to pass validation
|
|
119
|
+
// We will enforce 'required' manually in the action handler
|
|
120
|
+
commandName += isVariadic ? ` [...${cleanName}]` : ` [${cleanName}]`;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const cacCommand = this.cli.command(commandName, CommandClass.description || '');
|
|
124
|
+
|
|
125
|
+
// Register options
|
|
126
|
+
if (argsDef.options) {
|
|
127
|
+
argsDef.options.forEach((opt: any) => {
|
|
128
|
+
cacCommand.option(opt.name, opt.description, { default: opt.default });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
this.registerGlobalOptions(cacCommand);
|
|
132
|
+
|
|
133
|
+
cacCommand.action(async (...args: any[]) => {
|
|
134
|
+
const options = args.pop();
|
|
135
|
+
|
|
136
|
+
if (options.help) {
|
|
137
|
+
await this.runHelp([cmd.command]);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const positionalArgs = args;
|
|
142
|
+
|
|
143
|
+
if (argsDef.args) {
|
|
144
|
+
argsDef.args.forEach((arg: any, index: number) => {
|
|
145
|
+
const isVariadic = arg.name.endsWith('...');
|
|
146
|
+
const name = isVariadic ? arg.name.slice(0, -3) : arg.name;
|
|
147
|
+
const val = positionalArgs[index];
|
|
148
|
+
|
|
149
|
+
if (val !== undefined) {
|
|
150
|
+
options[name] = val;
|
|
151
|
+
} else if (arg.required) {
|
|
152
|
+
console.error(pc.red(`Missing required argument: ${name}`));
|
|
153
|
+
this.runHelp([cmd.command]).then(() => process.exit(1));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
await this.runCommand(CommandClass, options, [cmd.command]);
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
// Case 2: Command with subcommands (e.g. 'module add')
|
|
162
|
+
// Register 'module <subcommand>' catch-all
|
|
163
|
+
const commandName = `${root} [subcommand] [...args]`;
|
|
164
|
+
const cacCommand = this.cli.command(commandName, `Manage ${root} commands`);
|
|
165
|
+
|
|
166
|
+
cacCommand.allowUnknownOptions(); // Pass options to subcommand
|
|
167
|
+
this.registerGlobalOptions(cacCommand);
|
|
168
|
+
|
|
169
|
+
cacCommand.action(async (subcommand: string, ...args: any[]) => {
|
|
170
|
+
const options = args.pop(); // last is options
|
|
171
|
+
|
|
172
|
+
if (!subcommand || options.help) {
|
|
173
|
+
// If --help is passed to 'module --help', subcommand might be caught as 'module' if args parsing is weird?
|
|
174
|
+
// ACTUALLY: cac parses 'module add --help' as subcommand="add".
|
|
175
|
+
// 'module --help' might trigger the command itself? No, 'module <subcommand>' expects a subcommand.
|
|
176
|
+
// If I run 'module --help', it might fail validation or parse 'help' as subcommand if unlucky,
|
|
177
|
+
// but likely it just prints help if we didn't override.
|
|
178
|
+
|
|
179
|
+
await this.runHelp([root, subcommand].filter(Boolean));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
// Find matching command
|
|
185
|
+
// Match against "root subcommand"
|
|
186
|
+
const fullCommandName = `${root} ${subcommand}`;
|
|
187
|
+
const cmd = cmds.find(c => c.command === fullCommandName);
|
|
188
|
+
|
|
189
|
+
if (!cmd) {
|
|
190
|
+
console.error(pc.red(`Unknown subcommand '${subcommand}' for '${root}'`));
|
|
191
|
+
process.exit(1);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const CommandClass = cmd.class;
|
|
195
|
+
// Map remaining args?
|
|
196
|
+
// The args array contains positional args AFTER subcommand.
|
|
197
|
+
// But we didn't define them in CAC, so they are just strings.
|
|
198
|
+
// We need to map them manually to the Target Command's args definition.
|
|
199
|
+
// argsDef.args usually starts after the command.
|
|
200
|
+
// For 'module add <url>', <url> is the first arg after 'add'.
|
|
201
|
+
// So 'args' here corresponds to <url>.
|
|
202
|
+
|
|
203
|
+
const argsDef = CommandClass.args || {};
|
|
204
|
+
// If using [...args], the variadic args are collected into the first argument array
|
|
205
|
+
// args here is what remains after popping options.
|
|
206
|
+
const positionalArgs = (args.length > 0 && Array.isArray(args[0])) ? args[0] : args;
|
|
207
|
+
|
|
208
|
+
const childOptions = { ...options }; // Copy options
|
|
209
|
+
|
|
210
|
+
const cmdParts = [root, subcommand];
|
|
211
|
+
|
|
212
|
+
if (argsDef.args) {
|
|
213
|
+
argsDef.args.forEach((arg: any, index: number) => {
|
|
214
|
+
const isVariadic = arg.name.endsWith('...');
|
|
215
|
+
const name = isVariadic ? arg.name.slice(0, -3) : arg.name;
|
|
216
|
+
const val = positionalArgs[index];
|
|
217
|
+
|
|
218
|
+
if (val !== undefined) {
|
|
219
|
+
if (isVariadic) {
|
|
220
|
+
childOptions[name] = positionalArgs.slice(index);
|
|
221
|
+
} else {
|
|
222
|
+
childOptions[name] = val;
|
|
223
|
+
}
|
|
224
|
+
} else if (arg.required) {
|
|
225
|
+
console.error(pc.red(`Missing required argument: ${name}`));
|
|
226
|
+
this.runHelp(cmdParts).then(() => process.exit(1));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
await this.runCommand(CommandClass, childOptions, cmdParts);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// Disable default help
|
|
237
|
+
// this.cli.help();
|
|
238
|
+
|
|
239
|
+
// Manually register global help to ensure it's allowed
|
|
240
|
+
this.cli.option('--help, -h', 'Display help');
|
|
241
|
+
|
|
242
|
+
this.cli.version(pkg.version);
|
|
243
|
+
|
|
244
|
+
// Global help interception for root command
|
|
245
|
+
// If we run `app --help`, we need to catch it.
|
|
246
|
+
// CAC doesn't expose a clean global action without a command content.
|
|
247
|
+
// However, if we parse and no command matches, it usually errors or shows help.
|
|
248
|
+
// If we have default logic, we can put it here?
|
|
249
|
+
// Let's rely on standard parsing but maybe inspect raw args first?
|
|
250
|
+
|
|
251
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
252
|
+
// Inspect non-option args to see if there's a command?
|
|
253
|
+
const args = process.argv.slice(2).filter(a => !a.startsWith('-'));
|
|
254
|
+
if (args.length === 0) {
|
|
255
|
+
await this.runHelp([]);
|
|
256
|
+
process.exit(0);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
this.cli.parse();
|
|
262
|
+
} catch (e: any) {
|
|
263
|
+
console.error(pc.red(e.message));
|
|
264
|
+
|
|
265
|
+
// Try to provide helpful context
|
|
266
|
+
console.log('');
|
|
267
|
+
// Simple heuristic: find first non-flag arg as command
|
|
268
|
+
const args = process.argv.slice(2);
|
|
269
|
+
const potentialCommand = args.find(a => !a.startsWith('-'));
|
|
270
|
+
// If it matches a loaded command root, show help for it
|
|
271
|
+
// Otherwise show global help
|
|
272
|
+
|
|
273
|
+
// We need to match 'module add' etc?
|
|
274
|
+
// Just pass the potential command parts to runHelp.
|
|
275
|
+
// runHelp handles filtering itself? No, runHelp takes commandParts to pass to HelpCommand.
|
|
276
|
+
// HelpCommand expects `command` array.
|
|
277
|
+
|
|
278
|
+
const helpArgs = potentialCommand ? [potentialCommand] : [];
|
|
279
|
+
await this.runHelp(helpArgs);
|
|
280
|
+
|
|
281
|
+
process.exit(1);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private registerGlobalOptions(cacCommand: any) {
|
|
286
|
+
cacCommand.option('--root-dir <path>', 'Override project root');
|
|
287
|
+
cacCommand.option('--debug', 'Enable debug mode');
|
|
288
|
+
cacCommand.option('--help, -h', 'Display help message');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private async runHelp(commandParts: string[]) {
|
|
292
|
+
if (this.HelpCommandClass) {
|
|
293
|
+
const helpInstance = new this.HelpCommandClass(this);
|
|
294
|
+
await helpInstance.run({ command: commandParts });
|
|
295
|
+
} else {
|
|
296
|
+
// Fallback if HelpCommand not loaded (shouldn't happen)
|
|
297
|
+
this.cli.outputHelp();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private async runCommand(CommandClass: any, options: any, commandParts: string[] = []) {
|
|
302
|
+
try {
|
|
303
|
+
const instance = new CommandClass(this, options);
|
|
304
|
+
await instance.init();
|
|
305
|
+
await instance.run(options);
|
|
306
|
+
} catch (e: any) {
|
|
307
|
+
console.error(pc.red(e.message));
|
|
308
|
+
if (options.debug) console.error(e.stack);
|
|
309
|
+
|
|
310
|
+
console.log(''); // spacer
|
|
311
|
+
await this.runHelp(commandParts);
|
|
312
|
+
|
|
313
|
+
process.exit(1);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|