UNPKG

duo

Version:

A next-generation package manager for the front-end

312 lines (253 loc) 5.73 kB
(function outer(modules, cache, entries){ /** * Global */ var global = (function(){ return this; })(); /** * Require `name`. * * @param {String} name * @api public */ function require(name){ if (cache[name]) return cache[name].exports; if (modules[name]) return call(name, require); throw new Error('cannot find module "' + name + '"'); } /** * Call module `id` and cache it. * * @param {Number} id * @param {Function} require * @return {Function} * @api private */ function call(id, require){ var m = cache[id] = { exports: {} }; var mod = modules[id]; var name = mod[2]; var fn = mod[0]; var threw = true; try { fn.call(m.exports, function(req){ var dep = modules[id][1][req]; return require(dep || req); }, m, m.exports, outer, modules, cache, entries); threw = false; } finally { if (threw) { delete cache[id]; } else if (name) { // expose as 'name'. cache[name] = cache[id]; } } return cache[id].exports; } /** * Require all entries exposing them on global if needed. */ for (var id in entries) { if (entries[id]) { global[entries[id]] = require(id); } else { require(id); } } /** * Duo flag. */ require.duo = true; /** * Expose cache. */ require.cache = cache; /** * Expose modules */ require.modules = modules; /** * Return newest require. */ return require; })({ 1: [function(require, module, exports) { /** * Module Dependencies */ var assert = require('assert'); var Duo = require('../../'); var fs = require('fs'); var regen = require('regenerator'); var path = require('path'); var join = path.join; var util = require('../../lib/util'); var token = util.token(); /** * Paths */ out = join(__dirname, 'build.js'); /** * Initialize `Duo` */ var duo = Duo(__dirname) .token(token) .use(regenerator) .entry('main.js') /** * Run duo */ duo.run(function(err, results) { if (err) throw err; fs.writeFileSync(out, results.code); var len = Buffer.byteLength(results.code); console.log('all done, wrote %dkb', len / 1024 | 0); }); /** * Regenerator plugin */ function regenerator(file) { if ('js' != file.type) return; // TODO: only include runtime once, using duo#include('regenerator', fn) file.src = regen(file.src, { includeRuntime: true }); } }, {"../../lib/util":2}], 2: [function(require, module, exports) { /** * Module dependencies. */ var detect = require('language-classifier'); var resolve = require('path').resolve; var exists = require('fs').existsSync; var stat = require('fs').statSync; var netrc = require('node-netrc'); var glob = require('glob').sync; var path = require('path'); /** * Language mapping. */ var langmap = { javascript: 'js', css: 'css' }; /** * Pull GH auth from ~/.netrc or $GH_TOKEN env. */ exports.token = function () { if ('GH_TOKEN' in process.env) return process.env.GH_TOKEN; var auth = netrc('api.github.com'); if (auth && auth.password) return auth.password; return false; }; /** * Normalize entries list. * * - expand globs * - expand directories into list of all nested files * * @param {Array:String} * @return {Array:String} */ exports.entries = function (root, list) { return list.filter(globs).reduce(function (memo, entry) { if (isDir(path.join(root, entry))) { return memo.concat(listFiles(root, entry)); } else { return memo.concat(entry); } }, []); }; /** * Helper for collecting CLI params into a single array. * * @param {String} val * @param {Array:String} memo * @returns {Array:String} */ exports.collect = function (val, memo) { val.split(',').forEach(function (val) { memo.push(val); }); return memo; }; /** * Find the root. * * @param {String} root * @param {String} */ exports.findroot = function (root) { var cwd = process.cwd(); if (root) return resolve(cwd, root); var sep = path.sep; var parts = cwd.split(sep); var dir = cwd; while (!exists(path.join(dir, 'component.json')) && parts.length > 1) { parts.pop(); dir = parts.join(sep); } return parts.length <= 1 ? cwd : dir; }; /** * Detect the type of a source-file. * * @param {String} src * @returns {String} */ exports.type = function (src) { return langmap[detect(src)]; }; /** * Retrieve an array of plugins from `--use`. * * @param {Array:String} plugins * @return {Array:Function} */ exports.plugins = function (root, plugins) { return plugins.map(function (plugin) { var local = resolve(root, plugin); var npm = resolve(root, 'node_modules', plugin); var cwd = resolve(process.cwd(), 'node_modules', plugin); var mod; if (exists(local)) mod = require(local); else if (exists(local + '.js')) mod = require(local); else if (exists(npm)) mod = require(npm); else mod = require(cwd); return Array.isArray(mod) ? mod : mod(); }, []); }; /** * Filter out unexpanded globs. * * @param {String} entry * @return {Boolean} */ function globs(path) { return !/\*/.test(path); } /** * Gets a list of all files within a directory recursively (and synchronously) * * @param {String} path * @return {Array:String} */ function listFiles(root, dir, pattern) { var opts = { cwd: root, nodir: true }; return glob(path.join(dir, pattern || '**/*'), opts); } /** * Simple hueristic to check if `path` is a directory. * * @param {String} path * @return {Boolean} */ function isDir(path) { try { return stat(path).isDirectory(); } catch (e) { return false; } } }, {}]}, {}, {"1":""})