-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui-core.js
159 lines (143 loc) · 3.74 KB
/
ui-core.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
153
154
155
156
157
158
159
const fs = require("fs/promises");
const p = require("child_process");
const { Octokit } = require("@octokit/core");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function install(dep) {
return `npx elm-json install ${dep.name}@${dep.version} --yes`;
}
function npmInstall(
uiCorePath = "./elm-stuff/gitdeps/github.com/unisonweb/ui-core"
) {
return `npm install -C ${uiCorePath}`;
}
function replaceElmGitRepoSha(repo, sha) {
return fs
.readFile("./elm-git.json")
.then(JSON.parse)
.then((json) => {
const direct = json["git-dependencies"].direct;
// remove ui-core dependencies to replace with custom one
Object.keys(direct).forEach((key) => {
if (key.endsWith("/ui-core")) {
delete direct[key];
}
});
return {
...json,
["git-dependencies"]: {
...json["git-dependencies"],
direct: {
...direct,
[repo]: sha,
},
},
};
})
.then(JSON.stringify)
.then((data) => fs.writeFile("./elm-git.json", data));
}
function getUICoreOriginFromElmGit() {
return fs
.readFile("./elm-git.json")
.then(JSON.parse)
.then((json) => {
const directDict = json["git-dependencies"].direct;
for (const [key, value] of Object.entries(directDict)) {
if (key.includes("ui-core")) {
return {
url: key,
sha: value,
};
}
}
return undefined;
});
}
function getLatestUICoreSha() {
const octokit = new Octokit();
return octokit
.request("GET /repos/{owner}/{repo}/commits", {
owner: "unisonweb",
repo: "ui-core",
number: 1,
})
.then((x) => x.data[0].sha);
}
function elmDeps(elmJsonContents) {
let deps = elmJsonContents.dependencies;
if ("direct" in deps) {
deps = {
...deps.direct,
...deps.indirect,
};
}
return Object.keys(deps).map((name) => {
// A version range looks like so: "1.0.0 <= v < 2.0.0"
const versionRange = deps[name];
// take the major version
let version = versionRange.split(".")[0];
return { name, version };
});
}
function elmGitInstall(
uiCorePath = "./elm-stuff/gitdeps/github.com/unisonweb/ui-core"
) {
return run("npx elm-git-install")
.then(() => {
return fs.readFile(`${uiCorePath}/elm.json`);
})
.then(JSON.parse)
.then(elmDeps)
.then((uiCoreDeps) => {
console.log(`Found ${uiCoreDeps.length} UI Core dependencies`);
return fs
.readFile("./elm.json")
.then(JSON.parse)
.then(elmDeps)
.then((appDeps) => {
return uiCoreDeps.reduce((acc, dep) => {
if (!appDeps.some((d) => d.name === dep.name)) {
return acc.concat(dep);
} else {
return acc;
}
}, []);
});
})
.then((deps) => {
console.log(`${deps.length} need to be installed`);
return deps.reduce((p, d) => {
return p
.then((_) => {
console.log(`Installing ${d.name}@${d.version}`);
return run(install(d));
})
.then(sleep(250));
}, Promise.resolve());
})
.then(() => run(npmInstall(uiCorePath)));
function run(cmd) {
return new Promise((resolve, _reject) => {
p.exec(cmd, { maxBuffer: 1024 * 500 }, (error, stdout, stderr) => {
if (error) {
console.warn(error);
} else if (stdout) {
console.log(stdout);
} else {
console.log(stderr);
}
resolve(stdout ? true : false);
});
});
}
}
module.exports = {
install,
elmGitInstall,
replaceElmGitRepoSha,
elmGitInstall,
getLatestUICoreSha,
getUICoreOriginFromElmGit,
};