From d4fbc8b3cfce99fc084f27b2f365758302526720 Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Sat, 3 Aug 2024 22:47:12 -0700 Subject: [PATCH] fix: broken tsconfig deps --- src/commands/project.commands.ts | 45 +++++++++++++++++++++++++------- src/help/form.ts | 5 ++-- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/commands/project.commands.ts b/src/commands/project.commands.ts index cddd365..500d109 100644 --- a/src/commands/project.commands.ts +++ b/src/commands/project.commands.ts @@ -1,5 +1,5 @@ import { spawn } from "child_process"; -import { promises as fs, readFileSync } from "fs"; +import { promises as fs, readFileSync, existsSync, mkdirSync } from "fs"; import os from "os"; import path, { join } from "path"; import { CommandModule } from "yargs"; @@ -7,11 +7,37 @@ import { printError, printSuccess } from "../utils/cli-ui"; import Compiler from "../utils/compiler"; /** - * Load tsconfig path and extract outDir + * Helper function to load and extract outDir from tsconfig.build.json */ -const tsconfigBuildPath = join(process.cwd(), "tsconfig.build.json"); -const tsconfig = JSON.parse(readFileSync(tsconfigBuildPath, "utf-8")); -const outDir = tsconfig.compilerOptions.outDir; +function getOutDir(): string { + const tsconfigBuildPath = join(process.cwd(), "tsconfig.build.json"); + + if (!existsSync(tsconfigBuildPath)) { + printError( + "Cannot find tsconfig.build.json. Please create one in the root directory", + "tsconfig-build-path", + ); + process.exit(1); + } + + const tsconfig = JSON.parse(readFileSync(tsconfigBuildPath, "utf-8")); + const outDir = tsconfig.compilerOptions.outDir; + + if (!outDir) { + printError( + "Cannot find outDir in tsconfig.build.json. Please provide an outDir.", + "tsconfig-build-path", + ); + process.exit(1); + } + + if (!existsSync(outDir)) { + mkdirSync(outDir, { recursive: true }); + printSuccess(`Created outDir: ${outDir}`, "outdir-creation"); + } + + return outDir; +} /** * Load the configuration from the compiler @@ -107,7 +133,7 @@ function execCmd( /** * Helper function to clean the dist directory */ -const cleanDist = async (): Promise => { +const cleanDist = async (outDir: string): Promise => { await fs.rm(outDir, { recursive: true, force: true }); printSuccess(`Clean ${outDir} directory`, "clean-dist"); }; @@ -123,7 +149,7 @@ const compileTypescript = async () => { /** * Helper function to copy files to the dist directory */ -const copyFiles = async () => { +const copyFiles = async (outDir: string) => { const { opinionated } = await Compiler.loadConfig(); let filesToCopy: Array = []; if (opinionated) { @@ -159,6 +185,7 @@ export const runCommand = async ({ command: string; }): Promise => { const { opinionated } = await Compiler.loadConfig(); + const outDir = getOutDir(); try { switch (command) { @@ -176,9 +203,9 @@ export const runCommand = async ({ ); process.exit(1); } - await cleanDist(); + await cleanDist(outDir); await compileTypescript(); - await copyFiles(); + await copyFiles(outDir); break; case "prod": { if (!outDir) { diff --git a/src/help/form.ts b/src/help/form.ts index be900e0..4fae5a8 100644 --- a/src/help/form.ts +++ b/src/help/form.ts @@ -25,8 +25,9 @@ const helpForm = async (): Promise => { ["usecase", "g u", "Generate a usecase"], ["dto", "g d", "Generate a dto"], ["entity", "g e", "Generate an entity"], - ["provider", "add", "Add provider to the project"], - ["provider", "create", "Create a provider"], + ["provider", "g p", "Generate internal provider"], + ["provider", "add", "Add external provider to the project"], + ["provider", "create", "Create external provider"], ["module", "g mo", "Generate a module"], ["middleware", "g mi", "Generate a middleware"], );