forked from ef4/broccoli-sourcemap-concat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-concat.js
117 lines (94 loc) · 2.52 KB
/
simple-concat.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var CachingWriter = require('broccoli-caching-writer');
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var helpers = require('broccoli-kitchen-sink-helpers');
function Combined() {
this._internal = '';
}
Combined.prototype.append = function(string) {
this._internal += string;
};
Combined.prototype.valueOf = function() {
return this._internal;
};
Combined.prototype.toString = function() {
return this._internal;
};
module.exports = CachingWriter.extend({
enforceSingleInputTree: true,
init: function() {
this._super.apply(this, arguments);
if (!this.separator) {
this.separator = '\n';
}
if (!this.outputFile) {
throw new Error('outputFile is required');
}
},
description: 'SimpleConcat',
updateCache: function(inDir, outDir) {
var combined = new Combined();
var firstSection = true;
var separator = this.separator;
function beginSection() {
if (firstSection) {
firstSection = false;
} else {
combined.append(separator);
}
}
if (this.header) {
beginSection();
combined.append(this.header);
}
if (this.headerFiles) {
this.headerFiles.forEach(function(file) {
beginSection();
combined.append(fs.readFileSync(path.join(inDir, file), 'UTF-8'));
});
}
try {
this._addFiles(combined, inDir, beginSection);
} catch(error) {
// multiGlob is obtuse.
if (!error.message.match('did not match any files') || !this.allowNone) {
throw error;
}
}
if (this.footer) {
beginSection();
combined.append(this.footer);
}
if (this.footerFiles) {
this.footerFiles.forEach(function(file) {
beginSection();
combined.append(fs.readFileSync(path.join(inDir, file), 'UTF-8'));
}.bind(this));
}
var filePath = path.join(outDir, this.outputFile);
mkdirp.sync(path.dirname(filePath));
if (firstSection) {
combined.append('');
}
fs.writeFileSync(filePath, combined);
},
_addFiles: function(combined, inDir, beginSection) {
helpers.multiGlob(this.inputFiles, {
cwd: inDir,
root: inDir,
nomount: false
}).forEach(function(file) {
var filePath = path.join(inDir, file);
var stat;
try {
stat = fs.statSync(filePath);
} catch(err) {}
if (stat && !stat.isDirectory()) {
beginSection();
combined.append(fs.readFileSync(filePath, 'UTF-8'));
}
});
return combined;
}
});