1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var __read = (this && this.__read) || function (o, n) {
|
4 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
5 | if (!m) return o;
|
6 | var i = m.call(o), r, ar = [], e;
|
7 | try {
|
8 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
9 | }
|
10 | catch (error) { e = { error: error }; }
|
11 | finally {
|
12 | try {
|
13 | if (r && !r.done && (m = i["return"])) m.call(i);
|
14 | }
|
15 | finally { if (e) throw e.error; }
|
16 | }
|
17 | return ar;
|
18 | };
|
19 | var __spread = (this && this.__spread) || function () {
|
20 | for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
21 | return ar;
|
22 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | process.env.INIT_CWD = process.cwd();
|
25 | var chalk_1 = require("chalk");
|
26 | var spawn = require("cross-spawn");
|
27 | var log = require("fancy-log");
|
28 | var fs = require("fs");
|
29 | var path = require("path");
|
30 | var internal_1 = require("./internal");
|
31 | var gulpModulePath = path.dirname(require.resolve('gulp'));
|
32 | var gulpBinaryFile = path.join(gulpModulePath, '/bin/gulp.js');
|
33 | var gulpFile = path.resolve(__dirname, '../lib');
|
34 | var logLine = function () { return log(chalk_1.default.green("==================================================")); };
|
35 |
|
36 | var timplaWatch = internal_1.TIMPLA_CONFIG.development && internal_1.TIMPLA_CONFIG.development.timplaWatch;
|
37 |
|
38 | if (timplaWatch === undefined) {
|
39 | timplaWatch = true;
|
40 | }
|
41 | var args = process.argv.slice(2);
|
42 |
|
43 | var devMode = args.length === 0;
|
44 |
|
45 | args.unshift('--gulpfile', gulpFile);
|
46 | var defaultEnv = devMode ? 'development' : 'production';
|
47 | process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv;
|
48 | var spawnEnv = Object.create(process.env);
|
49 | var run = function (reload) {
|
50 | if (reload === void 0) { reload = false; }
|
51 |
|
52 | spawnEnv.TIMPLA_DEV_RELOAD = reload;
|
53 | var prc = spawn(gulpBinaryFile, args, { env: spawnEnv, stdio: 'inherit' });
|
54 | prc.on('close', function (code, signal) {
|
55 | if (code !== null) {
|
56 | process.exit(code);
|
57 | }
|
58 | if (signal) {
|
59 | if (signal === 'SIGKILL') {
|
60 | process.exit(137);
|
61 | }
|
62 |
|
63 | log(chalk_1.default.green("got signal " + signal + ", exiting"));
|
64 | process.exit(1);
|
65 | }
|
66 | process.exit(0);
|
67 | });
|
68 | prc.on('error', function (err) {
|
69 | log.error(err);
|
70 | process.exit(1);
|
71 | });
|
72 | return prc;
|
73 | };
|
74 | var proc = run();
|
75 | var restartServer = function (eventType, fileName) {
|
76 | log(chalk_1.default.green("\n> " + eventType + "d " + fileName + ", restarting the server..."));
|
77 |
|
78 | proc.removeAllListeners('close');
|
79 | proc.kill();
|
80 | proc = run(true);
|
81 | };
|
82 | if (devMode && timplaWatch) {
|
83 | var watchableFiles_1 = ['eslint', 'tslint', 'babel', 'tsconfig', 'timplaconfig'];
|
84 | var sessionWatchFiles_1 = __spread(watchableFiles_1);
|
85 | fs.watch(internal_1.TIMPLA_PROCESS.INIT_CWD, function (eventType, fileName) {
|
86 | if (watchableFiles_1.find(function (e) { return fileName.indexOf(e) > -1; })) {
|
87 |
|
88 | restartServer(eventType, fileName);
|
89 | }
|
90 | });
|
91 |
|
92 | if (timplaWatch instanceof Array) {
|
93 | timplaWatch.forEach(function (fileOrFolder) {
|
94 | var pathsToCheck = [path.resolve(internal_1.TIMPLA_PROCESS.INIT_CWD, fileOrFolder), fileOrFolder];
|
95 | var firstValidPath = pathsToCheck.find(function (e) { return fs.existsSync(e); });
|
96 | if (firstValidPath) {
|
97 | sessionWatchFiles_1.push(firstValidPath);
|
98 | fs.watch(firstValidPath, function (eventType, fileName) {
|
99 | restartServer(eventType, fileName);
|
100 | });
|
101 | }
|
102 | });
|
103 | }
|
104 | logLine();
|
105 | log(chalk_1.default.green("Timpla started"));
|
106 | logLine();
|
107 | log(chalk_1.default.green("========= Watching files for full-reload ========="));
|
108 | sessionWatchFiles_1.map(function (f) { return log(f); });
|
109 | logLine();
|
110 | }
|
111 | else if (devMode) {
|
112 | log(chalk_1.default.yellow("Timpla: reload disabled"));
|
113 | }
|
114 |
|
\ | No newline at end of file |