UNPKG

@mongez/gnz

Version:

Generator Z, the next generation of scaffolding tools.

41 lines (40 loc) 2.86 kB
'use strict';var fs=require('@mongez/fs'),chalk=require('chalk'),path=require('path'),namesFactory=require('../../factories/names-factory.js'),index=require('../generate-mongodb-migration/index.js');require('os'),require('eslint'),require('prettier');var throwIf=require('../../utils/throw-if.js'),template=require('./template.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var chalk__default=/*#__PURE__*/_interopDefault(chalk);var path__default=/*#__PURE__*/_interopDefault(path);const generate = async (options) => { options.collection = namesFactory.namesFactory.modelTableName(options.collection); options.className ||= namesFactory.namesFactory.modelClassName(options.collection); options.fileName ||= namesFactory.namesFactory.modelFilePath(options.collection); const modelFilePath = options.fileName + ".ts"; fs.ensureDirectory(options.saveTo); throwIf.throwIf(fs.fileExists(path__default.default.join(options.saveTo, modelFilePath)), `Model file ${modelFilePath} already exists`); console.log(`Generating ${chalk__default.default.cyan(options.className)} model...`); const modelContent = await template.generateModelContent(options); if (options.withMigration) { const savePath = options.withIndex ? options.saveTo + "/" + namesFactory.namesFactory.modelFolderPath(options.collection) : options.saveTo; await index.generateMongoDBMigration.generate({ modelClass: options.className, modelPath: options.fileName, saveTo: savePath + "/migrations", name: options.className, geo: options.geo, index: options.index, text: options.text, unique: options.unique, }); } // first we need to check if model will be in a directory // then we need to check if the directory exists if (options.withIndex) { const directoryName = namesFactory.namesFactory.modelFolderPath(options.collection); fs.ensureDirectory(path__default.default.join(options.saveTo, directoryName)); throwIf.throwIf(fs.fileExists(path__default.default.join(options.saveTo, directoryName, modelFilePath)), `Model file ${modelFilePath} already exists in ${directoryName} directory`); const indexContent = await template.generateModelIndexContent(options); fs.putFile(path__default.default.join(options.saveTo, directoryName, "index.ts"), indexContent); options.fileName = directoryName + "/" + options.fileName; } fs.putFile(path__default.default.join(options.saveTo, options.fileName + ".ts"), modelContent); console.log(`${chalk__default.default.green(options.className)} model has been generated successfully`); return { ...options, }; };exports.generate=generate;//# sourceMappingURL=generator.js.map