@oclif/core
Advanced tools
Comparing version 0.5.7 to 0.5.8
@@ -5,2 +5,9 @@ # Changelog | ||
### [0.5.8](https://github.com/oclif/core/compare/v0.5.7...v0.5.8) (2021-05-26) | ||
### Features | ||
* strengthened ModuleLoader & unit tests; now supports mixed ESM / CJS plugins ([#163](https://github.com/oclif/core/issues/163)) ([788bf17](https://github.com/oclif/core/commit/788bf175b7e39b7d61fc07279e5cedca2fdbd540)) | ||
### [0.5.7](https://github.com/oclif/core/compare/v0.5.6...v0.5.7) (2021-05-17) | ||
@@ -7,0 +14,0 @@ |
@@ -134,3 +134,3 @@ "use strict"; | ||
const patterns = [ | ||
'**/*.+(js|ts|tsx)', | ||
'**/*.+(js|cjs|mjs|ts|tsx)', | ||
'!**/*.+(d.ts|test.ts|test.js|spec.ts|spec.js)?(x)', | ||
@@ -137,0 +137,0 @@ ]; |
@@ -61,4 +61,5 @@ import { Config as IConfig } from './interfaces'; | ||
* Resolves a modulePath first by `require.resolve` to allow Node to resolve an actual module. If this fails then | ||
* the `modulePath` is resolved from the root of the provided config. `path.resolve` is used for ESM and `tsPath` | ||
* for non-ESM paths. | ||
* the `modulePath` is resolved from the root of the provided config. `Config.tsPath` is used for initial resolution. | ||
* If this file path does not exist then several extensions are tried from `s_EXTENSIONS` in order: '.js', '.mjs', | ||
* '.cjs'. After a file path has been selected `isPathModule` is used to determine if the file is an ES Module. | ||
* | ||
@@ -65,0 +66,0 @@ * @param {IConfig|IPlugin} config - Oclif config or plugin config. |
@@ -5,2 +5,3 @@ "use strict"; | ||
const url = require("url"); | ||
const fs = require("fs-extra"); | ||
const errors_1 = require("./errors"); | ||
@@ -10,2 +11,6 @@ const Config = require("./config"); | ||
/** | ||
* Defines file extension resolution when source files do not have an extension. | ||
*/ | ||
const s_EXTENSIONS = ['.js', '.mjs', '.cjs']; | ||
/** | ||
* Provides a mechanism to use dynamic import / import() with tsconfig -> module: commonJS as otherwise import() gets | ||
@@ -39,4 +44,6 @@ * transpiled to require(). | ||
static async load(config, modulePath) { | ||
const { isESM, filePath } = ModuleLoader.resolvePath(config, modulePath); | ||
let filePath; | ||
let isESM; | ||
try { | ||
({ isESM, filePath } = ModuleLoader.resolvePath(config, modulePath)); | ||
// It is important to await on _importDynamic to catch the error code. | ||
@@ -47,3 +54,3 @@ return isESM ? await _importDynamic(url.pathToFileURL(filePath)) : require(filePath); | ||
if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { | ||
throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath}`); | ||
throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`); | ||
} | ||
@@ -71,4 +78,6 @@ throw error; | ||
static async loadWithData(config, modulePath) { | ||
const { isESM, filePath } = ModuleLoader.resolvePath(config, modulePath); | ||
let filePath; | ||
let isESM; | ||
try { | ||
({ isESM, filePath } = ModuleLoader.resolvePath(config, modulePath)); | ||
const module = isESM ? await _importDynamic(url.pathToFileURL(filePath)) : require(filePath); | ||
@@ -79,3 +88,3 @@ return { isESM, module, filePath }; | ||
if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { | ||
throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath}`); | ||
throw new errors_1.ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`); | ||
} | ||
@@ -107,4 +116,5 @@ throw error; | ||
* Resolves a modulePath first by `require.resolve` to allow Node to resolve an actual module. If this fails then | ||
* the `modulePath` is resolved from the root of the provided config. `path.resolve` is used for ESM and `tsPath` | ||
* for non-ESM paths. | ||
* the `modulePath` is resolved from the root of the provided config. `Config.tsPath` is used for initial resolution. | ||
* If this file path does not exist then several extensions are tried from `s_EXTENSIONS` in order: '.js', '.mjs', | ||
* '.cjs'. After a file path has been selected `isPathModule` is used to determine if the file is an ES Module. | ||
* | ||
@@ -117,3 +127,3 @@ * @param {IConfig|IPlugin} config - Oclif config or plugin config. | ||
static resolvePath(config, modulePath) { | ||
let isESM = config.pjson.type === 'module'; | ||
let isESM; | ||
let filePath; | ||
@@ -125,3 +135,14 @@ try { | ||
catch (error) { | ||
filePath = isESM ? path.resolve(path.join(config.root, modulePath)) : Config.tsPath(config.root, modulePath); | ||
filePath = Config.tsPath(config.root, modulePath); | ||
// Try all supported extensions. | ||
if (!fs.existsSync(filePath)) { | ||
for (const extension of s_EXTENSIONS) { | ||
const testPath = `${filePath}${extension}`; | ||
if (fs.existsSync(testPath)) { | ||
filePath = testPath; | ||
break; | ||
} | ||
} | ||
} | ||
isESM = ModuleLoader.isPathModule(filePath); | ||
} | ||
@@ -128,0 +149,0 @@ return { isESM, filePath }; |
{ | ||
"name": "@oclif/core", | ||
"description": "base library for oclif CLIs", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"author": "Jeff Dickey @jdxcode", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/oclif/core/issues", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
163366
4273
30