-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitall
executable file
·139 lines (124 loc) · 3.57 KB
/
gitall
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
const { spawn } = require("child_process");
const fs = require("fs").promises;
const os = require("os");
const path = require("path");
const chalk = require("chalk");
const byFilterArg = (match) => {
if (match === undefined) return () => true;
return (s) =>
match
.split(" ")
.map((s) => s.trim())
.reduce((matches, m) => matches || s.includes(m), false);
};
const run = async (cmd, cwd = process.cwd(), progress = false) =>
new Promise((resolve, reject) => {
const [command, ...args] = cmd;
const p = spawn(command, args, { cwd });
const result = [];
const errors = [];
p.on("close", (code) => {
if (code !== 0) {
return reject(
new Error(
`${command} ${args && args.join(" ")} failed: ${errors.join(
os.EOL
)}`
)
);
}
return resolve(result.join(os.EOL));
});
p.stdout.on("data", (data) => {
result.push(data);
if (data && progress) {
data
.toString()
.split(os.EOL)
.forEach((l) => {
console.log(progress, l);
});
}
});
p.stderr.on("data", (data) => {
errors.push(data);
});
});
const gitall = async (cmd) => {
const folders = (await run(["ls", "-1"])).split(os.EOL).filter((f) => f);
const gitFolders = (
await Promise.all(
folders.filter(byFilterArg(process.env.REPO_FILTER)).map((folder) =>
fs
.stat(path.resolve(process.cwd(), folder, ".git"))
.then(() => ({ folder, isGit: true }))
.catch(() => ({ folder, isGit: false }))
)
)
).filter(({ isGit }) => isGit);
let ignored = [];
try {
ignored = (
await fs.readFile(path.join(process.cwd(), ".gitallignore"), "utf-8")
)
.trim()
.split("\n")
.map((s) => s.trim());
} catch {}
ignored.push(
...folders.filter((s) => !byFilterArg(process.env.REPO_FILTER)(s))
);
ignored.forEach((folder) => {
console.log(chalk.magenta(`[${folder}]`), chalk.grey("Ignored"));
});
const branches = new Map();
const changes = new Map();
await Promise.all(
gitFolders
.filter(({ folder }) => !ignored.includes(folder))
.map(async ({ folder }) => {
const wd = path.resolve(process.cwd(), folder);
const [branch, hasChanges] = await Promise.all([
run(["git", "rev-parse", "--abbrev-ref", "HEAD"], wd),
run(["git", "status", "--porcelain"], wd),
]);
branches.set(folder, branch.trim());
changes.set(folder, hasChanges.trim().length > 0);
})
);
const label = (folder) => {
const l = [];
if (changes.get(folder)) l.push(chalk.green(" *"));
l.push(
chalk.magenta(folder),
chalk.grey("@"),
chalk.blueBright(branches.get(folder)),
chalk.grey(">")
);
return l;
};
const res = await Promise.all(
gitFolders
.filter(({ folder }) => !ignored.includes(folder))
.map(async ({ folder }) => {
const wd = path.resolve(process.cwd(), folder);
console.log(...label(folder), chalk.yellow(wd));
try {
await run(cmd, wd, label(folder).join(" "));
return { success: true, folder };
} catch (error) {
return { success: false, error, folder };
}
})
);
res.forEach(({ success, error, folder }) => {
if (success) {
console.log(...label(folder), chalk.greenBright("OK"));
} else {
console.error(...label(folder), chalk.red(error));
}
});
};
const [, , ...cmd] = process.argv;
gitall(cmd);