forked from silexlabs/Silex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.js
More file actions
executable file
·47 lines (41 loc) · 1.77 KB
/
exec.js
File metadata and controls
executable file
·47 lines (41 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
// This script executes a command in each submodule
// It supports replacing `{{branch}}` with the branch name
// In most cases you will want to use `npm run exec` instead of running this script directly
// Or `git submodule foreach --recursive 'echo "Running command in $name"'`
import { readFileSync } from 'fs';
import { spawnSync } from 'child_process';
// Get the command from the command-line arguments
const command = process.argv[2];
// Ensure a command is provided
if (!command) {
console.error('Please provide a command to execute.');
process.exit(1);
}
// Get repos paths and branch from .gitmodules
const gitmodules = readFileSync('.gitmodules', 'utf8');
const repos = gitmodules.match(/\[submodule "(.*)"\]/g).map(repo => repo.match(/\[submodule "(.*)"\]/)[1]);
const branches = gitmodules.match(/branch = (.*)/g).map(branch => branch.match(/branch = (.*)/)[1]);
repos.forEach((repo, index) => {
const branch = branches[index];
try {
// Display a separator and message
// Display `file` in a box
console.log(`┌${'─'.repeat(repo.length + 2)}┐`);
console.log(`│ ${repo} │ `);
console.log(`└${'─'.repeat(repo.length + 2)}┘`);
// Substitute branch in command
const replaced = command.replace(/{{branch}}/g, branch);
console.log('\x1b[90m%s\x1b[0m', `Executing command \`${replaced}\``);
// Change directory and execute the command
process.chdir(`${__dirname}/../${repo}`);
const result = spawnSync(replaced, { shell: true, stdio: 'inherit' });
console.log('\n');
if(result.status !== 0) {
// Red error
console.error('\x1b[31m%s\x1b[0m', `Error executing command:`, result.error, '\n\n');
}
} catch (error) {
console.error(`Error executing command in ${repo}:`, error);
}
});