Created
November 17, 2021 06:36
-
-
Save odiak/ddb57e02a1377c1ebacf7d574921d536 to your computer and use it in GitHub Desktop.
Execute same command on multiple directories
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
#! 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