@m2c2kit/cli 0.3.5 → 0.3.7

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.
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The code in this file is adapted from a reference CLI implementation from
4
+ * the Angular devkit repository:
5
+ * https://github.com/angular/angular-cli/blob/b674bd2c3c3e63693881ba502493bc803a820772/packages/angular_devkit/schematics_cli/bin/schematics.ts
6
+ *
7
+ * The license for that code is as follows:
8
+ *
9
+ * The MIT License
10
+ * Copyright (c) 2010-2023 Google LLC. http://angular.io/license
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ */
15
+ import "symbol-observable";
16
+ import { ProcessOutput } from "@angular-devkit/core/node";
17
+ export interface MainOptions {
18
+ args: string[];
19
+ stdout?: ProcessOutput;
20
+ stderr?: ProcessOutput;
21
+ }
22
+ export declare function main({ args, stdout, stderr, }: MainOptions): Promise<0 | 1>;
package/dist/index.js ADDED
@@ -0,0 +1,446 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ /**
4
+ * The code in this file is adapted from a reference CLI implementation from
5
+ * the Angular devkit repository:
6
+ * https://github.com/angular/angular-cli/blob/b674bd2c3c3e63693881ba502493bc803a820772/packages/angular_devkit/schematics_cli/bin/schematics.ts
7
+ *
8
+ * The license for that code is as follows:
9
+ *
10
+ * The MIT License
11
+ * Copyright (c) 2010-2023 Google LLC. http://angular.io/license
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ */
16
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ var desc = Object.getOwnPropertyDescriptor(m, k);
19
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
20
+ desc = { enumerable: true, get: function() { return m[k]; } };
21
+ }
22
+ Object.defineProperty(o, k2, desc);
23
+ }) : (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ o[k2] = m[k];
26
+ }));
27
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
28
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
29
+ }) : function(o, v) {
30
+ o["default"] = v;
31
+ });
32
+ var __importStar = (this && this.__importStar) || function (mod) {
33
+ if (mod && mod.__esModule) return mod;
34
+ var result = {};
35
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.main = void 0;
41
+ // symbol polyfill must go first
42
+ require("symbol-observable");
43
+ const core_1 = require("@angular-devkit/core");
44
+ const node_1 = require("@angular-devkit/core/node");
45
+ const schematics_1 = require("@angular-devkit/schematics");
46
+ const tools_1 = require("@angular-devkit/schematics/tools");
47
+ const ansiColors = __importStar(require("ansi-colors"));
48
+ const fs_1 = require("fs");
49
+ const inquirer = __importStar(require("inquirer"));
50
+ const path = __importStar(require("path"));
51
+ const yargs_parser_1 = __importStar(require("yargs-parser"));
52
+ /**
53
+ * Parse the name of schematic passed in argument, and return a {collection, schematic} named
54
+ * tuple. The user can pass in `collection-name:schematic-name`, and this function will either
55
+ * return `{collection: 'collection-name', schematic: 'schematic-name'}`, or it will error out
56
+ * and show usage.
57
+ *
58
+ * In the case where a collection name isn't part of the argument, the default is to use the
59
+ * schematics package (@angular-devkit/schematics-cli) as the collection.
60
+ *
61
+ * This logic is entirely up to the tooling.
62
+ *
63
+ * @param str The argument to parse.
64
+ * @return {{collection: string, schematic: (string)}}
65
+ */
66
+ function parseSchematicName(str) {
67
+ //let collection = '@angular-devkit/schematics-cli';
68
+ let collection = "@m2c2kit/schematics";
69
+ let schematic = str;
70
+ if (schematic?.includes(":")) {
71
+ const lastIndexOfColon = schematic.lastIndexOf(":");
72
+ [collection, schematic] = [
73
+ schematic.slice(0, lastIndexOfColon),
74
+ schematic.substring(lastIndexOfColon + 1),
75
+ ];
76
+ }
77
+ return { collection, schematic };
78
+ }
79
+ function _listSchematics(workflow, collectionName, logger) {
80
+ try {
81
+ const collection = workflow.engine.createCollection(collectionName);
82
+ logger.info(collection.listSchematicNames().join("\n"));
83
+ }
84
+ catch (error) {
85
+ logger.fatal(error instanceof Error ? error.message : `${error}`);
86
+ return 1;
87
+ }
88
+ return 0;
89
+ }
90
+ function _createPromptProvider() {
91
+ return (definitions) => {
92
+ const questions = definitions.map((definition) => {
93
+ const question = {
94
+ name: definition.id,
95
+ message: definition.message,
96
+ default: definition.default,
97
+ };
98
+ const validator = definition.validator;
99
+ if (validator) {
100
+ question.validate = (input) => validator(input);
101
+ // Filter allows transformation of the value prior to validation
102
+ question.filter = async (input) => {
103
+ for (const type of definition.propertyTypes) {
104
+ let value;
105
+ switch (type) {
106
+ case "string":
107
+ value = String(input);
108
+ break;
109
+ case "integer":
110
+ case "number":
111
+ value = Number(input);
112
+ break;
113
+ default:
114
+ value = input;
115
+ break;
116
+ }
117
+ // Can be a string if validation fails
118
+ const isValid = (await validator(value)) === true;
119
+ if (isValid) {
120
+ return value;
121
+ }
122
+ }
123
+ return input;
124
+ };
125
+ }
126
+ switch (definition.type) {
127
+ case "confirmation":
128
+ return { ...question, type: "confirm" };
129
+ case "list":
130
+ return {
131
+ ...question,
132
+ type: definition.multiselect ? "checkbox" : "list",
133
+ choices: definition.items &&
134
+ definition.items.map((item) => {
135
+ if (typeof item == "string") {
136
+ return item;
137
+ }
138
+ else {
139
+ return {
140
+ name: item.label,
141
+ value: item.value,
142
+ };
143
+ }
144
+ }),
145
+ };
146
+ default:
147
+ return { ...question, type: definition.type };
148
+ }
149
+ });
150
+ return inquirer.prompt(questions);
151
+ };
152
+ }
153
+ function findUp(names, from) {
154
+ if (!Array.isArray(names)) {
155
+ names = [names];
156
+ }
157
+ const root = path.parse(from).root;
158
+ let currentDir = from;
159
+ while (currentDir && currentDir !== root) {
160
+ for (const name of names) {
161
+ const p = path.join(currentDir, name);
162
+ if ((0, fs_1.existsSync)(p)) {
163
+ return p;
164
+ }
165
+ }
166
+ currentDir = path.dirname(currentDir);
167
+ }
168
+ return null;
169
+ }
170
+ /**
171
+ * return package manager' name by lock file
172
+ */
173
+ function getPackageManagerName() {
174
+ // order by check priority
175
+ const LOCKS = {
176
+ "package-lock.json": "npm",
177
+ "yarn.lock": "yarn",
178
+ "pnpm-lock.yaml": "pnpm",
179
+ };
180
+ const lockPath = findUp(Object.keys(LOCKS), process.cwd());
181
+ if (lockPath) {
182
+ return LOCKS[path.basename(lockPath)];
183
+ }
184
+ return "npm";
185
+ }
186
+ // eslint-disable-next-line max-lines-per-function
187
+ async function main({ args, stdout = process.stdout, stderr = process.stderr, }) {
188
+ const { cliOptions, schematicOptions, _ } = parseArgs(args);
189
+ // Create a separate instance to prevent unintended global changes to the color configuration
190
+ const colors = ansiColors.create();
191
+ /** Create the DevKit Logger used through the CLI. */
192
+ const logger = (0, node_1.createConsoleLogger)(!!cliOptions.verbose, stdout, stderr, {
193
+ info: (s) => s,
194
+ debug: (s) => s,
195
+ warn: (s) => colors.bold.yellow(s),
196
+ error: (s) => colors.bold.red(s),
197
+ fatal: (s) => colors.bold.red(s),
198
+ });
199
+ if (cliOptions.help) {
200
+ logger.info(getUsage());
201
+ return 0;
202
+ }
203
+ /** Get the collection an schematic name from the first argument. */
204
+ const { collection: collectionName, schematic: schematicName } = parseSchematicName(_.shift() || null);
205
+ const isLocalCollection = collectionName.startsWith(".") || collectionName.startsWith("/");
206
+ /** Gather the arguments for later use. */
207
+ const debugPresent = cliOptions.debug !== null;
208
+ const debug = debugPresent ? !!cliOptions.debug : isLocalCollection;
209
+ const dryRunPresent = cliOptions["dry-run"] !== null;
210
+ const dryRun = dryRunPresent ? !!cliOptions["dry-run"] : debug;
211
+ const force = !!cliOptions.force;
212
+ const allowPrivate = !!cliOptions["allow-private"];
213
+ /** Create the workflow scoped to the working directory that will be executed with this run. */
214
+ const workflow = new tools_1.NodeWorkflow(process.cwd(), {
215
+ force,
216
+ dryRun,
217
+ resolvePaths: [process.cwd(), __dirname],
218
+ schemaValidation: true,
219
+ packageManager: getPackageManagerName(),
220
+ });
221
+ /** If the user wants to list schematics, we simply show all the schematic names. */
222
+ if (cliOptions["list-schematics"]) {
223
+ return _listSchematics(workflow, collectionName, logger);
224
+ }
225
+ if (!schematicName) {
226
+ logger.info(getUsage());
227
+ return 1;
228
+ }
229
+ if (debug) {
230
+ logger.info(`Debug mode enabled${isLocalCollection ? " by default for local collections" : ""}.`);
231
+ }
232
+ // Indicate to the user when nothing has been done. This is automatically set to off when there's
233
+ // a new DryRunEvent.
234
+ let nothingDone = true;
235
+ // Logging queue that receives all the messages to show the users. This only get shown when no
236
+ // errors happened.
237
+ let loggingQueue = [];
238
+ let error = false;
239
+ /**
240
+ * Logs out dry run events.
241
+ *
242
+ * All events will always be executed here, in order of discovery. That means that an error would
243
+ * be shown along other events when it happens. Since errors in workflows will stop the Observable
244
+ * from completing successfully, we record any events other than errors, then on completion we
245
+ * show them.
246
+ *
247
+ * This is a simple way to only show errors when an error occur.
248
+ */
249
+ workflow.reporter.subscribe((event) => {
250
+ nothingDone = false;
251
+ // Strip leading slash to prevent confusion.
252
+ const eventPath = event.path.startsWith("/")
253
+ ? event.path.slice(1)
254
+ : event.path;
255
+ switch (event.kind) {
256
+ case "error":
257
+ error = true;
258
+ // eslint-disable-next-line no-case-declarations
259
+ const desc = event.description == "alreadyExist"
260
+ ? "already exists"
261
+ : "does not exist";
262
+ logger.error(`ERROR! ${eventPath} ${desc}.`);
263
+ break;
264
+ case "update":
265
+ loggingQueue.push(`${colors.cyan("UPDATE")} ${eventPath} (${event.content.length} bytes)`);
266
+ break;
267
+ case "create":
268
+ loggingQueue.push(`${colors.green("CREATE")} ${eventPath} (${event.content.length} bytes)`);
269
+ break;
270
+ case "delete":
271
+ loggingQueue.push(`${colors.yellow("DELETE")} ${eventPath}`);
272
+ break;
273
+ case "rename":
274
+ // eslint-disable-next-line no-case-declarations
275
+ const eventToPath = event.to.startsWith("/")
276
+ ? event.to.slice(1)
277
+ : event.to;
278
+ loggingQueue.push(`${colors.blue("RENAME")} ${eventPath} => ${eventToPath}`);
279
+ break;
280
+ }
281
+ });
282
+ /**
283
+ * Listen to lifecycle events of the workflow to flush the logs between each phases.
284
+ */
285
+ workflow.lifeCycle.subscribe((event) => {
286
+ if (event.kind == "workflow-end" || event.kind == "post-tasks-start") {
287
+ if (!error) {
288
+ // Flush the log queue and clean the error state.
289
+ loggingQueue.forEach((log) => logger.info(log));
290
+ }
291
+ loggingQueue = [];
292
+ error = false;
293
+ }
294
+ });
295
+ // Show usage of deprecated options
296
+ workflow.registry.useXDeprecatedProvider((msg) => logger.warn(msg));
297
+ // Pass the rest of the arguments as the smart default "argv". Then delete it.
298
+ workflow.registry.addSmartDefaultProvider("argv", (schema) => "index" in schema ? _[Number(schema["index"])] : _);
299
+ // Add prompts.
300
+ if (cliOptions.interactive && isTTY()) {
301
+ workflow.registry.usePromptProvider(_createPromptProvider());
302
+ }
303
+ /**
304
+ * Execute the workflow, which will report the dry run events, run the tasks, and complete
305
+ * after all is done.
306
+ *
307
+ * The Observable returned will properly cancel the workflow if unsubscribed, error out if ANY
308
+ * step of the workflow failed (sink or task), with details included, and will only complete
309
+ * when everything is done.
310
+ */
311
+ try {
312
+ await workflow
313
+ .execute({
314
+ collection: collectionName,
315
+ schematic: schematicName,
316
+ options: schematicOptions,
317
+ allowPrivate: allowPrivate,
318
+ debug: debug,
319
+ logger: logger,
320
+ })
321
+ .toPromise();
322
+ if (nothingDone) {
323
+ logger.info("Nothing to be done.");
324
+ }
325
+ else if (dryRun) {
326
+ logger.info(`Dry run enabled${dryRunPresent ? "" : " by default in debug mode"}. No files written to disk.`);
327
+ }
328
+ return 0;
329
+ }
330
+ catch (err) {
331
+ if (err instanceof schematics_1.UnsuccessfulWorkflowExecution) {
332
+ // "See above" because we already printed the error.
333
+ logger.fatal("The Schematic workflow failed. See above.");
334
+ }
335
+ else if (debug && err instanceof Error) {
336
+ logger.fatal(`An error occured:\n${err.stack}`);
337
+ }
338
+ else {
339
+ logger.fatal(`Error: ${err instanceof Error ? err.message : err}`);
340
+ }
341
+ return 1;
342
+ }
343
+ }
344
+ exports.main = main;
345
+ /**
346
+ * Get usage of the CLI tool.
347
+ */
348
+ function getUsage() {
349
+ return core_1.tags.stripIndent `
350
+ m2 <command> [options, ...]
351
+
352
+ Commands:
353
+ m2 new <name> create a new m2c2kit app.
354
+
355
+ Options:
356
+ --debug Debug mode. This is true by default if the collection is a relative
357
+ path (in that case, turn off with --debug=false).
358
+
359
+ --allow-private Allow private schematics to be run from the command line. Default to
360
+ false.
361
+
362
+ --dry-run Do not output anything, but instead just show what actions would be
363
+ performed. Default to true if debug is also true.
364
+
365
+ --force Force overwriting files that would otherwise be an error.
366
+
367
+ --list-schematics List all schematics from the collection, by name. A collection name
368
+ should be suffixed by a colon. Example: '@angular-devkit/schematics-cli:'.
369
+
370
+ --no-interactive Disables interactive input prompts.
371
+
372
+ --verbose Show more information.
373
+
374
+ --help Show this message.
375
+
376
+ Any additional option is passed to the Schematics depending on its schema.
377
+ `;
378
+ }
379
+ /** Parse the command line. */
380
+ const booleanArgs = [
381
+ "allow-private",
382
+ "debug",
383
+ "dry-run",
384
+ "force",
385
+ "help",
386
+ "list-schematics",
387
+ "verbose",
388
+ "interactive",
389
+ ];
390
+ /** Parse the command line. */
391
+ function parseArgs(args) {
392
+ const { _, ...options } = (0, yargs_parser_1.default)(args, {
393
+ boolean: booleanArgs,
394
+ default: {
395
+ interactive: true,
396
+ debug: null,
397
+ "dry-run": null,
398
+ },
399
+ configuration: {
400
+ "dot-notation": false,
401
+ "boolean-negation": true,
402
+ "strip-aliased": true,
403
+ "camel-case-expansion": false,
404
+ },
405
+ });
406
+ // Camelize options as yargs will return the object in kebab-case when camel casing is disabled.
407
+ const schematicOptions = {};
408
+ const cliOptions = {};
409
+ const isCliOptions = (key) => booleanArgs.includes(key);
410
+ for (const [key, value] of Object.entries(options)) {
411
+ if (/[A-Z]/.test(key)) {
412
+ throw new Error(`Unknown argument ${key}. Did you mean ${(0, yargs_parser_1.decamelize)(key)}?`);
413
+ }
414
+ if (isCliOptions(key)) {
415
+ cliOptions[key] = value;
416
+ }
417
+ else {
418
+ schematicOptions[(0, yargs_parser_1.camelCase)(key)] = value;
419
+ }
420
+ }
421
+ return {
422
+ _: _.map((v) => v.toString()),
423
+ schematicOptions,
424
+ cliOptions,
425
+ };
426
+ }
427
+ function isTTY() {
428
+ const isTruthy = (value) => {
429
+ // Returns true if value is a string that is anything but 0 or false.
430
+ return (value !== undefined && value !== "0" && value.toUpperCase() !== "FALSE");
431
+ };
432
+ // If we force TTY, we always return true.
433
+ const force = process.env["NG_FORCE_TTY"];
434
+ if (force !== undefined) {
435
+ return isTruthy(force);
436
+ }
437
+ return !!process.stdout.isTTY && !isTruthy(process.env["CI"]);
438
+ }
439
+ if (require.main === module) {
440
+ const args = process.argv.slice(2);
441
+ main({ args })
442
+ .then((exitCode) => (process.exitCode = exitCode))
443
+ .catch((e) => {
444
+ throw e;
445
+ });
446
+ }
package/package.json CHANGED
@@ -1,44 +1,32 @@
1
1
  {
2
2
  "name": "@m2c2kit/cli",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "description": "m2c2kit command line interface",
5
- "module": "dist/cli.js",
6
5
  "files": [
7
6
  "dist/**"
8
7
  ],
9
- "type": "module",
10
8
  "bin": {
11
- "m2": "./dist/cli.js"
12
- },
13
- "engines": {
14
- "node": ">=16.0.0"
9
+ "m2": "./dist/index.js"
15
10
  },
16
11
  "scripts": {
17
- "build": "npm run clean && npm run compile && npm run copy-files && npm run write-dotenv",
12
+ "build": "npm run clean && npm run compile",
18
13
  "compile": "tsc",
19
- "clean": "rimraf dist build/",
20
- "copy-files": "cpy --flat build/src/*.js dist && cpy templates assets dist",
21
- "write-dotenv": "node write-dotenv.js"
14
+ "clean": "rimraf dist"
22
15
  },
23
16
  "author": "",
24
17
  "license": "MIT",
25
18
  "dependencies": {
26
- "axios": "1.2.4",
27
- "chalk": "5.2.0",
28
- "conf": "11.0.1",
29
- "form-data": "4.0.0",
30
- "handlebars": "4.7.7",
31
- "ora": "6.3.0",
32
- "prompts": "2.4.2",
33
- "yargs": "17.7.1"
19
+ "@angular-devkit/core": "^16.0.2",
20
+ "@angular-devkit/schematics": "^16.0.2",
21
+ "@m2c2kit/schematics": "^0.1.0",
22
+ "@schematics/angular": "^16.0.2",
23
+ "@types/inquirer": "^8.2.4",
24
+ "ansi-colors": "4.1.3",
25
+ "inquirer": "8.2.4",
26
+ "symbol-observable": "4.0.0",
27
+ "yargs-parser": "21.1.1"
34
28
  },
35
29
  "devDependencies": {
36
- "@types/node": "18.15.12",
37
- "@types/prompts": "2.4.4",
38
- "@types/yargs": "17.0.24",
39
- "cpy-cli": "4.2.0",
40
- "rimraf": "5.0.0",
41
- "tslib": "2.5.0",
42
- "typescript": "5.0.4"
30
+ "rimraf": "5.0.1"
43
31
  }
44
32
  }
package/dist/.env DELETED
@@ -1 +0,0 @@
1
- CLI_VERSION=0.3.5