-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrelease.js
90 lines (77 loc) · 2.94 KB
/
release.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
const git = require('git-rev');
const fs = require('fs-extra');
const webpack = require('webpack');
const npm = require('package-json');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const viewerWebpackConfig = require('./webpack.config.js');
const demoWebpackConfig = require('./demo/webpack.config.js');
const { version, name } = require('./package.json');
const exitWithErrorMsg = (msg) => {
console.error(msg);
process.exit(1);
};
const getGitBranch = () => new Promise(resolve => git.branch(branch => resolve(branch)));
const gitAddAll = () => exec('git add .');
const gitCheckout = branch => exec(`git checkout ${branch}`);
const gitCommitAndPush = (commitMsg, branch) => exec(`git add . --all && git commit -m ${commitMsg} && git push origin ${branch}`);
const checkPreconditions = () => npm(name);
const build = webpackConfig => new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
reject(err.stack || err);
if (err.details) {
reject(err.details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
reject(info.errors);
return;
}
if (stats.hasWarnings()) {
console.warn(info.warnings);
}
resolve();
});
});
const renameDemoBundleWithVersion = ver =>
fs.copy('./demo/resources/js/index.js', `./demo/resources/js/${ver}.index.js`)
.then(() => {
const bundlesJson = require('./demo/resources/js/bundles.json');
bundlesJson.bundles.push(`${ver}.index.js`);
bundlesJson.bundles = [...new Set([...bundlesJson.bundles])];
bundlesJson.latestVersion = ver;
return fs.writeJson('./demo/resources/js/bundles.json', bundlesJson);
});
const args = process.argv.slice(2);
const commands = {
CHECK_PRECONDITIONS: 'check-preconditions',
BUILD: 'build',
RELEASED: 'released',
};
if (args === null || args.length === 0) {
exitWithErrorMsg('args required for release.js');
}
const execArgs = (arg) => {
if (arg === commands.CHECK_PRECONDITIONS) {
return checkPreconditions();
} else if (arg === commands.BUILD) {
return build(viewerWebpackConfig)
.then(() => build(demoWebpackConfig))
.then(() => renameDemoBundleWithVersion(version))
.then(() => gitAddAll());
} else if (arg === commands.RELEASED) {
return getGitBranch().then(branch => gitCheckout('gh-pages')
.then(() => gitCheckout(`${branch} ./demo/index.html`))
.then(() => gitCheckout(`${branch} ./demo/resources/js/`))
.then(() => gitCheckout(`${branch} ./demo/resources/css/`))
.then(() => gitCheckout(`${branch} ./demo/resources/fonts/`))
.then(() => gitCheckout(`${branch} ./demo/resources/contents/`))
.then(() => gitCommitAndPush(`"Demo version update ${version}"`, 'gh-pages'))
.then(() => gitCheckout(`${branch}`)));
}
return Promise.reject(new Error('invalid args'));
};
execArgs(args[0]).catch(err => exitWithErrorMsg(err));