-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.git.js
More file actions
219 lines (173 loc) · 5.54 KB
/
util.git.js
File metadata and controls
219 lines (173 loc) · 5.54 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Generic git utility for waw (cross-platform, explicit, safe-by-default)
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const { execSync } = require("node:child_process");
const rmOpts = { recursive: true, force: true };
// ---------- helpers ----------
const exec = (dir, cmd, opts = {}) => {
const silent = !!opts.silent;
return execSync(cmd, {
cwd: dir,
stdio: silent ? "ignore" : "inherit",
});
};
const ensureDir = (p) => fs.mkdirSync(p, { recursive: true });
const exists = (p) => fs.existsSync(p);
const hasGit = () => {
try {
execSync("git --version", { stdio: "ignore" });
return true;
} catch {
return false;
}
};
// ---------- low-level ----------
const hasRepo = (dir) => exists(path.join(dir, ".git"));
const remove = (dir) => {
const g = path.join(dir, ".git");
if (exists(g)) fs.rmSync(g, rmOpts);
};
const init = (dir, opts = {}) => {
if (!exists(dir)) ensureDir(dir);
if (!hasRepo(dir)) exec(dir, "git init", opts);
};
const setOrigin = (dir, repo, opts = {}) => {
try {
exec(dir, "git remote remove origin", { silent: true });
} catch {}
exec(dir, `git remote add origin ${repo}`, opts);
};
const fetch = (dir, opts = {}) => exec(dir, "git fetch --all --prune", opts);
const checkout = (dir, branch, force = false, opts = {}) => {
if (force) {
exec(dir, `git checkout -B ${branch} origin/${branch}`, opts);
exec(dir, `git reset --hard origin/${branch}`, opts);
} else {
exec(dir, `git checkout ${branch}`, opts);
}
};
const commit = (dir, message, opts = {}) => {
exec(dir, "git add -A", opts);
try {
// keep commit quiet by default unless explicitly not silent
exec(dir, `git commit -m ${JSON.stringify(message)}`, { silent: true });
} catch {
// nothing to commit is OK
}
};
const push = (dir, branch, opts = {}) => exec(dir, `git push origin ${branch}`, opts);
const pull = (dir, branch, opts = {}) => exec(dir, `git pull origin ${branch}`, opts);
// ---------- high-level workflows ----------
/**
* FORCE sync (destructive)
* rm -rf *, fetch repo, hard reset to origin/branch
*/
const forceSync = (dir, { repo, branch = "master", silent = false } = {}) => {
if (!repo) throw new Error("repo is required for forceSync");
const opts = { silent };
if (exists(dir)) {
for (const n of fs.readdirSync(dir)) {
if (n === ".git") continue;
fs.rmSync(path.join(dir, n), rmOpts);
}
} else {
ensureDir(dir);
}
init(dir, opts);
setOrigin(dir, repo, opts);
fetch(dir, opts);
checkout(dir, branch, true, opts);
};
/**
* Attach git history WITHOUT touching working tree
* Uses temp folder and moves .git
*/
const attach = (dir, { repo, branch = "master", silent = false } = {}) => {
if (!repo) throw new Error("repo is required for attach");
if (!exists(dir)) throw new Error("target directory does not exist");
if (hasRepo(dir)) return; // already attached
const opts = { silent };
const tempRoot = path.join(os.homedir(), ".waw", "git-temp");
const temp = path.join(tempRoot, `${path.basename(dir)}-${Date.now()}`);
ensureDir(tempRoot);
ensureDir(temp);
// build git history in temp
exec(temp, "git init", opts);
exec(temp, `git remote add origin ${repo}`, opts);
exec(temp, "git fetch --all", opts);
exec(temp, `git checkout -B ${branch} origin/${branch}`, opts);
// move .git only
const from = path.join(temp, ".git");
const to = path.join(dir, ".git");
if (exists(to)) fs.rmSync(to, rmOpts);
fs.renameSync(from, to);
fs.rmSync(temp, rmOpts);
};
/**
* Publish current folder as-is
* attach -> commit -> push -> remove .git
*/
const publish = (dir, { repo, branch = "master", message, silent = false } = {}) => {
if (!message) throw new Error("commit message is required for publish");
const opts = { silent };
attach(dir, { repo, branch, silent });
commit(dir, message, opts);
push(dir, branch, opts);
remove(dir);
};
// ---------- hygiene ----------
const gitignore = `node_modules
package-lock.json
`;
const YEAR = new Date().getFullYear();
const LICENSE = `The MIT License (MIT)
Copyright (c) YEAR
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
`;
const ensureHygiene = function (moduleRoot) {
const name = path.basename(moduleRoot);
const gi = path.join(moduleRoot, ".gitignore");
if (!fs.existsSync(gi)) fs.writeFileSync(gi, gitignore, "utf8");
const readme = path.join(moduleRoot, "README.md");
if (!fs.existsSync(readme)) {
fs.writeFileSync(readme, `# waw module ${name}`, "utf8");
}
const lic = path.join(moduleRoot, "LICENSE");
if (!fs.existsSync(lic)) {
fs.writeFileSync(lic, LICENSE.replace("YEAR", YEAR), "utf8");
} else {
const content = fs.readFileSync(lic, "utf8");
if (content.startsWith("The MIT License (MIT)") && !content.includes(String(YEAR))) {
fs.writeFileSync(lic, LICENSE.replace("YEAR", YEAR), "utf8");
}
}
};
// ---------- exports ----------
module.exports = {
// checks
hasGit,
hasRepo,
// low-level
init,
setOrigin,
fetch,
checkout,
commit,
push,
pull,
remove,
// workflows
forceSync,
attach,
publish,
ensureHygiene,
};