|
1 | 1 | 'use strict';
|
2 | 2 | var gulp = require('gulp');
|
3 |
| -var tscConfig = require('./src/tsconfig.json'); |
4 |
| -var mxtBuilder = require("@maxxton/gulp-builder"); |
| 3 | +var gutil = require('gulp-util'); |
| 4 | +var htmlExtender = require('gulp-html-extend'); |
| 5 | +var rimraf = require('gulp-rimraf'); |
| 6 | +var typescript = require('gulp-typescript'); |
| 7 | +var async = require('async'); |
| 8 | +var merge = require('merge2'); |
| 9 | +var replace = require('gulp-replace'); |
| 10 | +var fs = require('fs'); |
| 11 | +var Gaze = require('gaze').Gaze; |
| 12 | +var mocha = require('gulp-mocha'); |
| 13 | +var tsConfig = require('./src/tsconfig.json'); |
5 | 14 |
|
6 |
| -mxtBuilder.setSettings({ |
| 15 | +var settings = { |
7 | 16 | distFolder: 'dist',
|
8 | 17 | projectName: 'angular2-rest',
|
9 |
| - tsConfig: tscConfig |
10 |
| -}); |
| 18 | + tsConfig: tsConfig |
| 19 | +}; |
11 | 20 |
|
12 | 21 | gulp.task('default', ['prepublish'], function (cb) {
|
13 |
| - mxtBuilder.watch(cb); |
| 22 | + watch(cb); |
14 | 23 | });
|
15 | 24 |
|
16 |
| -// TEMPORARY SOLUTION FOR LINKING NPM MODULES TO DEVELOPMENT |
17 |
| -gulp.task('link', function (cb) { |
18 |
| - mxtBuilder.link(cb); |
| 25 | +gulp.task('test', function (cb) { |
| 26 | + test(cb); |
19 | 27 | });
|
20 | 28 |
|
21 |
| -gulp.task('lint', function (cb) { |
22 |
| - mxtBuilder.lint(cb); |
| 29 | +gulp.task('prepublish', function (cb) { |
| 30 | + prepublish(cb); |
23 | 31 | });
|
24 | 32 |
|
25 |
| -gulp.task('bundle-javascript', function (cb) { |
26 |
| - mxtBuilder.bundleJavascript(cb); |
27 |
| -}); |
| 33 | +// ------------------- TASKS --------------------- |
28 | 34 |
|
29 |
| -gulp.task('test', function (cb) { |
30 |
| - mxtBuilder.test(cb); |
31 |
| -}); |
| 35 | +function prepublish(cb) { |
| 36 | + async.series([ |
| 37 | + function (next) { |
| 38 | + _clean(next); |
| 39 | + }, |
| 40 | + function (next) { |
| 41 | + _build(next); |
| 42 | + }, |
| 43 | + function (next) { |
| 44 | + _prepublishPackage(next); |
| 45 | + } |
| 46 | + ], cb); |
| 47 | +}; |
| 48 | + |
| 49 | +function _clean(cb) { |
| 50 | + gulp.src(settings.distFolder, { read: false }) |
| 51 | + .pipe(rimraf()) |
| 52 | + .on('finish', function () { |
| 53 | + cb(); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +function _build(cb) { |
| 58 | + async.series([ |
| 59 | + function (next) { |
| 60 | + _deployMisc(next); |
| 61 | + }, |
| 62 | + function (next) { |
| 63 | + _compileTypescript(next); |
| 64 | + } |
| 65 | + ], cb); |
| 66 | +} |
| 67 | + |
| 68 | +function _deployMisc(cb) { |
| 69 | + gutil.log('Starting ', gutil.colors.cyan('_deployMisc')); |
| 70 | + gulp.src(['src/test/*.js']).pipe(gulp.dest(settings.distFolder + '/test')); |
| 71 | + |
| 72 | + gulp.src(['package.json', 'LICENSE', 'README.md', 'src/systemjs.config.js', 'src/build.js']) |
| 73 | + .pipe(htmlExtender({ annotations: false, verbose: false })) |
| 74 | + .pipe(gulp.dest(settings.distFolder)) |
| 75 | + .on('finish', function () { |
| 76 | + gutil.log('Finished ', gutil.colors.cyan('_deployMisc')); |
| 77 | + cb(); |
| 78 | + }); |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +function _compileTypescript(cb) { |
| 83 | + _compileTypescriptFromSrc(['src/**/*.ts', '!src/**/*spec.ts', '!src/config/**/*.ts'], cb); |
| 84 | +} |
| 85 | + |
| 86 | +function watch(cb) { |
| 87 | + gutil.log('Starting ', gutil.colors.cyan('_watch')); |
| 88 | + _watchTypescript(); |
| 89 | + gulp.watch(['package.json', 'LICENSE', 'README.md', 'src/test/*.js'], ['_deployMisc']); |
| 90 | + gulp.watch(['src/**/*.spec.ts'], ['_compileSpecs']); |
| 91 | + gulp.watch(['src/**/*e2e*.ts'], ['_compileE2e']); |
| 92 | + gutil.log(gutil.colors.cyan('Watchers started')); |
| 93 | + cb(); |
| 94 | +} |
| 95 | + |
| 96 | +function _watchTypescript() { |
| 97 | + gutil.log('Watching', gutil.colors.cyan('Typescript')); |
| 98 | + var gaze = new Gaze(['src/**/*.ts', 'src/**/*.html', '!src/**/*spec.ts']); |
| 99 | + gaze.on('all', function (event, filepath) { |
| 100 | + _compileChangedTypescript(event, filepath); |
| 101 | + }); |
| 102 | + gaze.on('error', function (error) { |
| 103 | + gaze.close(); |
| 104 | + gutil.log('error occured during', gutil.colors.cyan('typescript'), 'watch. Compiling and re-watching', gutil.colors.cyan('typescript'), 'in a few seconds...'); |
| 105 | + setTimeout(function () { |
| 106 | + _compileTypescript(function () { |
| 107 | + _watchTypescript(); |
| 108 | + }); |
| 109 | + }, 3000); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +function _compileChangedTypescript(event, src) { |
| 114 | + if (event == 'changed') { |
| 115 | + src = src.substring(src.lastIndexOf("/")); |
| 116 | + src = src.substring(src.lastIndexOf("\\")); |
| 117 | + |
| 118 | + _compileTypescriptFromSrc(["src/**" + src], function () { |
| 119 | + }, true); |
| 120 | + } |
| 121 | + else if (event == 'added') { |
| 122 | + _compileTypescript(function () { |
| 123 | + }); |
| 124 | + } |
| 125 | + else if (event == 'deleted') { |
| 126 | + var jsDistFileLoc = src.replace("\\src\\", '\\' + settings.distFolder + '\\').replace('.ts', '.js'); |
| 127 | + var dTsDistFileLoc = src.replace("\\src\\", '\\' + settings.distFolder + '\\').replace('.ts', '.d.ts'); |
| 128 | + gutil.log("Deleting", gutil.colors.magenta(jsDistFileLoc)); |
| 129 | + return gulp.src([jsDistFileLoc, dTsDistFileLoc], {read: false}) |
| 130 | + .pipe(rimraf()); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function _compileTypescriptFromSrc(srcArray, cb) { |
| 135 | + gutil.log("Starting", gutil.colors.cyan('_compileTypescriptFromSrc'), "for file(s)", gutil.colors.magenta(srcArray)); |
| 136 | + |
| 137 | + var compilerOptions = Object.assign({}, settings.tsConfig.compilerOptions); |
| 138 | + |
| 139 | + //if we changed 1 file its not needed to compile the whole source code |
| 140 | + if (srcArray.length == 1 && srcArray[0].slice(-2) == "ts") { |
| 141 | + compilerOptions['isolatedModules'] = true; |
| 142 | + } |
| 143 | + |
| 144 | + var tsProject = typescript.createProject(compilerOptions); |
| 145 | + var tsResult = gulp.src(srcArray) |
| 146 | + .pipe(tsProject()); |
| 147 | + merge([ |
| 148 | + tsResult.dts.pipe(gulp.dest(settings.distFolder)), |
| 149 | + tsResult.js.pipe(gulp.dest(settings.distFolder)) |
| 150 | + ]).on('finish', function () { |
| 151 | + gutil.log('Finished ', gutil.colors.cyan('_compileTypescriptFromSrc')); |
| 152 | + cb(); |
| 153 | + }); |
| 154 | +} |
32 | 155 |
|
33 | 156 | /**
|
34 |
| - * Cleans, moves, and compiles the code |
| 157 | + * Copy part of the package.json to the dist folder for publishing. |
35 | 158 | */
|
36 |
| -gulp.task('prepublish', function (cb) { |
37 |
| - mxtBuilder.prepublish(cb); |
38 |
| -}); |
| 159 | +function _prepublishPackage(cb) { |
| 160 | + var json = JSON.parse(fs.readFileSync('./package.json')); |
| 161 | + var publishJson = { |
| 162 | + name: json.name, |
| 163 | + version: json.version, |
| 164 | + description: json.description, |
| 165 | + typings: json.typings, |
| 166 | + main: json.main, |
| 167 | + repository: json.repository, |
| 168 | + keywords: json.keywords, |
| 169 | + author: json.author, |
| 170 | + license: json.license, |
| 171 | + bugs: json.bugs, |
| 172 | + homepage: json.homepage, |
| 173 | + publishConfig: json.publishConfig, |
| 174 | + dependencies: json.dependencies |
| 175 | + }; |
| 176 | + |
| 177 | + var src = require('stream').Readable({ objectMode: true }); |
| 178 | + src._read = function () { |
| 179 | + this.push(new gutil.File({ |
| 180 | + cwd: "", |
| 181 | + base: "", |
| 182 | + path: "package.json", |
| 183 | + contents: new Buffer(JSON.stringify(publishJson, null, 2)) |
| 184 | + })); |
| 185 | + this.push(null) |
| 186 | + }; |
| 187 | + src.pipe(gulp.dest(settings.distFolder + '/')) |
| 188 | + .on('finish', function () { |
| 189 | + cb(); |
| 190 | + }); |
| 191 | +} |
| 192 | + |
| 193 | +function test(cb) { |
| 194 | + async.series([ |
| 195 | + function (next) { |
| 196 | + _clean(next); |
| 197 | + }, |
| 198 | + function (next) { |
| 199 | + _buildDeploy(next); |
| 200 | + }, |
| 201 | + function (next) { |
| 202 | + _compileSpecs(next); |
| 203 | + }, |
| 204 | + function (next) { |
| 205 | + _runMocha(next); |
| 206 | + } |
| 207 | + ], cb); |
| 208 | +}; |
| 209 | + |
| 210 | +function _buildDeploy(cb) { |
| 211 | + async.series([ |
| 212 | + function (next) { |
| 213 | + _deployMisc(next); |
| 214 | + }, |
| 215 | + function (next) { |
| 216 | + _compileTypescript(next); |
| 217 | + } |
| 218 | + ], cb); |
| 219 | +} |
| 220 | + |
| 221 | +function _compileSpecs(cb) { |
| 222 | + _compileTypescriptFromSrc(['src/**/*.spec.ts'], cb); |
| 223 | +} |
| 224 | + |
| 225 | +function _runMocha(cb) { |
| 226 | + return gulp.src(['dist/**/*.spec.js'], { read: false }) |
| 227 | + .pipe(mocha({ reporter: 'list' })) |
| 228 | + .on('error', gutil.log); |
| 229 | +} |
0 commit comments