milkee 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/main.js +104 -19
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  (function() {
3
- var CONFIG_FILE, CONFIG_PATH, CWD, argv, checkCoffee, compile, consola, exec, executePlugins, fs, getCompiledFiles, hideBin, path, pkg, runPlugins, setup, spawn, yargs;
3
+ var CONFIG_FILE, CONFIG_PATH, CWD, argv, checkCoffee, compile, consola, crypto, exec, executePlugins, fs, getCompiledFiles, hideBin, path, pkg, runPlugins, setup, spawn, yargs;
4
4
 
5
5
  yargs = require('yargs');
6
6
 
@@ -14,6 +14,8 @@
14
14
 
15
15
  ({exec, spawn} = require('child_process'));
16
16
 
17
+ crypto = require('crypto');
18
+
17
19
  pkg = require('../package.json');
18
20
 
19
21
  CWD = process.cwd();
@@ -156,7 +158,7 @@
156
158
  };
157
159
 
158
160
  compile = async function() {
159
- var compilerProcess, config, debounceTimeout, enabledOptions, enabledOptionsList, error, execCommand, execCommandParts, execOtherOptionStrings, i, item, itemPath, items, lastError, len, milkee, milkeeOptions, options, spawnArgs, stat, summary, targetDir, toContinue;
161
+ var backupFiles, backupName, backupPath, clearBackups, compilerProcess, config, debounceTimeout, dirName, enabledOptions, enabledOptionsList, error, execCommand, execCommandParts, execOtherOptionStrings, fileName, hash, i, item, items, lastError, len, milkee, milkeeOptions, options, originalPath, restoreBackups, spawnArgs, stat, summary, targetDir, toContinue;
160
162
  checkCoffee();
161
163
  if (!fs.existsSync(CONFIG_PATH)) {
162
164
  consola.error(`\`${CONFIG_FILE}\` not found in this directory: ${CWD}`);
@@ -260,28 +262,98 @@
260
262
  }
261
263
  }
262
264
  delete options.join;
265
+ backupFiles = [];
266
+ restoreBackups = function() {
267
+ var backup, e, i, len;
268
+ if (backupFiles.length > 0) {
269
+ consola.info("Restoring previous files...");
270
+ for (i = 0, len = backupFiles.length; i < len; i++) {
271
+ backup = backupFiles[i];
272
+ try {
273
+ if (fs.existsSync(backup.original)) {
274
+ fs.rmSync(backup.original, {
275
+ force: true
276
+ });
277
+ }
278
+ if (fs.existsSync(backup.backup)) {
279
+ fs.renameSync(backup.backup, backup.original);
280
+ }
281
+ } catch (error1) {
282
+ e = error1;
283
+ consola.warn(`Failed to restore ${backup.original}`);
284
+ }
285
+ }
286
+ return consola.success("Restored!");
287
+ }
288
+ };
289
+ clearBackups = function() {
290
+ var backup, e, i, len, results;
291
+ if (backupFiles.length > 0) {
292
+ consola.trace("Cleaning up backups...");
293
+ results = [];
294
+ for (i = 0, len = backupFiles.length; i < len; i++) {
295
+ backup = backupFiles[i];
296
+ try {
297
+ if (fs.existsSync(backup.backup)) {
298
+ results.push(fs.rmSync(backup.backup, {
299
+ force: true
300
+ }));
301
+ } else {
302
+ results.push(void 0);
303
+ }
304
+ } catch (error1) {
305
+ e = error1;
306
+ results.push(null);
307
+ }
308
+ }
309
+ return results;
310
+ }
311
+ };
263
312
  if (milkeeOptions.refresh) {
264
313
  targetDir = path.join(CWD, config.output);
265
314
  if (fs.existsSync(targetDir)) {
266
315
  stat = fs.statSync(targetDir);
267
- if (stat.isDirectory()) {
268
- consola.info("Executing: Refresh");
269
- items = fs.readdirSync(targetDir);
270
- for (i = 0, len = items.length; i < len; i++) {
271
- item = items[i];
272
- itemPath = path.join(targetDir, item);
273
- fs.rmSync(itemPath, {
274
- recursive: true,
275
- force: true
316
+ hash = crypto.randomBytes(4).toString('hex');
317
+ try {
318
+ if (stat.isDirectory()) {
319
+ consola.info("Executing: Refresh");
320
+ items = fs.readdirSync(targetDir);
321
+ for (i = 0, len = items.length; i < len; i++) {
322
+ item = items[i];
323
+ originalPath = path.join(targetDir, item);
324
+ backupName = `${hash}.${item}.bak`;
325
+ backupPath = path.join(targetDir, backupName);
326
+ fs.renameSync(originalPath, backupPath);
327
+ backupFiles.push({
328
+ original: originalPath,
329
+ backup: backupPath
330
+ });
331
+ }
332
+ // itemPath = path.join targetDir, item
333
+ // fs.rmSync itemPath, recursive: true, force: true
334
+ consola.success(`Existing: files backed up with hash \`${hash}\``);
335
+ } else {
336
+ // consola.success "Refreshed!"
337
+ consola.info("Executing: Refresh (Single File)");
338
+ originalPath = targetDir;
339
+ fileName = path.basename(originalPath);
340
+ dirName = path.dirname(originalPath);
341
+ backupName = `${hash}.${fileName}.bak`;
342
+ backupPath = path.join(dirName, backupName);
343
+ fs.renameSync(originalPath, backupPath);
344
+ backupFiles.push({
345
+ original: originalPath,
346
+ backup: backupPath
276
347
  });
348
+ consola.success(`Existing file backed up as \`${backupName}\``);
277
349
  }
278
- consola.success("Refreshed!");
279
- } else {
280
- consola.info("Executing: Refresh (Single File)");
281
- fs.rmSync(targetDir, {
282
- force: true
283
- });
284
- consola.success("Refreshed!");
350
+ } catch (error1) {
351
+ // fs.rmSync targetDir, force: true
352
+ // consola.success "Refreshed!"
353
+ error = error1;
354
+ consola.error("Failed to create backups during refresh:", error);
355
+ restoreBackups();
356
+ process.exit(1);
285
357
  }
286
358
  } else {
287
359
  consola.info("Refresh skipped.");
@@ -290,6 +362,10 @@
290
362
  if (options.watch) {
291
363
  consola.start(`Watching for changes in \`${config.entry}\`...`);
292
364
  consola.info(`Executing: coffee ${spawnArgs.join(' ')}`);
365
+ if (milkeeOptions.refresh) {
366
+ consola.warn("Refresh backup is disabled in watch mode (backups are cleared immediately).");
367
+ clearBackups();
368
+ }
293
369
  compilerProcess = spawn('coffee', spawnArgs, {
294
370
  shell: true
295
371
  });
@@ -309,6 +385,7 @@
309
385
  if (stdoutMsg) {
310
386
  consola.log(stdoutMsg);
311
387
  }
388
+ debounceTimeout = null;
312
389
  lastError = null;
313
390
  if (debounceTimeout) {
314
391
  clearTimeout(debounceTimeout);
@@ -339,10 +416,18 @@
339
416
  if (stderr) {
340
417
  consola.error(stderr.toString().trim());
341
418
  }
419
+ if (milkeeOptions.refresh) {
420
+ restoreBackups();
421
+ }
342
422
  process.exit(1);
343
423
  return;
424
+ setTimeout(function() {
425
+ if (milkeeOptions.refresh) {
426
+ clearBackups();
427
+ }
428
+ return consola.success('Compilation completed successfully!');
429
+ }, 500);
344
430
  }
345
- consola.success('Compilation completed successfully!');
346
431
  if (stdout) {
347
432
  process.stdout.write(stdout);
348
433
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "milkee",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "A simple CoffeeScript build tool with coffee.config.cjs",
5
5
  "main": "dist/main.js",
6
6
  "bin": {