@tsclean/scaffold
Version:
This CLI creates an initial structure of a project based on clean architecture.
85 lines (82 loc) • 3.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceCreateCommand = void 0;
const ora_1 = __importDefault(require("ora"));
const emojis_1 = require("../utils/emojis");
const messages_1 = require("../utils/messages");
const CommandUtils_1 = require("./CommandUtils");
const helpers_1 = require("../utils/helpers");
class ServiceCreateCommand {
constructor() {
this.command = "create:service";
this.describe = "Generates a new service.";
}
builder(args) {
return args.option("n", {
alias: "name",
describe: "Name the Service class",
demandOption: true
});
}
async handler(args) {
let spinner;
try {
const fileContent = ServiceCreateCommand.getTemplateService(args.name);
const fileContentRepository = ServiceCreateCommand.getTemplateIServices(args.name);
const basePath = `${process.cwd()}/src/domain/use-cases/`;
const filename = `${args.name}-service-impl.ts`;
const path = `${basePath}impl/${filename}`;
const pathRepository = `${basePath + args.name}-service.ts`;
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(pathRepository, fileContentRepository);
await CommandUtils_1.CommandUtils.createFile(path, fileContent);
setTimeout(() => {
spinner.succeed("Installation completed");
spinner.stopAndPersist({
symbol: emojis_1.EMOJIS.ROCKET,
prefixText: messages_1.MESSAGES.REPOSITORY_SUCCESS(pathRepository),
text: messages_1.MESSAGES.FILE_SUCCESS("Services", path)
});
}, 1000 * 5);
}
catch (error) {
setTimeout(() => (spinner.fail("Installation fail"), (0, helpers_1.errorMessage)(error, "service")), 2000);
}
}
/**
* Get contents services files
* @param param
* @protected
*/
static getTemplateService(param) {
const name = CommandUtils_1.CommandUtils.capitalizeString(param);
return `import {Service} from "@tsclean/core";
import {I${name}Service} from "@/domain/use-cases/${param}-service";
@Service()
export class ${name}ServiceImpl implements I${name}Service {
constructor() {
}
}`;
}
/**
* Get contents interface files
* @param param
* @protected
*/
static getTemplateIServices(param) {
const name = CommandUtils_1.CommandUtils.capitalizeString(param);
const nameTransform = param.replace(/-/g, "_");
const nameRef = nameTransform.toUpperCase();
return `export const ${nameRef}_SERVICES = '${nameRef}_SERVICES';
export interface I${name}Service {
}`;
}
}
exports.ServiceCreateCommand = ServiceCreateCommand;
//# sourceMappingURL=CommandCreateService.js.map