rollup-umd-scripts
Version:
CLI for rollup-umd projects
90 lines (76 loc) • 2.68 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-param-reassign, global-require, no-unused-vars, no-console, no-underscore-dangle, prefer-destructuring */
var path = require('path');
var fs = require('fs');
exports.command = ['add-section', 'section'];
exports.desc = 'Add a section to section/styleguide.ext.json';
exports.builder = function (yargs) {
return yargs.option('name', {
alias: 'n',
describe: 'name of the section'
}).option('content', {
alias: 'c',
describe: 'location of the markdown file'
}).option('before', {
alias: 'b',
describe: 'append before the section with name'
}).option('after', {
alias: 'a',
describe: 'append after the section with name'
}).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 configPath = path.join(argv.path, 'styleguide/styleguide.ext.json');
var config = require(configPath);
var before = argv.before,
after = argv.after,
name = argv.name,
content = argv.content;
var section = {
name: name,
content: content
};
if (!fs.existsSync(path.join(argv.path, content))) {
throw new Error('File cannot be found at location ' + path.join(argv.path, content) + '.');
}
config.sections.forEach(function (s) {
if (s.name.toLowerCase() === name.toLowerCase()) {
throw new Error('Section with name ' + name + ' already exist!');
}
});
if (before && after) {
throw new Error('You cannot use --before and --after together.');
}
var pos = void 0;
var compare = before || after;
config.sections.forEach(function (s, i) {
if (s.name.toLowerCase() === compare.toLowerCase()) {
pos = i;
}
});
if ((before || after) && !pos) {
throw Error('Section with name "' + name + '" can\'t be found!');
}
if (before) {
config.sections = config.sections.slice(0, pos).concat([section]).concat(config.sections.slice(pos));
} else if (after) {
config.sections = config.sections.slice(0, pos + 1).concat([section]).concat(config.sections.slice(pos + 1));
} else {
config.sections.push(section);
}
fs.writeFileSync(configPath, (0, _stringify2.default)(config, null, 2), { encoding: 'utf8' });
};