Skip to content

Instantly share code, notes, and snippets.

@yamadayuki
Last active July 21, 2020 12:16
Show Gist options
  • Save yamadayuki/93ac753dd0ad1fb46ec5ac1cc525dc33 to your computer and use it in GitHub Desktop.
Save yamadayuki/93ac753dd0ad1fb46ec5ac1cc525dc33 to your computer and use it in GitHub Desktop.
hard-update
#!/usr/bin/env node
// @ts-check
const { execSync } = require("child_process");
const { manifest } = require("pacote");
const { join } = require("path");
const { promises } = require("fs");
function getPackageJsonFiles() {
return execSync("git ls-files | grep package.json")
.toString()
.trim()
.split("\n")
.map((s) => s.trim());
}
/**
* @param {string} pkgJsonFilepath
*/
async function updatePackageJson(pkgJsonFilepath) {
const pkgJson = require(pkgJsonFilepath);
if (!pkgJson.dependencies && !pkgJson.devDependencies) {
return;
}
const targetDeps = Object.keys(pkgJson.dependencies || {});
const targetDevDeps = Object.keys(pkgJson.devDependencies || {});
const depsPromises = targetDeps.map((packageName) => manifest(packageName));
const devDepsPromises = targetDevDeps.map((packageName) => manifest(packageName));
const dependencyManifests = (await Promise.all(depsPromises)).map(({ name, version }) => [name, version]);
const devDependencyManifests = (await Promise.all(devDepsPromises)).map(({ name, version }) => [name, version]);
const newPkgJson = JSON.parse(JSON.stringify(pkgJson));
if (newPkgJson.dependencies) {
dependencyManifests.forEach(([packageName, version]) => {
newPkgJson.dependencies[packageName] = `^${version}`;
});
}
if (newPkgJson.devDependencies) {
devDependencyManifests.forEach(([packageName, version]) => {
newPkgJson.devDependencies[packageName] = `^${version}`;
});
}
await promises.writeFile(pkgJsonFilepath, `${JSON.stringify(newPkgJson, null, " ")}\n`, {
encoding: "utf-8",
flag: "w",
});
}
function main() {
const entries = getPackageJsonFiles();
entries.forEach(async (entry) => {
await updatePackageJson(join(process.cwd(), entry));
});
}
try {
main();
} catch (err) {
console.error(err);
process.exit(1);
}
{
"name": "hard-update",
"version": "1.0.0",
"bin": "./index.js",
"license": "MIT",
"dependencies": {
"pacote": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment