diff --git a/package.json b/package.json index f84663e..b9dc6e6 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "scripts": { "watch": "yarn tsc -d -w -p tsconfig.json", "cli": "ts-node ./src/bin/fster.ts", - "prepublish": "yarn rimraf ./config.db && yarn generate && yarn migrate && yarn build", + "prepublish": "dotenv -- yarn rimraf ./config.db && yarn generate && yarn migrate && yarn build", "build": "yarn generate && yarn tsc -d -p tsconfig.json", "postinstall": "yarn generate", "generate": "yarn prisma generate", diff --git a/prisma/migrations/20210122142525_fix_name/migration.sql b/prisma/migrations/20210122142525_fix_name/migration.sql new file mode 100644 index 0000000..7a7c2b2 --- /dev/null +++ b/prisma/migrations/20210122142525_fix_name/migration.sql @@ -0,0 +1,22 @@ +/* + Warnings: + + - You are about to drop the column `templatesDisplayFolder` on the `Settings` table. All the data in the column will be lost. + +*/ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Settings" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "packageManager" TEXT NOT NULL DEFAULT 'yarn', + "editor" TEXT NOT NULL DEFAULT 'code', + "displayFolders" BOOLEAN NOT NULL DEFAULT false, + "userId" INTEGER NOT NULL, + FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_Settings" ("id", "packageManager", "editor", "userId") SELECT "id", "packageManager", "editor", "userId" FROM "Settings"; +DROP TABLE "Settings"; +ALTER TABLE "new_Settings" RENAME TO "Settings"; +CREATE UNIQUE INDEX "Settings_userId_unique" ON "Settings"("userId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..963afcc --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,2 @@ +# Please do not edit this file manually +provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8215ef4..ad15d5d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -3,12 +3,11 @@ datasource db { provider = "sqlite" - url = "file:../config.db" + url = env("DATABASE_URL") } generator client { provider = "prisma-client-js" - output = "../client" } model Project { @@ -35,5 +34,5 @@ model Settings { user User packageManager String @default("yarn") editor String @default("code") - templatesDisplayFolder Boolean @default(false) + displayFolders Boolean @default(false) } \ No newline at end of file diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 08c6f30..e7bcd64 100644 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -13,17 +13,11 @@ import { getCurrentUser } from "../lib/getUser"; import { fetchContent, GithubDownloader } from "../lib/github-download"; import logger, { spinner } from "../lib/logger"; import { READMD_REGEX } from "../lib/markdown"; -import { client, Settings } from "../lib/prisma"; +import { checkSetup, client, Settings } from "../lib/prisma"; import { sync } from "../lib/sync"; import { updater } from "../lib/update"; import ci from 'ci-info' -const homedir = os.homedir(); -const configDir = fs.dir(path.join(homedir, ".config", "fster")); -const configDB = configDir.path("config.db"); -if (!fs.exists(configDB)) { - fs.copy(path.join(__dirname, "..", "..", "config.db"), configDB); -} inquirer.registerPrompt( "autocomplete", @@ -56,7 +50,10 @@ const pkg = fs.read( path.join(__dirname, "..", "..", "package.json"), "json" ) as Partial; + + async function run() { + await checkSetup() if (!ci.isCI && pkg.version && pkg.name && !pkg.version.includes('next')) { const updated = await updater({ name: pkg.name, version: pkg.version }); if (updated) { @@ -109,7 +106,7 @@ async function run() { let synced = false let projects = await client.project.findMany(); if(projects.length <= 0 || cli.flags.sync){ - projects = await logger.run('Synchronizing Projects', sync(currentUser)) ?? [] + projects = await logger.run('Synchronizing Projects', sync(currentUser), 'Synchronized') ?? [] synced = true } // const argOutputDir = cli.input[0]; @@ -130,7 +127,7 @@ async function run() { const parentDir = path.basename(p.path) const parentParentDir = path.basename(path.join(p.path, '..')) const projectPath = `${parentParentDir}/${parentDir}` - if(currentUser.settings?.templatesDisplayFolder){ + if(currentUser.settings?.displayFolders){ return `${p.name}${` `.repeat(maxPLength - (p.name?.length || 0))} ${chalk.dim(projectPath)}` ?? "None" } return `${p.name}` ?? "None" @@ -138,7 +135,7 @@ async function run() { } }, ]); - const selectedRepo = currentUser.settings?.templatesDisplayFolder ? repo.split(' ')[0] : repo + const selectedRepo = currentUser.settings?.displayFolders ? repo.split(' ')[0] : repo const project = projects.find((p) => p.name === selectedRepo); if (currentUser?.settings?.editor && project?.path) { if (fs.exists(project.path)) { @@ -289,7 +286,7 @@ async function run() { const settings: Partial = { packageManager: currentUser.settings?.packageManager ?? "yarn", editor: currentUser.settings?.editor ?? "code", - templatesDisplayFolder: currentUser.settings?.templatesDisplayFolder ?? false, + displayFolders: currentUser.settings?.displayFolders ?? false, }; printSettings(currentUser.settings); // const argOutputDir = cli.input[0]; @@ -309,7 +306,7 @@ async function run() { update: { editor: answers.editor, packageManager: answers.packageManager, - templatesDisplayFolder: answers.templatesDisplayFolder + displayFolders: answers.displayFolders }, }, }, diff --git a/src/lib/getUser.ts b/src/lib/getUser.ts index d8bab8e..1a38508 100644 --- a/src/lib/getUser.ts +++ b/src/lib/getUser.ts @@ -11,7 +11,9 @@ export async function getCurrentUser() { if(!usr){ usr = await client.user.create({ data: { - name, email, + name, email, settings : { + create: {} + } }, include: { projects: true, settings: true} }) diff --git a/src/lib/logger.ts b/src/lib/logger.ts index 7f23433..5f7f7c6 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -27,13 +27,13 @@ export function error(message: any, ...optionalParams: any[]) { export const spinner = (action: string) => new CLI.Spinner(action, ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷']) -export async function run(action: string, callback: Promise) { +export async function run(action: string, callback: Promise, onComplete?: string) { let countdown = new CLI.Spinner(action, ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷']) try { countdown.start() const value = await callback countdown.stop(); - success(action) + success(onComplete ?? action) return value } catch (reason){ countdown.stop(); diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index a88e804..cfcac04 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -2,17 +2,33 @@ import path from "path"; import { PrismaClient } from "../../client"; import fs from "fs-jetpack"; import os from "os"; +import logger from "./logger"; + const homedir = os.homedir(); const configDir = fs.dir(path.join(homedir, '.config', 'fster')) const configDB = configDir.path('config.db') -if(!fs.exists(configDB)){ - fs.copy(path.join(__dirname, '..', '..', 'config.db'), configDB) +const baseDBPath = path.join(__dirname, '..', '..', 'config.db') +export async function checkSetup(){ + if(!fs.exists(configDB)){ + logger.warn('No Local DB trying to copy base') + const spin = logger.spinner(`copying base from ${baseDBPath} -> ${configDB}`) + spin.start() + try { + fs.copy(baseDBPath, configDB) + } catch (err) { + spin.stop() + logger.error(err) + process.exit(1) + } + spin.message("Copied Based Successfully") + spin.stop() + } } - -process.env.DATABASE_URL = `file:${configDB}` - - export const client = new PrismaClient({ - // log:['error', 'info', 'warn', 'query'] + datasources: { + db: { + url: `file:${configDB}`, + } + } }); export * from '../../client' \ No newline at end of file