Last active
May 12, 2021 09:09
-
-
Save OlaoluwaM/601740b151930a892e0d4d7728cd5ec5 to your computer and use it in GitHub Desktop.
A script that auto updates dependencies defined in your template.json file for those who have custom CRA templates
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
#!/usr/bin/env node | |
import fsPromise from 'fs/promises'; | |
import { exec } from 'child_process'; | |
import { promisify } from 'util'; | |
const promisifiedExec = promisify(exec); | |
const normalize = str => (str === '' ? false : true); | |
const templateJSOnFile = JSON.parse( | |
await fsPromise.readFile('./template.json', { encoding: 'utf-8' }) | |
); | |
// TODO add support for devDependencies | |
const dependencies = Object.entries(templateJSOnFile.package.dependencies); | |
const promiseResults = await Promise.allSettled( | |
dependencies.map(async ([packageName, currVer]) => { | |
try { | |
const { stdout, stderr } = await promisifiedExec(`npm show ${packageName} version`); | |
if (normalize(stderr)) throw stderr; | |
return [packageName, `^${stdout.replace('\n', '')}`]; | |
} catch (error) { | |
console.error(error); | |
return [packageName, currVer]; | |
} | |
}) | |
); | |
const updatedDeps = promiseResults.map(({ value }) => value); | |
templateJSOnFile.package.dependencies = Object.fromEntries(updatedDeps); | |
await fsPromise.writeFile('./template.json', JSON.stringify(templateJSOnFile, null, 2)); | |
console.log('Packages updated!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment