-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathinit.ts
More file actions
59 lines (51 loc) · 1.77 KB
/
Copy pathinit.ts
File metadata and controls
59 lines (51 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Context } from "./get-project-context"
import path from "path"
import * as fs from "fs"
import pgstrapPackage from "../package.json"
import kyselyTypesTemplate from "./templates/kysely-types.template"
import { mkdirpSync } from "mkdirp"
import getDbClientTemplate from "./templates/get-db-client.template"
export const initPgstrap = async (ctx: Pick<Context, "cwd">) => {
const { cwd } = ctx
const pkg = JSON.parse(
fs.readFileSync(path.join(cwd, "package.json")).toString(),
)
if (!pkg.scripts) pkg.scripts = {}
pkg.scripts["db:migrate"] = "pgstrap migrate"
pkg.scripts["db:reset"] = "pgstrap reset"
pkg.scripts["db:generate"] = "pgstrap generate --pglite"
pkg.scripts["db:create-migration"] = "pgstrap create-migration"
if (!pkg.devDependencies) pkg.devDependencies = {}
if (!pkg.devDependencies["pgstrap"]) {
pkg.devDependencies["pgstrap"] =
process.env.PGSTRAP_VERSION ?? pgstrapPackage.version
}
if (!fs.existsSync(path.join(cwd, "pgstrap.config.js"))) {
fs.writeFileSync(
path.join(cwd, "pgstrap.config.js"),
`module.exports = ${JSON.stringify(
{
defaultDatabase: "my_service_name",
schemas: ["public"],
},
null,
2,
)}`,
)
}
if (!fs.existsSync(path.join(cwd, "src", "db", "kysely-types.ts"))) {
mkdirpSync(path.join(cwd, "src", "db"))
fs.writeFileSync(
path.join(cwd, "src", "db", "kysely-types.ts"),
kyselyTypesTemplate,
)
}
if (!fs.existsSync(path.join(cwd, "src", "db", "get-db-client.ts"))) {
mkdirpSync(path.join(cwd, "src", "db"))
fs.writeFileSync(
path.join(cwd, "src", "db", "get-db-client.ts"),
getDbClientTemplate,
)
}
fs.writeFileSync(path.join(cwd, "package.json"), JSON.stringify(pkg, null, 2))
}