UNPKG

rollup-umd-scripts

Version:

CLI for rollup-umd projects

158 lines (148 loc) 6.57 kB
'use strict'; var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* eslint-disable no-undef, no-param-reassign, global-require, no-unused-vars, no-console */ var async = require('async'); var path = require('path'); var fs = require('fs'); function install(dependency, argv, done) { var installName = dependency.split('*')[0]; var globalName = dependency.split('*')[1]; var reRepository = /(^git\+[ssh|https]+:\/\/.*@.*\/(.*)\.git)#?([\w\d]+)?/; var reNameGitUrl = new RegExp('/([A-z1-9-]+).git{1}'); async.auto({ install: function install(cb) { return spawn('npm install --prefix ' + argv.path + ' ' + installName + ' ' + (argv.singleton ? '--save-dev' : '--save'), cb); }, isRepositoryUrl: ['install', function (results, cb) { var match = dependency.match(reRepository); cb(null, match && match.length > 0); }], repositoryUrl: ['isRepositoryUrl', function (results, cb) { if (results.isRepositoryUrl) { var matches = installName.match(reRepository); var url = matches[1]; cb(null, url); } else { cb(null); } }], packageName: ['repositoryUrl', function (results, cb) { if (results.isRepositoryUrl) { var match = installName.match(reNameGitUrl); var packageName = match[1]; cb(null, packageName); return; } cb(null, installName.split('@')[0]); }], version: ['packageName', function (results, cb) { var re = new RegExp(results.packageName + '@\\^?([0-9A-z\\.-]+)', 'i'); var match = results.install[0].match(re); var version = match[1]; cb(null, version); }], installPeer: ['version', function (results, cb) { if (!argv.singleton) { cb(); return; } var value = results.isRepositoryUrl ? results.repositoryUrl + '#' + results.version : '^' + results.version; var packageJsonPath = path.join(argv.path, 'package.json'); var packageJson = require(packageJsonPath); if (!(packageJson.peerDependencies instanceof Object)) { packageJson.peerDependencies = {}; } packageJson.peerDependencies[results.packageName] = value; fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(packageJson, null, 2), { encoding: 'utf8' }); cb(); }], installNonPeer: ['installPeer', function (results, cb) { if (argv.singleton) { cb(); return; } var value = results.isRepositoryUrl ? results.repositoryUrl + '#' + results.version : '^' + results.version; var packageJsonPath = path.join(argv.path, 'package.json'); var packageJson = require(packageJsonPath); if (!(packageJson.dependencies instanceof Object)) { packageJson.dependencies = {}; } packageJson.dependencies[results.packageName] = value; fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(packageJson, null, 2), { encoding: 'utf8' }); cb(); }], installDev: ['installNonPeer', function (results, cb) { if (!argv.singleton) { cb(); return; } var value = results.isRepositoryUrl ? results.repositoryUrl + '#' + results.version : '^' + results.version; var packageJsonPath = path.join(argv.path, 'package.json'); var packageJson = require(packageJsonPath); if (!(packageJson.devDependencies instanceof Object)) { packageJson.devDependencies = {}; } packageJson.devDependencies[results.packageName] = value; fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(packageJson, null, 2), { encoding: 'utf8' }); cb(); }], installRollupExternal: ['installDev', function (results, cb) { if (!argv.singleton) { cb(); return; } var declinationJsonPath = path.join(argv.path, 'declination.json'); var declination = JSON.parse(fs.readFileSync(declinationJsonPath, { encoding: 'utf8' })); if (declination.external.indexOf(results.packageName) === -1) { declination.external.push(results.packageName); fs.writeFileSync(declinationJsonPath, (0, _stringify2.default)(declination, null, 2), { encoding: 'utf8' }); } cb(); }], installRollupGlobal: ['installRollupExternal', function (results, cb) { if (!argv.singleton) { cb(); return; } var declinationJsonPath = path.join(argv.path, 'declination.json'); var declination = JSON.parse(fs.readFileSync(declinationJsonPath, { encoding: 'utf8' })); if (!declination.globals[results.packageName]) { declination.globals[results.packageName] = globalName || results.packageName; fs.writeFileSync(declinationJsonPath, (0, _stringify2.default)(declination, null, 2), { encoding: 'utf8' }); } cb(); }] }, done); } exports.command = ['install <name> [names..]', 'add']; exports.desc = 'If the `--singleton` switch is on, it will prepare your declination.json and configure the dependency as `external` and `global`, \nIt will also add it to `peerDependencies` and `devDependencies` in your `package.json`.\n \nIf the `--singleton` switch is off (default), it will only add it to `dependencies` in your `package.json`.\n\nNote: \n- External will keep the dependency outside of our package and will require to be provided by your user.\n- Global will make your dependency available in a global scope so it can be accessed by external libraries.\n\nIf you want to have a different global name you can use `<name>*<globalName>` (ex: `npx rollup-umd-scripts peer add react*React --singleton`)'; exports.builder = function (yargs) { return yargs.option('path', { alias: 'p', describe: 'path', default: process.cwd() }).option('singleton', { alias: 's', describe: 'Singleton will be saved as peerDependencies, otherwise we keep it as a dependency of the module', default: false }); }; exports.handler = function (argv) { switch (argv.path[0]) { case '/': break; default: argv.path = argv.path[1] === '/' ? path.join(process.cwd(), argv.path.slice(2)) : path.join(process.cwd(), argv.path); break; } async.map([].concat(argv.name).concat(argv.names), function (name, cb) { return install(name, argv, cb); }, function (err, results) { if (err) { console.error('[ERROR] ' + err.message); process.exit(1); } }); };