UNPKG

rollup-umd-scripts

Version:

CLI for rollup-umd projects

91 lines (82 loc) 3.23 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, no-underscore-dangle, prefer-destructuring */ var async = require('async'); var path = require('path'); var fs = require('fs'); var pkg = require(path.join(__dirname, '../../../../package.json')); exports.command = 'declinations'; exports.desc = 'Generate declination documentation into `docs/declinations`.'; exports.builder = function (yargs) { return yargs.option('path', { alias: 'p', describe: 'path', default: process.cwd() }); }; exports.handler = function (argv) { var defPath = argv.path; argv.path = defPath || process.cwd(); 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 dirPath = path.join(argv.path, 'docs/declinations'); async.auto({ clean: function clean(cb) { return spawn('rm -rf ' + dirPath, cb); }, mkdir: ['clean', function (results, cb) { return spawn('mkdir -p ' + dirPath, cb); }], list: ['mkdir', function (results, cb) { return spawn('npx ' + pkg.name + ' declination list -p ' + argv.path + ' -t numbered-dashed', cb); }], createDoc: ['list', function (results, cb) { var declinationList = results.list[0].split('\n').filter(function (f) { return !!f; }) // remove empty // eslint-disable-next-line no-restricted-globals .filter(function (f) { return f.split('-')[0].length > 0 && !isNaN(f.split('-')[0]); }); // remove non numbered (npx fix) declinationList.forEach(function (d) { var script = fs.readFileSync(path.join(__dirname, '../../../../internals/declination', d), { encoding: 'utf8' }); var doc = '```bash\n' + script + '\n```'; fs.writeFileSync(path.join(dirPath, d + '.md'), doc, { encoding: 'utf8' }); }); var configPath = path.join(argv.path, 'styleguide/styleguide.ext.json'); var config = require(configPath); var pos = void 0; var page = void 0; config.sections.filter(function (p, i) { if (p.name === 'Declinations') { pos = i; page = p; } return p; }); if (!pos || !page) { throw new Error('You must have { name: \'Declinations:\' } existing.'); } page.sections = declinationList.map(function (d) { return { name: d, content: 'docs/declinations/' + d + '.md' }; }); var newSections = config.sections.slice(0, pos).concat(page).concat(config.sections.slice(pos + 1)); config.sections = newSections; fs.writeFileSync(configPath, (0, _stringify2.default)(config, null, 2), { encoding: 'utf8' }); cb(); }] }, function (err, res) { if (err) { console.error('[ERROR] ' + err.message); process.exit(1); } console.log('[Success] Declination documentation generated.'); }); };