Created
June 8, 2017 02:15
-
-
Save TylerDurham/c5bab4df4d631e92733d489c18e5a2bc to your computer and use it in GitHub Desktop.
My "go-to" TypeScript gulpfile.
This file contains hidden or 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
| import * as gulp from 'gulp'; | |
| import * as nodemon from 'gulp-nodemon'; | |
| import * as cp from 'child_process'; | |
| import * as path from 'path'; | |
| import * as del from 'del'; | |
| import * as _ from 'lodash'; | |
| import * as chalk from 'chalk'; | |
| import * as runsequence from 'run-sequence'; | |
| const exec = cp.exec; | |
| /** | |
| * A type that contains path information about the project. | |
| */ | |
| const projectPaths = { | |
| src: './src', | |
| build: './build', | |
| static: 'static', | |
| globs: { | |
| all: '/**/*.*', | |
| js: '/**/*.js', | |
| css: '/**/*.css', | |
| html: '/**/*.html', | |
| ts: '/**/*.ts', | |
| watch: null | |
| } | |
| } | |
| /** | |
| * Shortcut alias to Project Paths. | |
| */ | |
| const pp = projectPaths; | |
| /** | |
| * A type that contains style information for the console. | |
| */ | |
| const consoleStyles = { | |
| warn: chalk.red, | |
| emphasis: chalk.magenta, | |
| exe: chalk.cyan, | |
| arg: chalk.green | |
| } | |
| /** | |
| * Shortcut alias to Console Styles. | |
| */ | |
| const cs = consoleStyles; | |
| gulp.task('build:tsc', (done) => { | |
| exec('tsc --version', (err, stdout, stderr) => { | |
| stdout = _.trim(stdout.replace(/version/i, '')); | |
| if (!gulpSilent) { console.log(`Compiling '${cs.arg(pp.src)}' to '${cs.arg(pp.build)}' using ${cs.exe("tsc")} ${cs.emphasis(stdout)}`); } | |
| if (stderr) { console.warn(stderr); } | |
| }); | |
| return exec('tsc', (err, stdout, stderr) => { | |
| if (stderr) { console.warn(stderr); } | |
| done(err); | |
| }); | |
| }); | |
| gulp.task('build', (done) => { | |
| runsequence('build:clean', ['build:copy', 'build:tsc'], 'misc', () => { done() }); | |
| }); | |
| gulp.task('build:clean', () => { | |
| var path = `${pp.build}`; | |
| if (!gulpSilent) { console.log(`Cleaning files from '${cs.arg(path)}'`); } | |
| return del([path], { force: true }); | |
| }); | |
| gulp.task('build:copy', () => { | |
| var src = `${pp.src}/${pp.static}${pp.globs.all}`; | |
| var dest = `${pp.build}/${pp.static}`; | |
| if (!gulpSilent) { console.log(`Copying '${cs.arg(src)}' to '${cs.arg(dest)}'`); } | |
| return gulp.src(src) | |
| .pipe(gulp.dest(dest)) | |
| }); | |
| gulp.task('misc', () => { | |
| return exec(`chmod 755 ./lib/my-adal-cli.js`); | |
| }); | |
| gulp.task('test', ['build'], (done) => { | |
| exec('mocha --version', (err, stdout, stderr) => { | |
| stdout = _.trim(stdout); | |
| if (stderr) { console.warn(stderr); } | |
| if (!gulpSilent) { console.log(`Running tests at '${cs.arg(pp.build)}' using ${cs.exe("Mocha")} ${cs.emphasis(stdout)}`); } | |
| }); | |
| // Gulp will disable all colors in the gulp process, but since Mocha is spawned in a new process, | |
| // we'll need to let Mocha know if we want/don't want colors | |
| let noColors = (gulpNoColors===true) ? '--no-colors' : '--colors'; | |
| return exec(`mocha ${pp.build} ${noColors}`, (err, stdout, stderr) => { | |
| if (stderr) { console.warn(stderr); } | |
| if (!gulpSilent) { console.log(stdout); } | |
| done(err); | |
| }); | |
| }); | |
| gulp.task('watch', ['build'], (done) => { | |
| var watchDir = path.join(pp.src, pp.globs.all); | |
| if (!gulpSilent) { console.log(`Watching for changes at '${cs.arg(watchDir)}'`); } | |
| gulp.watch(watchDir, ['build'], () => { | |
| }); | |
| }) | |
| gulp.task('default', ['watch'], (done) => { | |
| done(); | |
| }); | |
| gulp.task('argv', () => { | |
| console.log (process.argv) | |
| }) | |
| /** | |
| * Indicates if a particular switch was passed as an argument. | |
| * @param switchName The name of the switch | |
| */ | |
| function hasSwitch(switchName: string):boolean { | |
| return (0 < process.argv.findIndex((value) => { return (value === switchName) })); | |
| } | |
| /** Adhere to Gulp's --silent switch. */ | |
| const gulpSilent = hasSwitch('--silent'); | |
| /** Adhere to Gulp's --no-colors switch. */ | |
| const gulpNoColors = hasSwitch('--no-color') || hasSwitch('--no-colors'); | |
| /** Let users know how to turn off console messages. */ | |
| if (!gulpSilent) { console.log(cs.emphasis.bold('Notice: ') + `Logging is ${cs.arg.bold('enabled')}. Use ${cs.exe.bold('Gulp')} with the ${cs.arg.bold('--silent')} switch to disable console messages.`); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment