1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var path = _interopDefault(require('path'));
|
6 | var events = require('events');
|
7 | var fs = _interopDefault(require('fs-extra'));
|
8 | var glob = _interopDefault(require('fast-glob'));
|
9 |
|
10 | function _asyncToGenerator(fn) {
|
11 | return function () {
|
12 | var self = this,
|
13 | args = arguments;
|
14 | return new Promise(function (resolve, reject) {
|
15 | var gen = fn.apply(self, args);
|
16 |
|
17 | function step(key, arg) {
|
18 | try {
|
19 | var info = gen[key](arg);
|
20 | var value = info.value;
|
21 | } catch (error) {
|
22 | reject(error);
|
23 | return;
|
24 | }
|
25 |
|
26 | if (info.done) {
|
27 | resolve(value);
|
28 | } else {
|
29 | Promise.resolve(value).then(_next, _throw);
|
30 | }
|
31 | }
|
32 |
|
33 | function _next(value) {
|
34 | step("next", value);
|
35 | }
|
36 |
|
37 | function _throw(err) {
|
38 | step("throw", err);
|
39 | }
|
40 |
|
41 | _next();
|
42 | });
|
43 | };
|
44 | }
|
45 |
|
46 | class Wares {
|
47 | constructor() {
|
48 | this.middlewares = [];
|
49 | }
|
50 |
|
51 | use(middleware) {
|
52 | this.middlewares = this.middlewares.concat(middleware);
|
53 | return this;
|
54 | }
|
55 |
|
56 | run(context) {
|
57 | return this.middlewares.reduce((current, next) => {
|
58 | return current.then(() => Promise.resolve(next(context)));
|
59 | }, Promise.resolve());
|
60 | }
|
61 |
|
62 | }
|
63 |
|
64 | class Majo extends events.EventEmitter {
|
65 | constructor() {
|
66 | super();
|
67 | |
68 |
|
69 |
|
70 |
|
71 | this.middlewares = [];
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 | this.meta = {};
|
78 | }
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | source(patterns, {
|
89 | baseDir = '.',
|
90 | dotFiles = true
|
91 | } = {}) {
|
92 | this.baseDir = path.resolve(baseDir);
|
93 | this.sourcePatterns = patterns;
|
94 | this.dotFiles = dotFiles;
|
95 | return this;
|
96 | }
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | use(middleware) {
|
104 | this.middlewares.push(middleware);
|
105 | return this;
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 | process() {
|
113 | var _this = this;
|
114 |
|
115 | return _asyncToGenerator(function* () {
|
116 | const allStats = yield glob(_this.sourcePatterns, {
|
117 | cwd: _this.baseDir,
|
118 | dot: _this.dotFiles,
|
119 | stats: true
|
120 | });
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | _this.files = {};
|
127 | yield Promise.all(allStats.map(stats => {
|
128 | const absolutePath = path.resolve(_this.baseDir, stats.path);
|
129 | return fs.readFile(absolutePath).then(contents => {
|
130 | const file = {
|
131 | contents,
|
132 | stats,
|
133 | path: absolutePath
|
134 | };
|
135 | _this.files[stats.path] = file;
|
136 | });
|
137 | }));
|
138 | yield new Wares().use(_this.middlewares).run(_this);
|
139 | return _this;
|
140 | })();
|
141 | }
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | filter(fn) {
|
149 | return this.use(context => {
|
150 | for (const relativePath in context.files) {
|
151 | if (!fn(relativePath, context.files[relativePath])) {
|
152 | delete context.files[relativePath];
|
153 | }
|
154 | }
|
155 | });
|
156 | }
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | transform(relativePath, fn) {
|
165 | const contents = this.files[relativePath].contents.toString();
|
166 | const result = fn(contents);
|
167 |
|
168 | if (!result.then) {
|
169 | this.files[relativePath].contents = Buffer.from(result);
|
170 | return;
|
171 | }
|
172 |
|
173 | return result.then(newContents => {
|
174 | this.files[relativePath].contents = Buffer.from(newContents);
|
175 | });
|
176 | }
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | dest(dest, {
|
187 | baseDir = '.',
|
188 | clean = false
|
189 | } = {}) {
|
190 | var _this2 = this;
|
191 |
|
192 | return _asyncToGenerator(function* () {
|
193 | const destPath = path.resolve(baseDir, dest);
|
194 | yield _this2.process();
|
195 |
|
196 | if (clean) {
|
197 | yield fs.remove(destPath);
|
198 | }
|
199 |
|
200 | yield Promise.all(Object.keys(_this2.files).map(filename => {
|
201 | const contents = _this2.files[filename].contents;
|
202 | const target = path.join(destPath, filename);
|
203 |
|
204 | _this2.emit('write', filename, target);
|
205 |
|
206 | return fs.ensureDir(path.dirname(target)).then(() => fs.writeFile(target, contents));
|
207 | }));
|
208 | return _this2;
|
209 | })();
|
210 | }
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | fileContents(relativePath) {
|
219 | return this.file(relativePath).contents.toString();
|
220 | }
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | writeContents(relativePath, string) {
|
229 | this.files[relativePath].contents = Buffer.from(string);
|
230 | return this;
|
231 | }
|
232 | |
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | fileStats(relativePath) {
|
240 | return this.file(relativePath).stats;
|
241 | }
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | file(relativePath) {
|
250 | return this.files[relativePath];
|
251 | }
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | deleteFile(relativePath) {
|
259 | delete this.files[relativePath];
|
260 | return this;
|
261 | }
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | createFile(relativePath, file) {
|
270 | this.files[relativePath] = file;
|
271 | return this;
|
272 | }
|
273 | |
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | get fileList() {
|
280 | return Object.keys(this.files).sort();
|
281 | }
|
282 |
|
283 | }
|
284 |
|
285 | const majo = () => new Majo();
|
286 |
|
287 | majo.glob = glob;
|
288 | majo.fs = fs;
|
289 |
|
290 | module.exports = majo;
|