-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup
executable file
·373 lines (307 loc) · 10.2 KB
/
setup
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/bin/env -S node
const http = require('http');
const stream = require('stream');
const {readdir, rm, mkdir, stat, writeFile, readFile} = require('fs/promises');
const {promisify} = require('util');
const {exec} = require('child_process');
const httpGet = promisify(http.get);
const pipeline = promisify(stream.pipeline);
const package_info = require('./package.json');
const github_api_url = 'https://api.github.com/';
const github_repo_str = 'repos/f-klubben/fappen/';
const options = {
verbose: false,
}
const print = (...args) => console.log(...args);
const print_v = (...args) => {
if (options.verbose) print(...args);
};
/*
Bootstrapping functions
---
The script may be run before the necessary dependencies have been
installed, so we use some helper functions to get the environment setup.
*/
const node_modules_exists = async () => {
try {
await readdir('./node_modules');
return true;
} catch (e) {
return false;
}
};
const npm_install = () => new Promise((resolve, reject) => {
exec('npm install', function (error, stdout, stderr) {
if (error)
return reject(error);
if (options.verbose)
print_v(stdout);
console.error(stderr);
})
});
const import_zx = async (r = true) => {
let zx;
try {
zx = await import('zx');
} catch (e) {
print('Installing dependencies...');
await npm_install();
if (r)
return import_zx(false);
}
return zx;
};
(async () => {
/*
Program checks
*/
const check_program = (name, run, regex, selector) => async () => {
print(`Checking ${name}...`);
try {
const out = await run();
const r = regex.exec(out.stdout);
if (r == null)
return null;
const version = await selector(r);
print(`${name} version found: ${version}`);
return version;
} catch (e) {
return null;
}
};
const pandoc_version_regex = /^pandoc (.*)$/;
const check_pandoc = check_program(
'Pandoc',
() => $`pandoc --version`,
pandoc_version_regex,
match => match[1],
);
/*
Github API
*/
const github_repo_endpoint = path => {
if (path[0] === '/')
path = path.slice(1);
return `${github_api_url}${github_repo_str}${path}`
}
const github_release = (version = 'latest') => {
let release_id = version;
if (version !== 'latest') {
throw new Error("this don't work yet");
}
const endpoint = github_repo_endpoint(`/releases/${release_id}`);
return fetch(endpoint);
};
/*
Songbook
*/
const songbook_clear_artifacts = async () => {
print('Removing old songbook artifacts...');
try {
await rm('./pages/songbook/songs.json', {force: true});
await rm('./pages/songbook/songs', {force: true, recursive: true});
} catch (e) {
console.error('Something went wrong removing artifacts.');
console.error(e);
}
};
const songbook_build = async () => {
await check_pandoc();
};
const songbook_fetch_artifacts = async (version = 'latest') => {
print(`Fetching release info for version: ${version}`);
const release_info = await github_release(version)
.then(res => res.json());
let archive_link;
for (const asset of release_info['assets']) {
if (asset['name'] === "songbook.tar.gz") {
archive_link = asset['browser_download_url'];
break;
}
}
if (archive_link == null) {
print(`Release '${version}' does not contain songbook artifacts.`);
throw new Error("unable to fetch songbook artifacts");
}
// fetch and unpack the artifacts
await pipeline(
(await fetch(archive_link)).body,
tar.x({cwd: './pages/songbook'}),
);
}
/*
local config & profile management
*/
const release_profile = 'release';
const dev_profile = 'dev';
const root = (path='') => `./${path}`;
const local = (path='') => root(`.local/${path}`);
const find_profile = async (profile) => {
const name = `profile.${profile}.json`;
const root_dir = await readdir(root());
if (root_dir.includes(name)) {
return root(name);
}
const local_dir = await readdir(local());
if (local_dir.includes(name)) {
return local(name);
}
return null;
};
const init_local_config = async (base_profile) => {
try {
const local_stat = await stat(local());
if (!local_stat.isDirectory()) {
console.error("File found instead of directory at `.local`. Please relocate the file and try again.");
process.exit();
}
} catch (_) {
await mkdir(local());
}
try {
const local_cfg_stat = await stat(local('config.json'));
if (!local_cfg_stat.isFile()) {
console.error("Directory found in place of `.local/config.json`. Please relocate the directory and try again.");
process.exit();
}
return await import(local('config.json'));
} catch (_) {
const profile = await find_profile(base_profile);
const config = {
profile,
}
await writeFile(local('config.json'), JSON.stringify(config));
return config;
}
};
const get_local_config = async () => {
try {
return JSON.parse((await readFile(local('config.json'))).toString());
} catch (e) {
console.error(
`No local config found. Creating one using the \`${dev_profile}\` profile as default.
use \`setup profile set <profile-name>\` to use a different profile.`);
return await init_local_config(dev_profile);
}
}
/*
Setup
*/
const setup = async argv => {
if (argv.noFetchSongbook !== true) {
await songbook_clear_artifacts();
await songbook_fetch_artifacts();
}
};
/*
Build
*/
const build = async argv => {
let profile;
if (argv.profile != null) {
const profile_path = await find_profile(argv.profile);
profile = JSON.parse((await readFile(profile_path)).toString());
} else {
profile = JSON.parse((await readFile(config.profile)).toString());
}
print("Building project...");
print(`Using build profile: ${profile.id}`);
if (profile.env == null)
profile.env = {};
console.log(profile.env.FA_FEATURES);
let profileFeatures = [];
if (profile.features instanceof Array) {
profileFeatures = profile.features;
} else if (typeof profile.features === 'object') {
profileFeatures = Object.keys(profile.features)
.map(key => {
if (profile.features[key]) {
return `+${key}`;
} else {
return `-${key}`;
}
});
}
// TODO check that feature strings are valid
process.env.FA_FEATURES = [
(process.env.FA_FEATURES || ""),
(profile.env.FA_FEATURES || ""),
argv.features,
profileFeatures.join(';'),
].join(';');
for (const key in profile.env) {
process.env[key] = profile.env[key];
}
try {
await $`npm run build`;
} catch (e) {
console.error(e);
}
const version = argv['build-version'] || package_info.version;
const tag = argv.tag;
tar.create({
gzip: true,
file: root(`build/build-${version}-${tag}.tar.gz`),
cwd: root('build/dist'),
}, ['.']);
print(`Build ${version}-${tag}`);
}
/*
entrypoint
*/
const zx = await import_zx();
const {$, fetch} = zx;
const {spinner} = await import('zx/experimental');
if (await node_modules_exists() === false) {
await spinner('Installing dependencies...', () => npm_install());
}
const tar = await import('tar');
const yargs = require('yargs/yargs');
const config = await get_local_config();
yargs(process.argv.slice(2))
.scriptName("setup")
.usage('$0 <cmd> [args]')
.command('profile', 'configure the active profile',
yargs => yargs
.command('set', 'set the active profile', argv => {
}),
argv => {
console.log(config.profile)
}
)
.command(['$0', 'setup'], 'prepare the workspace for developement',
yargs => yargs
.option('b', {
alias: 'no-fetch-songbook',
default: false,
describe: 'should songbook artifacts be fetched?',
type: 'boolean',
}),
setup
)
.command('build <tag>', 'build the project',
yargs => yargs
.option('p', {
alias: 'profile',
default: null,
describe: 'override the default profile',
})
.option('f', {
alias: 'features',
default: "",
describe: "toggle individual features (e.g. +cli-backend or -cli-backend;+feature2)"
})
.option('v', {
alias: 'build-version',
default: null,
describe: "override the version (default uses package.json value)"
})
.positional('tag', {
describe: "the name of the build (e.g. live or demo)",
default: "live",
}),
build
)
.help()
.parse()
})();