@ministryofjustice/module-alias
Advanced tools
Comparing version 1.0.6 to 1.0.7
81
index.js
@@ -54,3 +54,3 @@ const Module = require('module') | ||
function addPath (modulePath) { | ||
const normalizedModulePath = path.normalize(path.join(getPackagePath(), modulePath)) | ||
const normalizedModulePath = path.normalize(path.join(PACKAGE_PATH, modulePath)) | ||
@@ -93,10 +93,10 @@ if (!MODULE_PATH_LIST.includes(normalizedModulePath)) { | ||
function addAlias (alias, modulePath, aliasPathCache = ALIAS_PATH_CACHE) { | ||
const normalizedModulePath = path.normalize(path.join(getPackagePath(), modulePath)) | ||
function addAlias (alias, modulePath) { | ||
const normalizedModulePath = path.normalize(path.join(PACKAGE_PATH, modulePath)) | ||
aliasPathCache.set(alias, normalizedModulePath) | ||
ALIAS_KEY_LIST = Array.from(aliasPathCache.keys()).sort().reverse() | ||
ALIAS_PATH_CACHE.set(alias, normalizedModulePath) | ||
ALIAS_KEY_LIST = Array.from(ALIAS_PATH_CACHE.keys()).sort().reverse() | ||
} | ||
function reset (packageBaseCache = PACKAGE_BASE_CACHE, aliasPathCache = ALIAS_PATH_CACHE) { | ||
function reset () { | ||
Object.keys(require.cache).forEach((key) => { delete require.cache[key] }) | ||
@@ -138,4 +138,4 @@ | ||
packageBaseCache.clear() | ||
aliasPathCache.clear() | ||
PACKAGE_BASE_CACHE.clear() | ||
ALIAS_PATH_CACHE.clear() | ||
@@ -165,8 +165,18 @@ ALIAS_KEY_LIST = [] | ||
function getPackagePath () { | ||
return PACKAGE_PATH | ||
} | ||
function findPackageJson (packagePath) { | ||
try { | ||
/* | ||
* Skip over `require` | ||
*/ | ||
const filePath = path.join(packagePath, 'package.json') | ||
const fileData = fs.readFileSync(filePath, 'utf8') | ||
const json = JSON.parse(fileData) | ||
function setPackagePath (packagePath) { | ||
PACKAGE_PATH = packagePath | ||
return ( | ||
Reflect.has(json, '_moduleAliases') || | ||
Reflect.has(json, '_moduleDirectories') | ||
) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
@@ -176,34 +186,22 @@ | ||
let packagePath = path.dirname(packageBase) | ||
const packagePaths = [packageBase, packagePath] | ||
const packagePaths = [ | ||
packageBase, | ||
packagePath | ||
] | ||
while (packagePath !== (packageBase = path.dirname(packagePath))) packagePaths.push(packagePath = packageBase) | ||
return packagePaths.find((packagePath) => { | ||
try { | ||
/* | ||
* Skip over `require` | ||
*/ | ||
const fileData = fs.readFileSync(path.join(packagePath, 'package.json'), 'utf8') | ||
const json = JSON.parse(fileData) | ||
return ( | ||
Reflect.has(json, '_moduleAliases') || | ||
Reflect.has(json, '_moduleDirectories') | ||
) | ||
} catch (e) { | ||
return false | ||
} | ||
}) | ||
return packagePaths.find(findPackageJson) | ||
} | ||
function getPackagePathFromCache (packageBase, packageBaseCache = PACKAGE_BASE_CACHE) { | ||
if (packageBaseCache.has(packageBase)) return packageBaseCache.get(packageBase) | ||
function getPackagePathFromCache (packageBase) { | ||
if (PACKAGE_BASE_CACHE.has(packageBase)) return PACKAGE_BASE_CACHE.get(packageBase) | ||
} | ||
function setPackagePathIntoCache (packageBase, packagePath, packageBaseCache = PACKAGE_BASE_CACHE) { | ||
if (!packageBaseCache.has(packageBase)) packageBaseCache.set(packageBase, packagePath) | ||
function setPackagePathIntoCache (packageBase, packagePath) { | ||
if (!PACKAGE_BASE_CACHE.has(packageBase)) PACKAGE_BASE_CACHE.set(packageBase, packagePath) | ||
} | ||
function removePackageBaseFromCache (packageBase, packageBaseCache = PACKAGE_BASE_CACHE) { | ||
packageBaseCache.delete(packageBase) | ||
function removePackageBaseFromCache (packageBase) { | ||
PACKAGE_BASE_CACHE.delete(packageBase) | ||
} | ||
@@ -216,12 +214,13 @@ | ||
*/ | ||
function register (packageBase = process.cwd(), packageBaseCache = PACKAGE_BASE_CACHE) { | ||
function register (packageBase = process.cwd()) { | ||
packageBase = packageBase.replace(/\/package\.json$/, '') | ||
const packagePath = getPackagePathFromCache(packageBase, packageBaseCache) || getPackagePathFromFileSystem(packageBase) | ||
const packagePath = getPackagePathFromCache(packageBase) || getPackagePathFromFileSystem(packageBase) | ||
if (!packagePath) throw new Error(`(1) No \`package.json\` found for ${packageBase}`) | ||
setPackagePath(packagePath) | ||
setPackagePathIntoCache(packageBase, packagePath, packageBaseCache) | ||
PACKAGE_PATH = packagePath | ||
setPackagePathIntoCache(packageBase, packagePath) | ||
try { | ||
@@ -239,3 +238,3 @@ const packageJson = require(path.join(packagePath, 'package.json')) | ||
} catch (e) { | ||
removePackageBaseFromCache(packageBaseCache) | ||
removePackageBaseFromCache(packageBase) | ||
@@ -242,0 +241,0 @@ throw new Error(`(2) No \`package.json\` found for ${packageBase}`) |
{ | ||
"name": "@ministryofjustice/module-alias", | ||
"description": "Module aliases (based on work by Nick Gavrilov artnikpro@gmail.com)", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"author": "Form Builder Team <form-builder-team@digital.justice.gov.uk>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
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
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
10024
183