forked from lrsjng/h5ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghu.js
145 lines (120 loc) · 4.99 KB
/
ghu.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
const {resolve, join} = require('path');
const {
ghu, autoprefixer, cssmin, each, ife, includeit, jszip, less, mapfn,
pug, read, remove, run, uglify, watch, webpack, wrap, write
} = require('ghu');
const ROOT = resolve(__dirname);
const SRC = join(ROOT, 'src');
const TEST = join(ROOT, 'test');
const BUILD = join(ROOT, 'build');
const mapper = mapfn.p(SRC, BUILD).s('.less', '.css').s('.pug', '');
const webpack_cfg = (...include) => {
const cfg = webpack.cfg(include);
cfg.module.rules.push({
test: /jsdom/,
loader: 'null-loader'
});
return cfg;
};
ghu.defaults('release');
ghu.before(runtime => {
runtime.pkg = Object.assign({}, require('./package.json'));
const res = run.sync(`git rev-list v${runtime.pkg.version}..HEAD`, {silent: true});
if (res.code === 0) {
const hashes = res.stdout.split(/\r?\n/).filter(x => x);
if (hashes.length) {
const counter = ('000' + hashes.length).substr(-3);
const hash = hashes[0].substr(0, 7);
runtime.pkg.version += `+${counter}~${hash}`;
}
}
runtime.comment = `${runtime.pkg.name} v${runtime.pkg.version} - ${runtime.pkg.homepage}`;
runtime.comment_js = `/* ${runtime.comment} */\n`;
runtime.comment_html = `<!-- ${runtime.comment} -->`;
console.log(runtime.comment);
});
ghu.task('force-production', 'ensure :production flag is set', runtime => {
if (!runtime.args.production) {
runtime.args.production = true;
console.log('forcing production mode');
}
});
ghu.task('clean', 'delete build folder', () => {
return remove(BUILD);
});
ghu.task('build:scripts', runtime => {
return read(`${SRC}/_h5ai/public/js/scripts.js`)
.then(webpack(webpack_cfg(SRC)))
.then(wrap('\n\n// @include "pre.js"\n\n'))
.then(includeit())
.then(ife(() => runtime.args.production, uglify()))
.then(wrap(runtime.comment_js))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:styles', runtime => {
return read(`${SRC}/_h5ai/public/css/*.less`)
.then(includeit())
.then(less())
.then(autoprefixer())
.then(ife(() => runtime.args.production, cssmin()))
.then(wrap(runtime.comment_js))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:pages', runtime => {
return read(`${SRC}: **/*.pug, ! **/*.tpl.pug`)
.then(pug({pkg: runtime.pkg}))
.then(wrap('', runtime.comment_html))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:copy', runtime => {
const mapper_root = mapfn.p(ROOT, join(BUILD, '_h5ai'));
return Promise.all([
read(`${SRC}/**/conf/*.json`)
.then(wrap(runtime.comment_js))
.then(write(mapper, {overwrite: true, cluster: true})),
read(`${SRC}: **, ! **/*.js, ! **/*.less, ! **/*.pug, ! **/conf/*.json`)
.then(each(obj => {
if ((/index\.php$/).test(obj.source)) {
obj.content = obj.content.replace('{{VERSION}}', runtime.pkg.version);
}
}))
.then(write(mapper, {overwrite: true, cluster: true})),
read(`${ROOT}/*.md`)
.then(write(mapper_root, {overwrite: true, cluster: true}))
]);
});
ghu.task('build:tests', ['build:styles'], 'build the test suite', () => {
return Promise.all([
read(`${BUILD}/_h5ai/public/css/styles.css`)
.then(write(`${BUILD}/test/h5ai-styles.css`, {overwrite: true})),
read(`${TEST}/index.html`)
.then(write(`${BUILD}/test/index.html`, {overwrite: true})),
read(`${TEST}: index.js`)
.then(webpack(webpack_cfg(SRC, TEST)))
.then(wrap(`\n\n// @include "${SRC}/**/js/pre.js"\n\n`))
.then(includeit())
.then(write(mapfn.p(TEST, `${BUILD}/test`), {overwrite: true}))
]).then(() => {
console.log(`browse to file://${BUILD}/test/index.html to run the test suite`);
});
});
ghu.task('build', ['build:scripts', 'build:styles', 'build:pages', 'build:copy', 'build:tests'],
'build all updated files, optionally use :production');
ghu.task('deploy', ['build'], 'deploy to a specified path with :dest=/some/path', runtime => {
if (typeof runtime.args.dest !== 'string') {
throw new Error('no destination path (e.g. :dest=/some/path)');
}
console.log(`deploy to ${runtime.args.dest}`);
const mapper_deploy = mapfn.p(BUILD, resolve(runtime.args.dest));
return read(`${BUILD}/_h5ai/**`)
.then(write(mapper_deploy, {overwrite: true, cluster: true}));
});
ghu.task('watch', runtime => {
return watch([SRC, TEST], () => ghu.run(runtime.sequence.filter(x => x !== 'watch'), runtime.args, true));
});
ghu.task('release', ['force-production', 'clean', 'build'], 'create a zipball', runtime => {
const target = join(BUILD, `${runtime.pkg.name}-${runtime.pkg.version}.zip`);
return read(`${BUILD}/_h5ai/**`)
.then(jszip({dir: BUILD, level: 9}))
.then(write(target, {overwrite: true}));
});