Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- /** Initialze the Gulp library. */
- export function initialize(gulp) {
- gulp.task('copy', gulpCopy);
- gulp.task('clean', gulpClean);
- gulp.task('tsc', gulpTsc);
- gulp.task('watch', gulpWatch);
- gulp.task('build', gulpBuild);
- gulp.task('serve', ['build'], gulpServe)
- }
- /**
- * Source/Build/Lib paths. Feel free to modify.
- */
- export const TnProjectPaths = {
- src: './src',
- build: './build',
- static: `/static`,
- lib: './lib',
- glob: '/**/*.*'
- }
- /** Chalk styles utility object. Feel free to modify. */
- export const TnConsoleStyles = {
- error: chalk.red,
- param: chalk.green.italic,
- action: chalk.green,
- bullet: chalk.bold.green
- }
- /** Copies all static files from source to build directory. */
- function gulpCopy() {
- TnLogger.log(`${TnConsoleStyles.action('Copying')} static files to ${TnConsoleStyles.param(path.join(TnProjectPaths.build, TnProjectPaths.static))}`);
- return gulp.src(`${path.join(TnProjectPaths.src, TnProjectPaths.static, TnProjectPaths.glob)}`)
- .pipe(gulp.dest(`${path.join(TnProjectPaths.build, TnProjectPaths.static)}`));
- }
- /**
- * Cleans the build directory.
- */
- function gulpClean(done) {
- var p = path.join(TnProjectPaths.build, TnProjectPaths.glob);
- var s = path.join(TnProjectPaths.build, TnProjectPaths.static, TnProjectPaths.glob);
- TnLogger.log(`${TnConsoleStyles.action('Cleaning')} static files at ${TnConsoleStyles.param(TnProjectPaths.build)}`);
- del([p, s]).then(() => done()).catch((err) => done(err));
- }
- /**
- * Runs the TypeScript compiler.
- */
- function gulpTsc(done) {
- exec('tsc --version', (err, stdout, stderr) => {
- TnLogger.log(`${TnConsoleStyles.action('Compiling')} using ${TnConsoleStyles.param('tsc')} ${TnConsoleStyles.param(_.trim(stdout).toLowerCase())}`);
- if (stderr) TnLogger.warn(stderr);
- });
- return exec('tsc', (err, stdout, stderr) => {
- if (stderr) TnLogger.warn(stderr);
- done(err);
- });
- }
- /** Runs the entire build. */
- function gulpBuild(done) {
- runsequence('clean', ['copy', 'tsc'], () => { done() });
- }
- //** Runs the build, starts the server, and watches for changes in the source directory. */
- function gulpServe() {
- return nodemon({
- script: `${TnProjectPaths.build}/app.js`,
- ext: 'ts html js png jpg gif css',
- watch: './src',
- tasks: function(changedFiles: string[]) {
- var tasks = [];
- if(changedFiles != null) {
- tasks.push['copy']; //Always copy
- // Only compile .TS files if they have changed.
- changedFiles.forEach((file) => {
- var ext = path.extname(file).toLowerCase();
- if(ext === '.ts') { tasks.push('tsc') }
- });
- }
- return tasks;
- }
- });
- }
- /** Watches for changes in the source directory. */
- function gulpWatch(done) {
- TnLogger.log(`${TnConsoleStyles.action('Watching')} files at ${TnConsoleStyles.param(TnProjectPaths.src)}`);
- gulp.watch([TnProjectPaths.src], ['build'] );
- done();
- }
- /** Utility Console Logger. */
- export class TnLogger {
- /** Simple logging utility function for standard messages. */
- static log(msg : string) {
- console.warn(`${TnConsoleStyles.action('* ')}` + _.trim(msg));
- }
- /** Simple logging utility function for warning messages. */
- static warn(msg : string) {
- console.warn(TnConsoleStyles.error(`* ${_.trim(msg)}`));
- }
- /** Simple logging utility function for error messages. */
- static error(msg : string) {
- console.error(TnConsoleStyles.error(`* ${_.trim(msg)}`));
- }
- }
Advertisement