UNPKG

create-dolittle-microservice

Version:

In Norse mythology, the Vanir are a group of gods associated with health, fertility, wisdom, and the ability to see the future.

96 lines 4.6 kB
"use strict"; // Copyright (c) Dolittle. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const packageRoot_1 = __importDefault(require("./packageRoot")); const rudiments_1 = require("@dolittle/rudiments"); const edit_json_file_1 = __importDefault(require("edit-json-file")); const vanir_cli_1 = require("@dolittle/vanir-cli"); const templatesRootPath = path_1.default.join(packageRoot_1.default, 'templates', 'backend'); const webTemplatesRootPath = path_1.default.join(packageRoot_1.default, 'templates', 'web'); const portalTemplatesRootPath = path_1.default.join(packageRoot_1.default, 'templates', 'portal'); const packageJson = require(path_1.default.join(packageRoot_1.default, 'package.json')); const toPascalCase = function (input) { return input.replace(/(\w)(\w*)/g, function (g0, g1, g2) { return g1.toUpperCase() + g2.toLowerCase(); }); }; function appendMicroserviceToApplication(answers, config, plop) { const targetDirectory = answers.targetDirectory || process.cwd(); const applicationFile = path_1.default.join(targetDirectory, 'application.json'); const application = edit_json_file_1.default(applicationFile, { stringify_width: 4 }); const microservices = application.get('microservices'); if (microservices.some(_ => _ === answers.path)) { throw new Error('A microservice with the given name is already added to the application.json file.'); } microservices.push(answers.path); application.set('microservices', microservices); application.save(); return 'Added microservice to application.json file'; } function getActionForTemplateDirectory(templateDirectory, targetDirectory) { return { type: 'addMany', base: templateDirectory, destination: targetDirectory, force: true, templateFiles: [ vanir_cli_1.PathHelper.useUnixPathSeparator(templateDirectory), vanir_cli_1.PathHelper.useUnixPathSeparator(path_1.default.join(templateDirectory, '.*/**/*')) ], stripExtensions: ['hbs'] }; } function default_1(plop) { plop.setGenerator('microservice', { description: 'Creates a new Dolittle Microservice', prompts: [{ type: 'input', name: 'name', message: 'Name of the microservice:', validate: (input) => input && input.length > 0 }, { type: 'confirm', name: 'ui', message: 'Will the microservice have a UI?' }], actions: (answers) => { const actions = []; answers.id = answers.id || rudiments_1.Guid.create().toString(); answers.name = toPascalCase(answers.name); answers.path = `./Source/${answers.name}`; answers.vanirVersion = packageJson.version; const targetDirectory = answers.targetDirectory || process.cwd(); actions.push(appendMicroserviceToApplication); const applicationFile = path_1.default.join(targetDirectory, 'application.json'); const application = require(applicationFile); answers.applicationId = application.id; answers.applicationName = application.name; answers.tenant = application.tenant; answers.license = application.license; if (typeof answers.hasUIPrefix === 'undefined' || answers.hasUIPrefix === true) { answers.uiPath = `/_/${answers.name.toLowerCase()}`; answers.uiPrefix = `/_/${answers.name.toLowerCase()}`; answers.portal = false; } else { answers.uiPath = '/'; answers.uiPrefix = ''; answers.portal = true; } actions.push(getActionForTemplateDirectory(templatesRootPath, targetDirectory)); if (answers.ui) { actions.push(getActionForTemplateDirectory(webTemplatesRootPath, targetDirectory)); if (answers.portal) { actions.push(getActionForTemplateDirectory(portalTemplatesRootPath, targetDirectory)); } } return actions; } }); } exports.default = default_1; ; //# sourceMappingURL=plopfile.js.map