-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.js
More file actions
122 lines (100 loc) · 2.91 KB
/
push.js
File metadata and controls
122 lines (100 loc) · 2.91 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
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
import { spawnSync } from "child_process";
import { createInterface } from "readline";
import config from "./config.js";
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const repos = [...config.repos]; // Make a shallow copy to avoid modifying the original config
function hasStagedChanges(repoPath) {
const statusProcess = spawnSync("git", ["status", "--porcelain"], {
cwd: repoPath,
});
if (statusProcess.error) {
throw statusProcess.error;
}
const output = statusProcess.stdout.toString();
const lines = output.split("\n");
// Check for lines that start with A, M, or D followed by a space,
// which indicates staged additions, modifications, or deletions.
return lines.some(line => /^(A |M |D )/.test(line));
}
async function pushRepo(repoPath) {
if (!hasStagedChanges(repoPath)) {
console.log(`No changes to push for ${repoPath}`);
pushNextRepo();
return;
}
const commitMessage = await new Promise((resolve) => {
rl.question(`Enter commit message for ${repoPath} (press ⏎ to skip): `, resolve);
});
if (!commitMessage) {
console.log("Commit message is required. Skipping push.");
pushNextRepo();
return;
}
const commitProcess = spawnSync("git", ["commit", "-am", commitMessage], {
cwd: repoPath,
stdio: "inherit",
});
if (commitProcess.error || commitProcess.status !== 0) {
console.error(`Error committing in repo ${repoPath}`);
pushNextRepo();
return;
}
// Check if repo is an npm package
if (!!config.isNpm?.[repoPath]) {
const answer = await new Promise((resolve) => {
rl.question(
`Do you want to release ${repoPath} (npm package)? (y/n): `,
resolve
);
});
if (answer.toLowerCase() === "y") {
// Run npm release
const releaseProcess = spawnSync("npm", ["run", "release"], {
cwd: repoPath,
stdio: "inherit",
});
if (releaseProcess.error || releaseProcess.status !== 0) {
console.error(`Error releasing npm package in repo ${repoPath}`);
pushNextRepo();
return;
}
}
}
pushChanges(repoPath);
}
// Separate push logic into its own function for reusability
function pushChanges(repoPath) {
const pushProcess = spawnSync("git", ["push", "--follow-tags"], {
cwd: repoPath,
stdio: "inherit",
});
// Handle any push errors
if (pushProcess.error || pushProcess.status !== 0) {
console.error(`Error pushing repo ${repoPath}`);
pushNextRepo();
return;
}
console.log(`Pushed changes for ${repoPath}`);
pushNextRepo();
}
function pushNextRepo() {
if (repos.length > 0) {
const repoPath = repos.shift();
pushRepo(repoPath);
} else {
console.log("All repositories have been pushed.");
rl.close();
}
}
function main() {
if (repos.length > 0) {
pushNextRepo();
} else {
console.log("No repositories to push.");
rl.close();
}
}
main();