Coverage
100% line coverage
100% statement coverage
100% block coverage
29 SLOC
index.js
100% line coverage
100% statement coverage
100% block coverage
29 SLOC
| Line | Hits | Statements | Source | Action |
|---|---|---|---|---|
| 1 | not covered /** | |||
| 2 | not covered * Created by jhorlin.dearmas on 6/25/2015. | |||
| 3 | not covered */ | |||
| 4 | not covered /** | |||
| 5 | not covered * asyncronous module loader | |||
| 6 | not covered * @example | |||
| 7 | not covered * ```js | |||
| 8 | not covered * //ironically loading async-require using syncronaous require. | |||
| 9 | not covered * var asyncRequire = require('async-require'); | |||
| 10 | not covered * ``` | |||
| 11 | not covered * | |||
| 12 | not covered * @module async-require | |||
| 13 | not covered */ | |||
| 14 | not covered | |||
| 15 | 1 | 100% | (function (module, global, require) { | |
| 16 | 1 | 100% | 'use strict'; | |
| 17 | 1 | 100% | var vm = require('vm'), | |
| 18 | 1 | 100% | fs = require('fs'), | |
| 19 | 1 | 100% | path = require('path'), | |
| 20 | 1 | 100% | extend = require('extend'), | |
| 21 | 1 | 100% | callsite = require('callsite'), | |
| 22 | 1 | 100% | Module = require('module'), | |
| 23 | not covered stackDepth = 2, | |||
| 24 | 1 | 100% | Promise = require('bluebird'); | |
| 25 | not covered | |||
| 26 | not covered /** | |||
| 27 | not covered * @example | |||
| 28 | not covered * ```js | |||
| 29 | not covered * //load script myModule.js | |||
| 30 | not covered * asyncRequire('myModule').then(function(module){ | |||
| 31 | not covered * //module has been exported | |||
| 32 | not covered * }); | |||
| 33 | not covered * ``` | |||
| 34 | not covered * @alias module:async-require | |||
| 35 | not covered * @param {string} module - the path to the module without a .js extension | |||
| 36 | not covered * @returns {Promise<module|Error>} | |||
| 37 | not covered */ | |||
| 38 | not covered function requireAsync(module) { | |||
| 39 | 11 | 100% | var moduleId = module + '.js'; | |
| 40 | 11 | 100% | return moduleId in requireAsync.cache ? requireAsync.cache[moduleId] : requireAsync.cache[moduleId] = | |
| 41 | not covered requireAsync | |||
| 42 | 10 | 100% | .load(module) | |
| 43 | not covered .then(function (data) { | |||
| 44 | 10 | 100% | var script = data.toString(), sandboxModule = new Module(moduleId, module.parent), sandbox = { module: sandboxModule, exports: sandboxModule.exports }; | |
| 45 | not covered sandboxModule = new Module(moduleId, module.parent), | |||
| 46 | not covered sandbox = { | |||
| 47 | not covered module: sandboxModule, | |||
| 48 | not covered exports: sandboxModule.exports | |||
| 49 | not covered }; | |||
| 50 | 10 | 100% | vm.runInNewContext(script, extend({require:require}, global, sandbox)); | |
| 51 | 10 | 100% | return sandbox.module.exports; | |
| 52 | 11 | 100% | }); | |
| 53 | not covered } | |||
| 54 | not covered | |||
| 55 | 1 | 100% | requireAsync.cache = {}; | |
| 56 | not covered | |||
| 57 | not covered /** | |||
| 58 | not covered * load is used by async-require to load the script. By default it load a file relative to the calling module | |||
| 59 | not covered * similar to how require works when loading module not installed through npm. Since this function is public you can | |||
| 60 | not covered * set a new load method. | |||
| 61 | not covered *@example | |||
| 62 | not covered * load a script with request | |||
| 63 | not covered * ```js | |||
| 64 | not covered * var asyncRequire = require('async-require'), | |||
| 65 | not covered * request = require('request'), | |||
| 66 | not covered * Promise = require('promise'), | |||
| 67 | not covered * //load must return a promise | |||
| 68 | not covered * get = Promise.promisify(request.get); | |||
| 69 | not covered * | |||
| 70 | not covered * //overwrite the default load | |||
| 71 | not covered * asyncRequire.load = function(url){ | |||
| 72 | not covered * //get the script | |||
| 73 | not covered * return get(url).spread(function(res, body){ | |||
| 74 | not covered * //returrn only the body of the script | |||
| 75 | not covered * return body; | |||
| 76 | not covered * }) | |||
| 77 | not covered * } | |||
| 78 | not covered * ``` | |||
| 79 | not covered * | |||
| 80 | not covered * @function load - A promisified function that accepts a moduleId as an argument and returns a promise resolving in an object with a toString method such as a Buffer or a String. | |||
| 81 | not covered * @public | |||
| 82 | not covered */ | |||
| 83 | 1 | 100% | requireAsync.load = (function (readFile) { | |
| 84 | 1 | 100% | return function (file) { | |
| 85 | 10 | 100% | var stack = callsite(), | |
| 86 | 10 | 100% | requesterFile = stack[stackDepth].getFileName(), | |
| 87 | 10 | 100% | requesterPath = path.dirname(requesterFile); | |
| 88 | 10 | 100% | return readFile(path.join(requesterPath, file) + '.js'); | |
| 89 | not covered }; | |||
| 90 | 1 | 100% | }(Promise.promisify(fs.readFile))); | |
| 91 | not covered | |||
| 92 | 1 | 100% | module.exports = requireAsync; | |
| 93 | not covered }(module, global, require)); |