create-dolittle-microservice
Version:
[](https://github.com/dolittle-entropy/vanir/actions/workflows/build.yml) [](https://www.nuget.org/pack
96 lines • 4.7 kB
JavaScript
;
// 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 fs_1 = __importDefault(require("fs"));
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 Config_1 = require("./Config");
const Globals_1 = require("./Globals");
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: vanir_cli_1.PathHelper.useUnixPathSeparator(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 templatesRootPath = path_1.default.join(Config_1.Config.templatesRootPath, 'backend');
const webTemplatesRootPath = path_1.default.join(Config_1.Config.templatesRootPath, 'web');
const portalTemplatesRootPath = path_1.default.join(Config_1.Config.templatesRootPath, 'portal');
const actions = [];
answers.id = answers.id || rudiments_1.Guid.create().toString();
answers.name = toPascalCase(answers.name);
answers.path = `./Source/${answers.name}`;
answers.vanirVersion = Globals_1.Globals.version;
const targetDirectory = answers.targetDirectory || process.cwd();
actions.push(appendMicroserviceToApplication);
const applicationFile = path_1.default.join(targetDirectory, 'application.json');
const application = JSON.parse(fs_1.default.readFileSync(applicationFile).toString());
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