| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- // Generated by CoffeeScript 1.12.1
- (function() {
- var Adapter, W, clone, fs, partialRight, path, readFile, requireEngine, resolve, resolvePath,
- indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
- W = require('when');
- clone = require('lodash.clone');
- partialRight = require('lodash.partialright');
- resolve = require('resolve');
- path = require('path');
- fs = require('fs');
- readFile = require('when/node/function').lift(fs.readFile);
- Adapter = (function() {
- /**
- * The names of the npm modules that are supported to be used as engines by
- the adapter. Defaults to the name of the adapter.
- * @type {String[]}
- */
- Adapter.prototype.supportedEngines = void 0;
- /**
- * The name of the engine in-use. Generally this is the name of the package on
- npm.
- * @type {String}
- */
- Adapter.prototype.engineName = '';
- /**
- * The actual engine, no adapter wrapper. Defaults to the engine that we
- recommend for compiling that particular language (if it is installed).
- Otherwise, whatever engine we support that is installed.
- */
- Adapter.prototype.engine = void 0;
- /**
- * Array of all file extensions the compiler should match
- * @type {String[]}
- */
- Adapter.prototype.extensions = void 0;
- /**
- * Expected output extension
- * @type {String}
- */
- Adapter.prototype.output = '';
- /**
- * Specify if the output of the language is independent of other files or the
- evaluation of potentially stateful functions. This means that the only
- information passed into the engine is what gets passed to Accord's
- compile/render function, and whenever that same input is given, the output
- will always be the same.
- * @type {Boolean}
- * @todo Add detection for when a particular job qualifies as isolated
- */
- Adapter.prototype.isolated = false;
- /**
- * @param {String} [engine=Adapter.supportedEngines[0]] If you need to use a
- particular engine to compile/render with, then specify it here. Otherwise
- we use whatever engine you have installed.
- */
- function Adapter(engineName1, customPath) {
- var i, len, ref, ref1;
- this.engineName = engineName1;
- if (!this.supportedEngines || this.supportedEngines.length === 0) {
- this.supportedEngines = [this.name];
- }
- if (this.engineName != null) {
- if (ref = this.engineName, indexOf.call(this.supportedEngines, ref) < 0) {
- throw new Error("engine '" + this.engineName + "' not supported");
- }
- this.engine = requireEngine(this.engineName, customPath);
- } else {
- ref1 = this.supportedEngines;
- for (i = 0, len = ref1.length; i < len; i++) {
- this.engineName = ref1[i];
- try {
- this.engine = requireEngine(this.engineName, customPath);
- } catch (error) {
- continue;
- }
- return;
- }
- throw new Error("'tried to require: " + this.supportedEngines + "'.\nNone found. Make sure one has been installed!");
- }
- }
- /**
- * Render a string to a compiled string
- * @param {String} str
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.render = function(str, opts) {
- if (opts == null) {
- opts = {};
- }
- if (!this._render) {
- return W.reject(new Error('render not supported'));
- }
- return this._render(str, opts);
- };
- /**
- * Render a file to a compiled string
- * @param {String} file The path to the file
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.renderFile = function(file, opts) {
- if (opts == null) {
- opts = {};
- }
- opts = clone(opts, true);
- return readFile(file, 'utf8').then(partialRight(this.render, Object.assign({
- filename: file
- }, opts)).bind(this));
- };
- /**
- * Compile a string to a function
- * @param {String} str
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.compile = function(str, opts) {
- if (opts == null) {
- opts = {};
- }
- if (!this._compile) {
- return W.reject(new Error('compile not supported'));
- }
- return this._compile(str, opts);
- };
- /**
- * Compile a file to a function
- * @param {String} file The path to the file
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.compileFile = function(file, opts) {
- if (opts == null) {
- opts = {};
- }
- return readFile(file, 'utf8').then(partialRight(this.compile, Object.assign({
- filename: file
- }, opts)).bind(this));
- };
- /**
- * Compile a string to a client-side-ready function
- * @param {String} str
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.compileClient = function(str, opts) {
- if (opts == null) {
- opts = {};
- }
- if (!this._compileClient) {
- return W.reject(new Error('client-side compile not supported'));
- }
- return this._compileClient(str, opts);
- };
- /**
- * Compile a file to a client-side-ready function
- * @param {String} file The path to the file
- * @param {Object} [opts = {}]
- * @return {Promise}
- */
- Adapter.prototype.compileFileClient = function(file, opts) {
- if (opts == null) {
- opts = {};
- }
- return readFile(file, 'utf8').then(partialRight(this.compileClient, Object.assign(opts, {
- filename: file
- })).bind(this));
- };
- /**
- * Some adapters that compile for client also need helpers, this method
- returns a string of minfied JavaScript with all of them
- * @return {Promise} A promise for the client-side helpers.
- */
- Adapter.prototype.clientHelpers = void 0;
- return Adapter;
- })();
- requireEngine = function(engineName, customPath) {
- var engine, err;
- if (customPath != null) {
- engine = require(resolve.sync(path.basename(customPath), {
- basedir: customPath
- }));
- engine.__accord_path = customPath;
- } else {
- try {
- engine = require(engineName);
- engine.__accord_path = resolvePath(engineName);
- } catch (error) {
- err = error;
- throw new Error("'" + engineName + "' not found. make sure it has been installed!");
- }
- }
- try {
- if (!engine.version) {
- engine.version = require(engine.__accord_path + '/package.json').version;
- }
- } catch (error) {
- err = error;
- }
- return engine;
- };
- /**
- * Get the path to the root folder of a node module, given its name.
- * @param {String} name The name of the node module you want the path to.
- * @return {String} The root folder of node module `name`.
- * @private
- */
- resolvePath = function(name) {
- var filepath;
- filepath = require.resolve(name);
- while (true) {
- if (filepath === '/') {
- throw new Error("cannot resolve root of node module " + name);
- }
- filepath = path.dirname(filepath);
- if (fs.existsSync(path.join(filepath, 'package.json'))) {
- return filepath;
- }
- }
- };
- module.exports = Adapter;
- }).call(this);
|