Skip to content

Commit

Permalink
Log after subsequent compilations in --watch mode
Browse files Browse the repository at this point in the history
Co-authored-by: James Munson <jmunson@gmail.com>
  • Loading branch information
nicolo-ribaudo and jmunson committed Mar 6, 2020
1 parent 2057d2b commit 38a64ee
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions packages/babel-cli/src/babel/dir.js
@@ -1,6 +1,7 @@
// @flow

import defaults from "lodash/defaults";
import debounce from "lodash/debounce";
import { sync as makeDirSync } from "make-dir";
import slash from "slash";
import path from "path";
Expand Down Expand Up @@ -138,24 +139,34 @@ export default async function({
}
}

let compiledFiles = 0;
const logSuccess = debounce(
function() {
console.log(
`Successfully compiled ${compiledFiles} ${
compiledFiles !== 1 ? "files" : "file"
} with Babel.`,
);
compiledFiles = 0;
},
100,
{ trailing: true },
);

if (!cliOptions.skipInitialBuild) {
if (cliOptions.deleteDirOnStart) {
util.deleteDir(cliOptions.outDir);
}

makeDirSync(cliOptions.outDir);

let compiledFiles = 0;
for (const filename of cliOptions.filenames) {
compiledFiles += await handle(filename);
}

if (!cliOptions.quiet) {
console.log(
`Successfully compiled ${compiledFiles} ${
compiledFiles !== 1 ? "files" : "file"
} with Babel.`,
);
logSuccess();
logSuccess.flush();
}
}

Expand All @@ -172,16 +183,29 @@ export default async function({
},
});

// This, alongside with debounce, allows us to only log
// when we are sure that all the files have been compiled.
let processing = 0;

["add", "change"].forEach(function(type: string): void {
watcher.on(type, function(filename: string): void {
handleFile(
filename,
filename === filenameOrDir
? path.dirname(filenameOrDir)
: filenameOrDir,
).catch(err => {
watcher.on(type, async function(filename: string): void {
processing++;

try {
await handleFile(
filename,
filename === filenameOrDir
? path.dirname(filenameOrDir)
: filenameOrDir,
);

compiledFiles++;
} catch (err) {
console.error(err);
});
}

processing--;
if (processing === 0 && !cliOptions.quiet) logSuccess();
});
});
});
Expand Down

0 comments on commit 38a64ee

Please sign in to comment.