skypager-project
Version:
skypager project framework
82 lines (68 loc) • 1.89 kB
JavaScript
import Context from 'skypager-registry/lib/context'
import Helper from '../helper'
export class Transformer extends Helper {
static isCacheable = false
static dirname = __dirname
static cacheable(setting = true) {
return this.isCacheable = !!setting
}
static createRegistry = () => new Context('transformers', {
context: require.context('../transformers', false, /.js$/),
})
static attach(project, options = {}) {
return Helper.attach(project, Transformer, {
registryProp: 'transformers',
lookupProp: 'transformer',
registry: options.registry || Transformer.createRegistry(),
...options,
})
}
get projectCacheKey() {
return this.get('project.cacheKey', this.cacheKey)
}
run (...inputs) {
return Promise.all(inputs.map(input =>
() => Promise.resolve()
.then(() => this.parsed = this.parse(input))
.then(() => this.rendered = this.render(this.parsed))
.then(() => this.pick('parsed', 'rendered', 'projectCacheKey'))
.then((results) => ({
input,
results
}))
))
}
source(input, options = {}) {
if (typeof input === 'string') {
input = this.parse(input, {
parser: 'stringifier',
...options,
method: 'stringify'
})
}
return this.provider.applyTo(input, {
method: 'stringify',
parser: 'stringifier',
})
}
render(input, options = {}) {
if (typeof input === 'string') {
input = this.parse(input, {
...options,
method: 'parse'
})
}
return this.provider.applyTo(input, {
method: 'stringify',
...options,
})
}
parse(input, options = {}) {
return this.provider.applyTo(input, {
...options,
method: 'parse',
})
}
}
export default Transformer
export const attach = Transformer.attach