2017-06-01 02:04:27 +00:00
|
|
|
const EMPTY_ARG = '__EMPTY_NUNJUCKS_ARG__';
|
|
|
|
import * as models from '../models/index';
|
2017-06-09 01:10:12 +00:00
|
|
|
import * as templating from './index';
|
2017-06-01 02:04:27 +00:00
|
|
|
|
|
|
|
export default class BaseExtension {
|
|
|
|
constructor (ext) {
|
|
|
|
this._ext = ext;
|
|
|
|
this.tags = [this.getTag()];
|
|
|
|
}
|
|
|
|
|
|
|
|
getTag () {
|
|
|
|
return this._ext.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPriority () {
|
|
|
|
return this._ext.priority || -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
getName () {
|
|
|
|
return this._ext.displayName || this.getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
getDescription () {
|
|
|
|
return this._ext.description || 'no description';
|
|
|
|
}
|
|
|
|
|
|
|
|
getArgs () {
|
|
|
|
return this._ext.args || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
isDeprecated () {
|
|
|
|
return this._ext.deprecated || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
run (...args) {
|
|
|
|
return this._ext.run(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse (parser, nodes, lexer) {
|
|
|
|
const tok = parser.nextToken();
|
|
|
|
|
|
|
|
let args;
|
|
|
|
if (parser.peekToken().type !== lexer.TOKEN_BLOCK_END) {
|
|
|
|
args = parser.parseSignature(null, true);
|
|
|
|
} else {
|
|
|
|
// Not sure why this is needed, but it fails without it
|
|
|
|
args = new nodes.NodeList(tok.lineno, tok.colno);
|
|
|
|
args.addChild(new nodes.Literal(0, 0, EMPTY_ARG));
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.advanceAfterBlockEnd(tok.value);
|
|
|
|
return new nodes.CallExtensionAsync(this, 'asyncRun', args);
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:10:12 +00:00
|
|
|
asyncRun ({ctx: renderContext}, ...runArgs) {
|
2017-06-01 02:04:27 +00:00
|
|
|
// Pull the callback off the end
|
|
|
|
const callback = runArgs[runArgs.length - 1];
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Pull out the meta helper
|
2017-06-09 01:10:12 +00:00
|
|
|
const renderMeta = renderContext.getMeta ? renderContext.getMeta() : {};
|
2017-06-01 02:04:27 +00:00
|
|
|
|
|
|
|
// Extract the rest of the args
|
|
|
|
const args = runArgs
|
2017-06-09 01:10:12 +00:00
|
|
|
.slice(0, runArgs.length - 1)
|
2017-06-01 02:04:27 +00:00
|
|
|
.filter(a => a !== EMPTY_ARG);
|
|
|
|
|
2017-06-09 01:10:12 +00:00
|
|
|
// Define a helper context with utils
|
|
|
|
const helperContext = {
|
2017-06-01 02:04:27 +00:00
|
|
|
context: renderContext,
|
2017-06-09 01:10:12 +00:00
|
|
|
meta: renderMeta,
|
|
|
|
util: {
|
|
|
|
render: str => templating.render(str, {context: renderContext}),
|
|
|
|
models: {
|
|
|
|
request: {getById: models.request.getById},
|
|
|
|
workspace: {getById: models.workspace.getById},
|
2017-08-23 00:06:56 +00:00
|
|
|
cookieJar: {getOrCreateForWorkspace: workspace => {
|
|
|
|
return models.cookieJar.getOrCreateForParentId(workspace._id);
|
|
|
|
}},
|
2017-06-30 03:30:22 +00:00
|
|
|
response: {
|
|
|
|
getLatestForRequestId: models.response.getLatestForRequest,
|
|
|
|
getBodyBuffer: models.response.getBodyBuffer
|
|
|
|
}
|
2017-06-09 01:10:12 +00:00
|
|
|
}
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let result;
|
|
|
|
try {
|
2017-06-09 01:10:12 +00:00
|
|
|
result = this.run(helperContext, ...args);
|
2017-06-01 02:04:27 +00:00
|
|
|
} catch (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result is a promise, resolve it async
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
result.then(
|
|
|
|
r => callback(null, r),
|
|
|
|
err => callback(err)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result is not a Promise, return it synchronously
|
|
|
|
callback(null, result);
|
|
|
|
}
|
|
|
|
}
|