h5ai/ghu.js

158 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-12-11 19:13:00 -03:00
const {resolve, join} = require('path');
const {
2016-05-30 19:21:44 -04:00
ghu, autoprefixer, cssmin, each, ife, includeit, jszip, less, mapfn,
2019-04-23 20:09:57 -04:00
pug, read, remove, run, uglify, watch, webpack, wrap, write
2015-12-11 19:13:00 -03:00
} = require('ghu');
2015-11-18 13:27:16 -03:00
const ROOT = resolve(__dirname);
const SRC = join(ROOT, 'src');
2015-11-18 19:16:58 -03:00
const TEST = join(ROOT, 'test');
2015-11-18 13:27:16 -03:00
const BUILD = join(ROOT, 'build');
2016-06-03 20:43:48 -04:00
const mapper = mapfn.p(SRC, BUILD).s('.less', '.css').s('.pug', '');
2020-02-19 14:36:12 -03:00
const WEBPACK_CFG = {
mode: 'none',
module: {
rules: [
{
2020-02-19 14:56:23 -03:00
test: /\.js$/,
2020-02-19 14:36:12 -03:00
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /jsdom/,
use: 'null-loader'
}
]
}
2019-04-05 18:02:26 -03:00
};
2015-11-18 13:27:16 -03:00
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);
2015-12-13 18:23:40 -03:00
runtime.pkg.version += `+${counter}~${hash}`;
2015-11-18 13:27:16 -03:00
}
}
runtime.comment = `${runtime.pkg.name} v${runtime.pkg.version} - ${runtime.pkg.homepage}`;
2019-03-22 19:01:35 -03:00
runtime.comment_js = `/* ${runtime.comment} */\n`;
runtime.comment_html = `<!-- ${runtime.comment} -->`;
2015-11-18 13:27:16 -03:00
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`)
2020-02-19 14:36:12 -03:00
.then(webpack(WEBPACK_CFG))
2016-06-19 07:57:10 -04:00
.then(wrap('\n\n// @include "pre.js"\n\n'))
2015-11-18 13:27:16 -03:00
.then(includeit())
2019-04-05 18:02:26 -03:00
.then(ife(() => runtime.args.production, uglify()))
2019-03-22 19:01:35 -03:00
.then(wrap(runtime.comment_js))
2015-11-18 13:27:16 -03:00
.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()))
2019-03-22 19:01:35 -03:00
.then(wrap(runtime.comment_js))
2015-11-18 13:27:16 -03:00
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:pages', runtime => {
2016-06-03 20:43:48 -04:00
return read(`${SRC}: **/*.pug, ! **/*.tpl.pug`)
2016-05-30 19:21:44 -04:00
.then(pug({pkg: runtime.pkg}))
2019-03-22 19:01:35 -03:00
.then(wrap('', runtime.comment_html))
2015-11-18 13:27:16 -03:00
.then(write(mapper, {overwrite: true}));
});
ghu.task('build:copy', runtime => {
2019-03-22 19:01:35 -03:00
const mapper_root = mapfn.p(ROOT, join(BUILD, '_h5ai'));
2015-11-18 13:27:16 -03:00
return Promise.all([
read(`${SRC}/**/conf/*.json`)
2019-03-22 19:01:35 -03:00
.then(wrap(runtime.comment_js))
2015-11-18 13:27:16 -03:00
.then(write(mapper, {overwrite: true, cluster: true})),
2016-06-03 20:43:48 -04:00
read(`${SRC}: **, ! **/*.js, ! **/*.less, ! **/*.pug, ! **/conf/*.json`)
2015-12-19 13:01:45 -03:00
.then(each(obj => {
2019-02-20 18:06:03 -03:00
if ((/index\.php$/).test(obj.source)) {
2015-12-19 13:01:45 -03:00
obj.content = obj.content.replace('{{VERSION}}', runtime.pkg.version);
}
}))
2015-11-18 13:27:16 -03:00
.then(write(mapper, {overwrite: true, cluster: true})),
read(`${ROOT}/*.md`)
2019-03-22 19:01:35 -03:00
.then(write(mapper_root, {overwrite: true, cluster: true}))
2015-11-18 13:27:16 -03:00
]);
});
2016-06-03 19:06:25 -04:00
ghu.task('build:tests', ['build:styles'], 'build the test suite', () => {
2016-05-31 20:15:04 -04:00
return Promise.all([
read(`${BUILD}/_h5ai/public/css/styles.css`)
2016-06-03 19:06:25 -04:00
.then(write(`${BUILD}/test/h5ai-styles.css`, {overwrite: true})),
2016-05-31 20:15:04 -04:00
2016-07-21 07:44:31 -04:00
read(`${TEST}/index.html`)
.then(write(`${BUILD}/test/index.html`, {overwrite: true})),
2016-05-31 20:15:04 -04:00
2016-07-21 07:44:31 -04:00
read(`${TEST}: index.js`)
2020-02-19 14:36:12 -03:00
.then(webpack(WEBPACK_CFG))
2016-06-19 07:57:10 -04:00
.then(wrap(`\n\n// @include "${SRC}/**/js/pre.js"\n\n`))
2016-06-03 19:06:25 -04:00
.then(includeit())
.then(write(mapfn.p(TEST, `${BUILD}/test`), {overwrite: true}))
2016-05-31 20:15:04 -04:00
]).then(() => {
2016-07-21 07:44:31 -04:00
console.log(`browse to file://${BUILD}/test/index.html to run the test suite`);
2016-05-31 20:15:04 -04:00
});
});
2016-06-03 19:06:25 -04:00
ghu.task('build', ['build:scripts', 'build:styles', 'build:pages', 'build:copy', 'build:tests'],
2015-11-18 13:27:16 -03:00
'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}`);
2019-03-22 19:01:35 -03:00
const mapper_deploy = mapfn.p(BUILD, resolve(runtime.args.dest));
2015-11-18 13:27:16 -03:00
return read(`${BUILD}/_h5ai/**`)
2019-03-22 19:01:35 -03:00
.then(write(mapper_deploy, {overwrite: true, cluster: true}));
2015-11-18 13:27:16 -03:00
});
ghu.task('watch', runtime => {
2016-06-03 19:06:25 -04:00
return watch([SRC, TEST], () => ghu.run(runtime.sequence.filter(x => x !== 'watch'), runtime.args, true));
2015-11-18 13:27:16 -03:00
});
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}));
});