rollup-umd-scripts
Version:
CLI for rollup-umd projects
87 lines (75 loc) • 3.19 kB
JavaScript
;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
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');
exports.command = ['set [copyright-holder]', 'update', 'change'];
exports.desc = 'Modify the project license';
exports.builder = function (yargs) {
return yargs.positional('copyright-holder', {
default: 'Yeutech Company Limited',
describe: 'Change the company or individual holder'
}).option('license', {
alias: 'l',
describe: 'Change the license, you can read more on this link: https://spdx.org/licenses',
default: null,
choices: ['UNLICENSED', 'MIT', 'Apache-2.0', 'BSD-3-Clause', 'GPL-3.0-only']
}).option('force', {
alias: 'f',
describe: 'Force the modification of the copyright holder',
default: false
}).option('path', {
alias: 'p',
describe: 'path',
default: process.cwd()
});
};
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;
}
var projectLicensePath = path.join(argv.path, 'LICENSE.md');
var projectPkgPath = path.join(argv.path, 'package.json');
var licensesPath = path.join(__dirname, '../../../../internals/licenses');
var year = void 0;
var holder = void 0;
if (fs.existsSync(projectLicensePath)) {
var re = /(?:\([cC]\) )?Copyright (?:\([Cc]\) )?([0-9]{4}) ([^\n]+?)(?=,? *all rights reserved|$)/gm;
var matches = re.exec(fs.readFileSync(projectLicensePath, { encoding: 'utf8' }));
if (!matches) {
console.error('There was an error retrieving date and copyright holder within the current LICENSE.md file.');
process.exit(1);
}
year = matches[1]; // eslint-disable-line prefer-destructuring
holder = !argv.force ? matches[2] : argv['copyright-holder'];
} else {
year = new Date().getFullYear();
holder = argv['copyright-holder'];
}
fs.unlinkSync(projectLicensePath);
fs.copyFileSync(licensesPath + '/' + argv.license, projectLicensePath);
async.series([function (cb) {
return sedReplace(projectLicensePath, '$YEAR', year, projectLicensePath, cb);
}, function (cb) {
return sedReplace(projectLicensePath, '$COPYRIGHT_HOLDER', holder, projectLicensePath, cb);
}, function (cb) {
var pkg = require(projectPkgPath);
fs.writeFile(projectPkgPath, (0, _stringify2.default)((0, _extends3.default)({}, pkg, { license: argv.license }), null, 2), cb);
}], function (err, res) {
if (err) {
console.error('There was an error ', err.message);
process.exit(1);
}
console.log(fs.readFileSync(projectLicensePath, { encoding: 'utf8' }));
console.log(argv.license, 'license installed!\n');
});
};