1 | var staticModule = require('static-module');
|
2 | var quote = require('quote-stream');
|
3 | var through = require('through2');
|
4 | var fs = require('fs');
|
5 | var path = require('path');
|
6 | var resolve = require('resolve');
|
7 |
|
8 | module.exports = function (file, opts) {
|
9 | if (/\.json$/.test(file)) return through();
|
10 |
|
11 | function resolver (p) {
|
12 | return resolve.sync(p, { basedir: path.dirname(file) });
|
13 | }
|
14 | var vars = {
|
15 | __filename: file,
|
16 | __dirname: path.dirname(file),
|
17 | require: { resolve: resolver }
|
18 | };
|
19 | if (!opts) opts = {};
|
20 | if (opts.vars) Object.keys(opts.vars).forEach(function (key) {
|
21 | vars[key] = opts.vars[key];
|
22 | });
|
23 |
|
24 | var sm = staticModule(
|
25 | {
|
26 | fs: {
|
27 | readFileSync: readFileSync,
|
28 | readFile: readFile,
|
29 | readdirSync: readdirSync,
|
30 | readdir: readdir
|
31 | }
|
32 | },
|
33 | {
|
34 | vars: vars,
|
35 | varModules: { path: path },
|
36 | parserOpts: opts && opts.parserOpts,
|
37 | sourceMap: opts && (opts.sourceMap || opts._flags && opts._flags.debug),
|
38 | inputFilename: file
|
39 | }
|
40 | );
|
41 | return sm;
|
42 |
|
43 | function readFile (file, enc, cb) {
|
44 | if (typeof enc === 'function') {
|
45 | cb = enc;
|
46 | enc = null;
|
47 | }
|
48 | if (enc && typeof enc === 'object' && enc.encoding) {
|
49 | enc = enc.encoding;
|
50 | }
|
51 | var isBuffer = false;
|
52 | if (enc === null || enc === undefined) {
|
53 | isBuffer = true;
|
54 | enc = 'base64';
|
55 | }
|
56 |
|
57 | var stream = through(write, end);
|
58 | stream.push('process.nextTick(function(){(' + cb + ')(null,');
|
59 | if (isBuffer) stream.push('Buffer(');
|
60 |
|
61 | var s = fs.createReadStream(file, { encoding: enc });
|
62 | s.on('error', function (err) { sm.emit('error', err) });
|
63 | return s.pipe(quote()).pipe(stream);
|
64 |
|
65 | function write (buf, enc, next) {
|
66 | this.push(buf);
|
67 | next();
|
68 | }
|
69 | function end (next) {
|
70 | if (isBuffer) this.push(',"base64")');
|
71 | this.push(')})');
|
72 | this.push(null);
|
73 | sm.emit('file', file);
|
74 | next()
|
75 | }
|
76 | }
|
77 |
|
78 | function readFileSync (file, enc) {
|
79 | var isBuffer = false;
|
80 | if (enc === null || enc === undefined) {
|
81 | isBuffer = true;
|
82 | enc = 'base64';
|
83 | }
|
84 | if (enc && typeof enc === 'object' && enc.encoding) {
|
85 | enc = enc.encoding;
|
86 | }
|
87 | var stream = fs.createReadStream(file, { encoding: enc })
|
88 | .on('error', function (err) { sm.emit('error', err) })
|
89 | .pipe(quote()).pipe(through(write, end))
|
90 | ;
|
91 | if (isBuffer) {
|
92 | stream.push('Buffer(');
|
93 | }
|
94 | return stream;
|
95 |
|
96 | function write (buf, enc, next) {
|
97 | this.push(buf);
|
98 | next();
|
99 | }
|
100 | function end (next) {
|
101 | if (isBuffer) this.push(',"base64")');
|
102 | this.push(null);
|
103 | sm.emit('file', file);
|
104 | next();
|
105 | }
|
106 | }
|
107 |
|
108 | function readdir(path, cb) {
|
109 | var stream = through(write, end);
|
110 |
|
111 | stream.push('process.nextTick(function(){(' + cb + ')(null,');
|
112 | fs.readdir(path, function (err, src) {
|
113 | if (err) {
|
114 | stream.emit('error', err);
|
115 | return;
|
116 | }
|
117 | stream.push(JSON.stringify(src));
|
118 | stream.end(')})');
|
119 | });
|
120 | return stream;
|
121 |
|
122 | function write (buf, enc, next) {
|
123 | this.push(buf);
|
124 | next();
|
125 | }
|
126 | function end (next) {
|
127 | this.push(null);
|
128 | next();
|
129 | }
|
130 | }
|
131 |
|
132 | function readdirSync (path) {
|
133 | var stream = through(write, end);
|
134 | fs.readdir(path, function (err, src) {
|
135 | if (err) {
|
136 | stream.emit('error', err);
|
137 | return;
|
138 | }
|
139 | stream.end(JSON.stringify(src));
|
140 | });
|
141 | return stream;
|
142 |
|
143 | function write (buf, enc, next) {
|
144 | this.push(buf);
|
145 | next();
|
146 | }
|
147 | function end (next) {
|
148 | this.push(null);
|
149 | next();
|
150 | }
|
151 | }
|
152 | };
|