rollup-umd-scripts
Version:
CLI for rollup-umd projects
105 lines (96 loc) • 4.09 kB
JavaScript
;
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-unused-vars, no-unused-expressions */
var async = require('async');
var path = require('path');
var fs = require('fs');
var rimraf = require('rimraf');
var pkg = require(path.join(__dirname, '../../../package.json'));
var p = path.join(__dirname, '../../../internals/declination');
var files = fs.existsSync(p) ? fs.readdirSync(p) : [];
var declinationList = files.filter(function (f) {
return f !== 'create';
}).map(function (file) {
return '' + file.split('-').slice(1).join('-');
});
exports.command = ['create <name> [declination]', '*'];
exports.desc = 'Create new rollup';
exports.builder = function (yargs) {
return yargs.positional('declination', {
describe: 'Choose a declination',
default: '',
choices: [''].concat(declinationList)
}).option('token', {
alias: 't',
describe: 'Use a personal access token for the git clone'
}).option('repository', {
alias: 'r',
describe: 'Set the rollup-umd repository url',
default: 'ssh://git@module.kopaxgroup.com:20024/dev-tools/rollup-umd.git'
}).commandDir('declination_cmds');
};
exports.handler = function (argv) {
var pkgPath = path.join(argv.name, 'package.json');
fs.existsSync(path.join(argv.name, 'package.json')) ? console.log('[ERROR] Cannot create, already exists') || process.exit(1) : null;
var declination = argv.declination.length > 0 ? '-' + argv.declination : '';
var _process$env = process.env,
CI = _process$env.CI,
GL_TOKEN = _process$env.GL_TOKEN,
GITLAB_TOKEN = _process$env.GITLAB_TOKEN;
var cloneUrl = argv.repository;
if (CI || argv.token) {
var token = GL_TOKEN || GITLAB_TOKEN || argv.token;
if (!token) {
throw Error('You must have GL_TOKEN or GITLAB_TOKEN set to use create.');
}
cloneUrl = pkg.rollupRepository.url.replace(/^(https?:\/\/)/g, '$1gitlab-ci-token:' + token + '@');
}
console.log('Please wait while project is being created...');
async.auto({
clone: function clone(cb) {
return spawn('git clone ' + cloneUrl + ' ' + argv.name, function (err, stdout) {
if (err) {
cb(new Error('Access is denied. Did you read https://dev-tools.yeutech.com/rollup-umd/#installation ?'));
return;
}
cb();
});
},
releaseVersion: ['clone', function (results, cb) {
spawn('git -C ' + argv.name + ' describe --tags', function (err, stdout) {
var txt = typeof stdout === 'string' ? stdout : stdout[0];
var lastVersion = txt.split('-')[0].replace(/\n/g, '');
cb(null, lastVersion);
});
}],
checkoutLatest: ['releaseVersion', function (results, cb) {
return spawn('git -C ' + argv.name + ' checkout ' + results.releaseVersion + (declination || ''), cb);
}],
install: ['checkoutLatest', function (results, cb) {
return spawn('npm install --prefix ' + argv.name, cb);
}],
appendDeclination: ['install', function (results, cb) {
var pkg = require(path.join(process.cwd(), argv.name, 'package.json')); // eslint-disable-line
pkg.declinationId = argv.declination; /** store just between now and initialize */
fs.writeFileSync(pkgPath, (0, _stringify2.default)(pkg, null, 2), { encoding: 'utf8' });
cb();
}],
remove: ['appendDeclination', function (results, cb) {
var gitLocation = path.join(argv.name, '.git');
var gitlabciPath = path.join(argv.name, '.gitlab-ci.yml');
var sonarProjectPath = path.join(argv.name, 'sonar-project.properties');
rimraf.sync(gitLocation);
rimraf.sync(gitlabciPath);
rimraf.sync(sonarProjectPath);
cb();
}]
}, function (err, results) {
if (err) {
console.error('[ERROR] ' + err.message);
process.exit(1);
}
console.log('[Success] Created rollup package named ' + argv.name);
});
};