Last active
November 11, 2019 06:45
-
-
Save loopmode/318e881454dc0498874a4e764d3dce55 to your computer and use it in GitHub Desktop.
Node.js function to get the names of all devDependencies in a yarn or lerna workspace
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
module.exports = async function getDevDependencies(globs = ['package.json', 'packages/*/*.json']) { | |
const globby = require('globby'); | |
const packageFiles = await globby(globs, { absolute: true }); | |
return packageFiles.reduce((result, file) => { | |
const pkg = require(file); | |
if (pkg.devDependencies) { | |
const names = Object.keys(pkg.devDependencies); | |
return result.concat(names.filter(name => !result.includes(name))); | |
} | |
return result; | |
}, []).sort(); | |
} |
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
(async () => { | |
const getDevDependencies = require('./get-dev-dependencies'); | |
const devDependencies = await getDevDependencies(['package.json', 'client/*/*.json', 'server/*/*.json']); | |
console.log(devDependencies) | |
})(); |
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
// see https://gist.github.com/pxwise/c4833557930a5141c4e3c752347e2f2e | |
/** | |
* Approximate `npm prune --production` using `yarn remove`. | |
* @see https://github.com/yarnpkg/yarn/issues/696 | |
*/ | |
const exec = require('child_process').exec; | |
const getDevDependencies = require('./get-dev-dependencies'); | |
(async () => { | |
// given a package.json with "workspaces": ["client/*", "server/*"] | |
const devDependencies = await getDevDependencies(['package.json', 'client/*/*.json', 'server/*/*.json']).join(', '); | |
const command = 'yarn remove ' + devDependencies; | |
const child = exec(command, (err, stdout, stderr) => { | |
if (err) throw err; | |
console.log(`stdout: \n${stdout}`); | |
console.log(`stderr: \n${stderr}`); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment