Skip to content

Commit 1e3b21d

Browse files
committed
Change local config to js or ts files to support env variables
1 parent bb2adc9 commit 1e3b21d

7 files changed

Lines changed: 126 additions & 33 deletions

File tree

cli/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env node
22

3+
import "dotenv/config"
4+
35
import { createProgram } from "commandstruct"
46
import { Database } from "~/core/database"
57
import { dbCmd } from "./db"

core/entrypoint.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { glob } from "glob"
22
import { createJiti } from "jiti"
33
import { Database } from "monarch-orm"
44
import path from "node:path"
5-
import { localProjectSchema, projectSchema, type Project } from "./models"
5+
import { projectSchema, type Project } from "./models/project"
66

77
export type DirTree = { [k: string]: string | DirTree }
88

@@ -12,11 +12,19 @@ export class Entrypoint {
1212
public async getProject(
1313
projects: Record<string, Project> | undefined,
1414
): Promise<Project | undefined> {
15-
const configPath = path.resolve("monarch.config.json")
16-
const config = await this.import(configPath).catch(() => undefined)
17-
if (config) return localProjectSchema.safeParse(config).data
18-
const project = projects?.[process.cwd()]
19-
if (project) return projectSchema.safeParse(project).data
15+
const localProject = await this.import(path.resolve("monarch.config"), {
16+
default: true,
17+
}).catch(() => undefined)
18+
const project = localProject ?? projects?.[process.cwd()]
19+
const parsed = projectSchema.safeParse(project).data
20+
// @ts-expect-error local is an internal option
21+
if (parsed && localProject) parsed.local = true
22+
return parsed
23+
}
24+
25+
public isLocalProject(project: Project) {
26+
// @ts-expect-error local is an internal option
27+
return !!project.local
2028
}
2129

2230
public async getDatabaseExport(relpath: string, identifier: string) {

core/models.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

core/models/project.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z, ZodType } from "zod"
2+
3+
export interface Project {
4+
file: string
5+
export: string
6+
dbname: string
7+
dbs: Record<string, string>
8+
}
9+
10+
export const projectSchema = z.object({
11+
file: z.string(),
12+
export: z.string(),
13+
dbname: z.string(),
14+
dbs: z.record(z.string()),
15+
}) satisfies ZodType<Project>

lib/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Project } from "~/core/models/project"
2+
3+
type ConfigOptions = {
4+
file: string
5+
export: string
6+
dbname?: string
7+
dburl: string
8+
}
9+
10+
export function defineConfig(config: ConfigOptions): Project {
11+
const dbname = config.dbname ?? "Local"
12+
return {
13+
file: config.file,
14+
export: config.export,
15+
dbname,
16+
dbs: { [dbname]: config.dburl },
17+
}
18+
}
19+
20+
export type { Project }

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22
"name": "monarch-kit",
33
"version": "0.1.0",
44
"description": "Development kit for Monarch ORM",
5+
"private": false,
56
"type": "module",
67
"bin": {
78
"monarch": "./dist/index.js"
89
},
10+
"exports": {
11+
"./config": {
12+
"types": "./dist/config.d.ts",
13+
"default": "./dist/config.js"
14+
}
15+
},
916
"scripts": {
1017
"cli:build": "tsup cli/index.ts --clean --format esm",
1118
"cli:dev": "tsx cli/index.ts",
19+
"lib:build": "tsup lib/config.ts --dts --format esm",
1220
"ui:build": "react-router build",
1321
"ui:dev": "react-router dev --port 6543",
1422
"ui:preview": "react-router-serve ./dist/ui/server/index.js",
15-
"build": "npm run cli:build && npm run ui:build",
23+
"build": "npm run cli:build && npm run lib:build && npm run ui:build",
1624
"start": "node dist/index.js",
1725
"release": "npm run build && changeset publish",
1826
"check": "npm run lint && npm run format && react-router typegen && tsc",
@@ -47,6 +55,7 @@
4755
"dependencies": {
4856
"@clack/prompts": "^0.10.0",
4957
"@radix-ui/react-checkbox": "^1.1.4",
58+
"@radix-ui/react-collapsible": "^1.1.3",
5059
"@radix-ui/react-dialog": "^1.1.6",
5160
"@radix-ui/react-dropdown-menu": "^2.1.6",
5261
"@radix-ui/react-menubar": "^1.1.6",
@@ -56,11 +65,13 @@
5665
"@react-router/express": "^7.2.0",
5766
"@react-router/node": "^7.2.0",
5867
"@react-router/serve": "^7.2.0",
68+
"@tanstack/react-table": "^8.21.2",
5969
"class-variance-authority": "^0.7.1",
6070
"clsx": "^2.1.1",
6171
"cmdk": "1.0.0",
6272
"commandstruct": "^0.3.1",
6373
"compression": "^1.8.0",
74+
"dotenv": "^16.4.7",
6475
"express": "^4.21.2",
6576
"glob": "^11.0.1",
6677
"hollywood-di": "^0.6.1",

pnpm-lock.yaml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)