Created
April 23, 2019 13:17
-
-
Save optii/dbfdff082a9dc7399b9865ab9d0b3a5c to your computer and use it in GitHub Desktop.
A quick script to update projects to move to @Hapi scoped packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const FastGlob = require('fast-glob'); | |
const LatestVersion = require('latest-version'); | |
const ReplaceInFile = require('replace-in-file'); | |
const Fs = require('fs'); | |
const scope = '@hapi/'; // Scope to add to modules | |
const replaceRequireGlobs = [`${ process.cwd() }/lib/**/*.js`, `${ process.cwd() }/lib/*.js`]; // List of globs or files to replace any requires in | |
const updateVersion = true; // True to update to the latest version of the dep when changing the scope | |
const modules = [ | |
'accept', | |
'address', | |
'ammo', | |
'b64', | |
'basic', | |
'bell', | |
'boom', | |
'bossy', | |
'bounce', | |
'call', | |
'catbox', | |
'catbox-memecached', | |
'catbox-memory', | |
'catbox-redis', | |
'code', | |
'content', | |
'cookie', | |
'crumb', | |
'cryptiles', | |
'eslint-plugin-hapi', | |
'glue', | |
'good', | |
'good-console', | |
'good-squeeze', | |
'h2o2', | |
'hapi', | |
'hawk', | |
'heavy', | |
'hoek', | |
'inert', | |
'iron', | |
'joi', | |
'joi-date', | |
'lab', | |
'mimos', | |
'nes', | |
'nigel', | |
'oppsy', | |
'pez', | |
'podium', | |
'rule-capitalize-modules', | |
'rule-for-loop', | |
'rule-no-arrowception', | |
'rule-no-var', | |
'rule-scope-start', | |
'scooter', | |
'shot', | |
'sntp', | |
'somever', | |
'statehood', | |
'subtext', | |
'teamwork', | |
'topo', | |
'vise', | |
'vision', | |
'wreck', | |
'yar' | |
]; // List of modules to look up & replace | |
const updateScopedDeps = async (deps = {}, withVersion = true) => { | |
const updatedDeps = {}; | |
for (const dep of Object.keys(deps)) { | |
if (modules.includes(dep)) { | |
const scopedName = `${ scope }${ dep }`; | |
updatedDeps[scopedName] = withVersion ? await LatestVersion(scopedName) : deps[dep]; | |
await ReplaceInFile({ | |
files : replaceRequireGlobs, | |
from : new RegExp(`require\\('${ dep }'\\)`), | |
to : `require('${ scopedName }')` | |
}); | |
} | |
else { | |
updatedDeps[dep] = deps[dep]; | |
} | |
} | |
return updatedDeps; | |
}; | |
const run = async () => { | |
const paths = await FastGlob('**/package.json', { | |
ignore : ['**/node_modules/**/package.json'], | |
deep : 5 | |
}); | |
for (const path of paths) { | |
const packagePath = `${ process.cwd() }/${ path }`; | |
const pkg = require(packagePath); | |
const [scopedDependencies, scopedDevDependencies, scopedPeerDependencies] = await Promise.all([ | |
updateScopedDeps(pkg.dependencies, updateVersion), | |
updateScopedDeps(pkg.devDependencies, updateVersion), | |
updateScopedDeps(pkg.peerDependencies, updateVersion) | |
]); | |
Fs.writeFileSync(packagePath, JSON.stringify({ | |
...pkg, | |
dependencies : scopedDependencies, | |
devDependencies : scopedDevDependencies, | |
peerDependencies : scopedPeerDependencies | |
}, null, 4)); | |
} | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment