Skip to content

Instantly share code, notes, and snippets.

@lilianalillyy
Created February 7, 2024 08:22
Show Gist options
  • Save lilianalillyy/1670fd76e9c689f41b2ea274755a644a to your computer and use it in GitHub Desktop.
Save lilianalillyy/1670fd76e9c689f41b2ea274755a644a to your computer and use it in GitHub Desktop.
const { exec } = require("child_process");
const { statSync, existsSync, readFileSync } = require("fs");
const path = require("path");
const { sync: glob } = require("glob");
const DEPENDENCY = "axios";
const reinstall = (pth) => {
return new Promise((resolve, reject) => {
const packageJsonPath = path.join(pth, "package.json");
if (!existsSync(packageJsonPath)) {
console.warn(packageJsonPath, "not found, skipping");
return;
}
const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
const deps = Object.keys(pkg.dependencies ?? {});
const devDeps = Object.keys(pkg.devDependencies ?? {});
const depsHasDependency = deps.includes(DEPENDENCY);
const devDepsHasDependency = devDeps.includes(DEPENDENCY);
const hasDependency = depsHasDependency || devDepsHasDependency;
if (!hasDependency) {
console.log(
`${packageJsonPath} doesn't have ${DEPENDENCY} as a dependency`
);
return;
}
const isDevDependency = !depsHasDependency && devDepsHasDependency;
const cmd = `cd ${pth} && npm remove ${DEPENDENCY} && npm install ${
isDevDependency ? "-D" : ""
} ${DEPENDENCY}`;
const e = exec(cmd);
if (!e.stdout) return reject(new Error("There's no stdout."));
e.stdout.pipe(process.stdout);
e.stderr.pipe(process.stdout);
e.stdout.on("end", resolve);
});
};
const run = async () => {
const workspaces = glob(path.join(__dirname, "*")).filter(
(pth) => statSync(pth).isDirectory() && !pth.endsWith("node_modules")
);
await Promise.all(workspaces.map(reinstall));
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment