-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1fb045
commit 96e10d3
Showing
9 changed files
with
177 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Command } from "../deps.ts"; | ||
import { GlobalOpts, initProject } from "../common.ts"; | ||
import { cleanProject } from "../../project/project.ts"; | ||
|
||
export const cleanCommand = new Command<GlobalOpts>() | ||
.description("Removes all build artifacts") | ||
.action( | ||
async (opts) => { | ||
const project = await initProject(opts); | ||
await cleanProject(project); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
// Generates SQL migrations & generates client library. | ||
// Generates & deploys SQL migrations. See `deploy.ts` to only deploy migrations. | ||
// | ||
// Wrapper around `prisma migrate dev` | ||
|
||
import { copy, exists, resolve } from "../deps.ts"; | ||
import { buildPrismaPackage } from "./build_prisma_esm.ts"; | ||
import { assert, copy, exists, resolve } from "../deps.ts"; | ||
import { Module, Project } from "../project/mod.ts"; | ||
import { forEachPrismaSchema } from "./mod.ts"; | ||
|
||
|
@@ -16,10 +15,12 @@ export async function migrateDev( | |
modules: Module[], | ||
opts: MigrateDevOpts, | ||
) { | ||
assert(modules.every(m => ("local" in m.registry.config)), "Only local modules can run migrateDev because it generates migration files"); | ||
|
||
await forEachPrismaSchema( | ||
project, | ||
modules, | ||
async ({ databaseUrl, module, tempDir, generatedClientDir }) => { | ||
async ({ databaseUrl, module, tempDir }) => { | ||
// Generate migrations & client | ||
const status = await new Deno.Command("deno", { | ||
args: [ | ||
|
@@ -28,6 +29,7 @@ export async function migrateDev( | |
"npm:[email protected]", | ||
"migrate", | ||
"dev", | ||
"--skip-generate", | ||
...(opts.createOnly ? ["--create-only"] : []), | ||
], | ||
cwd: tempDir, | ||
|
@@ -36,149 +38,21 @@ export async function migrateDev( | |
stderr: "inherit", | ||
env: { | ||
DATABASE_URL: databaseUrl, | ||
PRISMA_CLIENT_FORCE_WASM: "true", | ||
}, | ||
}).output(); | ||
if (!status.success) { | ||
throw new Error("Failed to generate migrations"); | ||
} | ||
|
||
if (!opts.createOnly) { | ||
// Specify the path to the library & binary types | ||
await (async () => { | ||
for ( | ||
const filename of [ | ||
"index.d.ts", | ||
"default.d.ts", | ||
"wasm.d.ts", | ||
"edge.d.ts", | ||
] | ||
) { | ||
const filePath = resolve(generatedClientDir, filename); | ||
let content = await Deno.readTextFile(filePath); | ||
const replaceLineA = | ||
`import * as runtime from './runtime/library.js'`; | ||
const replaceLineB = | ||
`import * as runtime from './runtime/binary.js'`; | ||
content = content | ||
.replace( | ||
replaceLineA, | ||
`// @deno-types="./runtime/library.d.ts"\n${replaceLineA}`, | ||
) | ||
.replace( | ||
replaceLineB, | ||
`// @deno-types="./runtime/binary.d.ts"\n${replaceLineB}`, | ||
) | ||
.replace(/from '.\/default'/g, `from './default.d.ts'`); | ||
await Deno.writeTextFile(filePath, content); | ||
} | ||
})(); | ||
|
||
// Compile the ESM library | ||
buildPrismaPackage( | ||
generatedClientDir, | ||
resolve(generatedClientDir, "esm.js"), | ||
); | ||
} | ||
|
||
// Copy back migrations dir | ||
// | ||
// Copy for both `path` (that we'll use later in this script) and | ||
// `sourcePath` (which is the original module's source) | ||
const tempMigrationsDir = resolve(tempDir, "migrations"); | ||
const migrationsDir = resolve(module.path, "db", "migrations"); | ||
if (await exists(tempMigrationsDir)) { | ||
await copy(tempMigrationsDir, migrationsDir, { overwrite: true }); | ||
await copy(tempMigrationsDir, resolve(module.path, "db", "migrations"), { overwrite: true }); | ||
await copy(tempMigrationsDir, resolve(module.sourcePath, "db", "migrations"), { overwrite: true }); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// export async function __TEMP__migrateDev( | ||
// project: Project, | ||
// module: Module, | ||
// opts: MigrateDevOpts, | ||
// ) { | ||
// const databaseUrl = "postgres://username:password@localhost:5432/database"; | ||
// // Setup database | ||
// const defaultDatabaseUrl = Deno.env.get("DATABASE_URL") ?? | ||
// "postgres://postgres:password@localhost:5432/postgres"; | ||
|
||
// // Create dirs | ||
// const tempDir = await Deno.makeTempDir(); | ||
// const dbDir = resolve(module.path, "db"); | ||
// const generatedClientDir = resolve( | ||
// module.path, | ||
// "_gen", | ||
// "prisma", | ||
// ); | ||
|
||
// // Copy db | ||
// await copy(dbDir, tempDir, { overwrite: true }); | ||
|
||
// // Generate migrations & client | ||
// console.log("Generating migrations"); | ||
// const status = await new Deno.Command("deno", { | ||
// args: [ | ||
// "run", | ||
// "-A", | ||
// "npm:[email protected]", | ||
// "migrate", | ||
// "dev", | ||
// ...(opts.createOnly ? ["--create-only"] : []), | ||
// ], | ||
// cwd: tempDir, | ||
// stdin: "inherit", | ||
// stdout: "inherit", | ||
// stderr: "inherit", | ||
// env: { | ||
// DATABASE_URL: databaseUrl, | ||
// PRISMA_CLIENT_FORCE_WASM: "true", | ||
// }, | ||
// }).output(); | ||
// if (!status.success) { | ||
// throw new Error("Failed to generate migrations"); | ||
// } | ||
|
||
// if (!opts.createOnly) { | ||
// // Specify the path to the library & binary types | ||
// await (async () => { | ||
// for ( | ||
// const filename of [ | ||
// "index.d.ts", | ||
// "default.d.ts", | ||
// "wasm.d.ts", | ||
// "edge.d.ts", | ||
// ] | ||
// ) { | ||
// const filePath = resolve(generatedClientDir, filename); | ||
// let content = await Deno.readTextFile(filePath); | ||
// const replaceLineA = `import * as runtime from './runtime/library.js'`; | ||
// const replaceLineB = `import * as runtime from './runtime/binary.js'`; | ||
// content = content | ||
// .replace( | ||
// replaceLineA, | ||
// `// @deno-types="./runtime/library.d.ts"\n${replaceLineA}`, | ||
// ) | ||
// .replace( | ||
// replaceLineB, | ||
// `// @deno-types="./runtime/binary.d.ts"\n${replaceLineB}`, | ||
// ) | ||
// .replace(/from '.\/default'/g, `from './default.d.ts'`); | ||
// await Deno.writeTextFile(filePath, content); | ||
// } | ||
// })(); | ||
|
||
// // Compile the ESM library | ||
// console.log("Compiling ESM library"); | ||
// buildPrismaPackage( | ||
// generatedClientDir, | ||
// resolve(generatedClientDir, "esm.js"), | ||
// ); | ||
// } | ||
|
||
// // Copy back migrations dir | ||
// console.log("Copying migrations back"); | ||
// const tempMigrationsDir = resolve(tempDir, "migrations"); | ||
// const migrationsDir = resolve(module.path, "db", "migrations"); | ||
// if (await exists(tempMigrationsDir)) { | ||
// await copy(tempMigrationsDir, migrationsDir, { overwrite: true }); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Generates Prisma client libraries. | ||
// | ||
// Wrapper around `prisma generate` | ||
|
||
import { resolve } from "../deps.ts"; | ||
import { buildPrismaPackage } from "./build_prisma_esm.ts"; | ||
import { Module, Project } from "../project/mod.ts"; | ||
import { forEachPrismaSchema } from "./mod.ts"; | ||
|
||
export async function generateClient( | ||
project: Project, | ||
modules: Module[], | ||
) { | ||
await forEachPrismaSchema( | ||
project, | ||
modules, | ||
async ({ databaseUrl, tempDir, generatedClientDir }) => { | ||
// Generate migrations & client | ||
const status = await new Deno.Command("deno", { | ||
args: [ | ||
"run", | ||
"-A", | ||
"npm:[email protected]", | ||
"generate", | ||
], | ||
cwd: tempDir, | ||
stdin: "inherit", | ||
stdout: "inherit", | ||
stderr: "inherit", | ||
env: { | ||
DATABASE_URL: databaseUrl, | ||
PRISMA_CLIENT_FORCE_WASM: "true", | ||
}, | ||
}).output(); | ||
if (!status.success) { | ||
throw new Error("Failed to generate migrations"); | ||
} | ||
|
||
// Specify the path to the library & binary types | ||
for ( | ||
const filename of [ | ||
"index.d.ts", | ||
"default.d.ts", | ||
"wasm.d.ts", | ||
"edge.d.ts", | ||
] | ||
) { | ||
const filePath = resolve(generatedClientDir, filename); | ||
let content = await Deno.readTextFile(filePath); | ||
const replaceLineA = `import * as runtime from './runtime/library.js'`; | ||
const replaceLineB = `import * as runtime from './runtime/binary.js'`; | ||
content = content | ||
.replace( | ||
replaceLineA, | ||
`// @deno-types="./runtime/library.d.ts"\n${replaceLineA}`, | ||
) | ||
.replace( | ||
replaceLineB, | ||
`// @deno-types="./runtime/binary.d.ts"\n${replaceLineB}`, | ||
) | ||
.replace(/from '.\/default'/g, `from './default.d.ts'`); | ||
await Deno.writeTextFile(filePath, content); | ||
} | ||
|
||
// Compile the ESM library | ||
await buildPrismaPackage( | ||
generatedClientDir, | ||
resolve(generatedClientDir, "esm.js"), | ||
); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.