magica
Version:
ImageMagick for browser and Node.js, easy setup, high level API and Command Line Interface, including WASM binary for an easy setup.
71 lines • 3.38 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ejs_1 = require("ejs");
const command_1 = require("../command");
const fsHelper_1 = require("./fsHelper");
const imageHelper_1 = require("./imageHelper");
class Template {
constructor() {
this.name = 'template';
this.templateOptions = { async: true, escape: (s) => s, rmWhitespace: false };
this.templateRuntimeOptions = Object.assign(Object.assign({}, this.templateOptions), { delimiter: '$' });
if (!installed) {
installed = true;
addTemplateHelper(new fsHelper_1.LSHelper());
addTemplateHelper(new imageHelper_1.SizeHelper());
addTemplateHelper(new imageHelper_1.ImageInfoHelper());
addTemplateHelper(new imageHelper_1.HeightHelper());
addTemplateHelper(new imageHelper_1.WidthHelper());
}
}
fnCompileTime(context) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof context.script === 'string') {
const t = ejs_1.compile(context.script, this.templateOptions);
// context.debug && console.log('Template compiled: ', t.toString())
let c = Object.assign({}, context);
templateHelpers.forEach(fn => {
c[fn.name] = fn.fnCompileTime.bind(Object.assign(fn, { options: context }));
});
const script = yield t(c);
return Object.assign(Object.assign({}, context), { script });
}
else {
return context;
}
});
}
fnRuntime(commandOptions, commandIndex, runOptions) {
return __awaiter(this, void 0, void 0, function* () {
var cs = commandOptions.command === 'string' ? commandOptions.command : !commandOptions.command ? '' : command_1.arrayToCliOne(commandOptions.command);
const t = ejs_1.compile(cs, this.templateRuntimeOptions);
let c = { runOptions, commandOptions };
templateHelpers.filter(t => t.fnRunTime).forEach(fn => {
c[fn.name] = fn.fnRunTime.bind(fn);
});
var s = yield t(c);
commandOptions.command = command_1.processCommand(s);
});
}
}
exports.Template = Template;
const templateHelpers = [];
/**
* Allows to change the context object on which templates are evaluated to add new properties or functions
* so they can be evaluated in command templates.
*/
function addTemplateHelper(h) {
templateHelpers.push(h);
}
exports.addTemplateHelper = addTemplateHelper;
let installed = false;
//# sourceMappingURL=template.js.map