adapter_base.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Generated by CoffeeScript 1.12.1
  2. (function() {
  3. var Adapter, W, clone, fs, partialRight, path, readFile, requireEngine, resolve, resolvePath,
  4. 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; };
  5. W = require('when');
  6. clone = require('lodash.clone');
  7. partialRight = require('lodash.partialright');
  8. resolve = require('resolve');
  9. path = require('path');
  10. fs = require('fs');
  11. readFile = require('when/node/function').lift(fs.readFile);
  12. Adapter = (function() {
  13. /**
  14. * The names of the npm modules that are supported to be used as engines by
  15. the adapter. Defaults to the name of the adapter.
  16. * @type {String[]}
  17. */
  18. Adapter.prototype.supportedEngines = void 0;
  19. /**
  20. * The name of the engine in-use. Generally this is the name of the package on
  21. npm.
  22. * @type {String}
  23. */
  24. Adapter.prototype.engineName = '';
  25. /**
  26. * The actual engine, no adapter wrapper. Defaults to the engine that we
  27. recommend for compiling that particular language (if it is installed).
  28. Otherwise, whatever engine we support that is installed.
  29. */
  30. Adapter.prototype.engine = void 0;
  31. /**
  32. * Array of all file extensions the compiler should match
  33. * @type {String[]}
  34. */
  35. Adapter.prototype.extensions = void 0;
  36. /**
  37. * Expected output extension
  38. * @type {String}
  39. */
  40. Adapter.prototype.output = '';
  41. /**
  42. * Specify if the output of the language is independent of other files or the
  43. evaluation of potentially stateful functions. This means that the only
  44. information passed into the engine is what gets passed to Accord's
  45. compile/render function, and whenever that same input is given, the output
  46. will always be the same.
  47. * @type {Boolean}
  48. * @todo Add detection for when a particular job qualifies as isolated
  49. */
  50. Adapter.prototype.isolated = false;
  51. /**
  52. * @param {String} [engine=Adapter.supportedEngines[0]] If you need to use a
  53. particular engine to compile/render with, then specify it here. Otherwise
  54. we use whatever engine you have installed.
  55. */
  56. function Adapter(engineName1, customPath) {
  57. var i, len, ref, ref1;
  58. this.engineName = engineName1;
  59. if (!this.supportedEngines || this.supportedEngines.length === 0) {
  60. this.supportedEngines = [this.name];
  61. }
  62. if (this.engineName != null) {
  63. if (ref = this.engineName, indexOf.call(this.supportedEngines, ref) < 0) {
  64. throw new Error("engine '" + this.engineName + "' not supported");
  65. }
  66. this.engine = requireEngine(this.engineName, customPath);
  67. } else {
  68. ref1 = this.supportedEngines;
  69. for (i = 0, len = ref1.length; i < len; i++) {
  70. this.engineName = ref1[i];
  71. try {
  72. this.engine = requireEngine(this.engineName, customPath);
  73. } catch (error) {
  74. continue;
  75. }
  76. return;
  77. }
  78. throw new Error("'tried to require: " + this.supportedEngines + "'.\nNone found. Make sure one has been installed!");
  79. }
  80. }
  81. /**
  82. * Render a string to a compiled string
  83. * @param {String} str
  84. * @param {Object} [opts = {}]
  85. * @return {Promise}
  86. */
  87. Adapter.prototype.render = function(str, opts) {
  88. if (opts == null) {
  89. opts = {};
  90. }
  91. if (!this._render) {
  92. return W.reject(new Error('render not supported'));
  93. }
  94. return this._render(str, opts);
  95. };
  96. /**
  97. * Render a file to a compiled string
  98. * @param {String} file The path to the file
  99. * @param {Object} [opts = {}]
  100. * @return {Promise}
  101. */
  102. Adapter.prototype.renderFile = function(file, opts) {
  103. if (opts == null) {
  104. opts = {};
  105. }
  106. opts = clone(opts, true);
  107. return readFile(file, 'utf8').then(partialRight(this.render, Object.assign({
  108. filename: file
  109. }, opts)).bind(this));
  110. };
  111. /**
  112. * Compile a string to a function
  113. * @param {String} str
  114. * @param {Object} [opts = {}]
  115. * @return {Promise}
  116. */
  117. Adapter.prototype.compile = function(str, opts) {
  118. if (opts == null) {
  119. opts = {};
  120. }
  121. if (!this._compile) {
  122. return W.reject(new Error('compile not supported'));
  123. }
  124. return this._compile(str, opts);
  125. };
  126. /**
  127. * Compile a file to a function
  128. * @param {String} file The path to the file
  129. * @param {Object} [opts = {}]
  130. * @return {Promise}
  131. */
  132. Adapter.prototype.compileFile = function(file, opts) {
  133. if (opts == null) {
  134. opts = {};
  135. }
  136. return readFile(file, 'utf8').then(partialRight(this.compile, Object.assign({
  137. filename: file
  138. }, opts)).bind(this));
  139. };
  140. /**
  141. * Compile a string to a client-side-ready function
  142. * @param {String} str
  143. * @param {Object} [opts = {}]
  144. * @return {Promise}
  145. */
  146. Adapter.prototype.compileClient = function(str, opts) {
  147. if (opts == null) {
  148. opts = {};
  149. }
  150. if (!this._compileClient) {
  151. return W.reject(new Error('client-side compile not supported'));
  152. }
  153. return this._compileClient(str, opts);
  154. };
  155. /**
  156. * Compile a file to a client-side-ready function
  157. * @param {String} file The path to the file
  158. * @param {Object} [opts = {}]
  159. * @return {Promise}
  160. */
  161. Adapter.prototype.compileFileClient = function(file, opts) {
  162. if (opts == null) {
  163. opts = {};
  164. }
  165. return readFile(file, 'utf8').then(partialRight(this.compileClient, Object.assign(opts, {
  166. filename: file
  167. })).bind(this));
  168. };
  169. /**
  170. * Some adapters that compile for client also need helpers, this method
  171. returns a string of minfied JavaScript with all of them
  172. * @return {Promise} A promise for the client-side helpers.
  173. */
  174. Adapter.prototype.clientHelpers = void 0;
  175. return Adapter;
  176. })();
  177. requireEngine = function(engineName, customPath) {
  178. var engine, err;
  179. if (customPath != null) {
  180. engine = require(resolve.sync(path.basename(customPath), {
  181. basedir: customPath
  182. }));
  183. engine.__accord_path = customPath;
  184. } else {
  185. try {
  186. engine = require(engineName);
  187. engine.__accord_path = resolvePath(engineName);
  188. } catch (error) {
  189. err = error;
  190. throw new Error("'" + engineName + "' not found. make sure it has been installed!");
  191. }
  192. }
  193. try {
  194. if (!engine.version) {
  195. engine.version = require(engine.__accord_path + '/package.json').version;
  196. }
  197. } catch (error) {
  198. err = error;
  199. }
  200. return engine;
  201. };
  202. /**
  203. * Get the path to the root folder of a node module, given its name.
  204. * @param {String} name The name of the node module you want the path to.
  205. * @return {String} The root folder of node module `name`.
  206. * @private
  207. */
  208. resolvePath = function(name) {
  209. var filepath;
  210. filepath = require.resolve(name);
  211. while (true) {
  212. if (filepath === '/') {
  213. throw new Error("cannot resolve root of node module " + name);
  214. }
  215. filepath = path.dirname(filepath);
  216. if (fs.existsSync(path.join(filepath, 'package.json'))) {
  217. return filepath;
  218. }
  219. }
  220. };
  221. module.exports = Adapter;
  222. }).call(this);