Comparing version 0.14.2 to 0.15.0
@@ -0,1 +1,6 @@ | ||
# v0.15.0 | ||
- Allow multiple directories per Defiler instance, with per-directory `pre`-processing | ||
- Other API tweaks, see docs | ||
# v0.14.2 | ||
@@ -2,0 +7,0 @@ |
@@ -218,28 +218,7 @@ 'use strict'; | ||
// prettier-ignore | ||
let { _origData, _status, _watcher, _transform, _generators, _resolver, _active, _waitingFor, _whenFound, _deps, _queue: _queue$1, _isProcessing: _isProcessing$1, _startWave, _endWave, _enqueue: _enqueue$1, _processPhysicalFile, _processFile, _processGenerator, _checkWave, _cur, _newProxy, _processDependents, _markFound } = symbols; | ||
let { _origData, _status, _watchers: _watchers$1, _transform, _generators, _resolver, _active, _waitingFor, _whenFound, _deps, _queue: _queue$1, _isProcessing: _isProcessing$1, _startWave, _endWave, _enqueue: _enqueue$1, _processPhysicalFile, _processFile, _processGenerator, _checkWave, _cur, _newProxy, _processDependents, _markFound } = symbols; | ||
class Defiler extends EventEmitter { | ||
constructor({ | ||
dir, | ||
filter, | ||
read = true, | ||
enc = 'utf8', | ||
watch = true, | ||
debounce = 10, | ||
transform, | ||
generators = [], | ||
resolver, | ||
}) { | ||
if (typeof dir !== 'string') throw new TypeError('defiler: dir must be a string') | ||
if (typeof filter !== 'undefined' && typeof filter !== 'function') { | ||
throw new TypeError('defiler: filter must be a function') | ||
} | ||
if (typeof read !== 'boolean' && typeof read !== 'function') { | ||
throw new TypeError('defiler: read must be a boolean or a function') | ||
} | ||
if (!Buffer.isEncoding(enc) && typeof enc !== 'function') { | ||
throw new TypeError('defiler: enc must be a supported encoding or a function') | ||
} | ||
if (typeof watch !== 'boolean') throw new TypeError('defiler: watch must be a boolean') | ||
if (typeof debounce !== 'number') throw new TypeError('defiler: debounce must be a number') | ||
constructor(...dirs) { | ||
let { transform, generators = [], resolver } = dirs.pop(); | ||
if (typeof transform !== 'function') { | ||
@@ -262,3 +241,22 @@ throw new TypeError('defiler: transform must be a function') | ||
this[_status] = null; // null = exec not called; false = exec pending; true = exec finished | ||
this[_watcher] = new Watcher({ dir: path.resolve(dir), filter, read, enc, watch, debounce }); // Watcher instance | ||
this[_watchers$1] = dirs.map( | ||
({ dir, filter, read = true, enc = 'utf8', pre, watch = true, debounce = 10 }) => { | ||
if (typeof dir !== 'string') throw new TypeError('defiler: dir must be a string') | ||
if (typeof filter !== 'undefined' && typeof filter !== 'function') { | ||
throw new TypeError('defiler: filter must be a function') | ||
} | ||
if (typeof read !== 'boolean' && typeof read !== 'function') { | ||
throw new TypeError('defiler: read must be a boolean or a function') | ||
} | ||
if (!Buffer.isEncoding(enc) && typeof enc !== 'function') { | ||
throw new TypeError('defiler: enc must be a supported encoding or a function') | ||
} | ||
if (typeof pre !== 'undefined' && typeof pre !== 'function') { | ||
throw new TypeError('defiler: pre must be a function') | ||
} | ||
if (typeof watch !== 'boolean') throw new TypeError('defiler: watch must be a boolean') | ||
if (typeof debounce !== 'number') throw new TypeError('defiler: debounce must be a number') | ||
return new Watcher({ dir: path.resolve(dir), filter, read, enc, pre, watch, debounce }) | ||
}, | ||
); // Watcher instances | ||
this[_transform] = transform; // the transform to run on all files | ||
@@ -282,13 +280,22 @@ this[_generators] = new Map(generators.map(generator => [Symbol(), generator])); // unique symbols -> registered generators | ||
let done = this[_startWave](); | ||
// init the Watcher instance | ||
this[_watcher].on('', event => this[_enqueue$1](event)); | ||
let files = await this[_watcher].init(); | ||
// note that all files are pending transformation | ||
for (let { path: path$$1 } of files) { | ||
this.paths.add(path$$1); | ||
this[_active].add(path$$1); | ||
} | ||
// init the Watcher instances | ||
let files = []; | ||
await Promise.all( | ||
this[_watchers$1].map(async watcher => { | ||
watcher.on('', event => this[_enqueue$1](watcher, event)); | ||
// note that all files are pending transformation | ||
await Promise.all( | ||
(await watcher.init()).map(async file => { | ||
let { path: path$$1 } = file; | ||
if (watcher.pre) await watcher.pre(file); | ||
this.paths.add(file.path); | ||
this[_active].add(file.path); | ||
files.push([watcher, path$$1, file]); | ||
}), | ||
); | ||
}), | ||
); | ||
for (let symbol of this[_generators].keys()) this[_active].add(symbol); | ||
// process each physical file | ||
for (let { path: path$$1, stats } of files) this[_processPhysicalFile](path$$1, stats); | ||
for (let [watcher, path$$1, file] of files) this[_processPhysicalFile](watcher, path$$1, file); | ||
// process each generator | ||
@@ -340,13 +347,15 @@ for (let symbol of this[_generators].keys()) this[_processGenerator](symbol); | ||
// add a Watcher event to the queue, and handle queued events | ||
async [_enqueue$1](event) { | ||
if (event) this[_queue$1].push(event); | ||
async [_enqueue$1](watcher, event) { | ||
if (event) this[_queue$1].push([watcher, event]); | ||
if (this[_isProcessing$1]) return | ||
this[_isProcessing$1] = true; | ||
while (this[_queue$1].length) { | ||
let { event, path: path$$1, stats } = this[_queue$1].shift(); | ||
let done = this[_startWave](); | ||
if (event === '+') { | ||
this[_processPhysicalFile](path$$1, stats); | ||
} else if (event === '-') { | ||
let file = this.files.get(path$$1); | ||
let [watcher, { event, path: path$$1, stats }] = this[_queue$1].shift(); | ||
let file = { path: path$$1, stats }; | ||
if (watcher.pre) await watcher.pre(file); | ||
if (event === '+') this[_processPhysicalFile](watcher, path$$1, file); | ||
else if (event === '-') { | ||
let { path: path$$1 } = file; | ||
file = this.files.get(path$$1); | ||
this.paths.delete(path$$1); | ||
@@ -356,3 +365,3 @@ this[_origData].delete(path$$1); | ||
this.emit('deleted', { defiler: this, file }); | ||
if (this[_status]) this[_processDependents](path$$1); | ||
this[_processDependents](path$$1); | ||
} | ||
@@ -365,13 +374,11 @@ await done; | ||
// create a file object for a physical file and process it | ||
async [_processPhysicalFile](path$$1, stats) { | ||
let { dir, read, enc } = this[_watcher]; | ||
let data = { path: path$$1, stats }; | ||
if (read && (typeof read !== 'function' || (await read({ path: path$$1, stats })))) { | ||
data.bytes = await readFile(dir + '/' + path$$1); | ||
} | ||
data.enc = typeof enc === 'function' ? await enc({ path: path$$1, stats, bytes: data.bytes }) : enc; | ||
this.paths.add(path$$1); | ||
this[_origData].set(path$$1, data); | ||
this.emit('read', { defiler: this, file: Object.assign(new File(), data) }); | ||
await this[_processFile](data); | ||
async [_processPhysicalFile]({ dir, read, enc }, path$$1, file) { | ||
if (typeof read === 'function') read = await read({ path: path$$1, stats: file.stats }); | ||
if (read) file.bytes = await readFile(dir + '/' + path$$1); | ||
if (typeof enc === 'function') enc = await enc({ path: path$$1, stats: file.stats, bytes: file.bytes }); | ||
file.enc = enc; | ||
this.paths.add(file.path); | ||
this[_origData].set(file.path, file); | ||
this.emit('read', { defiler: this, file }); | ||
await this[_processFile](file); | ||
} | ||
@@ -405,7 +412,4 @@ | ||
for (let dependent of dependents) { | ||
if (this[_origData].has(dependent)) { | ||
this[_processFile](this[_origData].get(dependent)); | ||
} else if (this[_generators].has(dependent)) { | ||
this[_processGenerator](dependent); | ||
} | ||
if (this[_origData].has(dependent)) this[_processFile](this[_origData].get(dependent)); | ||
else if (this[_generators].has(dependent)) this[_processGenerator](dependent); | ||
} | ||
@@ -430,5 +434,4 @@ } | ||
[_checkWave]() { | ||
if (!this[_active].size) { | ||
this[_endWave](); | ||
} else if (!this[_status] && [...this[_active]].every(path$$1 => this[_waitingFor].get(path$$1))) { | ||
if (!this[_active].size) this[_endWave](); | ||
else if (!this[_status] && [...this[_active]].every(path$$1 => this[_waitingFor].get(path$$1))) { | ||
// all pending files are currently waiting for one or more other files to exist | ||
@@ -435,0 +438,0 @@ // break deadlock: assume all files that have not appeared yet will never do so |
@@ -214,28 +214,7 @@ import { readdir, readFile, stat, watch } from 'fs'; | ||
// prettier-ignore | ||
let { _origData, _status, _watcher, _transform, _generators, _resolver, _active, _waitingFor, _whenFound, _deps, _queue: _queue$1, _isProcessing: _isProcessing$1, _startWave, _endWave, _enqueue: _enqueue$1, _processPhysicalFile, _processFile, _processGenerator, _checkWave, _cur, _newProxy, _processDependents, _markFound } = symbols; | ||
let { _origData, _status, _watchers: _watchers$1, _transform, _generators, _resolver, _active, _waitingFor, _whenFound, _deps, _queue: _queue$1, _isProcessing: _isProcessing$1, _startWave, _endWave, _enqueue: _enqueue$1, _processPhysicalFile, _processFile, _processGenerator, _checkWave, _cur, _newProxy, _processDependents, _markFound } = symbols; | ||
class Defiler extends EventEmitter { | ||
constructor({ | ||
dir, | ||
filter, | ||
read = true, | ||
enc = 'utf8', | ||
watch: watch$$1 = true, | ||
debounce = 10, | ||
transform, | ||
generators = [], | ||
resolver, | ||
}) { | ||
if (typeof dir !== 'string') throw new TypeError('defiler: dir must be a string') | ||
if (typeof filter !== 'undefined' && typeof filter !== 'function') { | ||
throw new TypeError('defiler: filter must be a function') | ||
} | ||
if (typeof read !== 'boolean' && typeof read !== 'function') { | ||
throw new TypeError('defiler: read must be a boolean or a function') | ||
} | ||
if (!Buffer.isEncoding(enc) && typeof enc !== 'function') { | ||
throw new TypeError('defiler: enc must be a supported encoding or a function') | ||
} | ||
if (typeof watch$$1 !== 'boolean') throw new TypeError('defiler: watch must be a boolean') | ||
if (typeof debounce !== 'number') throw new TypeError('defiler: debounce must be a number') | ||
constructor(...dirs) { | ||
let { transform, generators = [], resolver } = dirs.pop(); | ||
if (typeof transform !== 'function') { | ||
@@ -258,3 +237,22 @@ throw new TypeError('defiler: transform must be a function') | ||
this[_status] = null; // null = exec not called; false = exec pending; true = exec finished | ||
this[_watcher] = new Watcher({ dir: resolve(dir), filter, read, enc, watch: watch$$1, debounce }); // Watcher instance | ||
this[_watchers$1] = dirs.map( | ||
({ dir, filter, read = true, enc = 'utf8', pre, watch: watch$$1 = true, debounce = 10 }) => { | ||
if (typeof dir !== 'string') throw new TypeError('defiler: dir must be a string') | ||
if (typeof filter !== 'undefined' && typeof filter !== 'function') { | ||
throw new TypeError('defiler: filter must be a function') | ||
} | ||
if (typeof read !== 'boolean' && typeof read !== 'function') { | ||
throw new TypeError('defiler: read must be a boolean or a function') | ||
} | ||
if (!Buffer.isEncoding(enc) && typeof enc !== 'function') { | ||
throw new TypeError('defiler: enc must be a supported encoding or a function') | ||
} | ||
if (typeof pre !== 'undefined' && typeof pre !== 'function') { | ||
throw new TypeError('defiler: pre must be a function') | ||
} | ||
if (typeof watch$$1 !== 'boolean') throw new TypeError('defiler: watch must be a boolean') | ||
if (typeof debounce !== 'number') throw new TypeError('defiler: debounce must be a number') | ||
return new Watcher({ dir: resolve(dir), filter, read, enc, pre, watch: watch$$1, debounce }) | ||
}, | ||
); // Watcher instances | ||
this[_transform] = transform; // the transform to run on all files | ||
@@ -278,13 +276,22 @@ this[_generators] = new Map(generators.map(generator => [Symbol(), generator])); // unique symbols -> registered generators | ||
let done = this[_startWave](); | ||
// init the Watcher instance | ||
this[_watcher].on('', event => this[_enqueue$1](event)); | ||
let files = await this[_watcher].init(); | ||
// note that all files are pending transformation | ||
for (let { path } of files) { | ||
this.paths.add(path); | ||
this[_active].add(path); | ||
} | ||
// init the Watcher instances | ||
let files = []; | ||
await Promise.all( | ||
this[_watchers$1].map(async watcher => { | ||
watcher.on('', event => this[_enqueue$1](watcher, event)); | ||
// note that all files are pending transformation | ||
await Promise.all( | ||
(await watcher.init()).map(async file => { | ||
let { path } = file; | ||
if (watcher.pre) await watcher.pre(file); | ||
this.paths.add(file.path); | ||
this[_active].add(file.path); | ||
files.push([watcher, path, file]); | ||
}), | ||
); | ||
}), | ||
); | ||
for (let symbol of this[_generators].keys()) this[_active].add(symbol); | ||
// process each physical file | ||
for (let { path, stats } of files) this[_processPhysicalFile](path, stats); | ||
for (let [watcher, path, file] of files) this[_processPhysicalFile](watcher, path, file); | ||
// process each generator | ||
@@ -336,13 +343,15 @@ for (let symbol of this[_generators].keys()) this[_processGenerator](symbol); | ||
// add a Watcher event to the queue, and handle queued events | ||
async [_enqueue$1](event) { | ||
if (event) this[_queue$1].push(event); | ||
async [_enqueue$1](watcher, event) { | ||
if (event) this[_queue$1].push([watcher, event]); | ||
if (this[_isProcessing$1]) return | ||
this[_isProcessing$1] = true; | ||
while (this[_queue$1].length) { | ||
let { event, path, stats } = this[_queue$1].shift(); | ||
let done = this[_startWave](); | ||
if (event === '+') { | ||
this[_processPhysicalFile](path, stats); | ||
} else if (event === '-') { | ||
let file = this.files.get(path); | ||
let [watcher, { event, path, stats }] = this[_queue$1].shift(); | ||
let file = { path, stats }; | ||
if (watcher.pre) await watcher.pre(file); | ||
if (event === '+') this[_processPhysicalFile](watcher, path, file); | ||
else if (event === '-') { | ||
let { path } = file; | ||
file = this.files.get(path); | ||
this.paths.delete(path); | ||
@@ -352,3 +361,3 @@ this[_origData].delete(path); | ||
this.emit('deleted', { defiler: this, file }); | ||
if (this[_status]) this[_processDependents](path); | ||
this[_processDependents](path); | ||
} | ||
@@ -361,13 +370,11 @@ await done; | ||
// create a file object for a physical file and process it | ||
async [_processPhysicalFile](path, stats) { | ||
let { dir, read, enc } = this[_watcher]; | ||
let data = { path, stats }; | ||
if (read && (typeof read !== 'function' || (await read({ path, stats })))) { | ||
data.bytes = await readFile$1(dir + '/' + path); | ||
} | ||
data.enc = typeof enc === 'function' ? await enc({ path, stats, bytes: data.bytes }) : enc; | ||
this.paths.add(path); | ||
this[_origData].set(path, data); | ||
this.emit('read', { defiler: this, file: Object.assign(new File(), data) }); | ||
await this[_processFile](data); | ||
async [_processPhysicalFile]({ dir, read, enc }, path, file) { | ||
if (typeof read === 'function') read = await read({ path, stats: file.stats }); | ||
if (read) file.bytes = await readFile$1(dir + '/' + path); | ||
if (typeof enc === 'function') enc = await enc({ path, stats: file.stats, bytes: file.bytes }); | ||
file.enc = enc; | ||
this.paths.add(file.path); | ||
this[_origData].set(file.path, file); | ||
this.emit('read', { defiler: this, file }); | ||
await this[_processFile](file); | ||
} | ||
@@ -401,7 +408,4 @@ | ||
for (let dependent of dependents) { | ||
if (this[_origData].has(dependent)) { | ||
this[_processFile](this[_origData].get(dependent)); | ||
} else if (this[_generators].has(dependent)) { | ||
this[_processGenerator](dependent); | ||
} | ||
if (this[_origData].has(dependent)) this[_processFile](this[_origData].get(dependent)); | ||
else if (this[_generators].has(dependent)) this[_processGenerator](dependent); | ||
} | ||
@@ -426,5 +430,4 @@ } | ||
[_checkWave]() { | ||
if (!this[_active].size) { | ||
this[_endWave](); | ||
} else if (!this[_status] && [...this[_active]].every(path => this[_waitingFor].get(path))) { | ||
if (!this[_active].size) this[_endWave](); | ||
else if (!this[_status] && [...this[_active]].every(path => this[_waitingFor].get(path))) { | ||
// all pending files are currently waiting for one or more other files to exist | ||
@@ -431,0 +434,0 @@ // break deadlock: assume all files that have not appeared yet will never do so |
{ | ||
"name": "defiler", | ||
"version": "0.14.2", | ||
"version": "0.15.0", | ||
"description": "A small, strange building block", | ||
@@ -5,0 +5,0 @@ "keywords": ["build", "framework", "async", "watch"], |
@@ -18,3 +18,2 @@ # Defiler: A small, strange building block. | ||
- [api](API.md#readme) | ||
- [cookbook](COOKBOOK.md#readme) | ||
- [changelog](CHANGELOG.md#readme) | ||
@@ -21,0 +20,0 @@ - [homepage](https://cndtr.io/defiler/) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113608
819
26