2015-11-14 07:54:40 +08:00
|
|
|
const gulp = require('gulp')
|
|
|
|
const path = require('path')
|
|
|
|
const merge = require('event-stream').merge
|
|
|
|
const map = require('map-stream')
|
|
|
|
const spawn = require('child_process').spawn
|
|
|
|
const $ = require('gulp-load-plugins')()
|
2014-05-14 12:22:09 +08:00
|
|
|
|
2015-11-11 05:23:35 +08:00
|
|
|
// Tasks
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('clean', () => {
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe('./tmp', $.clean())
|
2014-07-11 11:40:46 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('build', (cb) => {
|
2015-11-11 05:23:35 +08:00
|
|
|
$.runSequence('clean', 'styles', 'chrome', 'opera', 'safari', 'firefox', cb)
|
2014-07-11 07:53:08 +08:00
|
|
|
})
|
2014-07-11 11:40:46 +08:00
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('default', ['build'], () => {
|
2015-03-07 05:04:13 +08:00
|
|
|
gulp.watch(['./libs/**/*', './src/**/*'], ['default'])
|
2014-07-11 11:40:46 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('dist', ['build'], (cb) => {
|
2014-08-21 03:49:37 +08:00
|
|
|
$.runSequence('firefox:xpi', 'chrome:zip', 'chrome:crx', 'opera:nex', cb)
|
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('test', ['build'], (cb) => {
|
|
|
|
const ps = spawn(
|
2014-11-25 10:56:03 +08:00
|
|
|
'./node_modules/.bin/mocha',
|
|
|
|
['--harmony', '--reporter', 'spec', '--bail', '--recursive', '--timeout', '-1']
|
|
|
|
)
|
|
|
|
ps.stdout.pipe(process.stdout);
|
|
|
|
ps.stderr.pipe(process.stderr);
|
|
|
|
ps.on('close', cb)
|
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('styles', () => {
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe(
|
|
|
|
'./src/styles/octotree.less',
|
|
|
|
$.less(),
|
|
|
|
$.autoprefixer({cascade: true}),
|
|
|
|
'./tmp'
|
|
|
|
)
|
2014-08-21 03:49:37 +08:00
|
|
|
})
|
2014-07-11 11:40:46 +08:00
|
|
|
|
|
|
|
// Chrome
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome:template', () => {
|
2014-11-25 10:56:03 +08:00
|
|
|
return buildTemplate({CHROME: true})
|
2014-07-11 11:40:46 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome:js', ['chrome:template'], () => {
|
|
|
|
return buildJs(['./src/config/chrome/overrides.js'], {CHROME: true})
|
2014-07-11 11:40:46 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome', ['chrome:js'], () => {
|
2014-07-11 07:53:08 +08:00
|
|
|
return merge(
|
2014-05-31 03:57:35 +08:00
|
|
|
pipe('./icons/**/*', './tmp/chrome/icons'),
|
2015-11-15 14:05:57 +08:00
|
|
|
pipe(['./libs/**/*', './tmp/octotree.*', './src/config/chrome/manifest.json'], './tmp/chrome/'),
|
|
|
|
pipe('./src/config/chrome/background.js', $.babel(), './tmp/chrome/')
|
2014-05-31 03:57:35 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome:zip', () => {
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe('./tmp/chrome/**/*', $.zip('chrome.zip'), './dist')
|
2014-08-21 03:49:37 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome:_crx', (cb) => {
|
2014-11-25 10:56:03 +08:00
|
|
|
$.run('"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"' +
|
|
|
|
' --pack-extension=' + path.join(__dirname, './tmp/chrome') +
|
|
|
|
' --pack-extension-key=' + path.join(process.env.HOME, '.ssh/chrome.pem')
|
2014-08-21 03:49:37 +08:00
|
|
|
).exec(cb)
|
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('chrome:crx', ['chrome:_crx'], () => {
|
2014-08-21 03:49:37 +08:00
|
|
|
return pipe('./tmp/chrome.crx', './dist')
|
|
|
|
})
|
|
|
|
|
2014-07-11 11:40:46 +08:00
|
|
|
// Opera
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('opera', ['chrome'], () => {
|
2014-07-11 07:53:08 +08:00
|
|
|
return pipe('./tmp/chrome/**/*', './tmp/opera')
|
2014-05-14 12:22:09 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('opera:nex', () => {
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe('./dist/chrome.crx', $.rename('opera.nex'), './dist')
|
2014-05-14 12:22:09 +08:00
|
|
|
})
|
|
|
|
|
2014-07-11 11:40:46 +08:00
|
|
|
// Firefox
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('firefox:template', () => {
|
2014-11-25 10:56:03 +08:00
|
|
|
return buildTemplate({FIREFOX: true})
|
2014-07-11 11:40:46 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('firefox:js', ['firefox:template'], () => {
|
|
|
|
return buildJs([], {FIREFOX: true})
|
2014-07-11 07:53:08 +08:00
|
|
|
})
|
2014-07-11 11:40:46 +08:00
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('firefox', ['firefox:js'], () => {
|
2014-07-11 07:53:08 +08:00
|
|
|
return merge(
|
2014-05-31 03:57:35 +08:00
|
|
|
pipe('./icons/**/*', './tmp/firefox/data/icons'),
|
2015-11-15 14:05:57 +08:00
|
|
|
pipe(['./libs/**/*', './tmp/octotree.*'], './tmp/firefox/data'),
|
|
|
|
pipe('./src/config/firefox/firefox.js', $.babel(), './tmp/firefox/lib'),
|
2015-11-08 22:50:29 +08:00
|
|
|
pipe('./src/config/firefox/package.json', './tmp/firefox')
|
2014-05-14 12:22:09 +08:00
|
|
|
)
|
2014-08-21 03:49:37 +08:00
|
|
|
})
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
gulp.task('firefox:xpi', (cb) => {
|
2014-08-21 03:49:37 +08:00
|
|
|
$.run('cd ./tmp/firefox && cfx xpi --output-file=../../dist/firefox.xpi').exec(cb)
|
|
|
|
})
|
|
|
|
|
2015-11-15 14:05:57 +08:00
|
|
|
// Safari
|
|
|
|
gulp.task('safari:template', () => {
|
|
|
|
return buildTemplate({SAFARI: true})
|
|
|
|
})
|
2015-11-14 07:54:40 +08:00
|
|
|
|
2015-11-15 14:05:57 +08:00
|
|
|
gulp.task('safari:js', ['safari:template'], () => {
|
|
|
|
return buildJs([], {SAFARI: true})
|
|
|
|
})
|
2015-11-14 07:54:40 +08:00
|
|
|
|
2015-11-15 14:05:57 +08:00
|
|
|
gulp.task('safari', ['safari:js'], () => {
|
|
|
|
return merge(
|
|
|
|
pipe('./icons/**/*', './tmp/safari/octotree.safariextension/icons'),
|
|
|
|
pipe(
|
|
|
|
['./libs/**/*', './tmp/octotree.*', './src/config/safari/**/*'],
|
|
|
|
'./tmp/safari/octotree.safariextension/'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
function pipe(src, ...transforms) {
|
|
|
|
return transforms.reduce((stream, transform) => {
|
|
|
|
const isDest = typeof transform === 'string'
|
|
|
|
return stream.pipe(isDest ? gulp.dest(transform) : transform)
|
|
|
|
}, gulp.src(src))
|
2014-08-21 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function html2js(template) {
|
|
|
|
return map(escape)
|
|
|
|
|
|
|
|
function escape(file, cb) {
|
2015-11-14 07:54:40 +08:00
|
|
|
const path = $.util.replaceExtension(file.path, '.js')
|
|
|
|
const content = file.contents.toString()
|
|
|
|
const escaped = content
|
|
|
|
.replace(/\\/g, "\\\\")
|
|
|
|
.replace(/'/g, "\\'")
|
|
|
|
.replace(/\r?\n/g, "\\n' +\n '")
|
|
|
|
const body = template.replace('$$', escaped)
|
|
|
|
|
2014-08-21 03:49:37 +08:00
|
|
|
file.path = path
|
|
|
|
file.contents = new Buffer(body)
|
|
|
|
cb(null, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 07:54:40 +08:00
|
|
|
function buildJs(overrides, ctx) {
|
|
|
|
const src = [
|
2014-08-21 03:49:37 +08:00
|
|
|
'./tmp/template.js',
|
|
|
|
'./src/constants.js',
|
2015-11-08 22:50:29 +08:00
|
|
|
'./src/adapters/adapter.js',
|
2015-11-11 05:23:35 +08:00
|
|
|
'./src/adapters/github.js',
|
|
|
|
'./src/adapters/gitlab.js',
|
2014-08-21 03:49:37 +08:00
|
|
|
'./src/view.help.js',
|
|
|
|
'./src/view.error.js',
|
|
|
|
'./src/view.tree.js',
|
|
|
|
'./src/view.options.js',
|
|
|
|
'./src/util.location.js',
|
|
|
|
'./src/util.module.js',
|
|
|
|
'./src/util.async.js',
|
2015-11-14 07:54:40 +08:00
|
|
|
'./src/util.storage.js'
|
|
|
|
].concat(overrides)
|
|
|
|
.concat('./src/octotree.js')
|
|
|
|
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe(
|
|
|
|
src,
|
|
|
|
$.babel(),
|
2014-08-21 03:49:37 +08:00
|
|
|
$.concat('octotree.js'),
|
2015-11-11 15:05:53 +08:00
|
|
|
$.preprocess({context: ctx}),
|
2015-11-15 14:05:57 +08:00
|
|
|
'./tmp'
|
|
|
|
)
|
2014-08-21 03:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildTemplate(ctx) {
|
2015-11-16 02:40:54 +08:00
|
|
|
const LOTS_OF_SPACES = new Array(500).join(' ')
|
|
|
|
|
2015-11-15 14:05:57 +08:00
|
|
|
return pipe(
|
|
|
|
'./src/template.html',
|
2014-11-25 10:56:03 +08:00
|
|
|
$.preprocess({context: ctx}),
|
2015-11-16 02:40:54 +08:00
|
|
|
$.replace('__SPACES__', LOTS_OF_SPACES),
|
2015-11-15 14:05:57 +08:00
|
|
|
html2js('const TEMPLATE = \'$$\''),
|
|
|
|
'./tmp'
|
|
|
|
)
|
2014-08-21 03:49:37 +08:00
|
|
|
}
|