From 66c2fb74bed8372a75c5149a17a4e061f37cc487 Mon Sep 17 00:00:00 2001 From: rharkor Date: Thu, 14 Dec 2023 20:12:50 +0100 Subject: [PATCH] fix: init --- packages/scripts/env-setup.ts | 36 ++++++++++++++------------ packages/scripts/init.ts | 41 +++++++++++++++++++----------- packages/scripts/is-initialized.ts | 2 +- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/packages/scripts/env-setup.ts b/packages/scripts/env-setup.ts index f66f20d7..5ab14dab 100644 --- a/packages/scripts/env-setup.ts +++ b/packages/scripts/env-setup.ts @@ -3,7 +3,7 @@ */ import chalk from "chalk" -import { exec } from "child_process" +import { execSync } from "child_process" import * as fs from "fs" import * as path from "path" import * as url from "url" @@ -16,13 +16,13 @@ export const envSetup = async (initDb: boolean = true) => { const appEnvPath = path.join(rootDir, "packages", "app", ".env") if (!fs.existsSync(appEnvPath) && fs.existsSync(path.join(rootDir, "packages", "app", ".env.example"))) { fs.copyFileSync(path.join(rootDir, "packages", "app", ".env.example"), appEnvPath) - console.log(chalk.green("Created .env for app")) + console.log(chalk.gray("Created .env for app")) } else console.log(chalk.gray("Skipping .env for app")) // Landing const landingEnvPath = path.join(rootDir, "packages", "landing", ".env") if (!fs.existsSync(landingEnvPath) && fs.existsSync(path.join(rootDir, "packages", "landing", ".env.example"))) { fs.copyFileSync(path.join(rootDir, "packages", "landing", ".env.example"), landingEnvPath) - console.log(chalk.green("Created .env for landing")) + console.log(chalk.gray("Created .env for landing")) } else console.log(chalk.gray("Skipping .env for landing")) if (initDb) { @@ -32,19 +32,23 @@ export const envSetup = async (initDb: boolean = true) => { if (!fs.existsSync(appPath)) { console.log(chalk.gray("Skipping database initialization (no app folder)")) } else { - await new Promise((resolve, reject) => { - exec("npx prisma migrate dev && npm run seed", { cwd: appPath }, (err, stdout, stderr) => { - if (err) { - console.log(chalk.red(err.message)) - reject(err) - } - if (stderr) { - console.log(chalk.red(stderr)) - reject(stderr) - } - resolve() - }) - }) + //? Prisma command exists + try { + console.log(chalk.gray("Checking if prisma is installed...")) + execSync("npx prisma --version", { cwd: appPath }) + } catch { + console.log(chalk.gray("Prisma is not installed")) + console.log(chalk.gray("Installing prisma...")) + execSync("npm i -g prisma", { cwd: appPath }) + } + + //? Prisma migrate dev + console.log(chalk.gray("Migrating the database...")) + execSync("prisma migrate dev", { cwd: appPath }) + + //? Prisma seed + console.log(chalk.gray("Seeding the database...")) + execSync("prisma db seed", { cwd: appPath }) } } } diff --git a/packages/scripts/init.ts b/packages/scripts/init.ts index e7e375ec..fb37280e 100644 --- a/packages/scripts/init.ts +++ b/packages/scripts/init.ts @@ -17,24 +17,35 @@ const rootPath = path.join(__dirname, "..") config() async function main() { - const alreadyInitialized = await fs.access(path.join(rootPath, "scripts", ".init-todo")).catch(() => false) - if (alreadyInitialized) { - console.log(chalk.red("Project already initialized!")) - exit(1) + const alreadyInitialized = await fs + .access(path.join(rootPath, "scripts", ".init-todo")) + .then(() => false) + .catch(() => true) + + if (!alreadyInitialized) { + console.log(chalk.green("Welcome to the init script!")) + console.log(chalk.blue('Starting the "replace tokens" script...')) + await replaceTokens() + console.log(chalk.green("Done!")) + } else { + console.log(chalk.yellow("Skipping replaceTokens()")) } - console.log(chalk.green("Welcome to the init script!")) - console.log(chalk.blue('Starting the "replace tokens" script...')) - await replaceTokens() - console.log(chalk.green("Done!")) - - console.log(chalk.blue('Starting the "runtime" script...')) - await runtime() - console.log(chalk.green("Done!")) + if (!alreadyInitialized) { + console.log(chalk.blue('Starting the "runtime" script...')) + await runtime() + console.log(chalk.green("Done!")) + } else { + console.log(chalk.gray("Skipping runtime()")) + } - console.log(chalk.blue('Starting the "packages selection" script...')) - await packagesSelection() - console.log(chalk.green("Done!")) + if (!alreadyInitialized) { + console.log(chalk.blue('Starting the "packages selection" script...')) + await packagesSelection() + console.log(chalk.green("Done!")) + } else { + console.log(chalk.gray("Skipping packagesSelection()")) + } console.log(chalk.blue('Starting the "env setup" script...')) await envSetup() diff --git a/packages/scripts/is-initialized.ts b/packages/scripts/is-initialized.ts index 632d7397..898454d8 100644 --- a/packages/scripts/is-initialized.ts +++ b/packages/scripts/is-initialized.ts @@ -14,10 +14,10 @@ if (process.env.SKIP_INIT_CHECK === "true") process.exit(0) const main = async () => { try { + await fs.access(path.join(rootPath, "scripts", ".init-todo")) const projectInfo = await fs.readFile(path.join(rootPath, "scripts", ".pinfo.json"), "utf8") const projectInfoJson = JSON.parse(projectInfo) as { runtime: IRuntime } const currentRuntime = projectInfoJson.runtime - await fs.access(path.join(rootPath, "scripts", ".init-todo")) console.log(chalk.red("Project not initialized!")) console.log(chalk.yellow(`Run \`${currentRuntime.npm} run init\` to initialize the project`)) process.exit(1)