Skip to content

Instantly share code, notes, and snippets.

@odiak
Created November 17, 2021 06:36
Show Gist options
  • Save odiak/ddb57e02a1377c1ebacf7d574921d536 to your computer and use it in GitHub Desktop.
Save odiak/ddb57e02a1377c1ebacf7d574921d536 to your computer and use it in GitHub Desktop.
Execute same command on multiple directories
#! node
// example:
// each-dir dir1 somewhere/dir2 -- npm ci
const { spawn } = require("child_process");
(async () => {
const args = process.argv.slice(2);
const i = args.indexOf("--");
if (i === -1) throw new Error("specify command after --");
const dirs = args.slice(0, i);
const command = args.slice(i + 1);
for (const dir of dirs) {
await new Promise((resolve, reject) => {
const [commandName, ...commandArgs] = command;
spawn(commandName, commandArgs, {
cwd: dir,
stdio: ["inherit", "inherit", "inherit"]
}).on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment