@cuba-platform/front-generator
Version:
CUBA Platform front-end clients generator
133 lines • 5.8 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SdkModelGenerator = exports.SdkAllGenerator = void 0;
const base_generator_1 = require("../../common/base-generator");
const path = require("path");
const studio_integration_1 = require("../../common/studio/studio-integration");
const entities_generation_1 = require("./model/entities-generation");
const services_generation_1 = require("./services/services-generation");
const queries_generation_1 = require("./services/queries-generation");
var RunMode;
(function (RunMode) {
RunMode["ALL"] = "ALL";
RunMode["MODEL"] = "MODEL";
})(RunMode || (RunMode = {}));
/**
* Yeoman generator for SDK.
* Note, yeoman run all methods, declared in class - https://yeoman.io/authoring/#adding-your-own-functionality
*/
class SdkGenerator extends base_generator_1.BaseGenerator {
constructor(args, options, runMode) {
super(args, options);
this.runMode = runMode;
this.sourceRoot(path.join(__dirname, 'template'));
}
prompting() {
return __awaiter(this, void 0, void 0, function* () {
if (this.options.model) {
this.conflicter.force = true;
this.log('Skipping prompts since model provided');
this.cubaProjectModel = this._readProjectModel();
return;
}
const openedCubaProjects = yield (0, studio_integration_1.getOpenedCubaProjects)();
if (!openedCubaProjects || openedCubaProjects.length < 1) {
this.env.error(Error(studio_integration_1.ERR_STUDIO_NOT_CONNECTED));
return;
}
this.answers = (yield this.prompt([{
name: 'projectInfo',
type: 'list',
message: 'Please select CUBA project you want to use for generation',
choices: openedCubaProjects && openedCubaProjects.map(p => ({
name: `${p.name} [${p.path}]`,
value: p
}))
}]));
});
}
// noinspection JSUnusedGlobalSymbols
prepareModel() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.cubaProjectModel && this.answers) {
this.modelFilePath = path.join(process.cwd(), 'projectModel.json');
yield (0, studio_integration_1.exportProjectModel)(this.answers.projectInfo.locationHash, this.modelFilePath);
this.cubaProjectModel = this._readProjectModel();
}
});
}
writing() {
if (this.cubaProjectModel) {
const { restQueries, restServices } = this.cubaProjectModel;
const runMode = this.runMode.toString().toLowerCase();
this.log(`Generating sdk:${runMode} to ${this.destinationPath()}`);
const ctx = (0, entities_generation_1.generateEntities)(this.cubaProjectModel, path.join(this.destinationRoot()), this.fs);
if (this.runMode == RunMode.ALL) {
const services = (0, services_generation_1.generateServices)(restServices, ctx);
this.fs.write(this.destinationPath('services.ts'), services);
const queries = (0, queries_generation_1.generateQueries)(restQueries, ctx);
this.fs.write(this.destinationPath('queries.ts'), queries);
}
}
else {
this.env.error(Error('Skip sdk generation - no project model provided'));
}
}
end() {
this.log(`SDK been successfully generated into ${this.destinationRoot()}`);
}
}
class SdkGeneratorRunner extends SdkGenerator {
/**
yeoman run all class methods - https://yeoman.io/authoring/#adding-your-own-functionality,
but not from parent, so we need method to start generation
process from inheritor classes
NOTE that all new added methods in SdkGenerator should be added and run here
*/
generate() {
return __awaiter(this, void 0, void 0, function* () {
yield this.prompting();
yield this.prepareModel();
this.writing();
this.end();
});
}
}
class SdkAllGenerator extends SdkGeneratorRunner {
constructor(args, options) {
super(args, options, RunMode.ALL);
}
generate() {
const _super = Object.create(null, {
generate: { get: () => super.generate }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.generate.call(this);
});
}
}
exports.SdkAllGenerator = SdkAllGenerator;
class SdkModelGenerator extends SdkGeneratorRunner {
constructor(args, options) {
super(args, options, RunMode.MODEL);
}
generate() {
const _super = Object.create(null, {
generate: { get: () => super.generate }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.generate.call(this);
});
}
}
exports.SdkModelGenerator = SdkModelGenerator;
//# sourceMappingURL=sdk-generator.js.map