Created
March 21, 2016 13:54
-
-
Save nmccready/80542daa6d4e8a6dc2ba to your computer and use it in GitHub Desktop.
gulp 4.0 simplified with child_process spawn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
require('./backend/extensions') | |
let gulp = require('gulp'), | |
argv = require('yargs').argv, | |
del = require('del'), | |
$ = require('gulp-load-plugins')(), | |
spawn = require('child_process').spawn, | |
os = require('os'), | |
path = require('path'), | |
osCMD = os.platform() === 'win32' ? '.cmd' : '', | |
APP_NAME = require('./backend/config/config').APP_NAME, | |
INSTANCES = require('./backend/config/config').INSTANCES, | |
entry = `./backend/${APP_NAME}.js` | |
let paths = { | |
frontend: './frontend', | |
backend: './backend', | |
dist: './dist' | |
} | |
function getShellCmd(cliName, args) { | |
let cmd = path.resolve('./node_modules/.bin/' + cliName + osCMD) | |
return spawn(cmd, args) | |
} | |
function gulpShellCmd(done, cliName, args, steamRoll){ | |
let cmd = getShellCmd(cliName, args) | |
cmd.stderr.pipe(process.stderr) | |
cmd.stdout.on('data', (data) => $.util.log(String(data))); | |
['close', 'exit', 'error'].forEach((listName) => { | |
if(steamRoll){ | |
return cmd.on(listName, () => done()) | |
} | |
cmd.on(listName, done) | |
}) | |
} | |
let gsc = gulpShellCmd | |
function lint(done, doWatch, toWatch) { | |
// easy way to interact with CLI of esw | |
// (learned from inside of esw on how it interacts with eslint) | |
toWatch = toWatch || ['./backend/*.js', './backend/**/*.js'] | |
if (doWatch) | |
toWatch.push('-w') | |
gsc(done, 'esw', toWatch) | |
} | |
gulp.task('lint', (done) => lint(done)) | |
gulp.task('lint:watch', (done) => lint(done, true)) | |
gulp.task('serve', (done) => { | |
let args = [] | |
if(argv.debug) | |
args.push('--debug') | |
args.push(entry) | |
gsc(done, 'nodemon', args) | |
}) | |
gulp.task('serve:prod', (done) => gsc(done, 'pm2', ['start', '-i', INSTANCES, entry])) | |
gulp.task('assets', (done) => gsc(done, 'webpack', ['--progress', '-c', '-w'])) | |
gulp.task('assets:prod', (done) => gsc(done, 'webpack', ['-p', '--config', 'webpack.production.js'])) | |
gulp.task('clean', (done) => { | |
del([paths.dist, '*.log']).then(() => done()).catch((err) => done(err)) | |
}) | |
gulp.task('html', () => { | |
return gulp.src(`${paths.frontend}/*.html`) | |
.pipe(gulp.dest(paths.dist)) | |
}) | |
gulp.task('default', gulp.series('clean', gulp.parallel('html', 'assets', 'lint:watch', 'serve'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment