Skip to content

Commit

Permalink
fix: init
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Dec 14, 2023
1 parent 5805338 commit 66c2fb7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
36 changes: 20 additions & 16 deletions packages/scripts/env-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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<void>((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 })
}
}
}
41 changes: 26 additions & 15 deletions packages/scripts/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/is-initialized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 66c2fb7

Please sign in to comment.