Created
June 6, 2019 11:55
-
-
Save nodkz/a775f2f35561a5ad44ff152cb0b7fe1c to your computer and use it in GitHub Desktop.
Make fixed versions for packages in package.json according to installed packages (w/ Lerna support)
This file contains 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
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