UNPKG

4.41 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __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};
19var __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};
23Object.defineProperty(exports, "__esModule", { value: true });
24process.env.INIT_CWD = process.cwd();
25var chalk_1 = require("chalk");
26var spawn = require("cross-spawn");
27var log = require("fancy-log");
28var fs = require("fs");
29var path = require("path");
30var internal_1 = require("./internal");
31var gulpModulePath = path.dirname(require.resolve('gulp'));
32var gulpBinaryFile = path.join(gulpModulePath, '/bin/gulp.js');
33var gulpFile = path.resolve(__dirname, '../lib');
34var logLine = function () { return log(chalk_1.default.green("==================================================")); };
35// Check if the user specified timplaWatch
36var timplaWatch = internal_1.TIMPLA_CONFIG.development && internal_1.TIMPLA_CONFIG.development.timplaWatch;
37// If the user didn't specify anything, set timplaWatch to true
38if (timplaWatch === undefined) {
39 timplaWatch = true;
40}
41var args = process.argv.slice(2);
42// when no args are passed
43var devMode = args.length === 0;
44// Prepend everything with npx
45args.unshift('--gulpfile', gulpFile);
46var defaultEnv = devMode ? 'development' : 'production';
47process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv;
48var spawnEnv = Object.create(process.env);
49var run = function (reload) {
50 if (reload === void 0) { reload = false; }
51 // Used to check if pages should reload, on dev mode.
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 // eslint-disable-next-line
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};
74var proc = run();
75var restartServer = function (eventType, fileName) {
76 log(chalk_1.default.green("\n> " + eventType + "d " + fileName + ", restarting the server..."));
77 // Don't listen to 'close' now since otherwise parent gets killed by listener
78 proc.removeAllListeners('close');
79 proc.kill();
80 proc = run(true);
81};
82if (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 // eslint-disable-next-line
88 restartServer(eventType, fileName);
89 }
90 });
91 // Check if it is an array
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}
111else if (devMode) {
112 log(chalk_1.default.yellow("Timpla: reload disabled"));
113}
114//# sourceMappingURL=bin.js.map
\No newline at end of file