UNPKG

rollup-umd-scripts

Version:

CLI for rollup-umd projects

274 lines (260 loc) 10.4 kB
'use strict'; var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* eslint-disable */ var path = require('path'); var fs = require('fs'); var async = require('async'); var state = require(path.join(__dirname, 'status')); exports.command = 'set <privilege>'; exports.desc = 'Set publishing configuration'; exports.builder = function (yargs) { return yargs.positional('privilege', { default: 'private', describe: 'Switch the publish target to:\nPrivate: GitLab\nProtected: GitLab and private npm\nPublic: GitHub and public npm\nYou can only increase the privilege level.', choices: ['private', 'protected', 'public'] }).option('registry', { alias: 'r', describe: 'Registry to be used', default: 'https://npm.kopaxgroup.com', hidden: true }).option('doc-installation-path', { alias: 'd', describe: 'The relative path to your installation.md', default: 'docs/installation.md' }).option('path', { alias: 'p', describe: 'The project path', default: process.cwd() }); }; function configureProtected(argv, callback) { var packageJsonPath = path.join(argv.path, 'package.json'); async.auto({ isPrivate: function isPrivate(cb) { state.isPrivate(argv, function (err, isPrivate) { if (!isPrivate) { var isState = state.isTransitive(argv) ? 'transitive' : 'public'; isState = state.isProtected(argv) ? 'protected' : isState; cb(new Error('Publishing is already an higher privilege: ' + isState)); return; } cb(); }); }, enableNpmPublish: ['isPrivate', function (results, cb) { var pkg = require(packageJsonPath); /** we turn to false the pkg private that prevent accidental publishing */ pkg.private = false; // to switch public after that, you will need to have this key /** we tell @semantic-release/npm to release the package */ pkg.release.npmPublish = true; /** we set the repository url */ pkg.publishConfig = pkg.publish ? (0, _extends3.default)({}, pkg.publish, { registry: argv.registry }) : { registry: argv.registry, tag: 'latest' }; fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(pkg, null, 2), { encoding: 'utf8' }); cb(); }], updateInstallDoc: ['enableNpmPublish', function (res, cb) { if (!fs.existsSync(argv.d)) { console.log('[Warning] File ' + argv.d + ' was not found.\n[Warning] The \'npm install\' command has not been updated with the npm registry one.'); cb(); return; } var placeholder = '$PACKAGE_NAME --registry ' + argv.registry + ' --save'; sedReplace(argv.d, 'git+$CI_REPOSITORY_URL --save', placeholder, argv.d, cb); }] }, callback); } function configurePublic(argv, callback) { var packageJsonPath = path.join(argv.path, 'package.json'); async.auto({ isPublic: function isPublic(cb) { state.isPublic(argv, function (err, isPublic) { if (isPublic) { cb(new Error('Publishing as already public.')); return; } cb(); }); }, isTransitiveCI: ['isPublic', function (results, cb) { state.isTransitive(argv, function (err, isTransitive) { var pkg = require(packageJsonPath); if (isTransitive && !process.env.CI) { cb(new Error('You are in transitive state, upon next release, it will be deployed to GitHub')); return; } else if (!isTransitive && process.env.CI) { // should fail silently until it's transitive state process.exit(0); } else if (!isTransitive && !process.env.CI) { /** This will set the transitive state by removing the key private */ delete pkg.private; fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(pkg, null, 2), { encoding: 'utf8' }); console.log('Package will be deployed publicly upon next release.\nIf you also want to update the LICENSE.md, use: npx rollup-umd-scripts license set --help'); process.exit(0); } // we are in CI if (process.env.CI_COMMIT_REF_NAME !== pkg.release.branch) { cb(new Error('Public publishing failed because you are not on the release branch "' + pkg.release.branch + '" (current branch: ' + process.env.CI_COMMIT_REF_NAME + ')')); return; } cb(null, true); }); }], isProtected: ['isTransitiveCI', function (results, cb) { state.isProtected(argv, function (err, isProtected) { if (isProtected) { cb(); return; } configureProtected(argv, cb); }); }], configureSemantic: ['isProtected', function (results, cb) { var pkg = require(packageJsonPath); /** we unset the default registry */ delete pkg.publishConfig.registry; delete pkg.release.npmPublish; /** we delete the success and fail because github don't need it */ delete pkg.release.success; delete pkg.release.fail; /** we replace gitlab by github in verifyConditions */ var _pkg$release = pkg.release, verifyConditions = _pkg$release.verifyConditions, prepare = _pkg$release.prepare, publish = _pkg$release.publish; pkg.release.verifyConditions = verifyConditions.map(function (cond) { if (typeof cond === 'string') { if (cond === '"@semantic-release/gitlab"') { return '@semantic-release/github'; } return cond; } if (cond.path === '"@semantic-release/gitlab"') { return '@semantic-release/github'; } return cond; }); var githubPublishCfg = { path: '@semantic-release/github', assets: [{ path: 'package.json' }, { path: 'LICENSE.md' }, { path: 'CHANGELOG.md' }, { path: 'README.md' }, { path: 'dist/*.es.js', label: '1 file ES6' }, { path: 'dist/*.es.js.map', label: '1 file ES6 sourcemap' }, { path: 'dist/*.min.js', label: '1 file UMD compressed' }, { path: 'dist/*.min.js.map', label: '1 file UMD compressed sourcemap' }, { path: 'dist/*.js', label: '1 file UMD uncompressed' }, { path: 'dist/*.js.map', label: '1 file UMD uncompressed sourcemap' }, { path: 'lib/*.js', label: 'source files ES5' }] }; pkg.prepare.publish = publish.map(function (cond) { if (typeof cond === 'string') { if (cond === '@semantic-release/gitlab') { return githubPublishCfg; } return cond; } if (cond.path === '@semantic-release/gitlab') { return githubPublishCfg; } return cond; }); fs.writeFileSync(packageJsonPath, (0, _stringify2.default)(pkg, null, 2), { encoding: 'utf8' }); cb(); }], installDependencies: ['configureSemantic', function (res, next) { async.series([function (cb) { return spawn('npm uninstall --prefix ' + argv.path + ' @semantic-release/gitlab --save-dev', cb); }, function (cb) { return spawn('npm install --prefix ' + argv.path + ' @semantic-release/git --save-dev', cb); }, function (cb) { return spawn('npm install --prefix ' + argv.path + ' @semantic-release/github --save-dev', cb); }], next); }], // installTravisCI: ['installDependencies', (res, callback) => { // // const travisCiSource = path.join(process.cwd(), ''); // const travisCiDest = path.join(argv.path, '.travis.yml'); // async.series([ // (cb) => exec(`git -C ${argv.path} pull --tags`, cb), // (cb) => spawn(`git -C ${argv.path} git show REVISION: >${travisCiDest}`, cb), // ], callback); // }], updateInstallDoc: ['installDependencies', function (res, cb) { if (!fs.existsSync(argv.d)) { console.log('[Warning] File ' + argv.d + ' was not found. The npm install command has not been updated with the npm registry one.'); cb(); return; } var file = fs.readFileSync(argv.d, { encoding: 'utf8' }); var re = new RegExp(/( --registry (?:https?):\/\/[\w.-]+)? (?:--save-dev|--save)/, 'gm'); var matches = re.exec(file); /** this will remove the registry parameter */ var newFile = '' + file.slice(0, matches.index) + file.slice(matches.index + matches[1].length); fs.writeFileSync(argv.d, newFile, { encoding: 'utf8' }); cb(); }] }, callback); } 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; } // /* we switch the relative to absolute for the rest of our program */ argv['doc-installation-path'] = argv.d = path.join(argv.path, argv.d); // eslint-disable-line no-multi-assign switch (argv.privilege) { case 'protected': configureProtected(argv, function (err) { if (err) { console.error('[ERROR] ' + err.message); process.exit(1); } console.log('[Success] Publishing configuration is now protected.\nThe release will break if you haven\'t configured your $NPM_TOKEN on your CI environment for ' + argv.registry + ' registry.'); }); break; case 'public': configurePublic(argv, function (err) { if (err) { console.error('[ERROR] ' + err.message); process.exit(1); } console.log('[Success] Publishing configuration is now public.'); }); break; default: state.isPrivate(argv, function (err, results) { if (!results.isPrivate) { console.error('[ERROR] You can\'t downgrade publishing'); process.exit(1); } console.log('[Info] publishing is private by default.'); }); break; } };