forked from vadimdemedes/pastel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (121 loc) · 4.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
const EventEmitter = require('events');
const {promisify} = require('util');
const path = require('path');
const fs = require('fs');
const Bundler = require('parcel-bundler');
const makeDir = require('make-dir');
const watch = require('watch');
const del = require('del');
const readCommands = require('./lib/read-commands');
const getEntrypointPaths = require('./lib/get-entrypoint-paths');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const chmod = promisify(fs.chmod);
class Pastel extends EventEmitter {
constructor(appPath, {testMode} = {}) {
super();
this.appPath = appPath;
this.commandsPath = path.join(appPath, 'commands');
this.buildPath = path.join(appPath, 'build');
this.cachePath = path.join(appPath, 'node_modules', '.cache', 'parcel');
this.tsConfigPath = path.join(appPath, 'tsconfig.json');
this.testMode = testMode;
}
checkOrCreateTsConfig() {
if (!fs.existsSync(this.tsConfigPath)) {
fs.writeFileSync(this.tsConfigPath, JSON.stringify({compilerOptions: {jsx: 'react'}}, null, 2));
console.log('We detected TypeScript in your project and created a `tsconfig.json` file for you.');
}
}
getEntryPointPaths(commands) {
const paths = getEntrypointPaths(this.commandsPath, commands);
if (paths.some(filePath => path.extname(filePath) === '.tsx')) {
this.checkOrCreateTsConfig();
}
return paths;
}
async createBuildDir() {
// `del` refuses to delete "build" directory when running Pastel's tests,
// so we use `force` option to override this behavior in test mode
await del(this.buildPath, {
force: this.testMode
});
return makeDir(this.buildPath);
}
async scanCommands() {
const commands = await readCommands(this.commandsPath, this.commandsPath);
await writeFile(path.join(this.buildPath, 'commands.json'), JSON.stringify({commands}, null, '\t'));
return commands;
}
async saveEntrypoint() {
const entrypointSource = await readFile(path.join(__dirname, 'entry.js'));
await writeFile(path.join(this.buildPath, 'cli.js'), entrypointSource);
await chmod(path.join(this.buildPath, 'cli.js'), 0o777);
}
createBundler(paths, {watch}) {
return new Bundler(paths, {
outDir: path.join(this.buildPath, 'commands'),
cacheDir: this.cachePath,
target: 'node',
watch,
logLevel: 0,
throwErrors: !watch
});
}
async build() {
await this.createBuildDir();
await this.saveEntrypoint();
const commands = await this.scanCommands();
const bundler = this.createBundler(this.getEntryPointPaths(commands), {watch: false});
return bundler.bundle();
}
async watch({onStart, onFinish, onError}) {
// Track errors to ensure they're emitted only once
const pastErrors = [];
const handleError = error => {
if (pastErrors.includes(error.stack)) {
return;
}
pastErrors.push(error.stack);
onError(error);
};
// There's a lot of callbacks here that are async functions, so to avoid
// using .catch() all the time this helper function will do that for us
// to ensure any errors in these callbacks are propagated via `onError`
const handleAsyncErrors = fn => {
return (...args) => {
fn(...args).catch(handleError);
};
};
await this.createBuildDir();
await this.saveEntrypoint();
let bundler;
const rebuild = handleAsyncErrors(async () => {
if (bundler) {
bundler.removeAllListeners('buildStart');
bundler.removeAllListeners('bundled');
bundler.removeAllListeners('buildError');
bundler.stop();
}
const commands = await this.scanCommands();
bundler = this.createBundler(this.getEntryPointPaths(commands), {watch: true});
bundler.on('buildStart', onStart);
bundler.on('bundled', handleAsyncErrors(async () => {
await this.scanCommands();
onFinish();
}));
bundler.on('buildError', handleError);
return bundler.bundle();
});
rebuild();
watch.createMonitor(this.commandsPath, {
ignoreDotFiles: true,
ignoreUnreadableDir: true
}, monitor => {
monitor.on('created', rebuild);
monitor.on('removed', rebuild);
});
}
}
module.exports = Pastel;