TylerDurham

TNBlues Core Gulp Tasks

May 27th, 2017
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as gulp from 'gulp';
  2. import * as nodemon from 'gulp-nodemon';
  3. import * as cp from 'child_process';
  4. import * as path from 'path';
  5. import * as del from 'del';
  6. import * as _ from 'lodash';
  7. import * as chalk from 'chalk';
  8. import * as runsequence from 'run-sequence';
  9. const exec = cp.exec;
  10.  
  11. /** Initialze the Gulp library. */
  12. export function initialize(gulp) {
  13.     gulp.task('copy', gulpCopy);
  14.     gulp.task('clean', gulpClean);
  15.     gulp.task('tsc', gulpTsc);
  16.     gulp.task('watch', gulpWatch);
  17.     gulp.task('build', gulpBuild);
  18.     gulp.task('serve', ['build'], gulpServe)
  19. }
  20.  
  21. /**
  22.  * Source/Build/Lib paths. Feel free to modify.
  23.  */
  24. export const TnProjectPaths = {
  25.     src: './src',
  26.     build: './build',
  27.     static: `/static`,
  28.     lib: './lib',
  29.     glob: '/**/*.*'
  30. }
  31.  
  32. /** Chalk styles utility object. Feel free to modify. */
  33. export const TnConsoleStyles = {
  34.     error: chalk.red,
  35.     param: chalk.green.italic,
  36.     action: chalk.green,
  37.     bullet: chalk.bold.green
  38. }
  39.  
  40. /** Copies all static files from source to build directory. */
  41. function gulpCopy() {
  42.     TnLogger.log(`${TnConsoleStyles.action('Copying')} static files to ${TnConsoleStyles.param(path.join(TnProjectPaths.build, TnProjectPaths.static))}`);
  43.     return gulp.src(`${path.join(TnProjectPaths.src, TnProjectPaths.static, TnProjectPaths.glob)}`)
  44.     .pipe(gulp.dest(`${path.join(TnProjectPaths.build, TnProjectPaths.static)}`));
  45. }
  46.  
  47. /**
  48.  * Cleans the build directory.
  49.  */
  50. function gulpClean(done) {
  51.     var p = path.join(TnProjectPaths.build, TnProjectPaths.glob);
  52.     var s = path.join(TnProjectPaths.build, TnProjectPaths.static, TnProjectPaths.glob);
  53.     TnLogger.log(`${TnConsoleStyles.action('Cleaning')} static files at ${TnConsoleStyles.param(TnProjectPaths.build)}`);
  54.     del([p, s]).then(() => done()).catch((err) => done(err));
  55. }
  56.  
  57. /**
  58.  * Runs the TypeScript compiler.
  59.  */
  60. function gulpTsc(done) {
  61.     exec('tsc --version', (err, stdout, stderr) => {
  62.         TnLogger.log(`${TnConsoleStyles.action('Compiling')} using ${TnConsoleStyles.param('tsc')} ${TnConsoleStyles.param(_.trim(stdout).toLowerCase())}`);
  63.         if (stderr) TnLogger.warn(stderr);  
  64.     });
  65.  
  66.     return exec('tsc', (err, stdout, stderr) => {
  67.         if (stderr) TnLogger.warn(stderr);  
  68.         done(err);
  69.     });
  70. }
  71.  
  72. /** Runs the entire build. */
  73. function gulpBuild(done) {
  74.     runsequence('clean', ['copy', 'tsc'], () => { done() });
  75. }
  76.  
  77. //** Runs the build, starts the server, and watches for changes in the source directory. */
  78. function gulpServe() {
  79.     return nodemon({
  80.         script: `${TnProjectPaths.build}/app.js`,
  81.         ext: 'ts html js png jpg gif css',
  82.         watch: './src',
  83.         tasks: function(changedFiles: string[]) {
  84.             var tasks = [];
  85.  
  86.             if(changedFiles != null) {
  87.                 tasks.push['copy']; //Always copy
  88.  
  89.                 // Only compile .TS files if they have changed.
  90.                 changedFiles.forEach((file) => {
  91.                     var ext = path.extname(file).toLowerCase();
  92.                     if(ext === '.ts') { tasks.push('tsc') }
  93.                 });
  94.             }
  95.  
  96.             return tasks;
  97.         }
  98.     });
  99. }
  100.  
  101. /** Watches for changes in the source directory.  */
  102. function gulpWatch(done) {
  103.     TnLogger.log(`${TnConsoleStyles.action('Watching')} files at ${TnConsoleStyles.param(TnProjectPaths.src)}`);
  104.     gulp.watch([TnProjectPaths.src], ['build'] );
  105.     done();
  106. }
  107.  
  108. /** Utility Console Logger. */
  109. export class TnLogger {
  110.     /** Simple logging utility function for standard messages. */
  111.     static log(msg : string) {
  112.         console.warn(`${TnConsoleStyles.action('* ')}` + _.trim(msg));
  113.     }
  114.  
  115.     /** Simple logging utility function for warning messages. */
  116.     static warn(msg : string) {
  117.         console.warn(TnConsoleStyles.error(`* ${_.trim(msg)}`));
  118.     }
  119.  
  120.     /** Simple logging utility function for error messages. */
  121.     static error(msg : string) {
  122.         console.error(TnConsoleStyles.error(`* ${_.trim(msg)}`));
  123.     }
  124. }
Advertisement