1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _os = _interopRequireDefault(require("os"));
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _sbFs = _interopRequireDefault(require("sb-fs"));
|
13 |
|
14 | var _chalk = _interopRequireDefault(require("chalk"));
|
15 |
|
16 | var _pQueue = _interopRequireDefault(require("p-queue"));
|
17 |
|
18 | var _makeDir = _interopRequireDefault(require("make-dir"));
|
19 |
|
20 | var _chokidar = _interopRequireDefault(require("chokidar"));
|
21 |
|
22 | var _anymatch = _interopRequireDefault(require("anymatch"));
|
23 |
|
24 | var _debounce = _interopRequireDefault(require("lodash/debounce"));
|
25 |
|
26 | var _child_process = _interopRequireDefault(require("child_process"));
|
27 |
|
28 | var _package = _interopRequireDefault(require("../package.json"));
|
29 |
|
30 | var _iterate = _interopRequireDefault(require("./iterate"));
|
31 |
|
32 | var _helpers = require("./helpers");
|
33 |
|
34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
35 |
|
36 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
37 |
|
38 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
39 |
|
40 | function main(_x) {
|
41 | return _main.apply(this, arguments);
|
42 | }
|
43 |
|
44 | function _main() {
|
45 | _main = _asyncToGenerator(function* (config) {
|
46 | let spawnedProcess;
|
47 |
|
48 | const resolvedSourceDirectory = _path.default.resolve(config.root, config.sourceDirectory);
|
49 |
|
50 | const extensions = config.typescript ? ['.ts', '.tsx', '.js'] : ['.js'];
|
51 | const timestampCache = yield (0, _helpers.getCacheDB)(resolvedSourceDirectory, !config.disableCache);
|
52 | const babelTransformFile = (0, _helpers.getBabelTransformFile)(resolvedSourceDirectory);
|
53 | const transformationQueue = new _pQueue.default({
|
54 | concurrency: _os.default.cpus().length
|
55 | });
|
56 |
|
57 | const getOutputFilePath = filePath => {
|
58 | const extName = _path.default.extname(filePath);
|
59 |
|
60 | if (extName.length) {
|
61 | return `${filePath.slice(0, -1 * _path.default.extname(filePath).length)}.js`;
|
62 | }
|
63 |
|
64 | return filePath;
|
65 | };
|
66 |
|
67 | function log(...items) {
|
68 | if (config.execute) {
|
69 | console.log(`${_chalk.default.yellow('[sb-babel-cli]')}`, ...items);
|
70 | } else {
|
71 | console.log(...items);
|
72 | }
|
73 | }
|
74 |
|
75 | function processFile(_x2, _x3, _x4) {
|
76 | return _processFile.apply(this, arguments);
|
77 | }
|
78 |
|
79 | function _processFile() {
|
80 | _processFile = _asyncToGenerator(function* (sourceFile, outputFile, stats) {
|
81 | if (!extensions.includes(_path.default.extname(sourceFile))) return;
|
82 | const transformed = yield babelTransformFile(sourceFile, {
|
83 | root: config.root
|
84 | });
|
85 | yield (0, _makeDir.default)(_path.default.dirname(outputFile));
|
86 | yield _sbFs.default.writeFile(outputFile, transformed.code, {
|
87 | mode: stats.mode
|
88 | });
|
89 | log(sourceFile, '->', outputFile);
|
90 |
|
91 | if (config.writeFlowSources) {
|
92 | const flowOutputFile = `${outputFile}.flow`;
|
93 |
|
94 | try {
|
95 | yield _sbFs.default.unlink(flowOutputFile);
|
96 | } catch (_) {
|
97 |
|
98 | }
|
99 |
|
100 | try {
|
101 | yield _sbFs.default.symlink(_path.default.resolve(sourceFile), flowOutputFile);
|
102 | log(sourceFile, '->', flowOutputFile);
|
103 | } catch (error) {
|
104 |
|
105 | }
|
106 | }
|
107 |
|
108 | timestampCache.set((0, _helpers.getSha1)(sourceFile), stats.mtime.getTime()).write();
|
109 | });
|
110 | return _processFile.apply(this, arguments);
|
111 | }
|
112 |
|
113 | function execute() {
|
114 | if (!config.execute) return;
|
115 |
|
116 | if (!spawnedProcess) {
|
117 | log(_chalk.default.yellow(_package.default.version));
|
118 | log(_chalk.default.yellow('to restart at any time, enter `rs`'));
|
119 | }
|
120 |
|
121 | log(_chalk.default.green(`starting 'node ${config.execute}'`));
|
122 |
|
123 | if (spawnedProcess) {
|
124 | spawnedProcess.kill();
|
125 | }
|
126 |
|
127 | spawnedProcess = _child_process.default.spawn(process.execPath, [config.execute], {
|
128 | stdio: 'inherit'
|
129 | });
|
130 | }
|
131 |
|
132 | const debounceExecute = (0, _debounce.default)(function () {
|
133 | try {
|
134 | execute();
|
135 | } catch (error) {
|
136 | (0, _helpers.logError)(error);
|
137 | }
|
138 | }, config.executeDelay);
|
139 | yield (0, _iterate.default)({
|
140 | extensions,
|
141 | getOutputFilePath,
|
142 | rootDirectory: config.sourceDirectory,
|
143 | sourceDirectory: config.sourceDirectory,
|
144 | outputDirectory: config.outputDirectory,
|
145 | ignored: config.ignored,
|
146 | keepExtraFiles: config.keepExtraFiles,
|
147 | filesToKeep: input => input.concat(config.writeFlowSources ? input.map(i => `${i}.flow`) : []),
|
148 |
|
149 | callback(sourceFile, outputFile, stats) {
|
150 | return _asyncToGenerator(function* () {
|
151 | const cachedTimestamp = yield timestampCache.get((0, _helpers.getSha1)(sourceFile)).value();
|
152 |
|
153 | if (cachedTimestamp === stats.mtime.getTime()) {
|
154 | if (!config.execute) {
|
155 | log(sourceFile, 'is unchanged');
|
156 | }
|
157 |
|
158 | return;
|
159 | }
|
160 |
|
161 | transformationQueue.add(() => processFile(sourceFile, outputFile, stats)).catch(_helpers.logError);
|
162 | })();
|
163 | }
|
164 |
|
165 | });
|
166 |
|
167 | if (!config.watch) {
|
168 | yield transformationQueue.onIdle();
|
169 | return;
|
170 | }
|
171 |
|
172 | if (config.execute && process.stdin) {
|
173 | if (typeof process.stdin.unref === 'function') {
|
174 | process.stdin.unref();
|
175 | }
|
176 |
|
177 | process.stdin.on('data', function (chunk) {
|
178 | if (chunk.toString().trim() === 'rs') {
|
179 | debounceExecute();
|
180 | }
|
181 | });
|
182 | }
|
183 |
|
184 | const watcher = _chokidar.default.watch(resolvedSourceDirectory, {
|
185 | ignored: config.ignored,
|
186 | alwaysStat: true,
|
187 | ignoreInitial: true
|
188 | });
|
189 |
|
190 | watcher.on('add', function (givenFileName, stats) {
|
191 | const fileName = _path.default.relative(resolvedSourceDirectory, givenFileName);
|
192 |
|
193 | const sourceFile = _path.default.join(config.sourceDirectory, fileName);
|
194 |
|
195 | const outputFile = _path.default.join(config.outputDirectory, fileName);
|
196 |
|
197 | transformationQueue.add(() => processFile(sourceFile, getOutputFilePath(outputFile), stats)).catch(_helpers.logError).then(() => {
|
198 | if (!config.ignoredForRestart || !(0, _anymatch.default)(config.ignoredForRestart, sourceFile)) {
|
199 | debounceExecute();
|
200 | }
|
201 | });
|
202 | });
|
203 | watcher.on('change', function (givenFileName, stats) {
|
204 | const fileName = _path.default.relative(resolvedSourceDirectory, givenFileName);
|
205 |
|
206 | const sourceFile = _path.default.join(config.sourceDirectory, fileName);
|
207 |
|
208 | const outputFile = _path.default.join(config.outputDirectory, fileName);
|
209 |
|
210 | transformationQueue.add(() => processFile(sourceFile, getOutputFilePath(outputFile), stats)).catch(_helpers.logError).then(() => {
|
211 | if (!config.ignoredForRestart || !(0, _anymatch.default)(config.ignoredForRestart, sourceFile)) {
|
212 | debounceExecute();
|
213 | }
|
214 | });
|
215 | });
|
216 | watcher.on('unlink', function (givenFileName) {
|
217 | const fileName = _path.default.relative(resolvedSourceDirectory, givenFileName);
|
218 |
|
219 | const outputFile = _path.default.join(config.outputDirectory, fileName);
|
220 |
|
221 | _sbFs.default.unlink(outputFile).catch(function () {
|
222 |
|
223 | });
|
224 | });
|
225 | yield transformationQueue.onIdle();
|
226 | debounceExecute();
|
227 | });
|
228 | return _main.apply(this, arguments);
|
229 | }
|
230 |
|
231 | var _default = main;
|
232 | exports.default = _default; |
\ | No newline at end of file |