2015-04-26 03:31:53 -04:00
|
|
|
/// <reference path="../typings/node/node.d.ts" />
|
|
|
|
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
|
2015-09-22 15:03:39 -04:00
|
|
|
/// <reference path="./broccoli-writer.d.ts" />
|
|
|
|
|
2015-04-13 19:22:23 -04:00
|
|
|
import Writer = require('broccoli-writer');
|
|
|
|
import fs = require('fs');
|
|
|
|
import fsx = require('fs-extra');
|
|
|
|
var minimatch = require('minimatch');
|
|
|
|
var path = require('path');
|
|
|
|
var glob = require('glob');
|
|
|
|
|
|
|
|
export interface MultiCopyOptions {
|
|
|
|
/** The path of the file to copy. */
|
2015-05-27 14:52:35 -04:00
|
|
|
srcPath: string;
|
2015-04-13 19:22:23 -04:00
|
|
|
/** A list of glob patterns of folders to copy to, matched against the input tree. */
|
2015-05-27 14:52:35 -04:00
|
|
|
targetPatterns: string[];
|
2015-04-13 19:22:23 -04:00
|
|
|
/** List of glob patterns to *not* copy to, matched against the matches from `targetPatterns`. */
|
2015-05-27 14:52:35 -04:00
|
|
|
exclude?: string[];
|
2015-04-13 19:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A writer that copies an input file from an input path into (potentially many) output locations
|
|
|
|
* given by glob patterns, .
|
|
|
|
*/
|
|
|
|
export class MultiCopy extends Writer {
|
|
|
|
constructor(private inputTree, private options: MultiCopyOptions) { super(); }
|
|
|
|
|
|
|
|
write(readTree: (tree) => Promise<string>, destDir: string): Promise<any> {
|
2016-04-07 20:17:50 -04:00
|
|
|
return readTree(this.inputTree).then((inputPath: string) => {
|
|
|
|
var fileName = path.basename(this.options.srcPath);
|
|
|
|
var data = fs.readFileSync(path.join(inputPath, this.options.srcPath), 'utf-8');
|
2015-04-13 19:22:23 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
this.options.targetPatterns.forEach(pattern => {
|
|
|
|
var paths: string[] = glob.sync(pattern);
|
|
|
|
paths = paths.filter(p => fs.statSync(p).isDirectory());
|
|
|
|
if (this.options.exclude) {
|
|
|
|
paths = paths.filter(p => !this.options.exclude.some((excl) => minimatch(p, excl)));
|
|
|
|
}
|
|
|
|
paths.forEach(p => {
|
|
|
|
var folder = path.join(destDir, p);
|
|
|
|
fsx.mkdirsSync(folder);
|
|
|
|
var outputPath = path.join(folder, fileName);
|
|
|
|
fs.writeFileSync(outputPath, data);
|
2015-04-13 19:22:23 -04:00
|
|
|
});
|
2016-04-07 20:17:50 -04:00
|
|
|
});
|
|
|
|
});
|
2015-04-13 19:22:23 -04:00
|
|
|
}
|
|
|
|
}
|