-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.92 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
58
59
60
'use strict';
const fs = require('fs');
const path = require('path');
const Transform = require('stream').Transform;
const Material = require('fuller-material-file');
const prologue = fs.readFileSync(path.join(__dirname, 'js', 'prologue.js')).toString();
const epilogue = fs.readFileSync(path.join(__dirname, 'js', 'epilogue.js')).toString();
const prologueManualInit = fs.readFileSync(path.join(__dirname, 'js', 'prologue_manual_init.js')).toString();
const epilogueManualInit = fs.readFileSync(path.join(__dirname, 'js', 'epilogue_manual_init.js')).toString();
const itemPrologue = fs.readFileSync(path.join(__dirname, 'js', 'item_prologue.js')).toString();
const itemEpilogue = fs.readFileSync(path.join(__dirname, 'js', 'item_epilogue.js')).toString();
const CommonJS = function(fuller, options) {
fuller.bind(this);
this.src = options.src;
this.dst = options.dst;
this.external = options.external;
this.prologue = options.prologue || options.external ? prologueManualInit : prologue;
this.epilogue = options.epilogue || options.external ? epilogueManualInit : epilogue;
this.itemPrologue = options.itemPrologue || itemPrologue;
this.itemEpilogue = options.itemEpilogue || itemEpilogue;
};
CommonJS.prototype.build = function(src, dst) {
const newMat = new Material({
id: dst,
path: path.join(this.dst, dst)
}).error(err => this.error(err));
let newContent = this.prologue;
if (this.external) {
newContent = newContent.replace('<^FILENAME^>', path.basename(dst));
}
const next = new Transform({
objectMode: true,
transform: (mat, enc, cb) => {
mat.getContent(content => {
newContent += this.itemPrologue;
newContent += content.toString();
newContent += this.itemEpilogue;
cb();
});
},
flush: done => {
newContent += this.epilogue;
next.push(newMat.setContent(newContent));
done();
}
});
return next;
};
module.exports = CommonJS;