2015-04-24 13:00:38 -04:00
|
|
|
/// <reference path="../typings/node/node.d.ts" />
|
|
|
|
|
|
|
|
import fs = require('fs');
|
|
|
|
import path = require('path');
|
|
|
|
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function tryStatSync(path: string) {
|
2015-05-18 13:39:28 -04:00
|
|
|
try {
|
|
|
|
return fs.statSync(path);
|
|
|
|
} catch (e) {
|
2016-04-12 12:40:37 -04:00
|
|
|
if (e.code === "ENOENT") return null;
|
2015-05-18 13:39:28 -04:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
export class TreeDiffer {
|
2015-04-24 13:00:38 -04:00
|
|
|
private fingerprints: {[key: string]: string} = Object.create(null);
|
|
|
|
private nextFingerprints: {[key: string]: string} = Object.create(null);
|
|
|
|
private rootDirName: string;
|
2015-05-06 19:24:10 -04:00
|
|
|
private include: RegExp = null;
|
|
|
|
private exclude: RegExp = null;
|
2015-04-24 13:00:38 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
constructor(private label: string, private rootPath: string, includeExtensions?: string[],
|
|
|
|
excludeExtensions?: string[]) {
|
2015-05-06 19:24:10 -04:00
|
|
|
this.rootDirName = path.basename(rootPath);
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
let buildRegexp = (arr: string[]) => new RegExp(`(${arr.reduce(combine, "")})$`, "i");
|
2015-05-06 19:24:10 -04:00
|
|
|
|
|
|
|
this.include = (includeExtensions || []).length ? buildRegexp(includeExtensions) : null;
|
|
|
|
this.exclude = (excludeExtensions || []).length ? buildRegexp(excludeExtensions) : null;
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function combine(prev: string, curr: string) {
|
2016-04-12 12:40:37 -04:00
|
|
|
if (curr.charAt(0) !== ".") {
|
2015-05-27 21:26:28 -04:00
|
|
|
throw new Error(`Extension must begin with '.'. Was: '${curr}'`);
|
|
|
|
}
|
2015-05-07 18:15:15 -04:00
|
|
|
let kSpecialRegexpChars = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
|
|
|
|
curr = '(' + curr.replace(kSpecialRegexpChars, '\\$&') + ')';
|
2015-05-06 19:24:10 -04:00
|
|
|
return prev ? (prev + '|' + curr) : curr;
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 13:00:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
public diffTree(): DiffResult {
|
2015-05-27 18:57:19 -04:00
|
|
|
let result = new DirtyCheckingDiffResult(this.label, this.rootDirName);
|
2015-04-24 13:00:38 -04:00
|
|
|
this.dirtyCheckPath(this.rootPath, result);
|
|
|
|
this.detectDeletionsAndUpdateFingerprints(result);
|
|
|
|
result.endTime = Date.now();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
private dirtyCheckPath(rootDir: string, result: DirtyCheckingDiffResult) {
|
2015-04-24 13:00:38 -04:00
|
|
|
fs.readdirSync(rootDir).forEach((segment) => {
|
|
|
|
let absolutePath = path.join(rootDir, segment);
|
2015-05-18 13:39:28 -04:00
|
|
|
let pathStat = fs.lstatSync(absolutePath);
|
|
|
|
if (pathStat.isSymbolicLink()) {
|
|
|
|
pathStat = tryStatSync(absolutePath);
|
|
|
|
if (pathStat === null) return;
|
|
|
|
}
|
2015-04-24 13:00:38 -04:00
|
|
|
|
|
|
|
if (pathStat.isDirectory()) {
|
|
|
|
result.directoriesChecked++;
|
|
|
|
this.dirtyCheckPath(absolutePath, result);
|
|
|
|
} else {
|
2015-05-06 19:24:10 -04:00
|
|
|
if (!(this.include && !absolutePath.match(this.include)) &&
|
|
|
|
!(this.exclude && absolutePath.match(this.exclude))) {
|
|
|
|
result.filesChecked++;
|
2015-05-31 20:24:21 -04:00
|
|
|
let relativeFilePath = path.relative(this.rootPath, absolutePath);
|
|
|
|
|
|
|
|
switch (this.isFileDirty(absolutePath, pathStat)) {
|
2015-08-26 16:40:12 -04:00
|
|
|
case FileStatus.Added:
|
2015-05-31 20:24:21 -04:00
|
|
|
result.addedPaths.push(relativeFilePath);
|
|
|
|
break;
|
2015-08-26 16:40:12 -04:00
|
|
|
case FileStatus.Changed:
|
2015-05-31 20:24:21 -04:00
|
|
|
result.changedPaths.push(relativeFilePath);
|
2015-05-06 19:24:10 -04:00
|
|
|
}
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
private isFileDirty(path: string, stat: fs.Stats): FileStatus {
|
2015-04-24 13:00:38 -04:00
|
|
|
let oldFingerprint = this.fingerprints[path];
|
|
|
|
let newFingerprint = `${stat.mtime.getTime()} # ${stat.size}`;
|
|
|
|
|
|
|
|
this.nextFingerprints[path] = newFingerprint;
|
|
|
|
|
|
|
|
if (oldFingerprint) {
|
|
|
|
this.fingerprints[path] = null;
|
|
|
|
|
|
|
|
if (oldFingerprint === newFingerprint) {
|
|
|
|
// nothing changed
|
2015-08-26 16:40:12 -04:00
|
|
|
return FileStatus.Unchanged;
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
2015-05-31 20:24:21 -04:00
|
|
|
|
2015-08-26 16:40:12 -04:00
|
|
|
return FileStatus.Changed;
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 16:40:12 -04:00
|
|
|
return FileStatus.Added;
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private detectDeletionsAndUpdateFingerprints(result: DiffResult) {
|
|
|
|
for (let absolutePath in this.fingerprints) {
|
2015-05-06 19:24:10 -04:00
|
|
|
if (!(this.include && !absolutePath.match(this.include)) &&
|
|
|
|
!(this.exclude && absolutePath.match(this.exclude))) {
|
|
|
|
if (this.fingerprints[absolutePath] !== null) {
|
|
|
|
let relativePath = path.relative(this.rootPath, absolutePath);
|
|
|
|
result.removedPaths.push(relativePath);
|
|
|
|
}
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fingerprints = this.nextFingerprints;
|
|
|
|
this.nextFingerprints = Object.create(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-25 19:59:35 -04:00
|
|
|
export class DiffResult {
|
|
|
|
public addedPaths: string[] = [];
|
|
|
|
public changedPaths: string[] = [];
|
|
|
|
public removedPaths: string[] = [];
|
|
|
|
|
|
|
|
constructor(public label: string = '') {}
|
2015-06-12 13:12:10 -04:00
|
|
|
|
2015-06-25 19:59:35 -04:00
|
|
|
log(verbose: boolean): void {}
|
2015-06-23 16:21:17 -04:00
|
|
|
|
2015-06-25 19:59:35 -04:00
|
|
|
toString(): string {
|
|
|
|
// TODO(@caitp): more meaningful logging
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DirtyCheckingDiffResult extends DiffResult {
|
2015-04-24 13:00:38 -04:00
|
|
|
public filesChecked: number = 0;
|
|
|
|
public directoriesChecked: number = 0;
|
|
|
|
public startTime: number = Date.now();
|
|
|
|
public endTime: number = null;
|
|
|
|
|
2015-06-25 19:59:35 -04:00
|
|
|
constructor(label: string, public directoryName: string) { super(label); }
|
2015-04-24 13:00:38 -04:00
|
|
|
|
|
|
|
toString() {
|
2015-05-27 18:57:19 -04:00
|
|
|
return `${pad(this.label, 30)}, ${pad(this.endTime - this.startTime, 5)}ms, ` +
|
2016-04-12 12:40:37 -04:00
|
|
|
`${pad(this.addedPaths.length + this.changedPaths.length + this.removedPaths.length, 5)} changes ` +
|
|
|
|
`(files: ${pad(this.filesChecked, 5)}, dirs: ${pad(this.directoriesChecked, 4)})`;
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
log(verbose: boolean) {
|
2015-05-31 20:24:21 -04:00
|
|
|
let prefixedPaths = this.addedPaths.map(p => `+ ${p}`)
|
|
|
|
.concat(this.changedPaths.map(p => `* ${p}`))
|
|
|
|
.concat(this.removedPaths.map(p => `- ${p}`));
|
2016-04-12 12:40:37 -04:00
|
|
|
console.log(`Tree diff: ${this}` + ((verbose && prefixedPaths.length) ?
|
|
|
|
` [\n ${prefixedPaths.join('\n ')}\n]` :
|
|
|
|
''));
|
2015-04-24 13:00:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function pad(v: string | number, length: number) {
|
|
|
|
let value = '' + v;
|
2015-04-24 13:00:38 -04:00
|
|
|
let whitespaceLength = (value.length < length) ? length - value.length : 0;
|
|
|
|
whitespaceLength = whitespaceLength + 1;
|
|
|
|
return new Array(whitespaceLength).join(' ') + value;
|
|
|
|
}
|
2015-05-31 20:24:21 -04:00
|
|
|
|
|
|
|
|
2015-06-19 13:18:44 -04:00
|
|
|
enum FileStatus {
|
2015-08-26 16:40:12 -04:00
|
|
|
Added,
|
|
|
|
Unchanged,
|
|
|
|
Changed
|
2015-06-19 13:18:44 -04:00
|
|
|
}
|