-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (43 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
var Plugin = require('broccoli-plugin');
var Path = require('path');
var FS = require('fs');
var Funnel = require('broccoli-funnel');
var mkdirp = require('mkdirp');
var mergeTrees = require('broccoli-merge-trees');
// Create a subclass ModifyAndMergeFiles derived from Plugin
ModifyAndMergeFiles.prototype = Object.create(Plugin.prototype);
ModifyAndMergeFiles.prototype.constructor = ModifyAndMergeFiles;
function ModifyAndMergeFiles(inputNode, options) {
if (!(this instanceof ModifyAndMergeFiles)) { return new ModifyAndMergeFiles(inputNode, options); }
if (!options || !options.modify) { throw new Error('No `modify` function given'); }
if (!options || !options.outputFile) { throw new Error('No `outputFile` given'); }
inputNode = Funnel(inputNode, options);
Plugin.call(this, [inputNode], options);
this.options = options;
}
ModifyAndMergeFiles.prototype.build = function() {
var inputPath = this.inputPaths[0];
var fullOutputPath = Path.join(this.outputPath, this.options.outputFile);
var targetDir = Path.dirname(fullOutputPath);
var fileList = FS.readdirSync(inputPath);
var files = fileList.map(function(fileName) {
return FS.readFileSync(Path.join(inputPath, fileName));
});
// Convert files
var outputFile = this.options.modify(files);
if (!FS.existsSync(targetDir)) {
mkdirp.sync(targetDir);
}
FS.writeFileSync(fullOutputPath, outputFile);
};
module.exports = {
name: 'ember-cli-modify-and-merge-files',
treeForApp: function() {
var modifyFiles = (this.app && this.app.options && this.app.options.modifyFiles) || [];
var trees = modifyFiles.map(fileOptions => {
return ModifyAndMergeFiles(fileOptions.inputPath || this.app.trees.app, fileOptions);
});
return mergeTrees(trees)
}
};