@tsclean/scaffold
Version:
This CLI creates an initial structure of a project based on clean architecture.
66 lines (65 loc) • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityCreateCommand = void 0;
const ora_1 = __importDefault(require("ora"));
const CommandUtils_1 = require("./CommandUtils");
const helpers_1 = require("../utils/helpers");
const messages_1 = require("../utils/messages");
const emojis_1 = require("../utils/emojis");
class EntityCreateCommand {
constructor() {
this.command = "create:entity";
this.describe = "Generates a new entity.";
}
builder(args) {
return args
.option("n", {
alias: "name",
describe: "Name of the entity type",
demand: true
});
}
async handler(args) {
let spinner;
try {
const fileContent = EntityCreateCommand.getTemplate(args.name);
const basePath = `${process.cwd()}/src/domain/entities/`;
const filename = `${args.name}.ts`;
const path = basePath + filename;
const fileExists = await CommandUtils_1.CommandUtils.fileExists(path);
setTimeout(() => (spinner = (0, ora_1.default)('Installing...').start()), 1000);
if (fileExists)
throw messages_1.MESSAGES.FILE_EXISTS(path);
await CommandUtils_1.CommandUtils.createFile(path, fileContent);
setTimeout(() => {
spinner.succeed("Installation completed");
spinner.stopAndPersist({
symbol: emojis_1.EMOJIS.ROCKET,
text: messages_1.MESSAGES.FILE_SUCCESS('Entity', path)
});
}, 1000 * 5);
}
catch (error) {
setTimeout(() => (spinner.fail("Installation fail"), (0, helpers_1.errorMessage)(error, 'entity')), 2000);
}
}
/**
* Gets content of the entity file
*
* @param param
* @returns
*/
static getTemplate(param) {
const name = CommandUtils_1.CommandUtils.capitalizeString(param);
return `export type ${name}Entity = {
// Attributes
}
export type Add${name}Params = Omit<${name}Entity, 'id'>
`;
}
}
exports.EntityCreateCommand = EntityCreateCommand;
//# sourceMappingURL=CommandCreateEntity.js.map