|
| 1 | +import fs = require('fs'); |
| 2 | +import fse = require('fs-extra'); |
| 3 | +import path = require('path'); |
| 4 | +var _ = require('lodash'); |
| 5 | +import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin'; |
| 6 | + |
| 7 | +interface LodashRendererOptions { |
| 8 | + encoding?: string; |
| 9 | + context?: any; |
| 10 | + // files option unsupported --- use Funnel on inputTree instead. |
| 11 | + files?: string[]; |
| 12 | +} |
| 13 | + |
| 14 | +const kDefaultOptions: LodashRendererOptions = {encoding: 'utf-8', context: {}, files: []}; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Intercepts each changed file and replaces its contents with |
| 19 | + * the associated changes. |
| 20 | + */ |
| 21 | +export class LodashRenderer implements DiffingBroccoliPlugin { |
| 22 | + constructor(private inputPath, private cachePath, |
| 23 | + private options: LodashRendererOptions = kDefaultOptions) {} |
| 24 | + |
| 25 | + rebuild(treeDiff: DiffResult) { |
| 26 | + let{encoding = 'utf-8', context = {}} = this.options; |
| 27 | + let processFile = (relativePath) => { |
| 28 | + let sourceFilePath = path.join(this.inputPath, relativePath); |
| 29 | + let destFilePath = path.join(this.cachePath, relativePath); |
| 30 | + let content = fs.readFileSync(sourceFilePath, {encoding}); |
| 31 | + let transformedContent = _.template(content)(context); |
| 32 | + fse.outputFileSync(destFilePath, transformedContent); |
| 33 | + }; |
| 34 | + |
| 35 | + let removeFile = (relativePath) => { |
| 36 | + let destFilePath = path.join(this.cachePath, relativePath); |
| 37 | + fs.unlinkSync(destFilePath); |
| 38 | + }; |
| 39 | + |
| 40 | + treeDiff.changedPaths.forEach(processFile); |
| 41 | + treeDiff.removedPaths.forEach(removeFile); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export default wrapDiffingPlugin(LodashRenderer); |
0 commit comments