2015-06-09 01:08:13 -04:00
|
|
|
import fs = require('fs');
|
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
|
|
|
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
|
2015-05-31 20:24:21 -04:00
|
|
|
var symlinkOrCopy = require('symlink-or-copy').sync;
|
|
|
|
|
|
|
|
var isWindows = process.platform === 'win32';
|
2015-06-09 01:08:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts each changed file and replaces its contents with
|
|
|
|
* the associated changes.
|
|
|
|
*/
|
|
|
|
export class DiffingFlatten implements DiffingBroccoliPlugin {
|
|
|
|
constructor(private inputPath, private cachePath, private options) {}
|
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
|
2015-06-09 01:08:13 -04:00
|
|
|
rebuild(treeDiff: DiffResult) {
|
2015-05-31 20:24:21 -04:00
|
|
|
let pathsToUpdate = treeDiff.addedPaths;
|
|
|
|
|
|
|
|
// since we need to run on Windows as well we can't rely on symlinks being available,
|
|
|
|
// which means that we need to respond to both added and changed paths
|
|
|
|
if (isWindows) {
|
|
|
|
pathsToUpdate = pathsToUpdate.concat(treeDiff.changedPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
pathsToUpdate.forEach((changedFilePath) => {
|
2015-06-09 01:08:13 -04:00
|
|
|
var sourceFilePath = path.join(this.inputPath, changedFilePath);
|
|
|
|
var destFilePath = path.join(this.cachePath, path.basename(changedFilePath));
|
|
|
|
var destDirPath = path.dirname(destFilePath);
|
|
|
|
|
|
|
|
if (!fs.existsSync(destDirPath)) {
|
|
|
|
fse.mkdirpSync(destDirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(destFilePath)) {
|
2015-05-31 20:24:21 -04:00
|
|
|
symlinkOrCopy(sourceFilePath, destFilePath);
|
|
|
|
} else {
|
2016-04-07 20:17:50 -04:00
|
|
|
throw new Error(
|
|
|
|
`Duplicate file '${path.basename(changedFilePath)}' ` +
|
|
|
|
`found in path '${changedFilePath}'`);
|
2015-06-09 01:08:13 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
treeDiff.removedPaths.forEach((removedFilePath) => {
|
|
|
|
var destFilePath = path.join(this.cachePath, path.basename(removedFilePath));
|
|
|
|
fs.unlinkSync(destFilePath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default wrapDiffingPlugin(DiffingFlatten);
|