forked from adaptlearning/adapt_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitmodules.js
81 lines (76 loc) · 2.42 KB
/
gitmodules.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
const ChildProcess = require('child_process');
const fs = require('fs');
/**
* Fetch and parse .gitmodules
* @returns {Object}
*/
function getModuleConfig() {
if (!fs.existsSync('./.gitmodules')) return false;
const str = fs.readFileSync('./.gitmodules').toString();
const lines = str.split('\n');
const ret = {};
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const result = line.match(/\[submodule\s+"(.*?)"\]/);
if (!result || result.length < 2) continue;
const submodule = result[1];
const obj = {};
if (!submodule) continue;
let subline = lines[++i];
while (subline && subline.trim()) {
subline = lines[i];
if (!subline || subline.match(/\[submodule\s+"(.*?)"\]/)) {
i--;
break;
}
subline = subline.trim();
const result = subline.match(/(.*?)\s*=\s*(.*)/);
const key = result[1];
const value = result[2];
if (key && value) obj[key] = value;
i++;
}
ret[submodule] = obj;
}
return ret;
}
const modules = getModuleConfig();
if (!modules) process.exit();
// Fix PATH environment variables for git bash in both the terminal and AAT
const GITCOMPATIBILITY = `${process.env.PROGRAMFILES}\\Git\\bin;${process.env.PROGRAMFILES}\\Git\\mingw64\\libexec\\git-core;${process.env.APPDATA}\\..\\Local\\Programs\\Git\\mingw64\\libexec\\git-core`;
const env = Object.assign({}, process.env, {
Path: `${process.env.Path};${GITCOMPATIBILITY}`,
PATH: `${process.env.PATH};${GITCOMPATIBILITY}`,
});
const isFrameworkClone = fs.existsSync('.git');
if (isFrameworkClone) {
// Download submodules
ChildProcess.execSync('git submodule update --init --remote', {
env,
cwd: process.cwd(),
stdio: 'inherit'
});
} else {
// Clone submodules
for (const path in modules) {
if (fs.existsSync(`${path}/package.json`)) continue;
const url = modules[path].url;
console.log(`Cloning submodule ${url} to ${path}`);
ChildProcess.execSync(`git clone ${url} ${path}`, {
cwd: process.cwd(),
env,
stdio: 'inherit'
});
}
}
// Ensure submodules are on the appropriate branch
for (const path in modules) {
const branch = (modules[path].branch || 'master');
if (!fs.existsSync(`${path}/.git`)) continue;
console.log(`Switching submodule ${path} to branch ${branch}`);
ChildProcess.execSync(`git checkout ${branch}`, {
cwd: `${process.cwd()}/${path}`,
env,
stdio: 'inherit'
});
}