@gasket/resolve
Advanced tools
Comparing version 6.43.2-canary-esm-port.0 to 6.44.0
@@ -1,13 +0,11 @@ | ||
import path from 'path'; | ||
import { readdir } from 'fs/promises'; | ||
import pkg from 'lodash'; | ||
const { defaultsdeep: defaultsDeep } = pkg; | ||
import { applyConfigOverrides, tryResolve } from '@gasket/utils'; | ||
import { flattenPresets } from './preset-utils.js'; | ||
import { default as diagnostics } from 'diagnostics'; | ||
const debug = diagnostics('gasket:resolver'); | ||
const path = require('path'); | ||
const { readdir } = require('fs').promises; | ||
const defaultsDeep = require('lodash.defaultsdeep'); | ||
const { applyConfigOverrides, tryResolve } = require('@gasket/utils'); | ||
const { flattenPresets } = require('./preset-utils'); | ||
const jsExtension = /\.(js|cjs)$/i; | ||
const debug = require('diagnostics')('gasket:resolve:config'); | ||
export async function loadGasketConfigFile(root, env, commandId, configFile = 'gasket.config') { | ||
async function loadGasketConfigFile(root, env, commandId, configFile = 'gasket.config') { | ||
let gasketConfig = loadConfigFile(root, configFile); | ||
@@ -23,3 +21,3 @@ if (gasketConfig) { | ||
export function loadConfigFile(root, configFile) { | ||
function loadConfigFile(root, configFile) { | ||
const absolutePath = !path.isAbsolute(configFile) ? path.join(root, configFile) : configFile; | ||
@@ -34,3 +32,3 @@ const resolvedPath = tryResolve(absolutePath); | ||
export async function addUserPlugins(gasketConfig) { | ||
async function addUserPlugins(gasketConfig) { | ||
const moduleNames = (await Promise.all([ | ||
@@ -59,3 +57,3 @@ resolveUserPlugins(gasketConfig.root, 'plugins'), | ||
*/ | ||
export async function resolveUserPlugins(root, ...parts) { | ||
async function resolveUserPlugins(root, ...parts) { | ||
const dir = path.join(root, ...parts); | ||
@@ -86,3 +84,3 @@ | ||
*/ | ||
export function assignPresetConfig(gasket) { | ||
function assignPresetConfig(gasket) { | ||
const { presets } = gasket.loader.loadConfigured(gasket.config.plugins); | ||
@@ -93,1 +91,7 @@ // @ts-ignore | ||
} | ||
module.exports = { | ||
loadGasketConfigFile, | ||
assignPresetConfig, | ||
addUserPlugins | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { projectIdentifier } from './package-identifier.js'; | ||
const { projectIdentifier } = require('./package-identifier'); | ||
@@ -65,3 +65,3 @@ /** | ||
*/ | ||
export const pluginIdentifier = projectIdentifier('gasket'); | ||
const pluginIdentifier = projectIdentifier('gasket'); | ||
@@ -74,2 +74,8 @@ /** | ||
*/ | ||
export const presetIdentifier = projectIdentifier('gasket', 'preset'); | ||
const presetIdentifier = projectIdentifier('gasket', 'preset'); | ||
module.exports = { | ||
pluginIdentifier, | ||
presetIdentifier | ||
}; |
@@ -1,6 +0,17 @@ | ||
export { Resolver } from './resolver.js'; | ||
export { Loader } from './loader.js'; | ||
export { pluginIdentifier, presetIdentifier } from './identifiers.js'; | ||
export { projectIdentifier } from './package-identifier.js'; | ||
export { loadGasketConfigFile, assignPresetConfig } from './config.js'; | ||
export { flattenPresets } from './preset-utils.js'; | ||
const { Resolver } = require('./resolver'); | ||
const { Loader } = require('./loader'); | ||
const { pluginIdentifier, presetIdentifier } = require('./identifiers'); | ||
const { projectIdentifier } = require('./package-identifier'); | ||
const { loadGasketConfigFile, assignPresetConfig } = require('./config'); | ||
const { flattenPresets } = require('./preset-utils'); | ||
module.exports = { | ||
Resolver, | ||
Loader, | ||
pluginIdentifier, | ||
presetIdentifier, | ||
projectIdentifier, | ||
loadGasketConfigFile, | ||
assignPresetConfig, | ||
flattenPresets | ||
}; |
@@ -1,4 +0,4 @@ | ||
import path from 'path'; | ||
import { Resolver } from './resolver.js'; | ||
import { pluginIdentifier, presetIdentifier } from './identifiers.js'; | ||
const path = require('path'); | ||
const { Resolver } = require('./resolver'); | ||
const { pluginIdentifier, presetIdentifier } = require('./identifiers'); | ||
@@ -58,3 +58,3 @@ /** | ||
*/ | ||
export class Loader extends Resolver { | ||
class Loader extends Resolver { | ||
@@ -79,3 +79,3 @@ constructor() { | ||
*/ | ||
async getModuleInfo(module, moduleName, meta = {}) { | ||
getModuleInfo(module, moduleName, meta = {}) { | ||
const info = { | ||
@@ -90,5 +90,4 @@ name: moduleName, | ||
if (pkgPath) { | ||
const assertion = pkgPath.match(/.json/).length ? { assert: { type: "json" } } : {} | ||
info.path = path.dirname(pkgPath); | ||
info.package = (await this.require(pkgPath, assertion)).default; | ||
info.package = this.require(pkgPath); | ||
info.version = info.package.version; | ||
@@ -108,6 +107,5 @@ info.name = info.package.name; | ||
*/ | ||
async loadModule(moduleName, meta = {}) { | ||
const { default: pkg } = await this.require(`${moduleName}/package.json`, { assert: { type: "json" } }); | ||
const module = await this.require(`${moduleName}/${pkg.main || 'index.js'}`); | ||
return await this.getModuleInfo(module, moduleName, meta); | ||
loadModule(moduleName, meta = {}) { | ||
const module = this.require(moduleName); | ||
return this.getModuleInfo(module, moduleName, meta); | ||
} | ||
@@ -122,3 +120,3 @@ | ||
*/ | ||
async loadPlugin(module, meta = {}) { | ||
loadPlugin(module, meta = {}) { | ||
// If the provide plugin is an already required module, just gather info. | ||
@@ -131,11 +129,11 @@ if (typeof module !== 'string') { | ||
const moduleName = pluginIdentifier(module.name).fullName; | ||
return await this.getModuleInfo(module, moduleName, { ...meta, preloaded: true }); | ||
return this.getModuleInfo(module, moduleName, { ...meta, preloaded: true }); | ||
} | ||
if (isModulePath.test(module)) { | ||
return await this.loadModule(module, meta); | ||
return this.loadModule(module, meta); | ||
} | ||
const identifier = pluginIdentifier.lookup(module, id => this.tryRequire(id.fullName)); | ||
return await this.loadModule(identifier ? identifier.fullName : module, meta); | ||
return this.loadModule(identifier ? identifier.fullName : module, meta); | ||
} | ||
@@ -153,3 +151,3 @@ | ||
// eslint-disable-next-line max-statements | ||
async loadPreset(module, meta, { shallow = false } = {}) { | ||
loadPreset(module, meta, { shallow = false } = {}) { | ||
let moduleName; | ||
@@ -162,3 +160,3 @@ if (isModulePath.test(module)) { | ||
} | ||
const presetInfo = await this.loadModule(moduleName, meta); | ||
const presetInfo = this.loadModule(moduleName, meta); | ||
@@ -177,7 +175,7 @@ const { name: from, dependencies } = presetInfo.package; | ||
if (shallow) { | ||
presets = presetNames.map(async name => await resolver.getModuleInfo(null, name, { from, range: dependencies[name] })); | ||
plugins = pluginNames.map(async name => await resolver.getModuleInfo(null, name, { from, range: dependencies[name] })); | ||
presets = presetNames.map(name => resolver.getModuleInfo(null, name, { from, range: dependencies[name] })); | ||
plugins = pluginNames.map(name => resolver.getModuleInfo(null, name, { from, range: dependencies[name] })); | ||
} else { | ||
presets = presetNames.map(async name => await resolver.loadPreset(name, { from, range: dependencies[name] })); | ||
plugins = pluginNames.map(async name => await resolver.loadPlugin(name, { from, range: dependencies[name] })); | ||
presets = presetNames.map(name => resolver.loadPreset(name, { from, range: dependencies[name] })); | ||
plugins = pluginNames.map(name => resolver.loadPlugin(name, { from, range: dependencies[name] })); | ||
} | ||
@@ -200,8 +198,10 @@ | ||
*/ | ||
async loadConfigured(pluginConfig) { | ||
loadConfigured(pluginConfig) { | ||
if (this._loaded.has(pluginConfig)) return this._loaded.get(pluginConfig); | ||
const { presets = [], add = [], remove = [] } = pluginConfig || {}; | ||
const loadedPresets = await Promise.all(presets.map(name => this.loadPreset(name, { from: 'config' }))); | ||
const loadedPlugins = await Promise.all(add.map(module => this.loadPlugin(module, { from: 'config' }))); | ||
const loadedPresets = presets.map(name => this.loadPreset(name, { from: 'config' })); | ||
const loadedPlugins = add.map(module => this.loadPlugin(module, { from: 'config' })); | ||
let plugins = []; | ||
@@ -243,1 +243,5 @@ | ||
} | ||
module.exports = { | ||
Loader | ||
}; |
@@ -12,3 +12,3 @@ const reScope = /(@[\w-.]+)(\/.+)?/; | ||
*/ | ||
export function matchMaker(projectName, type = 'plugin') { | ||
function matchMaker(projectName, type = 'plugin') { | ||
if (!projectName) throw new Error('projectName required.'); | ||
@@ -31,3 +31,3 @@ return { | ||
*/ | ||
export function expandMaker(projectName, type = 'plugin') { | ||
function expandMaker(projectName, type = 'plugin') { | ||
if (!projectName) throw new Error('projectName required.'); | ||
@@ -68,3 +68,3 @@ const projectScope = `@${projectName}`; | ||
*/ | ||
export function projectIdentifier(projectName, type = 'plugin') { | ||
function projectIdentifier(projectName, type = 'plugin') { | ||
@@ -111,3 +111,2 @@ /** | ||
function createPackageIdentifier(rawName, options) { | ||
console.log('------------hit createPackageIdentifier', rawName); | ||
if (!rawName) { | ||
@@ -400,1 +399,7 @@ // eslint-disable-next-line max-len | ||
} | ||
module.exports = { | ||
matchMaker, | ||
expandMaker, | ||
projectIdentifier | ||
}; |
@@ -10,3 +10,3 @@ /** | ||
*/ | ||
export function flattenPresets(presetInfos = []) { | ||
function flattenPresets(presetInfos = []) { | ||
const flattened = [[...presetInfos]]; | ||
@@ -26,1 +26,5 @@ | ||
} | ||
module.exports = { | ||
flattenPresets | ||
}; |
@@ -1,3 +0,2 @@ | ||
import { default as diagnostics } from 'diagnostics'; | ||
const debug = diagnostics('gasket:resolver'); | ||
const debug = require('diagnostics')('gasket:resolver'); | ||
@@ -20,3 +19,3 @@ /** | ||
*/ | ||
export class Resolver { | ||
class Resolver { | ||
/** | ||
@@ -30,2 +29,3 @@ * @param {object} options - Options | ||
resolveFrom, | ||
require: _require | ||
} = options || {}; | ||
@@ -36,3 +36,3 @@ | ||
} | ||
this._import = async (path, assertion) => await import(path, assertion); | ||
this._require = _require || require; | ||
} | ||
@@ -47,3 +47,4 @@ | ||
resolve(moduleName) { | ||
return import.meta.resolve(moduleName); | ||
const options = this._resolveFrom ? { paths: this._resolveFrom } : {}; | ||
return this._require.resolve(moduleName, options); | ||
} | ||
@@ -57,7 +58,5 @@ | ||
*/ | ||
async require(moduleName, assertion = {}) { | ||
console.log('------------hit require') | ||
require(moduleName) { | ||
const modulePath = this.resolve(moduleName); | ||
console.log('------------hit modulePath', modulePath) | ||
return await this._import(modulePath, assertion); | ||
return this._require(modulePath); | ||
} | ||
@@ -91,6 +90,6 @@ | ||
*/ | ||
async tryRequire(moduleName) { | ||
tryRequire(moduleName) { | ||
try { | ||
debug('try-require', moduleName); | ||
return await this.require(moduleName); | ||
return this.require(moduleName); | ||
} catch (err) { | ||
@@ -106,1 +105,5 @@ debug('try-require error', err.message); | ||
} | ||
module.exports = { | ||
Resolver | ||
}; |
{ | ||
"name": "@gasket/resolve", | ||
"version": "6.43.2-canary-esm-port.0", | ||
"version": "6.44.0", | ||
"description": "Essential module resolution & configuration management for gasket plugins & presets.", | ||
"main": "lib/index.js", | ||
"main": "lib", | ||
"types": "types", | ||
@@ -12,3 +12,2 @@ "files": [ | ||
], | ||
"type": "module", | ||
"scripts": { | ||
@@ -52,3 +51,3 @@ "lint": "eslint .", | ||
"devDependencies": { | ||
"@gasket/engine": "^6.43.2-canary-esm-port.0", | ||
"@gasket/engine": "^6.44.0", | ||
"@godaddy/dmd": "^1.0.0", | ||
@@ -62,3 +61,3 @@ "eslint": "^8.7.0", | ||
"jest": "^29.3.1", | ||
"jsdoc-to-markdown": "^8.0.0" | ||
"jsdoc-to-markdown": "^7.1.0" | ||
}, | ||
@@ -83,3 +82,3 @@ "eslintConfig": { | ||
}, | ||
"gitHead": "f7e3283fa63e20f353587fe7f4653ebf684538c8" | ||
"gitHead": "49d5341c44d1d87e03b948ad22867ae289da4069" | ||
} |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1269
1
64672
3
No