-
Notifications
You must be signed in to change notification settings - Fork 206
/
generate-build-json.js
61 lines (55 loc) · 1.44 KB
/
generate-build-json.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
const fs = require('fs-extra');
const path = require('path');
const git = require('git-rev-sync');
const buildfile = require('./build');
const yargs = require('yargs')(process.argv.slice(2));
const argv = yargs
.usage('Usage: $0 -f <file>')
.help('help')
.alias('version', 'v')
.option('help', {
alias: 'h'
})
.option('file', {
alias: 'f',
type: 'string',
requiresArg: true,
describe: 'The target file path which would be generated.',
})
.option('dryrun', {
type: 'boolean',
describe: 'Display the generated content instead of saving to target file.',
})
.option('verbose', {
alias: 'V',
type: 'boolean',
describe: 'Enable verbose log.',
})
.argv;
function getBuildJson() {
let commit = buildfile.gitCommit;
try {
commit = git.short();
} catch (ex) {
console.log('cannot get commit, because ' + ex);
}
return {
gitCommit: commit
};
}
if (!argv.file || argv.help) {
yargs.showHelp();
} else {
const buildJsonObj = getBuildJson();
const buildJsonContent = JSON.stringify(buildJsonObj);
if (argv.dryrun) {
console.log(buildJsonContent);
} else {
if (!fs.existsSync(path.dirname(argv.file))) {
fs.mkdirpSync(path.dirname(argv.file));
}
fs.writeFileSync(argv.file, buildJsonContent, {
encoding: 'UTF-8'
});
}
}