UNPKG

@o3r/core

Version:
83 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.askQuestionsToGetRulesOrThrowIfPackageNotAvailable = void 0; const prompt_1 = require("@angular/cli/src/utilities/prompt"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); /** * Ask questions to get rules to execute * or throw if the package is not installed * @param path file path * @param optionName name of the option to setup * @param defaultApplyRule should the rule be applied by default * @param ruleQuestion the question to ask * @param schematicsNameToUpdate list of schematics to update * @param packageName package name of the schematic to execute * @param schematicName schematic name to execute * @param schematicOptions options of the schematic to execute */ const askQuestionsToGetRulesOrThrowIfPackageNotAvailable = (path, optionName, defaultApplyRule, ruleQuestion, schematicsNameToUpdate, packageName, schematicName, schematicOptions = {}) => { let applyRule = defaultApplyRule; let alwaysApplyRule = null; return async (tree, context) => { const packageJson = tree.readJson('./package.json'); if (!packageJson.dependencies?.[packageName] && !packageJson.devDependencies?.[packageName]) { if (applyRule) { throw new schematics_2.O3rCliError(` You need to install '${packageName}' to be able to generate component with the option ${optionName}. Please run 'ng add ${packageName}'. `); } } else { if (typeof applyRule !== 'boolean' && context.interactive) { applyRule = await (0, prompt_1.askConfirmation)(ruleQuestion, true); if (applyRule) { alwaysApplyRule = await (0, prompt_1.askQuestion)(`Generate future components with ${optionName} by default?`, [ { name: 'Yes, always', value: 'yes' }, { name: 'Ask me again next time', value: 'ask-again' }, { name: `No, don't apply ${optionName} by default`, value: 'no' } ], 0, 'yes'); } else { context.logger.info(` You can add it later to this component via this command: ng g ${packageName}:${schematicName} --path="${path}" `); } } } const options = { ...schematicOptions, path }; return applyRule ? (0, schematics_1.chain)([ packageName === '@o3r/core' ? (0, schematics_1.schematic)(schematicName, options) : (0, schematics_1.externalSchematic)(packageName, schematicName, options), ...(alwaysApplyRule === 'ask-again' ? [] : [ (0, schematics_2.setupSchematicsParamsForProject)(schematicsNameToUpdate.reduce((acc, schematicToUpdateName) => { acc[schematicToUpdateName] = { [optionName]: alwaysApplyRule === 'yes' }; return acc; }, {}), options.projectName) ]) ]) : schematics_1.noop; }; }; exports.askQuestionsToGetRulesOrThrowIfPackageNotAvailable = askQuestionsToGetRulesOrThrowIfPackageNotAvailable; //# sourceMappingURL=common.js.map