UNPKG

@liferay/generator-js

Version:

Yeoman generators for Liferay DXP and Portal CE JavaScript projects.

107 lines (106 loc) 3.88 kB
"use strict"; /** * SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com> * SPDX-License-Identifier: LGPL-3.0-or-later */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const yargs_1 = require("yargs"); const yeoman_generator_1 = __importDefault(require("yeoman-generator")); const utils_1 = require("../utils"); // If --which parameter is given show path to generator and exit if (yargs_1.argv.which) { // eslint-disable-next-line no-console console.log(require.resolve('./index')); process.exit(0); } /** * Default main generator that makes the user choose between available targets. */ class default_1 extends yeoman_generator_1.default { /** * Standard Yeoman initialization function */ async initializing() { const targets = this._findTargets(); const answers = await utils_1.promptWithConfig(this, 'app', [ { choices: targets, message: 'What type of project do you want to create?', name: 'target', type: 'list', }, { default: 'my-project', message: 'What name shall I give to the folder hosting your project?', name: 'folder', type: 'input', }, ]); this.destinationRoot(path_1.default.resolve(answers['folder'])); this.composeWith(require.resolve(`../target-${answers['target']}`), undefined); } /** * Find existing target generators and return them as a prompt choices * object. * @return {Array} a prompt choices object */ _findTargets() { const tds = fs_extra_1.default .readdirSync(path_1.default.join(__dirname, '..')) .filter((file) => file.indexOf('target-') === 0) .map((target) => target.replace('target-', '')) .map((target) => ({ ...getTargetDescription(target), value: target, })); const categories = this._getTargetCategories(tds); const targets = []; categories.forEach((category, index) => { if (index > 0) { targets.push({ line: ' ', type: 'separator' }); } targets.push({ line: `-- ${category} --`, type: 'separator' }); targets.push(...tds .filter((td) => td.category === category) .sort(compareTargetDescriptionPriorities)); }); targets.push({ line: ' ', type: 'separator' }); return targets; } /** * Get target categories based on their descriptions. * @return {Array} an array containing the categories */ _getTargetCategories(tds) { const map = tds.reduce((map, td) => { map[td.category] = true; return map; }, {}); return Object.keys(map); } } exports.default = default_1; /** * Compare two target description priorities. * @param {string} ltd left target description to compare * @param {string} rtd right target description to compare * @return {int} the priority difference */ function compareTargetDescriptionPriorities(ltd, rtd) { return ltd.priority - rtd.priority; } /** * Get the description of a discovered target reading its * target-decription.json file. * @param {string} target target's technical name * @return {object} parsed JSON file */ function getTargetDescription(target) { return fs_extra_1.default.readJsonSync(path_1.default.join(__dirname, '..', `target-${target}`, 'target-description.json')); } module.exports = exports['default'];