UNPKG

11.2 kBJavaScriptView Raw
1Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
2 "use strict";
3
4 if (!Function.prototype.$asyncbind) {
5 Object.defineProperty(Function.prototype, "$asyncbind", {
6 value: $asyncbind,
7 enumerable: false,
8 configurable: true,
9 writable: true
10 });
11 }
12
13 if (!$asyncbind.trampoline) {
14 $asyncbind.trampoline = function trampoline(t, x, s, e, u) {
15 return function b(q) {
16 while (q) {
17 if (q.then) {
18 q = q.then(b, e);
19 return u ? undefined : q;
20 }
21
22 try {
23 if (q.pop) {
24 if (q.length) return q.pop() ? x.call(t) : q;
25 q = s;
26 } else q = q.call(t);
27 } catch (r) {
28 return e(r);
29 }
30 }
31 };
32 };
33 }
34
35 if (!$asyncbind.LazyThenable) {
36 $asyncbind.LazyThenable = function () {
37 function isThenable(obj) {
38 return obj && obj instanceof Object && typeof obj.then === "function";
39 }
40
41 function resolution(p, r, how) {
42 try {
43 var x = how ? how(r) : r;
44 if (p === x) return p.reject(new TypeError("Promise resolution loop"));
45
46 if (isThenable(x)) {
47 x.then(function (y) {
48 resolution(p, y);
49 }, function (e) {
50 p.reject(e);
51 });
52 } else {
53 p.resolve(x);
54 }
55 } catch (ex) {
56 p.reject(ex);
57 }
58 }
59
60 function Chained() {}
61
62 ;
63 Chained.prototype = {
64 resolve: _unchained,
65 reject: _unchained,
66 then: thenChain
67 };
68
69 function _unchained(v) {}
70
71 function thenChain(res, rej) {
72 this.resolve = res;
73 this.reject = rej;
74 }
75
76 function then(res, rej) {
77 var chain = new Chained();
78
79 try {
80 this._resolver(function (value) {
81 return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
82 }, function (ex) {
83 resolution(chain, ex, rej);
84 });
85 } catch (ex) {
86 resolution(chain, ex, rej);
87 }
88
89 return chain;
90 }
91
92 function Thenable(resolver) {
93 this._resolver = resolver;
94 this.then = then;
95 }
96
97 ;
98
99 Thenable.resolve = function (v) {
100 return Thenable.isThenable(v) ? v : {
101 then: function then(resolve) {
102 return resolve(v);
103 }
104 };
105 };
106
107 Thenable.isThenable = isThenable;
108 return Thenable;
109 }();
110
111 $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
112 tick = tick || typeof process === "object" && process.nextTick || typeof setImmediate === "function" && setImmediate || function (f) {
113 setTimeout(f, 0);
114 };
115
116 var soon = function () {
117 var fq = [],
118 fqStart = 0,
119 bufferSize = 1024;
120
121 function callQueue() {
122 while (fq.length - fqStart) {
123 try {
124 fq[fqStart]();
125 } catch (ex) {}
126
127 fq[fqStart++] = undefined;
128
129 if (fqStart === bufferSize) {
130 fq.splice(0, bufferSize);
131 fqStart = 0;
132 }
133 }
134 }
135
136 return function (fn) {
137 fq.push(fn);
138 if (fq.length - fqStart === 1) tick(callQueue);
139 };
140 }();
141
142 function Zousan(func) {
143 if (func) {
144 var me = this;
145 func(function (arg) {
146 me.resolve(arg);
147 }, function (arg) {
148 me.reject(arg);
149 });
150 }
151 }
152
153 Zousan.prototype = {
154 resolve: function resolve(value) {
155 if (this.state !== undefined) return;
156 if (value === this) return this.reject(new TypeError("Attempt to resolve promise with self"));
157 var me = this;
158
159 if (value && (typeof value === "function" || typeof value === "object")) {
160 try {
161 var first = 0;
162 var then = value.then;
163
164 if (typeof then === "function") {
165 then.call(value, function (ra) {
166 if (!first++) {
167 me.resolve(ra);
168 }
169 }, function (rr) {
170 if (!first++) {
171 me.reject(rr);
172 }
173 });
174 return;
175 }
176 } catch (e) {
177 if (!first) this.reject(e);
178 return;
179 }
180 }
181
182 this.state = STATE_FULFILLED;
183 this.v = value;
184 if (me.c) soon(function () {
185 for (var n = 0, l = me.c.length; n < l; n++) STATE_FULFILLED(me.c[n], value);
186 });
187 },
188 reject: function reject(reason) {
189 if (this.state !== undefined) return;
190 this.state = STATE_REJECTED;
191 this.v = reason;
192 var clients = this.c;
193 if (clients) soon(function () {
194 for (var n = 0, l = clients.length; n < l; n++) STATE_REJECTED(clients[n], reason);
195 });
196 },
197 then: function then(onF, onR) {
198 var p = new Zousan();
199 var client = {
200 y: onF,
201 n: onR,
202 p: p
203 };
204
205 if (this.state === undefined) {
206 if (this.c) this.c.push(client);else this.c = [client];
207 } else {
208 var s = this.state,
209 a = this.v;
210 soon(function () {
211 s(client, a);
212 });
213 }
214
215 return p;
216 }
217 };
218
219 function STATE_FULFILLED(c, arg) {
220 if (typeof c.y === "function") {
221 try {
222 var yret = c.y.call(undefined, arg);
223 c.p.resolve(yret);
224 } catch (err) {
225 c.p.reject(err);
226 }
227 } else c.p.resolve(arg);
228 }
229
230 function STATE_REJECTED(c, reason) {
231 if (typeof c.n === "function") {
232 try {
233 var yret = c.n.call(undefined, reason);
234 c.p.resolve(yret);
235 } catch (err) {
236 c.p.reject(err);
237 }
238 } else c.p.reject(reason);
239 }
240
241 Zousan.resolve = function (val) {
242 if (val && val instanceof Zousan) return val;
243 var z = new Zousan();
244 z.resolve(val);
245 return z;
246 };
247
248 Zousan.reject = function (err) {
249 if (err && err instanceof Zousan) return err;
250 var z = new Zousan();
251 z.reject(err);
252 return z;
253 };
254
255 Zousan.version = "2.3.3-nodent";
256 return Zousan;
257 })();
258 }
259
260 var resolver = this;
261
262 switch (catcher) {
263 case true:
264 return new $asyncbind.Thenable(boundThen);
265
266 case 0:
267 return new $asyncbind.LazyThenable(boundThen);
268
269 case undefined:
270 boundThen.then = boundThen;
271 return boundThen;
272
273 default:
274 return function () {
275 try {
276 return resolver.apply(self, arguments);
277 } catch (ex) {
278 return catcher(ex);
279 }
280 };
281 }
282
283 function boundThen() {
284 return resolver.apply(self, arguments);
285 }
286};
287
288const Path = require("path");
289
290const _require = require("@patternplate/load-meta"),
291 loadMeta = _require.loadMeta;
292
293const Observable = require("zen-observable");
294
295const chokidar = require("chokidar");
296
297const globParent = require("glob-parent");
298
299const micromatch = require("micromatch");
300
301const commonDir = require("common-dir");
302
303const debug = require("util").debuglog("PATTERNPLATE");
304
305module.exports.createWatcher = function createWatcher(options) {
306 return new Promise(function ($return, $error) {
307 let watching = false;
308 let stopped = false;
309 let subscribers = [];
310 let watcher;
311 const _options$config = options.config,
312 config = _options$config === void 0 ? {} : _options$config;
313 const _config$entry = config.entry,
314 entry = _config$entry === void 0 ? [] : _config$entry,
315 _config$docs = config.docs,
316 docs = _config$docs === void 0 ? [] : _config$docs;
317 const cwd = options.cwd;
318 const configPath = Path.join(cwd, "patternplate.config.js");
319
320 const next = message => subscribers.forEach(subs => subs.next(message));
321
322 const obs = new Observable(subs => {
323 if (!watching) {
324 watching = true;
325
326 (() => new Promise(function ($return, $error) {
327 var meta, parents;
328 return loadMeta({
329 entry,
330 cwd
331 }).then(function ($await_1) {
332 meta = $await_1;
333
334 if (meta.errors && meta.errors.length > 0) {
335 meta.errors.forEach(error => next({
336 type: "error",
337 payload: error
338 }));
339 }
340
341 parents = getParents({
342 globs: [...entry, ...docs],
343 paths: [configPath, meta.patterns.length > 0 ? commonDir(meta.patterns.map(m => Path.join(cwd, m.path))) : null].filter(Boolean)
344 }, {
345 cwd
346 });
347 debug(`subscribing to changes on: ${parents.map(p => Path.relative(cwd, p)).join(", ")}`);
348
349 if (stopped) {
350 return $return();
351 }
352
353 watcher = chokidar.watch(parents, {
354 ignoreInitial: true
355 });
356 obs.watcher = watcher;
357 watcher.on("all", (e, p) => new Promise(function ($return, $error) {
358 const rel = Path.relative(cwd, p);
359
360 if (p === configPath) {
361 next({
362 type: "change",
363 payload: {
364 file: p,
365 contentType: "config"
366 }
367 });
368 }
369
370 if (Path.extname(rel) === ".md") {
371 next({
372 type: "change",
373 payload: {
374 file: p,
375 contentType: "pattern"
376 }
377 });
378 }
379
380 if (Path.basename(rel) === "pattern.json" || Path.basename(rel) === "package.json") {
381 next({
382 type: "change",
383 payload: {
384 file: p,
385 contentType: "pattern"
386 }
387 });
388 }
389
390 if (micromatch.some(rel, docs, {
391 matchBase: true
392 })) {
393 next({
394 type: "change",
395 payload: {
396 file: p,
397 contentType: "doc"
398 }
399 });
400 }
401
402 return $return();
403 }.$asyncbind(this)));
404 return $return();
405 }.$asyncbind(this, $error), $error);
406 }.$asyncbind(this)))();
407 }
408
409 subscribers.push(subs);
410 return () => {
411 subscribers = subscribers.filter(s => s !== subs);
412 };
413 });
414
415 obs.stop = () => {
416 stopped = true;
417
418 if (watcher) {
419 watcher.close();
420 }
421 };
422
423 return $return(obs);
424 }.$asyncbind(this));
425};
426
427function getParents({
428 globs = [],
429 paths = []
430}, {
431 cwd
432}) {
433 return [...paths, ...globs.filter(g => g.charAt(0) !== "!").map(g => Path.join(cwd, globParent(g)))];
434}
435//# sourceMappingURL=create-watcher.js.map
\No newline at end of file