1 |
|
2 | "use strict";
|
3 | var gulp = require("gulp");
|
4 | var fs = require("fs");
|
5 |
|
6 | var colors = require('ansi-colors');
|
7 | const color = require('../log/color');
|
8 | var compileTs = require("../compiler/compile-typescript");
|
9 | var unlink = require("../lib/unlink");
|
10 | var extToGlob = require("../lib/ext-to-glob");
|
11 | var watchLog = require("../log/watch");
|
12 | var log = require('../log/logger');
|
13 |
|
14 | var TS_EXTS = ["ts"];
|
15 |
|
16 |
|
17 |
|
18 | exports.build = function (config) {
|
19 | if (config.tsconfig || fs.existsSync("tsconfig.json")) {
|
20 | return function () {
|
21 | config.tsconfig = config.tsconfig || "tsconfig.json";
|
22 | return compileTs(config);
|
23 | }
|
24 | } else {
|
25 | log.info(
|
26 | color('typescript:'),
|
27 | colors.gray('`tsconfig.json` was found. Skip typescript compilation!')
|
28 | );
|
29 | return function (cb) {
|
30 | cb && cb();
|
31 | }
|
32 | }
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.watch = function (config) {
|
39 | return function (cb) {
|
40 | var glob = extToGlob(config, TS_EXTS);
|
41 | watchLog("typescript", glob);
|
42 | gulp.watch(glob, {
|
43 | ignored: config.src + "/*/**.d.ts",
|
44 | })
|
45 | .on("change", function (file) {
|
46 | return compileTs(config, file);
|
47 | })
|
48 | .on("add", function (file) {
|
49 | return compileTs(config, file);
|
50 | })
|
51 | .on("unlink", unlink(config.src, config.dist, ".js"));
|
52 | cb && cb();
|
53 | };
|
54 | };
|