UNPKG

@tsclean/scaffold

Version:

This CLI creates an initial structure of a project based on clean architecture.

151 lines 9.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InitCommand = void 0; const ora_1 = __importDefault(require("ora")); const path = __importStar(require("path")); const child_process_1 = require("child_process"); const emojis_1 = require("../utils/emojis"); const messages_1 = require("../utils/messages"); const CommandUtils_1 = require("./CommandUtils"); const helpers_1 = require("../utils/helpers"); const ProjectInitTemplate_1 = require("../templates/ProjectInitTemplate"); const paths_1 = require("../utils/paths"); class InitCommand { constructor() { this.command = "create:project"; this.describe = "Generate initial Clean Architecture project structure."; } builder(args) { return args.option("n", { alias: "name", describe: "Name of project directory", demandOption: true }); } async handler(args) { let spinner; try { const basePath = process.cwd() + (args.name ? "/" + args.name : ""); const projectName = args.name ? path.basename(args.name) : undefined; const fileExists = await CommandUtils_1.CommandUtils.fileExists(basePath); if (!fileExists) (0, helpers_1.structureInitialProject)(args.name); setTimeout(async () => { spinner = (0, ora_1.default)(`Installing... ${emojis_1.EMOJIS.COFFEE}`).start(); }, 2000); if (fileExists) throw messages_1.MESSAGES.PROJECT_EXISTS(basePath); await CommandUtils_1.CommandUtils.createFile(basePath + "/.dockerignore", ProjectInitTemplate_1.ProjectInitTemplate.getDockerIgnore()); await CommandUtils_1.CommandUtils.createFile(basePath + "/.env", ProjectInitTemplate_1.ProjectInitTemplate.getEnvExampleTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/.env.example", ProjectInitTemplate_1.ProjectInitTemplate.getEnvExampleTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/.gitignore", ProjectInitTemplate_1.ProjectInitTemplate.getGitIgnoreFile()); await CommandUtils_1.CommandUtils.createFile(basePath + "/docker-compose.yml", ProjectInitTemplate_1.ProjectInitTemplate.getDockerCompose()); await CommandUtils_1.CommandUtils.createFile(basePath + "/package.json", ProjectInitTemplate_1.ProjectInitTemplate.getPackageJsonTemplate(projectName), false); await CommandUtils_1.CommandUtils.createFile(basePath + "/README.md", ProjectInitTemplate_1.ProjectInitTemplate.getReadmeTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/tsconfig.json", ProjectInitTemplate_1.ProjectInitTemplate.getTsConfigTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/tsconfig-build.json", ProjectInitTemplate_1.ProjectInitTemplate.getTsConfigBuildTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/application/config/environment.ts", ProjectInitTemplate_1.ProjectInitTemplate.getEnvironmentTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/application/app.ts", ProjectInitTemplate_1.ProjectInitTemplate.getAppTemplate()); await CommandUtils_1.CommandUtils.createFile(paths_1.PATHS.PATH_SINGLETON(basePath), ProjectInitTemplate_1.ProjectInitTemplate.getSingleton()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getIndexTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/deployment/Dockerfile", ProjectInitTemplate_1.ProjectInitTemplate.getDockerfileTemplate()); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/domain/entities"); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/domain/use-cases/impl"); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/infrastructure/driven-adapters/adapters"); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/infrastructure/driven-adapters/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getDrivenAdaptersIndex()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/infrastructure/driven-adapters/adapters/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getAdaptersIndex()); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/infrastructure/driven-adapters/providers"); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/infrastructure/driven-adapters/providers/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getProvidersTemplate()); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/infrastructure/entry-points/api"); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/src/infrastructure/entry-points/helpers"); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/infrastructure/entry-points/api/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getIndexApiTemplate()); await CommandUtils_1.CommandUtils.createFile(basePath + "/src/infrastructure/entry-points/index.ts", ProjectInitTemplate_1.ProjectInitTemplate.getEntryPointsTemplate()); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/tests/domain"); await CommandUtils_1.CommandUtils.createDirectories(basePath + "/tests/infrastructure"); const packageJsonContents = await CommandUtils_1.CommandUtils.readFile(basePath + "/package.json"); await CommandUtils_1.CommandUtils.createFile(basePath + "/package.json", ProjectInitTemplate_1.ProjectInitTemplate.appendPackageJson(packageJsonContents)); if (args.name) { setTimeout(() => { spinner.stopAndPersist({ symbol: emojis_1.EMOJIS.ROCKET, text: messages_1.MESSAGES.PROJECT_SUCCESS(basePath) }); console.log(""); console.log("👉 Get started with the following commands:"); console.log(""); console.log(`$ cd ${args.name}`); console.log(`$ npm run watch`); console.log(""); spinner.text = `We continue to work for you... ${emojis_1.EMOJIS.COFFEE}`; spinner.start(); const installProcess = (0, child_process_1.exec)("npm install", { cwd: basePath }); installProcess.on("exit", (code) => { if (code === 0) { spinner.succeed("Installation completed"); } else { spinner.fail("Installation failed"); } }); }, 5000); } await InitCommand.executeCommand("npm install", basePath); } catch (error) { setTimeout(() => (spinner.fail("Installation fail"), (0, helpers_1.errorMessage)(error, "project")), 3000); } } static executeCommand(command, basePath) { return new Promise((resolve, reject) => { (0, child_process_1.exec)(`cd ${basePath} && ${command}`, (error, stdout, stderr) => { if (stdout) return resolve(stdout); if (stderr) return reject(stderr); if (error) return reject(error); resolve(""); }); }); } } exports.InitCommand = InitCommand; //# sourceMappingURL=CommandInit.js.map