Skip to content

Commit a912235

Browse files
authored
Build only changed files with "-c" flag (#25)
With "-c" flag, build only modified files, i.e. those that are newer than the output.
1 parent 0508a1a commit a912235

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ function collectDiagnostics(program: ts.Program) {
281281
});
282282
}
283283

284+
function needsUpdate(srcPath: string, outPath: string): boolean {
285+
if (!fs.existsSync(outPath)) {
286+
return true;
287+
}
288+
289+
const lastBuildTime = fs.statSync(outPath).mtime;
290+
const lastCodeTime = fs.statSync(srcPath).mtime;
291+
292+
return lastBuildTime < lastCodeTime;
293+
}
294+
284295
/**
285296
* Main entry point when used from the command line.
286297
*/
@@ -295,10 +306,12 @@ export function main() {
295306
.option("-s, --suffix <suffix>", `Suffix to append to generated files (default ${defaultSuffix})`, defaultSuffix)
296307
.option("-o, --outDir <path>", `Directory for output files; same as source file if omitted`)
297308
.option("-v, --verbose", "Produce verbose output")
309+
.option("-c, --changed-only", "Skip the build if the output file exists with a newer timestamp")
298310
.parse(process.argv);
299311

300312
const files: string[] = commander.args;
301313
const verbose: boolean = commander.verbose;
314+
const changedOnly: boolean = commander.changedOnly;
302315
const suffix: string = commander.suffix;
303316
const outDir: string|undefined = commander.outDir;
304317
const options: ICompilerOptions = {
@@ -319,6 +332,14 @@ export function main() {
319332
const ext = path.extname(filePath);
320333
const dir = outDir || path.dirname(filePath);
321334
const outPath = path.join(dir, path.basename(filePath, ext) + suffix + (options.format === "ts" ? ".ts" : ".js"));
335+
336+
if (changedOnly && !needsUpdate(filePath, outPath)) {
337+
if (verbose) {
338+
console.log(`Skipping ${filePath} because ${outPath} is newer`);
339+
}
340+
continue;
341+
}
342+
322343
if (verbose) {
323344
console.log(`Compiling ${filePath} -> ${outPath}`);
324345
}

0 commit comments

Comments
 (0)