From 53b450f2b034df294744727a7c73e68a3488a42f Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Wed, 20 Mar 2024 23:48:56 -0700 Subject: [PATCH] feat: cmds add for op and nonop templates --- package.json | 1 + src/commands/project.commands.ts | 74 +++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 4ba1063..7fbd2a2 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "husky": "^8.0.3", "prettier": "^2.8.4", "release-it": "^16.1.5", + "rimraf": "^5.0.5", "shx": "^0.3.4", "ts-node-dev": "^2.0.0", "typescript": "^4.9.5", diff --git a/src/commands/project.commands.ts b/src/commands/project.commands.ts index 7d61d60..c580743 100644 --- a/src/commands/project.commands.ts +++ b/src/commands/project.commands.ts @@ -2,7 +2,30 @@ import { spawn } from "child_process"; import { promises as fs } from "fs"; import path from "path"; import { Argv, CommandModule } from "yargs"; -// TODO: To validate on non opinionated template +import Compiler from "../utils/compiler"; + +/** + * Load the configuration from the compiler + * @param compiler The compiler to load the configuration from + * @returns The configuration + */ + +const opinionatedConfig: Array = [ + "--transpile-only", + "-r", + "dotenv/config", + "-r", + "tsconfig-paths/register", + "./src/main.ts", +]; + +const nonOpinionatedConfig: Array = [ + "--transpile-only", + "-r", + "dotenv/config", + "./src/main.ts", +]; + /** * Helper function to execute a command * @param command The command to execute @@ -44,11 +67,17 @@ const compileTypescript = async () => { // Helper to copy files const copyFiles = async () => { - const filesToCopy = [ - "./register-path.js", - "tsconfig.build.json", - "package.json", - ]; + const { opinionated } = await Compiler.loadConfig(); + let filesToCopy: Array = []; + if (opinionated) { + filesToCopy = [ + "./register-path.js", + "tsconfig.build.json", + "package.json", + ]; + } else { + filesToCopy = ["tsconfig.json", "package.json"]; + } filesToCopy.forEach((file) => { fs.copyFile(file, path.join("./dist", path.basename(file))); }); @@ -74,18 +103,15 @@ export const runCommandModule: CommandModule<{}, { command: string }> = { }; const runCommand = async ({ command }: { command: string }): Promise => { + const { opinionated } = await Compiler.loadConfig(); try { switch (command) { case "dev": // Use execSync or spawn to run ts-node-dev programmatically - execCmd("tsnd", [ - "--transpile-only", - "-r", - "dotenv/config", - "-r", - "tsconfig-paths/register", - "./src/main.ts", - ]); + execCmd( + "tsnd", + opinionated ? opinionatedConfig : nonOpinionatedConfig, + ); break; case "build": await cleanDist(); @@ -93,14 +119,20 @@ const runCommand = async ({ command }: { command: string }): Promise => { await copyFiles(); break; case "prod": + let config: Array = []; + if (opinionated) { + config = [ + "-r", + "dotenv/config", + "-r", + "./dist/register-path.js", + "./dist/src/main.js", + ]; + } else { + config = ["-r", "dotenv/config", "./dist/main.js"]; + } // Ensure environment variables are set - execCmd("node", [ - "-r", - "dotenv/config", - "-r", - "./dist/register-path.js", - "./dist/src/main.js", - ]); + execCmd("node", config); break; default: console.log(`Unknown command: ${command}`);