@mason-api/cli
Version:
CLI assistant for Mason builder
340 lines (266 loc) • 13.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handler = exports.desc = exports.command = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _safe = _interopRequireDefault(require("colors/safe"));
var _path = _interopRequireDefault(require("path"));
var _child_process = _interopRequireDefault(require("child_process"));
var _generator = require("@mason-api/generator");
var _login = require("./login");
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
var command = 'guide';
exports.command = command;
var desc = 'Guide through template generation';
exports.desc = desc;
var handler =
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(argv) {
var idToken, apiKey, userId, format, projects, projectId, project, componentId, component, page, params, outputDir, components, datasourceIds, datasources, embedded, chosenFormat, formatOptions, optionName, option, optionPrompt, optionValue, optionTrue, _optionValue, argList, args, _require, _spawn;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
console.log(_safe.default.bold('Welcome to Mason wizard!'));
console.log("It will guide you through a process to generate static templates\nout of your components to be used with a server-side application");
_context.next = 4;
return (0, _login.getIdToken)();
case 4:
idToken = _context.sent;
_context.next = 7;
return (0, _login.getAPIKey)(false);
case 7:
apiKey = _context.sent;
_context.next = 10;
return (0, _login.getUserId)(false);
case 10:
userId = _context.sent;
console.log(_safe.default.bold('\nWhich format would you like to use?'));
_context.next = 14;
return (0, _utils.promptChoice)('Format: ', 'format name', {
html: 'Static HTML',
ejs: 'EJS (JavaScript)',
pug: 'PUG (JavaScript)',
php: 'PHP native templating (PHP)',
twig: 'TWIG (PHP)',
erb: 'ERB (Ruby)',
haml: 'HAML (Ruby)',
handlebars: 'Handlebars',
mustache: 'Mustache'
});
case 14:
format = _context.sent;
console.log(_safe.default.bold('\nWhich project would you like to work with?'));
_context.next = 18;
return (0, _utils.fetchProjects)(idToken, argv.verbose);
case 18:
projects = _context.sent;
_context.next = 21;
return (0, _utils.promptChoice)('Project: ', 'project id', _lodash.default.mapValues(_lodash.default.keyBy(_lodash.default.filter(projects, function (p) {
return !p.isArchived && _lodash.default.findIndex(p.members, ['id', userId]) > -1;
}), 'id'), function (project) {
return project.name;
}));
case 21:
projectId = _context.sent;
project = _lodash.default.find(projects, ['id', projectId]);
console.log(_safe.default.bold("\nWhich ".concat(_safe.default.italic('published'), " component would you like to generate templates for?")));
_context.next = 26;
return (0, _utils.promptChoice)('Component: ', 'component id', _lodash.default.mapValues(_lodash.default.keyBy(project.publishedComponents, 'id'), function (component) {
return component.name;
}), 'Leave blank to generate templates for all components');
case 26:
_context.t0 = _context.sent;
if (_context.t0) {
_context.next = 29;
break;
}
_context.t0 = '*';
case 29:
componentId = _context.t0;
component = _lodash.default.find(project.components, ['id', componentId]);
page = '';
if (!component) {
_context.next = 44;
break;
}
if (!(_lodash.default.keys(_lodash.default.get(component, 'config.data', []).length) > 1)) {
_context.next = 43;
break;
}
console.log(_safe.default.bold("\nWhich ".concat(_safe.default.italic('published'), " component would you like to generate templates for?")));
_context.next = 37;
return (0, _utils.promptChoice)('Page: ', 'page name', _lodash.default.mapValues(_lodash.default.keyBy(_lodash.default.keys(component.config.data)), _lodash.default.startCase), 'Leave blank to generate templates for all pages');
case 37:
_context.t1 = _context.sent;
if (_context.t1) {
_context.next = 40;
break;
}
_context.t1 = '*';
case 40:
page = _context.t1;
_context.next = 44;
break;
case 43:
page = 'default';
case 44:
params = []; // if (componentId === '*' || page === '*') {
console.log(_safe.default.bold('\nProvide an output directory for the generated files '));
console.log(_safe.default.italic(_safe.default.gray('Leave blank to place files in the current directory')));
_context.t3 = _lodash.default;
_context.next = 50;
return (0, _utils.prompt)('Output: ');
case 50:
_context.t4 = _context.sent;
_context.t2 = _context.t3.trim.call(_context.t3, _context.t4);
if (_context.t2) {
_context.next = 54;
break;
}
_context.t2 = '.';
case 54:
outputDir = _context.t2;
params.push("-o ".concat(outputDir)); // }
if (!(format !== 'html')) {
_context.next = 106;
break;
}
console.log('\nWe\'re almost there!');
console.log(_safe.default.bold('Would you like to adjust settings specific to generated templates?'));
console.log(_safe.default.gray(_safe.default.italic('Example: setting prefix for data paths, embedding data into a template, toggling safe mode')));
_context.t5 = _lodash.default;
_context.t6 = _lodash.default;
_context.next = 64;
return (0, _utils.prompt)("".concat(_safe.default.bold('y'), "/n: "));
case 64:
_context.t7 = _context.sent;
_context.t8 = _context.t6.toLower.call(_context.t6, _context.t7);
if (_context.t5.startsWith.call(_context.t5, _context.t8, 'n')) {
_context.next = 106;
break;
}
components = component ? [component] : project.publishedComponents;
datasourceIds = [];
_lodash.default.forEach(components, function (comp) {
_lodash.default.forEach(_lodash.default.get(comp, 'config.data'), function (page, name) {
_lodash.default.forEach((0, _generator.getDatasourceIds)(comp.config, name), function (datasourceId) {
if (!_lodash.default.includes(datasourceIds, datasourceId)) {
datasourceIds.push(datasourceId);
}
});
});
});
datasources = _lodash.default.compact(_lodash.default.map(datasourceIds, function (id) {
return project.datasources[id];
}));
if (!datasources.length) {
_context.next = 84;
break;
}
console.log(_safe.default.bold('\nShould some of datasources be embedded into template directly?'));
console.log('Datasources in component turn into loops and variables in the template.');
console.log('Data for those variables needs to be provided when template is rendered.');
console.log('You can pick certain datasources to inline their data into template statically.');
console.log('Chosen templates use the following datasources:');
_context.next = 79;
return (0, _utils.promptChoice)('Page: ', 'page name', _lodash.default.mapValues(_lodash.default.keyBy(datasources, 'id'), function (datasource) {
return datasource.name || datasource.id;
}), 'Leave blank, choose one, or provide multiple choices separated with comma (e.g. 1,2)', true);
case 79:
_context.t9 = _context.sent;
if (_context.t9) {
_context.next = 82;
break;
}
_context.t9 = '';
case 82:
embedded = _context.t9;
if (embedded.length) {
params.push("--embed-data=".concat(_lodash.default.join(embedded, ' ')));
}
case 84:
chosenFormat = _generator.formats[format];
formatOptions = _lodash.default.get(chosenFormat, 'options', {}); // eslint-disable-next-line
_context.t10 = regeneratorRuntime.keys(formatOptions);
case 87:
if ((_context.t11 = _context.t10()).done) {
_context.next = 106;
break;
}
optionName = _context.t11.value;
option = formatOptions[optionName];
console.log(_safe.default.bold("\n".concat(option.description)));
if (option.before) {
console.log(_safe.default.gray('Before: '), option.before({}));
console.log(_safe.default.gray('After: '), option.after({}));
}
if (!(typeof option.defaultValue === 'boolean')) {
_context.next = 100;
break;
}
optionPrompt = option.defaultValue ? "".concat(_safe.default.bold('y'), "/n: ") : "y/".concat(_safe.default.bold('n'), ": ");
_context.next = 96;
return (0, _utils.prompt)(optionPrompt);
case 96:
optionValue = _context.sent;
if (optionValue) {
optionTrue = _lodash.default.startsWith(_lodash.default.toLower(optionValue), 'y');
if (option.defaultValue !== optionTrue) {
params.push("--".concat(_lodash.default.kebabCase(optionName), "=").concat(optionTrue));
}
}
_context.next = 104;
break;
case 100:
_context.next = 102;
return (0, _utils.prompt)("".concat(_lodash.default.startCase(optionName), ": "));
case 102:
_optionValue = _context.sent;
if (_optionValue) {
params.push("--".concat(_lodash.default.kebabCase(optionName), "=").concat(_optionValue));
}
case 104:
_context.next = 87;
break;
case 106:
params.push("-k ".concat(apiKey));
console.log(_safe.default.bold('\nGenerated command: '));
console.log(_safe.default.italic(_safe.default.gray('Use this command next time to avoid going through wizard again\nFor example you can use this as a part of your build process')));
argList = [format, projectId, componentId, page].concat(params);
args = _lodash.default.join(_lodash.default.compact(argList), ' ');
console.log(_safe.default.green("mason ".concat(_lodash.default.replace(args, '*', '\\*'))));
console.log(_safe.default.bold('\nExecute command now?'));
console.log(_safe.default.gray("It will output to ".concat(_safe.default.white(_path.default.resolve(__dirname, outputDir)), " directory.")));
_context.t12 = _lodash.default;
_context.t13 = _lodash.default;
_context.next = 118;
return (0, _utils.prompt)("".concat(_safe.default.bold('y'), "/n: "));
case 118:
_context.t14 = _context.sent;
_context.t15 = _context.t13.trim.call(_context.t13, _context.t14);
if (_context.t12.startsWith.call(_context.t12, _context.t15, 'n')) {
_context.next = 123;
break;
}
_require = require('child_process'), _spawn = _require.spawn;
yargs.parse("generate ".concat(args));
case 123:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function handler(_x) {
return _ref.apply(this, arguments);
};
}();
exports.handler = handler;