This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
get-build.js
152 lines (119 loc) · 3.98 KB
/
get-build.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
/* Install: npm install node-fetch
* Run: node get-build.js [REPO] [BRANCH]
* This script automatically takes the latest build from given repo and branch
* and places it to label_studio/static/<REPO>
*/
/**
* global _dirname
*/
const fs = require("fs");
const { spawn, execSync } = require("child_process");
const path = require("path");
const dir = path.resolve(__dirname, "build-tmp");
const TOKEN = process.env.GITHUB_TOKEN;
// coloring for console output
const RED = "\033[0;31m";
const NC = "\033[0m"; // NO COLOR to reset coloring
const PROJECTS = {
lsf: "heartexlabs/label-studio-frontend",
dm: "heartexlabs/dm2",
};
const DIST_DIR = "/public/static";
/**
* @param {string} ref commit or branch
*/
async function get(projectName, ref = "master") {
let res,
json,
sha,
branch = "";
const REPO = PROJECTS[projectName || "lsf"];
if (!REPO) {
const repos = Object.entries(PROJECTS)
.map((a) => "\t" + a.join("\t"))
.join("\n");
console.error(
`\n${RED}Cannot fetch from repo ${REPO}.${NC}\nOnly available:\n${repos}`,
);
return;
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
if (ref.length < 30) {
const commitUrl = `https://api.github.com/repos/${REPO}/git/ref/heads/${ref}`;
console.info(`Fetching ${commitUrl}`);
res = await fetch(commitUrl, {
headers: { Authorization: `token ${TOKEN}` },
});
json = await res.json();
if (!json || !json.object) {
console.log(
`\n${RED}Wrong response from GitHub. Check that you use correct GITHUB_TOKEN.${NC}`,
);
console.log(json);
return;
}
sha = json.object.sha;
console.info(`Last commit in ${ref}:`, sha);
branch = ref;
} else {
sha = ref;
}
const artifactsUrl = `https://api.github.com/repos/${REPO}/actions/artifacts`;
res = await fetch(artifactsUrl, {
headers: { Authorization: `token ${TOKEN}` },
});
json = await res.json();
const artifact = json.artifacts.find((art) => art.name === `build ${sha}`);
if (!artifact)
throw new Error(`Artifact for commit ${sha} was not found. Build failed?`);
const buildUrl = artifact.archive_download_url;
console.info("Found an artifact:", buildUrl);
res = await fetch(buildUrl, { headers: { Authorization: `token ${TOKEN}` } });
const filename = `${dir}/${sha}.zip`;
console.info("Create write stream:", filename);
const fileStream = fs.createWriteStream(filename);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
fileStream.on("error", reject);
fileStream.on("finish", () => {
console.info("Downloaded:", filename);
const unzip = spawn("unzip", ["-d", dir, "-o", filename]);
unzip.stderr.on("data", reject);
unzip.on("close", resolve);
});
}).then(() => console.log("Build unpacked"));
const commitInfoUrl = `https://api.github.com/repos/${REPO}/git/commits/${sha}`;
res = await fetch(commitInfoUrl, {
headers: { Authorization: `token ${TOKEN}` },
});
json = await res.json();
const info = {
message: json.message,
commit: json.sha,
branch,
date:
(json.author && json.author.date) ||
(json.committer && json.committer.date),
};
fs.writeFileSync(`${dir}/static/version.json`, JSON.stringify(info, null, 2));
console.info("Version info written to static/version.json");
const needsBuild = ["dm"].includes(projectName);
// move build to target folder
var oldPath = path.join(dir, "static");
var newPath = path.join(__dirname, DIST_DIR, projectName);
fs.rmdirSync(newPath, { recursive: true });
fs.mkdirSync(newPath, { recursive: true });
fs.rename(oldPath, newPath, function(err) {
if (err) throw err;
console.log(`Successfully renamed - AKA moved into ${newPath}`);
fs.rmdirSync(dir, { recursive: true });
console.log(`Cleaned up tmp directory [${dir}]`);
if (needsBuild) {
execSync(`npx webpack`, { stdio: "inherit" });
}
});
}
// repo name and branch name
get(process.argv[2], process.argv[3]);