@oclif/core 3.0.0-beta.2 → 3.0.0-beta.20

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.
Files changed (112) hide show
  1. package/README.md +10 -6
  2. package/flush.js +1 -1
  3. package/handle.js +1 -1
  4. package/lib/args.d.ts +1 -1
  5. package/lib/args.js +17 -18
  6. package/lib/cli-ux/action/base.d.ts +3 -5
  7. package/lib/cli-ux/action/base.js +32 -26
  8. package/lib/cli-ux/action/simple.js +13 -18
  9. package/lib/cli-ux/action/spinner.d.ts +4 -2
  10. package/lib/cli-ux/action/spinner.js +27 -19
  11. package/lib/cli-ux/action/spinners.js +1 -1
  12. package/lib/cli-ux/action/types.d.ts +5 -0
  13. package/lib/cli-ux/action/types.js +2 -0
  14. package/lib/cli-ux/config.d.ts +0 -1
  15. package/lib/cli-ux/config.js +17 -21
  16. package/lib/cli-ux/exit.d.ts +1 -1
  17. package/lib/cli-ux/exit.js +4 -1
  18. package/lib/cli-ux/flush.d.ts +1 -0
  19. package/lib/cli-ux/flush.js +28 -0
  20. package/lib/cli-ux/index.d.ts +10 -30
  21. package/lib/cli-ux/index.js +32 -75
  22. package/lib/cli-ux/list.js +3 -3
  23. package/lib/cli-ux/prompt.js +32 -22
  24. package/lib/cli-ux/stream.js +1 -0
  25. package/lib/cli-ux/styled/index.d.ts +5 -6
  26. package/lib/cli-ux/styled/index.js +11 -11
  27. package/lib/cli-ux/styled/json.js +8 -5
  28. package/lib/cli-ux/styled/object.js +7 -9
  29. package/lib/cli-ux/styled/table.d.ts +4 -4
  30. package/lib/cli-ux/styled/table.js +61 -64
  31. package/lib/cli-ux/styled/tree.js +1 -3
  32. package/lib/cli-ux/wait.js +3 -5
  33. package/lib/command.d.ts +15 -19
  34. package/lib/command.js +117 -96
  35. package/lib/config/config.d.ts +16 -23
  36. package/lib/config/config.js +180 -334
  37. package/lib/config/index.d.ts +1 -1
  38. package/lib/config/index.js +1 -2
  39. package/lib/config/plugin-loader.d.ts +30 -0
  40. package/lib/config/plugin-loader.js +145 -0
  41. package/lib/config/plugin.d.ts +6 -11
  42. package/lib/config/plugin.js +112 -78
  43. package/lib/config/ts-node.d.ts +2 -1
  44. package/lib/config/ts-node.js +64 -51
  45. package/lib/config/util.d.ts +1 -11
  46. package/lib/config/util.js +6 -59
  47. package/lib/errors/config.js +1 -1
  48. package/lib/errors/errors/cli.d.ts +1 -1
  49. package/lib/errors/errors/cli.js +18 -14
  50. package/lib/errors/errors/exit.d.ts +0 -3
  51. package/lib/errors/errors/exit.js +1 -1
  52. package/lib/errors/errors/module-load.d.ts +0 -3
  53. package/lib/errors/errors/module-load.js +1 -1
  54. package/lib/errors/errors/pretty-print.js +11 -9
  55. package/lib/errors/handle.d.ts +12 -2
  56. package/lib/errors/handle.js +28 -18
  57. package/lib/errors/index.d.ts +2 -2
  58. package/lib/errors/index.js +20 -19
  59. package/lib/errors/logger.js +9 -8
  60. package/lib/execute.d.ts +49 -0
  61. package/lib/execute.js +63 -0
  62. package/lib/flags.d.ts +102 -31
  63. package/lib/flags.js +81 -46
  64. package/lib/help/command.d.ts +2 -0
  65. package/lib/help/command.js +68 -53
  66. package/lib/help/docopts.js +9 -13
  67. package/lib/help/formatter.d.ts +1 -1
  68. package/lib/help/formatter.js +35 -24
  69. package/lib/help/index.d.ts +7 -3
  70. package/lib/help/index.js +77 -55
  71. package/lib/help/root.js +7 -9
  72. package/lib/help/util.d.ts +1 -7
  73. package/lib/help/util.js +8 -28
  74. package/lib/index.d.ts +19 -18
  75. package/lib/index.js +36 -48
  76. package/lib/interfaces/config.d.ts +30 -30
  77. package/lib/interfaces/errors.d.ts +1 -1
  78. package/lib/interfaces/hooks.d.ts +3 -3
  79. package/lib/interfaces/index.d.ts +14 -14
  80. package/lib/interfaces/parser.d.ts +188 -116
  81. package/lib/interfaces/pjson.d.ts +2 -1
  82. package/lib/interfaces/plugin.d.ts +10 -1
  83. package/lib/main.d.ts +0 -48
  84. package/lib/main.js +11 -66
  85. package/lib/module-loader.d.ts +68 -79
  86. package/lib/module-loader.js +183 -150
  87. package/lib/parser/errors.d.ts +3 -3
  88. package/lib/parser/errors.js +17 -10
  89. package/lib/parser/help.js +5 -5
  90. package/lib/parser/parse.d.ts +3 -0
  91. package/lib/parser/parse.js +114 -115
  92. package/lib/parser/validate.js +45 -25
  93. package/lib/performance.d.ts +5 -1
  94. package/lib/performance.js +40 -19
  95. package/lib/util/aggregate-flags.d.ts +2 -0
  96. package/lib/util/aggregate-flags.js +13 -0
  97. package/lib/util/cache-command.d.ts +3 -0
  98. package/lib/util/cache-command.js +109 -0
  99. package/lib/util/cache-default-value.d.ts +2 -0
  100. package/lib/util/cache-default-value.js +28 -0
  101. package/lib/util/ensure-arg-object.d.ts +12 -0
  102. package/lib/util/ensure-arg-object.js +14 -0
  103. package/lib/util/fs.d.ts +7 -0
  104. package/lib/util/fs.js +54 -0
  105. package/lib/util/os.d.ts +19 -0
  106. package/lib/util/os.js +28 -0
  107. package/lib/{util.d.ts → util/util.d.ts} +6 -15
  108. package/lib/util/util.js +98 -0
  109. package/package.json +32 -34
  110. package/lib/cli-ux/action/pride-spinner.d.ts +0 -4
  111. package/lib/cli-ux/action/pride-spinner.js +0 -30
  112. package/lib/util.js +0 -126
@@ -1,82 +1,71 @@
1
- import { Config as IConfig } from './interfaces';
2
- import { Plugin as IPlugin } from './interfaces';
1
+ import { Config as IConfig, Plugin as IPlugin } from './interfaces';
2
+ import { Command } from './command';
3
3
  /**
4
- * Provides a static class with several utility methods to work with Oclif config / plugin to load ESM or CJS Node
5
- * modules and source files.
4
+ * Loads and returns a module.
6
5
  *
7
- * @author Michael Leahy <support@typhonjs.io> (https://github.com/typhonrt)
6
+ * Uses `getPackageType` to determine if `type` is set to 'module. If so loads '.js' files as ESM otherwise uses
7
+ * a bare require to load as CJS. Also loads '.mjs' files as ESM.
8
+ *
9
+ * Uses dynamic import to load ESM source or require for CommonJS.
10
+ *
11
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
12
+ * provides a consistent stack trace and info.
13
+ *
14
+ * @param {IConfig|IPlugin} config - Oclif config or plugin config.
15
+ * @param {string} modulePath - NPM module name or file path to load.
16
+ *
17
+ * @returns {Promise<*>} The entire ESM module from dynamic import or CJS module by require.
18
+ */
19
+ export declare function load(config: IConfig | IPlugin, modulePath: string): Promise<any>;
20
+ /**
21
+ * Loads a module and returns an object with the module and data about the module.
22
+ *
23
+ * Uses `getPackageType` to determine if `type` is set to `module`. If so loads '.js' files as ESM otherwise uses
24
+ * a bare require to load as CJS. Also loads '.mjs' files as ESM.
25
+ *
26
+ * Uses dynamic import to load ESM source or require for CommonJS.
27
+ *
28
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
29
+ * provides a consistent stack trace and info.
30
+ *
31
+ * @param {IConfig|IPlugin} config - Oclif config or plugin config.
32
+ * @param {string} modulePath - NPM module name or file path to load.
33
+ *
34
+ * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
35
+ * file path and whether the module is ESM.
36
+ */
37
+ export declare function loadWithData(config: IConfig | IPlugin, modulePath: string): Promise<{
38
+ isESM: boolean;
39
+ module: any;
40
+ filePath: string;
41
+ }>;
42
+ /**
43
+ * Loads a module and returns an object with the module and data about the module.
44
+ *
45
+ * Uses cached `isESM` and `relativePath` in plugin manifest to determine if dynamic import (isESM = true)
46
+ * or require (isESM = false | undefined) should be used.
47
+ *
48
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
49
+ * provides a consistent stack trace and info.
50
+ *
51
+ * @param {Command.Cached} cached - Cached command data from plugin manifest.
52
+ * @param {string} modulePath - NPM module name or file path to load.
53
+ *
54
+ * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
55
+ * file path and whether the module is ESM.
56
+ */
57
+ export declare function loadWithDataFromManifest(cached: Command.Cached, modulePath: string): Promise<{
58
+ isESM: boolean;
59
+ module: any;
60
+ filePath: string;
61
+ }>;
62
+ /**
63
+ * For `.js` files uses `getPackageType` to determine if `type` is set to `module` in associated `package.json`. If
64
+ * the `modulePath` provided ends in `.mjs` it is assumed to be ESM.
65
+ *
66
+ * @param {string} filePath - File path to test.
67
+ *
68
+ * @returns {boolean} The modulePath is an ES Module.
69
+ * @see https://www.npmjs.com/package/get-package-type
8
70
  */
9
- export default class ModuleLoader {
10
- /**
11
- * Loads and returns a module.
12
- *
13
- * Uses `getPackageType` to determine if `type` is set to 'module. If so loads '.js' files as ESM otherwise uses
14
- * a bare require to load as CJS. Also loads '.mjs' files as ESM.
15
- *
16
- * Uses dynamic import to load ESM source or require for CommonJS.
17
- *
18
- * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
19
- * provides a consistent stack trace and info.
20
- *
21
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
22
- * @param {string} modulePath - NPM module name or file path to load.
23
- *
24
- * @returns {Promise<*>} The entire ESM module from dynamic import or CJS module by require.
25
- */
26
- static load(config: IConfig | IPlugin, modulePath: string): Promise<any>;
27
- /**
28
- * Loads a module and returns an object with the module and data about the module.
29
- *
30
- * Uses `getPackageType` to determine if `type` is set to `module`. If so loads '.js' files as ESM otherwise uses
31
- * a bare require to load as CJS. Also loads '.mjs' files as ESM.
32
- *
33
- * Uses dynamic import to load ESM source or require for CommonJS.
34
- *
35
- * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
36
- * provides a consistent stack trace and info.
37
- *
38
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
39
- * @param {string} modulePath - NPM module name or file path to load.
40
- *
41
- * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
42
- * file path and whether the module is ESM.
43
- */
44
- static loadWithData(config: IConfig | IPlugin, modulePath: string): Promise<{
45
- isESM: boolean;
46
- module: any;
47
- filePath: string;
48
- }>;
49
- /**
50
- * For `.js` files uses `getPackageType` to determine if `type` is set to `module` in associated `package.json`. If
51
- * the `modulePath` provided ends in `.mjs` it is assumed to be ESM.
52
- *
53
- * @param {string} filePath - File path to test.
54
- *
55
- * @returns {boolean} The modulePath is an ES Module.
56
- * @see https://www.npmjs.com/package/get-package-type
57
- */
58
- static isPathModule(filePath: string): boolean;
59
- /**
60
- * Resolves a modulePath first by `require.resolve` to allow Node to resolve an actual module. If this fails then
61
- * the `modulePath` is resolved from the root of the provided config. `Config.tsPath` is used for initial resolution.
62
- * If this file path does not exist then several extensions are tried from `s_EXTENSIONS` in order: '.js', '.mjs',
63
- * '.cjs'. After a file path has been selected `isPathModule` is used to determine if the file is an ES Module.
64
- *
65
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
66
- * @param {string} modulePath - File path to load.
67
- *
68
- * @returns {{isESM: boolean, filePath: string}} An object including file path and whether the module is ESM.
69
- */
70
- static resolvePath(config: IConfig | IPlugin, modulePath: string): {
71
- isESM: boolean;
72
- filePath: string;
73
- };
74
- /**
75
- * Try adding the different extensions from `s_EXTENSIONS` to find the file.
76
- *
77
- * @param {string} filePath - File path to load.
78
- *
79
- * @returns {string | null} Modified file path including extension or null if file is not found.
80
- */
81
- static findFile(filePath: string): string | null;
82
- }
71
+ export declare function isPathModule(filePath: string): boolean;
@@ -1,177 +1,210 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const path = require("path");
4
- const url = require("url");
5
- const fs = require("fs-extra");
3
+ exports.isPathModule = exports.loadWithDataFromManifest = exports.loadWithData = exports.load = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
6
  const errors_1 = require("./errors");
7
- const config_1 = require("./config");
7
+ const node_url_1 = require("node:url");
8
+ const ts_node_1 = require("./config/ts-node");
8
9
  const getPackageType = require('get-package-type');
9
10
  /**
10
11
  * Defines file extension resolution when source files do not have an extension.
11
12
  */
12
13
  // eslint-disable-next-line camelcase
13
14
  const s_EXTENSIONS = ['.ts', '.js', '.mjs', '.cjs'];
14
- const isPlugin = (config) => {
15
- return config.type !== undefined;
16
- };
15
+ const isPlugin = (config) => config.type !== undefined;
17
16
  /**
18
- * Provides a static class with several utility methods to work with Oclif config / plugin to load ESM or CJS Node
19
- * modules and source files.
17
+ * Loads and returns a module.
20
18
  *
21
- * @author Michael Leahy <support@typhonjs.io> (https://github.com/typhonrt)
19
+ * Uses `getPackageType` to determine if `type` is set to 'module. If so loads '.js' files as ESM otherwise uses
20
+ * a bare require to load as CJS. Also loads '.mjs' files as ESM.
21
+ *
22
+ * Uses dynamic import to load ESM source or require for CommonJS.
23
+ *
24
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
25
+ * provides a consistent stack trace and info.
26
+ *
27
+ * @param {IConfig|IPlugin} config - Oclif config or plugin config.
28
+ * @param {string} modulePath - NPM module name or file path to load.
29
+ *
30
+ * @returns {Promise<*>} The entire ESM module from dynamic import or CJS module by require.
22
31
  */
23
- // eslint-disable-next-line unicorn/no-static-only-class
24
- class ModuleLoader {
25
- /**
26
- * Loads and returns a module.
27
- *
28
- * Uses `getPackageType` to determine if `type` is set to 'module. If so loads '.js' files as ESM otherwise uses
29
- * a bare require to load as CJS. Also loads '.mjs' files as ESM.
30
- *
31
- * Uses dynamic import to load ESM source or require for CommonJS.
32
- *
33
- * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
34
- * provides a consistent stack trace and info.
35
- *
36
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
37
- * @param {string} modulePath - NPM module name or file path to load.
38
- *
39
- * @returns {Promise<*>} The entire ESM module from dynamic import or CJS module by require.
40
- */
41
- static async load(config, modulePath) {
42
- let filePath;
43
- let isESM;
44
- try {
45
- ({ isESM, filePath } = ModuleLoader.resolvePath(config, modulePath));
46
- // It is important to await on import to catch the error code.
47
- return isESM ? await import(url.pathToFileURL(filePath).href) : require(filePath);
48
- }
49
- catch (error) {
50
- if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
51
- throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`);
52
- }
53
- throw error;
54
- }
32
+ async function load(config, modulePath) {
33
+ let filePath;
34
+ let isESM;
35
+ try {
36
+ ;
37
+ ({ isESM, filePath } = resolvePath(config, modulePath));
38
+ return isESM ? await import((0, node_url_1.pathToFileURL)(filePath).href) : require(filePath);
55
39
  }
56
- /**
57
- * Loads a module and returns an object with the module and data about the module.
58
- *
59
- * Uses `getPackageType` to determine if `type` is set to `module`. If so loads '.js' files as ESM otherwise uses
60
- * a bare require to load as CJS. Also loads '.mjs' files as ESM.
61
- *
62
- * Uses dynamic import to load ESM source or require for CommonJS.
63
- *
64
- * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
65
- * provides a consistent stack trace and info.
66
- *
67
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
68
- * @param {string} modulePath - NPM module name or file path to load.
69
- *
70
- * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
71
- * file path and whether the module is ESM.
72
- */
73
- static async loadWithData(config, modulePath) {
74
- let filePath;
75
- let isESM;
76
- try {
77
- ({ isESM, filePath } = ModuleLoader.resolvePath(config, modulePath));
78
- const module = isESM ? await import(url.pathToFileURL(filePath).href) : require(filePath);
79
- return { isESM, module, filePath };
40
+ catch (error) {
41
+ if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
42
+ throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`);
80
43
  }
81
- catch (error) {
82
- if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
83
- throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}: ${error.message}`);
84
- }
85
- throw error;
44
+ throw error;
45
+ }
46
+ }
47
+ exports.load = load;
48
+ /**
49
+ * Loads a module and returns an object with the module and data about the module.
50
+ *
51
+ * Uses `getPackageType` to determine if `type` is set to `module`. If so loads '.js' files as ESM otherwise uses
52
+ * a bare require to load as CJS. Also loads '.mjs' files as ESM.
53
+ *
54
+ * Uses dynamic import to load ESM source or require for CommonJS.
55
+ *
56
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
57
+ * provides a consistent stack trace and info.
58
+ *
59
+ * @param {IConfig|IPlugin} config - Oclif config or plugin config.
60
+ * @param {string} modulePath - NPM module name or file path to load.
61
+ *
62
+ * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
63
+ * file path and whether the module is ESM.
64
+ */
65
+ async function loadWithData(config, modulePath) {
66
+ let filePath;
67
+ let isESM;
68
+ try {
69
+ ;
70
+ ({ isESM, filePath } = resolvePath(config, modulePath));
71
+ const module = isESM ? await import((0, node_url_1.pathToFileURL)(filePath).href) : require(filePath);
72
+ return { isESM, module, filePath };
73
+ }
74
+ catch (error) {
75
+ if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
76
+ throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}: ${error.message}`);
86
77
  }
78
+ throw error;
79
+ }
80
+ }
81
+ exports.loadWithData = loadWithData;
82
+ /**
83
+ * Loads a module and returns an object with the module and data about the module.
84
+ *
85
+ * Uses cached `isESM` and `relativePath` in plugin manifest to determine if dynamic import (isESM = true)
86
+ * or require (isESM = false | undefined) should be used.
87
+ *
88
+ * A unique error, ModuleLoadError, combines both CJS and ESM loader module not found errors into a single error that
89
+ * provides a consistent stack trace and info.
90
+ *
91
+ * @param {Command.Cached} cached - Cached command data from plugin manifest.
92
+ * @param {string} modulePath - NPM module name or file path to load.
93
+ *
94
+ * @returns {Promise<{isESM: boolean, module: *, filePath: string}>} An object with the loaded module & data including
95
+ * file path and whether the module is ESM.
96
+ */
97
+ async function loadWithDataFromManifest(cached, modulePath) {
98
+ const { isESM, relativePath, id } = cached;
99
+ if (!relativePath) {
100
+ throw new errors_1.ModuleLoadError(`Cached command ${id} does not have a relative path`);
87
101
  }
88
- /**
89
- * For `.js` files uses `getPackageType` to determine if `type` is set to `module` in associated `package.json`. If
90
- * the `modulePath` provided ends in `.mjs` it is assumed to be ESM.
91
- *
92
- * @param {string} filePath - File path to test.
93
- *
94
- * @returns {boolean} The modulePath is an ES Module.
95
- * @see https://www.npmjs.com/package/get-package-type
96
- */
97
- static isPathModule(filePath) {
98
- const extension = path.extname(filePath).toLowerCase();
99
- switch (extension) {
100
- case '.js':
101
- case '.jsx':
102
- case '.ts':
103
- case '.tsx':
104
- return getPackageType.sync(filePath) === 'module';
105
- case '.mjs':
106
- case '.mts':
107
- return true;
108
- default:
109
- return false;
102
+ if (isESM === undefined) {
103
+ throw new errors_1.ModuleLoadError(`Cached command ${id} does not have the isESM property set`);
104
+ }
105
+ const filePath = (0, node_path_1.join)(modulePath, relativePath.join(node_path_1.sep));
106
+ try {
107
+ const module = isESM ? await import((0, node_url_1.pathToFileURL)(filePath).href) : require(filePath);
108
+ return { isESM, module, filePath };
109
+ }
110
+ catch (error) {
111
+ if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
112
+ throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}: ${error.message}`);
110
113
  }
114
+ throw error;
111
115
  }
112
- /**
113
- * Resolves a modulePath first by `require.resolve` to allow Node to resolve an actual module. If this fails then
114
- * the `modulePath` is resolved from the root of the provided config. `Config.tsPath` is used for initial resolution.
115
- * If this file path does not exist then several extensions are tried from `s_EXTENSIONS` in order: '.js', '.mjs',
116
- * '.cjs'. After a file path has been selected `isPathModule` is used to determine if the file is an ES Module.
117
- *
118
- * @param {IConfig|IPlugin} config - Oclif config or plugin config.
119
- * @param {string} modulePath - File path to load.
120
- *
121
- * @returns {{isESM: boolean, filePath: string}} An object including file path and whether the module is ESM.
122
- */
123
- static resolvePath(config, modulePath) {
124
- let isESM;
125
- let filePath;
126
- try {
127
- filePath = require.resolve(modulePath);
128
- isESM = ModuleLoader.isPathModule(filePath);
116
+ }
117
+ exports.loadWithDataFromManifest = loadWithDataFromManifest;
118
+ /**
119
+ * For `.js` files uses `getPackageType` to determine if `type` is set to `module` in associated `package.json`. If
120
+ * the `modulePath` provided ends in `.mjs` it is assumed to be ESM.
121
+ *
122
+ * @param {string} filePath - File path to test.
123
+ *
124
+ * @returns {boolean} The modulePath is an ES Module.
125
+ * @see https://www.npmjs.com/package/get-package-type
126
+ */
127
+ function isPathModule(filePath) {
128
+ const extension = (0, node_path_1.extname)(filePath).toLowerCase();
129
+ switch (extension) {
130
+ case '.js':
131
+ case '.jsx':
132
+ case '.ts':
133
+ case '.tsx': {
134
+ return getPackageType.sync(filePath) === 'module';
129
135
  }
130
- catch {
131
- filePath = (isPlugin(config) ? (0, config_1.tsPath)(config.root, modulePath, config) : (0, config_1.tsPath)(config.root, modulePath)) ?? modulePath;
132
- let fileExists = false;
133
- let isDirectory = false;
134
- if (fs.existsSync(filePath)) {
135
- fileExists = true;
136
- try {
137
- if (fs.lstatSync(filePath)?.isDirectory?.()) {
138
- fileExists = false;
139
- isDirectory = true;
140
- }
136
+ case '.mjs':
137
+ case '.mts': {
138
+ return true;
139
+ }
140
+ default: {
141
+ return false;
142
+ }
143
+ }
144
+ }
145
+ exports.isPathModule = isPathModule;
146
+ /**
147
+ * Resolves a modulePath first by `require.resolve` to allow Node to resolve an actual module. If this fails then
148
+ * the `modulePath` is resolved from the root of the provided config. `Config.tsPath` is used for initial resolution.
149
+ * If this file path does not exist then several extensions are tried from `s_EXTENSIONS` in order: '.js', '.mjs',
150
+ * '.cjs'. After a file path has been selected `isPathModule` is used to determine if the file is an ES Module.
151
+ *
152
+ * @param {IConfig|IPlugin} config - Oclif config or plugin config.
153
+ * @param {string} modulePath - File path to load.
154
+ *
155
+ * @returns {{isESM: boolean, filePath: string}} An object including file path and whether the module is ESM.
156
+ */
157
+ function resolvePath(config, modulePath) {
158
+ let isESM;
159
+ let filePath;
160
+ try {
161
+ filePath = require.resolve(modulePath);
162
+ isESM = isPathModule(filePath);
163
+ }
164
+ catch {
165
+ filePath =
166
+ (isPlugin(config) ? (0, ts_node_1.tsPath)(config.root, modulePath, config) : (0, ts_node_1.tsPath)(config.root, modulePath)) ?? modulePath;
167
+ let fileExists = false;
168
+ let isDirectory = false;
169
+ if ((0, node_fs_1.existsSync)(filePath)) {
170
+ fileExists = true;
171
+ try {
172
+ if ((0, node_fs_1.lstatSync)(filePath)?.isDirectory?.()) {
173
+ fileExists = false;
174
+ isDirectory = true;
141
175
  }
142
- catch { }
143
176
  }
144
- if (!fileExists) {
145
- // Try all supported extensions.
146
- let foundPath = ModuleLoader.findFile(filePath);
147
- if (!foundPath && isDirectory) {
148
- // Since filePath is a directory, try looking for index file.
149
- foundPath = ModuleLoader.findFile(path.join(filePath, 'index'));
150
- }
151
- if (foundPath) {
152
- filePath = foundPath;
153
- }
177
+ catch { }
178
+ }
179
+ if (!fileExists) {
180
+ // Try all supported extensions.
181
+ let foundPath = findFile(filePath);
182
+ if (!foundPath && isDirectory) {
183
+ // Since filePath is a directory, try looking for index file.
184
+ foundPath = findFile((0, node_path_1.join)(filePath, 'index'));
185
+ }
186
+ if (foundPath) {
187
+ filePath = foundPath;
154
188
  }
155
- isESM = ModuleLoader.isPathModule(filePath);
156
189
  }
157
- return { isESM, filePath };
190
+ isESM = isPathModule(filePath);
158
191
  }
159
- /**
160
- * Try adding the different extensions from `s_EXTENSIONS` to find the file.
161
- *
162
- * @param {string} filePath - File path to load.
163
- *
164
- * @returns {string | null} Modified file path including extension or null if file is not found.
165
- */
166
- static findFile(filePath) {
167
- // eslint-disable-next-line camelcase
168
- for (const extension of s_EXTENSIONS) {
169
- const testPath = `${filePath}${extension}`;
170
- if (fs.existsSync(testPath)) {
171
- return testPath;
172
- }
192
+ return { isESM, filePath };
193
+ }
194
+ /**
195
+ * Try adding the different extensions from `s_EXTENSIONS` to find the file.
196
+ *
197
+ * @param {string} filePath - File path to load.
198
+ *
199
+ * @returns {string | null} Modified file path including extension or null if file is not found.
200
+ */
201
+ function findFile(filePath) {
202
+ // eslint-disable-next-line camelcase
203
+ for (const extension of s_EXTENSIONS) {
204
+ const testPath = `${filePath}${extension}`;
205
+ if ((0, node_fs_1.existsSync)(testPath)) {
206
+ return testPath;
173
207
  }
174
- return null;
175
208
  }
209
+ return null;
176
210
  }
177
- exports.default = ModuleLoader;
@@ -1,6 +1,6 @@
1
- import { CLIError } from '../errors';
2
- import { OptionFlag, Flag } from '../interfaces';
3
1
  import { Arg, ArgInput, CLIParseErrorOptions } from '../interfaces/parser';
2
+ import { Flag, OptionFlag } from '../interfaces';
3
+ import { CLIError } from '../errors';
4
4
  export { CLIError } from '../errors';
5
5
  export type Validation = {
6
6
  name: string;
@@ -22,7 +22,7 @@ export declare class InvalidArgsSpecError extends CLIParseError {
22
22
  }
23
23
  export declare class RequiredArgsError extends CLIParseError {
24
24
  args: Arg<any>[];
25
- constructor({ args, parse, flagsWithMultiple }: CLIParseErrorOptions & {
25
+ constructor({ args, parse, flagsWithMultiple, }: CLIParseErrorOptions & {
26
26
  args: Arg<any>[];
27
27
  flagsWithMultiple?: string[];
28
28
  });
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FailedFlagValidationError = exports.ArgInvalidOptionError = exports.FlagInvalidOptionError = exports.NonExistentFlagsError = exports.UnexpectedArgsError = exports.RequiredFlagError = exports.RequiredArgsError = exports.InvalidArgsSpecError = exports.CLIParseError = exports.CLIError = void 0;
4
+ const tslib_1 = require("tslib");
4
5
  const errors_1 = require("../errors");
6
+ const chalk_1 = tslib_1.__importDefault(require("chalk"));
5
7
  const help_1 = require("./help");
6
8
  const list_1 = require("../cli-ux/list");
7
- const chalk = require("chalk");
8
- const util_1 = require("../config/util");
9
+ const util_1 = require("../util/util");
9
10
  var errors_2 = require("../errors");
10
11
  Object.defineProperty(exports, "CLIError", { enumerable: true, get: function () { return errors_2.CLIError; } });
11
12
  class CLIParseError extends errors_1.CLIError {
13
+ parse;
12
14
  constructor(options) {
13
15
  options.message += '\nSee more help with --help';
14
16
  super(options.message);
@@ -17,11 +19,12 @@ class CLIParseError extends errors_1.CLIError {
17
19
  }
18
20
  exports.CLIParseError = CLIParseError;
19
21
  class InvalidArgsSpecError extends CLIParseError {
22
+ args;
20
23
  constructor({ args, parse }) {
21
24
  let message = 'Invalid argument spec';
22
- const namedArgs = Object.values(args).filter(a => a.name);
25
+ const namedArgs = Object.values(args).filter((a) => a.name);
23
26
  if (namedArgs.length > 0) {
24
- const list = (0, list_1.renderList)(namedArgs.map(a => [`${a.name} (${a.required ? 'required' : 'optional'})`, a.description]));
27
+ const list = (0, list_1.renderList)(namedArgs.map((a) => [`${a.name} (${a.required ? 'required' : 'optional'})`, a.description]));
25
28
  message += `:\n${list}`;
26
29
  }
27
30
  super({ parse, message });
@@ -30,15 +33,16 @@ class InvalidArgsSpecError extends CLIParseError {
30
33
  }
31
34
  exports.InvalidArgsSpecError = InvalidArgsSpecError;
32
35
  class RequiredArgsError extends CLIParseError {
33
- constructor({ args, parse, flagsWithMultiple }) {
36
+ args;
37
+ constructor({ args, parse, flagsWithMultiple, }) {
34
38
  let message = `Missing ${args.length} required arg${args.length === 1 ? '' : 's'}`;
35
- const namedArgs = args.filter(a => a.name);
39
+ const namedArgs = args.filter((a) => a.name);
36
40
  if (namedArgs.length > 0) {
37
- const list = (0, list_1.renderList)(namedArgs.map(a => [a.name, a.description]));
41
+ const list = (0, list_1.renderList)(namedArgs.map((a) => [a.name, a.description]));
38
42
  message += `:\n${list}`;
39
43
  }
40
44
  if (flagsWithMultiple?.length) {
41
- const flags = flagsWithMultiple.map(f => `--${f}`).join(', ');
45
+ const flags = flagsWithMultiple.map((f) => `--${f}`).join(', ');
42
46
  message += `\n\nNote: ${flags} allow${flagsWithMultiple.length === 1 ? 's' : ''} multiple values. Because of this you need to provide all arguments before providing ${flagsWithMultiple.length === 1 ? 'that flag' : 'those flags'}.`;
43
47
  message += '\nAlternatively, you can use "--" to signify the end of the flags and the beginning of arguments.';
44
48
  }
@@ -48,6 +52,7 @@ class RequiredArgsError extends CLIParseError {
48
52
  }
49
53
  exports.RequiredArgsError = RequiredArgsError;
50
54
  class RequiredFlagError extends CLIParseError {
55
+ flag;
51
56
  constructor({ flag, parse }) {
52
57
  const usage = (0, list_1.renderList)((0, help_1.flagUsages)([flag], { displayRequired: false }));
53
58
  const message = `Missing required flag:\n${usage}`;
@@ -57,6 +62,7 @@ class RequiredFlagError extends CLIParseError {
57
62
  }
58
63
  exports.RequiredFlagError = RequiredFlagError;
59
64
  class UnexpectedArgsError extends CLIParseError {
65
+ args;
60
66
  constructor({ parse, args }) {
61
67
  const message = `Unexpected argument${args.length === 1 ? '' : 's'}: ${args.join(', ')}`;
62
68
  super({ parse, message });
@@ -65,6 +71,7 @@ class UnexpectedArgsError extends CLIParseError {
65
71
  }
66
72
  exports.UnexpectedArgsError = UnexpectedArgsError;
67
73
  class NonExistentFlagsError extends CLIParseError {
74
+ flags;
68
75
  constructor({ parse, flags }) {
69
76
  const message = `Nonexistent flag${flags.length === 1 ? '' : 's'}: ${flags.join(', ')}`;
70
77
  super({ parse, message });
@@ -88,10 +95,10 @@ class ArgInvalidOptionError extends CLIParseError {
88
95
  exports.ArgInvalidOptionError = ArgInvalidOptionError;
89
96
  class FailedFlagValidationError extends CLIParseError {
90
97
  constructor({ parse, failed }) {
91
- const reasons = failed.map(r => r.reason);
98
+ const reasons = failed.map((r) => r.reason);
92
99
  const deduped = (0, util_1.uniq)(reasons);
93
100
  const errString = deduped.length === 1 ? 'error' : 'errors';
94
- const message = `The following ${errString} occurred:\n ${chalk.dim(deduped.join('\n '))}`;
101
+ const message = `The following ${errString} occurred:\n ${chalk_1.default.dim(deduped.join('\n '))}`;
95
102
  super({ parse, message });
96
103
  }
97
104
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.flagUsages = exports.flagUsage = void 0;
4
- const chalk = require("chalk");
5
- const util_1 = require("../util");
4
+ const tslib_1 = require("tslib");
5
+ const chalk_1 = tslib_1.__importDefault(require("chalk"));
6
+ const util_1 = require("../util/util");
6
7
  function flagUsage(flag, options = {}) {
7
8
  const label = [];
8
9
  if (flag.helpLabel) {
@@ -18,14 +19,13 @@ function flagUsage(flag, options = {}) {
18
19
  let description = flag.summary || flag.description || '';
19
20
  if (options.displayRequired && flag.required)
20
21
  description = `(required) ${description}`;
21
- description = description ? chalk.dim(description) : undefined;
22
+ description = description ? chalk_1.default.dim(description) : undefined;
22
23
  return [` ${label.join(',').trim()}${usage}`, description];
23
24
  }
24
25
  exports.flagUsage = flagUsage;
25
26
  function flagUsages(flags, options = {}) {
26
27
  if (flags.length === 0)
27
28
  return [];
28
- return (0, util_1.sortBy)(flags, f => [f.char ? -1 : 1, f.char, f.name])
29
- .map(f => flagUsage(f, options));
29
+ return (0, util_1.sortBy)(flags, (f) => [f.char ? -1 : 1, f.char, f.name]).map((f) => flagUsage(f, options));
30
30
  }
31
31
  exports.flagUsages = flagUsages;