Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make fixed versions for packages in package.json according to installed packages (w/ Lerna support)
import * as glob from 'glob';
import * as fs from 'fs';
import * as path from 'path';
async function changePackage(ppp: any) {
try {
const filesToUpdate: string[] = await new Promise((resolve, reject) => {
glob(ppp, function(err, files) {
if (err) return reject(err);
return resolve(files);
});
});
for (const file of filesToUpdate) {
console.log('Updating file: ', file);
const packageFile = JSON.parse(fs.readFileSync(file, 'utf8'));
const dependencies = packageFile.dependencies;
for (const dependency in dependencies) {
const depPackageFile = JSON.parse(
fs.readFileSync(
`${path.resolve(process.cwd(), 'node_modules')}/${dependency}/package.json`,
'utf8'
)
);
packageFile.dependencies[dependency] = `${depPackageFile.version}`;
}
const devDependencies = packageFile.devDependencies;
for (const dependency in devDependencies) {
const depPackageFile = JSON.parse(
fs.readFileSync(
`${path.resolve(process.cwd(), 'node_modules')}/${dependency}/package.json`,
'utf8'
)
);
packageFile.devDependencies[dependency] = `${depPackageFile.version}`;
}
fs.writeFileSync(file, JSON.stringify(packageFile, null, 2), { flag: 'w+' });
}
} catch (e) {
console.log('ERROR: ', e);
}
}
changePackage(`${path.resolve(process.cwd(), 'apps')}/*/package.json`);
changePackage(`${path.resolve(process.cwd(), 'packages')}/*/package.json`);
changePackage(`package.json`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.