forked from lrsjng/h5ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghu.js
169 lines (142 loc) · 5.83 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const {resolve, join} = require('path');
const {
ghu, autoprefixer, cssmin, each, ife, includeit, jszip, less, mapfn,
newerThan, 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 webpackCfg = include => ({
module: {
loaders: [
{
include,
loader: 'babel-loader',
query: {
cacheDirectory: true,
presets: ['es2015']
}
},
{
test: /jsdom/,
loader: 'null-loader'
}
]
}
});
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.commentJs = `/* ${runtime.comment} */\n`;
runtime.commentHtml = `<!-- ${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('lint', 'lint all JavaScript files with eslint', () => {
return run('eslint .', {stdio: 'inherit'});
});
ghu.task('build:scripts', runtime => {
return read(`${SRC}/_h5ai/public/js/scripts.js`)
.then(newerThan(mapper, `${SRC}/_h5ai/public/js/**`))
.then(webpack(webpackCfg([SRC]), {showStats: false}))
.then(wrap('\n\n// @include "pre.js"\n\n'))
.then(includeit())
.then(ife(() => runtime.args.production, uglify({compressor: {warnings: false}})))
.then(wrap(runtime.commentJs))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:styles', runtime => {
return read(`${SRC}/_h5ai/public/css/*.less`)
.then(newerThan(mapper, `${SRC}/_h5ai/public/css/**`))
.then(includeit())
.then(less())
.then(autoprefixer())
.then(ife(() => runtime.args.production, cssmin()))
.then(wrap(runtime.commentJs))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:pages', runtime => {
return read(`${SRC}: **/*.pug, ! **/*.tpl.pug`)
.then(newerThan(mapper, `${SRC}/**/*.tpl.pug`))
.then(pug({pkg: runtime.pkg}))
.then(wrap('', runtime.commentHtml))
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:copy', runtime => {
const mapperRoot = mapfn.p(ROOT, join(BUILD, '_h5ai'));
return Promise.all([
read(`${SRC}/**/conf/*.json`)
.then(newerThan(mapper))
.then(wrap(runtime.commentJs))
.then(write(mapper, {overwrite: true, cluster: true})),
read(`${SRC}: **, ! **/*.js, ! **/*.less, ! **/*.pug, ! **/conf/*.json`)
.then(newerThan(mapper))
.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(newerThan(mapperRoot))
.then(write(mapperRoot, {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(newerThan(`${BUILD}/test/h5ai-styles.css`))
.then(write(`${BUILD}/test/h5ai-styles.css`, {overwrite: true})),
read(`${TEST}/index.html`)
.then(newerThan(`${BUILD}/test/index.html`))
.then(write(`${BUILD}/test/index.html`, {overwrite: true})),
read(`${TEST}: index.js`)
.then(webpack(webpackCfg([SRC, TEST]), {showStats: false}))
.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 mapperDeploy = mapfn.p(BUILD, resolve(runtime.args.dest));
return read(`${BUILD}/_h5ai/**`)
.then(newerThan(mapperDeploy))
.then(write(mapperDeploy, {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}));
});