Skip to content

Instantly share code, notes, and snippets.

@fivethreeo
Created September 18, 2020 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fivethreeo/3fdc7dadc15d2fd2cf96b0dc8465227c to your computer and use it in GitHub Desktop.
Save fivethreeo/3fdc7dadc15d2fd2cf96b0dc8465227c to your computer and use it in GitHub Desktop.
addWorkspaces:(stageName) => {
const stagePath = path.join(rootDir, stageName);
const packagesPath = path.join(rootDir, 'packages');
const packageJson = path.join(stagePath, 'package.json');
if (fs.existsSync(packageJson)) {
const dirs = fs.readdirSync(packagesPath, { withFileTypes:true })
.filter(dirent=>dirent.isDirectory()).map(dir=>dir.name);
let workspaces = [];
let packages = [];
for (const packageName of dirs) {
packages.push(packageName);
workspaces.push(path.join(packagesPath, packageName));
}
const packageJsonData = JSON.parse(fs.readFileSync(packageJson));
const newPackageJsonData = ["dependencies", "devDependencies"].reduce((acc, depType) => {
if (acc[depType]) {
acc[depType] = Object.keys(acc[depType]).reduce((depsAcc, dep) => {
if (packages.includes(dep)) {
delete depsAcc[dep];
}
return depsAcc;
}, acc[depType]);
}
return acc;
}, packageJsonData);
newPackageJsonData.workspaces = workspaces;
newPackageJsonData.private = true;
return fs.writeFileSync(packageJson, JSON.stringify(newPackageJsonData, null, ' '));
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment