-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-and-watch.js
executable file
·324 lines (262 loc) · 8.32 KB
/
compile-and-watch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const { exec } = require('child_process');
const chokidar = require('chokidar');
const fs = require('fs-extra');
const argv = require('minimist')(process.argv.slice(2));
const watchAndBuildAnimations = require('./build-animations');
const {
port = 6001,
env = 'development',
compileFile = 'build.hxml'
} = argv;
require('./dev-server/main');
const compileLogger = require('debug')('watcher.compile');
const fileClearedStates = new Map();
const logToDisk = async (file, label, result, logFn) => {
try {
const formattedLabel = (
/err/.test(label)
? `=== ${label} ===`
: label
).toUpperCase() + ` -- [${env}]`;
const formattedResult = `\n${formattedLabel}\n${new Date()}\n${result}\n`;
const logFile = `./.logs/${file}`;
logFn(formattedResult);
await fs.ensureDir('./.logs');
if (!fileClearedStates.has(logFile)) {
fileClearedStates.set(logFile, true);
// truncate file for new sessions to
// prevent log files from growing too large
await fs.writeFile(logFile, '');
}
await fs.appendFile(
logFile,
formattedResult);
} catch (err) {
console.dir('=== log to disk error ===', err);
}
}
const compile = (buildFile) => {
logToDisk('build.txt', `compiling... ${buildFile}`, '', compileLogger);
const flags = env === 'development'
? '-D debugMode'
: '-D production';
const cmd = `haxe ${flags} ${buildFile} --connect ${port}`;
exec(cmd, (err, stdout, stderr) => {
if (err) {
logToDisk(
'build.txt', 'compile error', err, compileLogger);
} else if (stderr) {
logToDisk(
'build.txt', 'compile stderr', stderr, compileLogger);
} else {
logToDisk(
'build.txt', 'compile success', '', compileLogger);
}
});
}
const asepriteExportDir = './src/art/aseprite_exports';
const filenameWithoutExtension = (filePath) =>
filePath.split('/').slice(-1)[0].replace(/\.[^]+/g, '');
const makeExportDir = (filename) =>
`${asepriteExportDir}/${filenameWithoutExtension(filename)}`
const cleanupAsepriteExport = async (exportDir) => {
return fs.remove(exportDir);
}
const asepriteLogger = require('debug')('watcher.aseprite');
const asepriteExport = async (
fileEvent, exportDir, asepriteArgs) => {
try {
if (fileEvent == 'unlink') {
// only trigger the directory cleanup
return;
}
await fs.ensureDir(exportDir);
const asepriteExecutable = '\'/mnt/c/Program Files (x86)/Steam/steamapps/common/Aseprite/Aseprite.exe\'';
const cmd = `${asepriteExecutable} ${asepriteArgs}`;
exec(cmd, (err, stdout, stderr) => {
if (err) {
logToDisk(
'build.txt', 'aseprite error', err, asepriteLogger);
} else if (stderr) {
logToDisk(
'build.txt', 'aseprite stderr', stderr, asepriteLogger);
} else {
logToDisk(
'build.txt', 'aseprite export success', '', asepriteLogger);
}
});
} catch (err) {
logToDisk(
'build.txt', 'aseprite error', err, asepriteLogger);
}
}
exec(`haxe --wait ${port}`, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log('server ready')
}
});
let pending = null;
const rebuild = (eventType, filename, options) => {
const { delay } = options;
if (options.verbose) {
console.log(`${ filename } changed`)
}
clearTimeout(pending)
pending = setTimeout(() => {
compile(compileFile)
}, delay)
}
const startCompileWatcher = (options = {}) => {
chokidar.watch([
'./Main.hx',
'./src/**/*.hx',
'./src/res/**/*.*',
], {
// this prevents locking up the file system for other windows applications
usePolling: true,
}).on('all', (event, path) => {
rebuild(event, path, options);
});
}
// [Aseprite](https://www.aseprite.org/)
const startAsepriteWatcher = (options) => {
const debounceStates = new Map();
const handleAesepriteExport = (eventType, path) => {
// filenames with the pattern {my_file}_animation.aseprite
const isAnimationFile = options.animationFilePattern.test(filenameWithoutExtension(path));
if (isAnimationFile) {
return;
}
if (options.verbose) {
console.log(`[aseprite watch][${eventType}] \`${path}\``);
}
const previousPendingBuild = debounceStates.get(path);
const lightSourceLayer = 'light_source';
const shadowLayer = 'shadow';
const collisionHitboxLayer = 'collision_hitbox';
const exportDir = makeExportDir(path);
clearTimeout(previousPendingBuild);
const exportBaseSprites = () => {
const filePath = '{slice}.png';
console.log('[filename]', path);
const exportFullPath = `${exportDir}/${filePath}`;
const asepriteArgs = [
`-b ${path}`,
`--ignore-layer ${lightSourceLayer}`,
`--ignore-layer ${shadowLayer}`,
`--ignore-layer ${collisionHitboxLayer}`,
`--save-as ${exportFullPath}`
].join(' ');
asepriteExport(eventType, exportDir, asepriteArgs);
}
const exportLayer = (layer) => {
const filePath = `{slice}--${layer}.png`;
console.log('[filename]', path);
const exportFullPath = `${exportDir}/${filePath}`;
const asepriteArgs = [
`-b ${path}`,
`--layer "${layer}"`,
`--all-layers`,
`--ignore-empty`,
`--save-as ${exportFullPath}`
].join(' ');
asepriteExport(eventType, exportDir, asepriteArgs);
}
const execBuild = async () => {
console.log(`[aseprite static] cleaning export directory \`${exportDir}\`...`);
await cleanupAsepriteExport(exportDir);
console.log(`[aseprite static], removed export directory \`${exportDir}\``);
exportBaseSprites();
exportLayer(lightSourceLayer);
exportLayer(shadowLayer);
exportLayer(collisionHitboxLayer);
};
const newPendingBuild = setTimeout(execBuild, 300);
debounceStates.set(path, newPendingBuild);
}
chokidar.watch('./src/art/*.aseprite', {
usePolling: true,
}).on('all', handleAesepriteExport);
}
const startTexturePackerWatcher = (options = {}) => {
const tpLogger = require('debug')('watcher.texturePacker');
let pending = 0;
const {
destination,
sourceFile
} = options;
const handleTexturePackerExport = (eventType) => {
clearTimeout(pending)
pending = setTimeout(() => {
const tpExecutable = '\'/mnt/c/Program Files/CodeAndWeb/TexturePacker/bin/TexturePacker.exe\'';
const cmd = `${tpExecutable} --sheet ${destination} ${sourceFile}`;
const name = 'texturepacker'
exec(cmd, (err, stdout, stderr) => {
if (err) {
logToDisk(
`build.txt`, `${name} error`, err, tpLogger);
} else if (stderr) {
logToDisk(
`build.txt`, `${name} stderr`, stderr, tpLogger);
} else {
const msg = `\`${sourceFile}\` to \`${destination}\``;
logToDisk(
`build.txt`, `${name} success`, msg, tpLogger);
}
});
}, 500);
}
chokidar.watch([
'./src/art/*.tps',
asepriteExportDir,
], {
usePolling: true,
}).on('all', handleTexturePackerExport);
}
function startReplWatcher() {
chokidar.watch('./Repl.hx').on('all', () => {
exec(`haxe repl-build.hxml --connect ${port}`, (err, stdout, stderr) => {
if (err) {
console.log('\n==== repl build error ===='.toUpperCase());
console.error(err);
return;
}
if (stderr) {
console.log('\n==== repl build stderror ===='.toUpperCase());
console.error(stderr);
return;
}
console.log('\n==== repl build success ===='.toUpperCase());
console.log(stdout);
delete require.cache[require.resolve('./temp/repl-haxe.js')];
try {
require('./temp/repl-haxe.js');
} catch (err) {
console.error('repl build error', err);
}
});
});
}
startReplWatcher({});
startCompileWatcher({
delay: 1000
});
startAsepriteWatcher({
// verbose: true,
animationFilePattern: /_animation$/
});
watchAndBuildAnimations(
'./src/art/*_animation.aseprite'
);
startTexturePackerWatcher({
sourceFile: './src/art/sprite_sheet.tps',
destination: './src/res/sprite_sheet.png'
});
startTexturePackerWatcher({
sourceFile: './src/art/sprite_sheet_ui_cursor.tps',
destination: './src/res/sprite_sheet_ui_cursor.png'
});