This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-submodules.ts
49 lines (47 loc) · 2.1 KB
/
git-submodules.ts
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
(function() {
const
system = requireModule<System>("system"),
path = require("path"),
gulp = requireModule<Gulp>("gulp"),
log = requireModule<Log>("log"),
{ existsSync, readTextFile, mkdir } = require("yafs"),
subModulesFile = ".gitmodules";
gulp.task("git-submodules", "Updates (with --init) all submodules in tree", function() {
return system("git", [ "submodule", "update", "--init", "--recursive" ]);
});
gulp.task("git-submodules-as-externals", async () => {
if (!existsSync(subModulesFile)) {
log.notice("no submodules file found");
return;
}
log.notice("performing submodule init/update...");
await system("git", [ "submodule", "update", "--init", "--recursive" ]);
log.info("getting list of local submodules...")
const buffer = await readTextFile(subModulesFile);
log.info("grokking paths of local submodules...");
const lines = buffer.split("\n");
const submodulePaths = lines.reduce((acc: string[], cur: string) => {
const parts = cur.split(" = ")
.map(item => item.trim());
if (parts.length > 1 && parts[0] === "path") {
acc.push(parts[1]);
}
return acc;
}, [] as string[]);
for (const dir of submodulePaths) {
await mkdir(dir);
}
log.info("making sure local submodules are up to date...");
for (const mod of submodulePaths) {
const workingFolder = path.join(process.cwd(), mod);
log.info(`working with submodule at: ${ mod }`);
log.debug("- fetch changes");
const opts = { cwd: workingFolder } as SystemOptions;
await system("git", [ "fetch " ], opts);
await system("git", [ "checkout", "master" /* TODO: cater for 'main' */ ], opts);
log.debug("- fast-forward to head");
await system("git", [ "rebase" ], opts);
}
log.info("git submodule magick complete");
});
})();