1
Fork 0

add get wrapper argument to transform and generator callbacks

This commit is contained in:
Conduitry 2017-05-29 13:22:50 -04:00
parent 76aab8c8e0
commit b0b7640394
2 changed files with 8 additions and 7 deletions

6
API.md
View file

@ -72,7 +72,7 @@ Returns the `Defiler` instance for chaining.
Register a new transform to be applied to all files.
- `transform(file)` - a transformer function, which is passed a `File` instance to mutate. In your function, `this` will be the current `Defiler` instance. The function can return a `Promise` to indicate when it's done
- `transform(file, get(path))` - a transformer function, which is passed a `File` instance to mutate; it is also passed a function `get(path)` which calls the `get(path, dependent)` method (see below) with the appropriate `dependent` (that is, the current file's path), as a convenience. In your function, `this` will be the current `Defiler` instance. The function can return a `Promise` to indicate when it's done
- `if(file)` - _(optional)_ a function that, if present, will be called with the `File` instance before calling the main `transform`. In your function, `this` will be the current `Defiler` instance. If the function returns `false` or a `Promise` resolving to `false`, the transform is skipped
Returns the `Defiler` instance for chaining.
@ -82,7 +82,7 @@ Returns the `Defiler` instance for chaining.
Register a new generated file, not directly sourced from a physical file.
- `path` - the relative path of the file to register the generator for
- `generator(file)` - a function that is passed a new `File` instance containing only a path, which it should then mutate. In your function, `this` will be the current `Defiler` instance. The function can return a `Promise` to indicate when it's done
- `generator(file, get(path))` - a function that is passed a new `File` instance containing only a path, which it should then mutate; it is also passed a function `get(path)` which calls the `get(path, dependent)` method (see below) with the appropriate `dependent` (that is, the current file's path), as a convenience. In your function, `this` will be the current `Defiler` instance. The function can return a `Promise` to indicate when it's done
Returns the `Defiler` instance for chaining.
@ -109,6 +109,8 @@ This can be asked for physical or generated files. If you ask for one or more ph
If a path `dependent` is passed, `dependent` is registered as depending on the file or files in `path`. When the file or files in `path` change, the file at `dependent` will be automatically re-transformed (using `refile`, below). If you're calling `get` inside a transform or generator, `dependent` should typically be the path of the file you're transforming or generating.
Typically, you would not call this directly, and would instead call the function passed as a second argument to the transform or generator callback, which then calls this method with the appropriate `dependent`.
### `refile(path)`
Manually re-transform a `File`. This can be from a physical file or a generated one. Returns a `Promise` to indicate when all processing is complete. Re-transforming a physical file will use the version of it that was last read into memory. Re-transforming a generated file will call its generator again.

View file

@ -77,7 +77,7 @@ export default class Defiler extends EventEmitter {
exec({ close = false } = {}) {
this._checkBeforeExec('exec')
this._processing = true
this._ready = new Promise(async res => {
this._ready = (async () => {
await Promise.all(this._gazePromises)
this._gazePromises = null
@ -121,8 +121,7 @@ export default class Defiler extends EventEmitter {
this._filePromises = null
this._processing = false
res()
})
})()
return this
}
@ -232,7 +231,7 @@ export default class Defiler extends EventEmitter {
try {
for (let { transform, if: if_ } of this._transforms) {
if (!if_ || (await if_.call(this, file))) {
await transform.call(this, file)
await transform.call(this, file, dependency => this.get(dependency, path))
}
}
} catch (err) {
@ -244,7 +243,7 @@ export default class Defiler extends EventEmitter {
let file
try {
file = new File(path)
await this._customGenerators.get(path).call(this, file)
await this._customGenerators.get(path).call(this, file, dependency => this.get(dependency, path))
await this.addFile(file)
} catch (err) {
this.emit('error', path, file, err)