gulpfile.js 4.33 KB
Newer Older
chinguyen's avatar
chinguyen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
"use strict";
let gulp = require("gulp");
let sourcemaps = require("gulp-sourcemaps");
let typescript = require("gulp-typescript");
let nodemon = require("gulp-nodemon");
let tslint = require("gulp-tslint");
let rimraf = require("rimraf");
let typedoc = require("gulp-typedoc");
let mocha = require("gulp-mocha");
let istanbul = require("gulp-istanbul");
let plumber = require("gulp-plumber");
let remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul");

const CLEAN_BUILD = "clean:build";
const CLEAN_COVERAGE = "clean:coverage";
const CLEAN_DOC = "clean:doc";
const TSLINT = "tslint";
const COMPILE_TYPESCRIPT = "compile:typescript";
const COPY_STATIC_FILES = "copy:static";
const BUILD = "build";
const GENERATE_DOC = "generate:doc";
const PRETEST = "pretest";
const RUN_TESTS = "run:tests";
const TEST = "test";
const REMAP_COVERAGE = "remap:coverage";

const TS_SRC_GLOB = "./src/**/*.ts";
const TS_TEST_GLOB = "./test/**/*.ts";
const JS_TEST_GLOB = "./build/**/*.js";
const JS_SRC_GLOB = "./build/**/*.js";
const TS_GLOB = [TS_SRC_GLOB];
const STATIC_FILES = ['./src/**/*.json']

const tsProject = typescript.createProject("tsconfig.json");

// Removes the ./build directory with all its content.
gulp.task(CLEAN_BUILD, function (callback) {
  rimraf("./build", callback);
});

// Removes the ./coverage directory with all its content.
gulp.task(CLEAN_COVERAGE, function (callback) {
  rimraf("./coverage", callback);
});

// Removes the ./docs directory with all its content.
gulp.task(CLEAN_DOC, function (callback) {
  rimraf("./docs", callback);
});

// Checks all *.ts-files if they are conform to the rules specified in tslint.json.
gulp.task(TSLINT, function () {
  return gulp.src(TS_GLOB)
    .pipe(tslint({ formatter: "verbose" }))
    .pipe(tslint.report({
      // set this to true, if you want the build process to fail on tslint errors.
      emitError: false
    }));
});

// Compiles all *.ts-files to *.js-files.
gulp.task(COPY_STATIC_FILES, function () {
  return gulp.src(STATIC_FILES)
    .pipe(gulp.dest("build"));
});

gulp.task(COMPILE_TYPESCRIPT, function () {
  return gulp.src(TS_GLOB)
    .pipe(sourcemaps.init())
    .pipe(tsProject())
    .pipe(sourcemaps.write(".", { sourceRoot: "../src" }))
    .pipe(gulp.dest("build"));
});

// Runs all required steps for the build in sequence.
gulp.task(BUILD, gulp.series(CLEAN_BUILD, TSLINT, COMPILE_TYPESCRIPT, COPY_STATIC_FILES));

// Generates a documentation based on the code comments in the *.ts files.
gulp.task(GENERATE_DOC, gulp.series(CLEAN_DOC, function () {
  return gulp.src(TS_SRC_GLOB)
    .pipe(typedoc({
      out: "docs",
      target: "es5",
      name: "Express + Sequelize",
      module: "commonjs",
      readme: "readme.md",
      includeDeclarations: true,
      version: true
    }))
}));

// Sets up the istanbul coverage
gulp.task(PRETEST, function () {
  return gulp.src(JS_SRC_GLOB)
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(istanbul({ includeUntested: true }))
    .pipe(istanbul.hookRequire())
});

// Run the tests via mocha and generate a istanbul json report.
gulp.task(RUN_TESTS, function (callback) {
  let mochaError;
  gulp.src(JS_TEST_GLOB)
    .pipe(plumber())
    .pipe(mocha({ reporter: "spec" }))
    .on("error", function (err) {
      mochaError = err;
    })
    .pipe(istanbul.writeReports({
      reporters: ["json"]
    }))
    .on("end", function () {
      callback(mochaError);
    });
});

// Remap Coverage to *.ts-files and generate html, text and json summary
gulp.task(REMAP_COVERAGE, function () {
  return gulp.src("./coverage/coverage-final.json")
    .pipe(remapIstanbul({
      // basePath: ".",
      fail: true,
      reports: {
        "html": "./coverage",
        "json": "./coverage",
        "text-summary": null,
        "lcovonly": "./coverage/lcov.info"
      }
    }))
    .pipe(gulp.dest("coverage"))
    .on("end", function () {
      console.log("--> For a more detailed report, check the ./coverage directory <--")
    });
});

// Runs all required steps for testing in sequence.
gulp.task(TEST, gulp.series(BUILD, CLEAN_COVERAGE, PRETEST, RUN_TESTS, REMAP_COVERAGE));

// Runs the build task and starts the server every time changes are detected.
gulp.task("watch", gulp.series(BUILD, function () {
  return nodemon({
    ext: "ts json",
    script: "build/server.js",
    watch: ["src/*", "test/*"],
    legacyWatch: true,
    tasks: [BUILD]
  });
}));