|
| 1 | +import fs = require('fs'); |
| 2 | +import fse = require('fs-extra'); |
| 3 | +import path = require('path'); |
| 4 | +var symlinkOrCopySync = require('symlink-or-copy').sync; |
| 5 | +import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin'; |
| 6 | + |
| 7 | +function pathExists(filePath) { |
| 8 | + try { |
| 9 | + if (fs.statSync(filePath)) { |
| 10 | + return true; |
| 11 | + } |
| 12 | + } catch (e) { |
| 13 | + if (e.code !== "ENOENT") { |
| 14 | + throw e; |
| 15 | + } |
| 16 | + } |
| 17 | + return false; |
| 18 | +} |
| 19 | + |
| 20 | +function outputFileSync(sourcePath, destPath) { |
| 21 | + let dirname = path.dirname(destPath); |
| 22 | + fse.mkdirsSync(dirname, {fs: fs}); |
| 23 | + fse.removeSync(destPath); |
| 24 | + symlinkOrCopySync(sourcePath, destPath); |
| 25 | +} |
| 26 | + |
| 27 | +export class MergeTrees implements DiffingBroccoliPlugin { |
| 28 | + private mergedPaths: {[key: string]: number} = Object.create(null); |
| 29 | + |
| 30 | + constructor(public inputPaths: string[], public cachePath: string, public options) {} |
| 31 | + |
| 32 | + rebuild(treeDiffs: DiffResult[]) { |
| 33 | + treeDiffs.forEach((treeDiff: DiffResult, index) => { |
| 34 | + let inputPath = this.inputPaths[index]; |
| 35 | + let existsLater = (relativePath) => { |
| 36 | + for (let i = treeDiffs.length - 1; i > index; --i) { |
| 37 | + if (pathExists(path.join(this.inputPaths[i], relativePath))) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | + return false; |
| 42 | + }; |
| 43 | + let existsSooner = (relativePath) => { |
| 44 | + for (let i = index - 1; i >= 0; --i) { |
| 45 | + if (pathExists(path.join(this.inputPaths[i], relativePath))) { |
| 46 | + return i; |
| 47 | + } |
| 48 | + } |
| 49 | + return -1; |
| 50 | + }; |
| 51 | + treeDiff.changedPaths.forEach((changedPath) => { |
| 52 | + let inputTreeIndex = this.mergedPaths[changedPath]; |
| 53 | + if (inputTreeIndex !== index && !existsLater(changedPath)) { |
| 54 | + inputTreeIndex = this.mergedPaths[changedPath] = index; |
| 55 | + let sourcePath = path.join(inputPath, changedPath); |
| 56 | + let destPath = path.join(this.cachePath, changedPath); |
| 57 | + outputFileSync(sourcePath, destPath); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + treeDiff.removedPaths.forEach((removedPath) => { |
| 62 | + let inputTreeIndex = this.mergedPaths[removedPath]; |
| 63 | + |
| 64 | + // if inputTreeIndex !== index, this same file was handled during |
| 65 | + // changedPaths handling |
| 66 | + if (inputTreeIndex !== index) return; |
| 67 | + |
| 68 | + let destPath = path.join(this.cachePath, removedPath); |
| 69 | + fse.removeSync(destPath); |
| 70 | + let newInputTreeIndex = existsSooner(removedPath); |
| 71 | + |
| 72 | + // Update cached value (to either newInputTreeIndex value or undefined) |
| 73 | + this.mergedPaths[removedPath] = newInputTreeIndex; |
| 74 | + |
| 75 | + if (newInputTreeIndex >= 0) { |
| 76 | + // Copy the file from the newInputTreeIndex inputPath if necessary. |
| 77 | + let newInputPath = this.inputPaths[newInputTreeIndex]; |
| 78 | + let sourcePath = path.join(newInputPath, removedPath); |
| 79 | + outputFileSync(sourcePath, destPath); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export default wrapDiffingPlugin(MergeTrees); |
0 commit comments