-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
Β·152 lines (136 loc) Β· 4.02 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const { execSync, spawnSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const hookScript = path.resolve(
path.dirname(require.main.filename),
"scripts/commit-msg"
);
const dummyPackageJSON = path.resolve(
path.dirname(require.main.filename),
"scripts/dummy-package.json"
);
const configKey = "coworking.coauthor";
let [...args] = process.argv.slice(2);
const errorCodes = {
NO_ERROR: 0,
ALREADY_COWORKING: 2,
USAGE: 3,
EXISTING_HOOK: 4,
NOT_COWORKING: 5,
NOT_IN_REPO: 6,
COAUTHOR_NO_FOUND: 7,
};
function showUsage() {
console.log("coworking-with [-h] [--stop] <username>...");
process.exit(errorCodes.USAGE);
}
async function getUserInfoFromGitHub(username) {
const response = await fetch(
`https://api.github.com/search/commits?q=author:${username}&user:${username}&per_page=1`,
{ headers: { Accept: "application/vnd.github.cloak-preview" } }
);
const json = await response.json();
return json.items[0] && json.items[0].commit.author;
}
function getGitRoot() {
const { stdout, status } = spawnSync("git", ["rev-parse", "--show-toplevel"]);
if (status === 0) {
return stdout.toString().trim()
}
}
const coworkers = [];
async function main() {
const gitRoot = getGitRoot();
if (!gitRoot) {
console.log("You aren't in a git repository");
process.exit(errorCodes.NOT_IN_REPO);
}
const repoHookLocation = path.resolve(gitRoot, ".git/hooks/commit-msg");
if (args.includes("-h") || args.includes("--help")) {
showUsage();
} else if (args.includes("--stop")) {
const { status } = spawnSync("git", ["config", configKey]);
if (status !== 0) {
console.log("You weren't coworking!");
process.exit(errorCodes.NOT_COWORKING);
}
execSync(`git config --unset-all ${configKey}`);
if (fs.existsSync(repoHookLocation)) {
fs.unlinkSync(repoHookLocation);
fs.unlinkSync(path.resolve(gitRoot, ".git/hooks/package.json"));
}
console.log("Hope you had a good time!");
process.exit(errorCodes.NO_ERROR);
} else {
const { stdout, status } = spawnSync(
"git",
["config", "--get-all", configKey],
{ encoding: "utf8" }
);
if (status === 0) {
console.log(
`You are already working with ${stdout
.split("\n")
.filter((x) => x)
.join(", ")}`
);
process.exit(errorCodes.ALREADY_COWORKING);
}
if (args.length === 0) {
showUsage();
}
if (fs.existsSync(repoHookLocation)) {
// TODO: Create git hook docs.
console.log(
"You are already have a `commit-msg` git hook. See [URL] for fixes."
);
process.exit(errorCodes.EXISTING_HOOK);
}
for (const coauthor of args) {
const { stdout } = spawnSync(
"git",
[
"log",
"--no-merges",
"-1",
"--author",
coauthor,
"--format=%an <%ae>",
],
{ encoding: "utf8" }
);
if (stdout) {
coworkers.push(stdout.trim());
} else {
console.log(
`Coauthor '${coauthor}' was not found in git log. Trying to fetch info from GitHub..`
);
try {
const { name, email } = await getUserInfoFromGitHub(coauthor);
console.log(`Found the user in GitHub!`);
coworkers.push(`${name} <${email}>`);
} catch (error) {
console.log("Failed finding the user in GitHub");
}
}
}
if (coworkers.length === args.length) {
fs.copyFileSync(hookScript, repoHookLocation);
fs.copyFileSync(
dummyPackageJSON,
path.resolve(gitRoot, ".git/hooks/package.json")
);
for (const coworker of coworkers) {
spawnSync("git", ["config", "--add", configKey, coworker]);
}
console.log("Happy coworking!");
process.exit(errorCodes.NO_ERROR);
} else {
console.log("Coworking session failed to start.");
process.exit(errorCodes.COAUTHOR_NO_FOUND);
}
}
}
main();