Skip to content

Commit

Permalink
feat: lots of qol
Browse files Browse the repository at this point in the history
  • Loading branch information
William Luke committed Jan 22, 2021
1 parent 402fcc3 commit c6f665b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions prisma/migrations/20210122142525_fix_name/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Please do not edit this file manually
provider = "sqlite"
5 changes: 2 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,5 +34,5 @@ model Settings {
user User
packageManager String @default("yarn")
editor String @default("code")
templatesDisplayFolder Boolean @default(false)
displayFolders Boolean @default(false)
}
21 changes: 9 additions & 12 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -56,7 +50,10 @@ const pkg = fs.read(
path.join(__dirname, "..", "..", "package.json"),
"json"
) as Partial<PackageJson>;


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) {
Expand Down Expand Up @@ -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];
Expand All @@ -130,15 +127,15 @@ 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"
}, input)
}
},
]);
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)) {
Expand Down Expand Up @@ -289,7 +286,7 @@ async function run() {
const settings: Partial<Settings> = {
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];
Expand All @@ -309,7 +306,7 @@ async function run() {
update: {
editor: answers.editor,
packageManager: answers.packageManager,
templatesDisplayFolder: answers.templatesDisplayFolder
displayFolders: answers.displayFolders
},
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/lib/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export function error(message: any, ...optionalParams: any[]) {

export const spinner = (action: string) => new CLI.Spinner(action, ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'])

export async function run<T>(action: string, callback: Promise<T>) {
export async function run<T>(action: string, callback: Promise<T>, 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();
Expand Down
30 changes: 23 additions & 7 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit c6f665b

Please sign in to comment.