forked from manga-download/hakuneko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-web.js
94 lines (84 loc) · 2.89 KB
/
build-web.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
const path = require('path');
const fs = require('fs-extra');
const exec = require('child_process').exec;
const vinyl = require('vinyl-fs');
const mergeStream = require('merge-stream');
const PolymerProject = require('polymer-build').PolymerProject;
const config = require('./build-web.config');
config.source = config.source || 'src';
config.target = config.target || 'build';
function execute(command, silent) {
if(!silent) {
console.log('>', command);
}
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if(!silent) {
console.log(stdout);
console.log(stderr);
}
if(error) {
reject(error);
} else {
resolve(stdout);
}
});
});
}
async function gitStashPush(identifier) {
identifier = identifier || 'DEPLOY#' + Date.now().toString(16).toUpperCase();
await execute(`git stash push -u -m '${identifier}'`);
return identifier;
}
async function gitStashPop(identifier) {
let out = await execute(`git stash list`);
if(out.includes(identifier)) {
await execute(`git stash pop`);
}
}
function pipe(streamReader, streamWriter) {
return new Promise((resolve, reject) => {
let stream = streamReader.pipe(streamWriter);
stream.on('error', reject);
stream.on('end', resolve);
});
}
async function polymerBuild(settings) {
let target = path.resolve(config.target);
let cwd = process.cwd();
if(config.source) {
process.chdir(config.source);
}
let project = new PolymerProject(settings);
let streamIn = mergeStream(project.sources(), project.dependencies());
let streamOut = vinyl.dest(target);
await pipe(streamIn, streamOut);
process.chdir(cwd);
}
async function createVersionInfo(file) {
let branch = /*process.env.GITHUB_REF ? process.env.GITHUB_REF.split('/').pop() : */(await execute(`git rev-parse --abbrev-ref HEAD`)).trim();
let revision = (await execute(`git rev-parse HEAD`)).trim();
let content = [
`export default {`,
` branch: {`,
` label: '${branch}',`,
` link: 'https://github.com/manga-download/hakuneko/commits/${branch}',`,
` },`,
` revision: {`,
` label: '${revision.slice(0, 6)}',`,
` link: 'https://github.com/manga-download/hakuneko/commits/${revision}',`,
` }`,
`};`
].join('\n');
await fs.writeFile(file, content);
}
async function main() {
let stashID = await gitStashPush();
await fs.remove(config.target);
await polymerBuild(config.polymer);
await createVersionInfo(path.join(config.target, config.version));
await gitStashPop(stashID);
}
// exit application as soon as any uncaught exception is thrown
process.on('unhandledRejection', error => { throw error; });
main();