| 16 | } |
| 17 | |
| 18 | export async function generateInFolder({ projectDir, packageSource }: GenerateInFolderOptions): Promise<number> { |
| 19 | const before = performance.now() |
| 20 | if (!projectDir) { |
| 21 | throw new Error(`Project dir missing. Usage: ts-node examples/generate.ts examples/accounts`) |
| 22 | } |
| 23 | if (!fs.existsSync(projectDir)) { |
| 24 | throw new Error(`Path ${projectDir} does not exist`) |
| 25 | } |
| 26 | |
| 27 | let schemaPathResult: GetSchemaResult | null = null |
| 28 | const schemaNotFoundError = new Error(`Could not find any schema.prisma in ${projectDir} or sub directories.`) |
| 29 | |
| 30 | try { |
| 31 | schemaPathResult = await getSchemaWithPath({ schemaPath: { baseDir: projectDir }, cwd: projectDir }) |
| 32 | } catch (e) { |
| 33 | debug('Error in getSchemaPath', e) |
| 34 | } |
| 35 | |
| 36 | if (!schemaPathResult) { |
| 37 | throw schemaNotFoundError |
| 38 | } |
| 39 | |
| 40 | const { schemas, schemaPath } = schemaPathResult |
| 41 | |
| 42 | const config = await getConfig({ datamodel: schemas }) |
| 43 | |
| 44 | const outputDir = path.join(projectDir, 'node_modules/@prisma/client') |
| 45 | |
| 46 | await fs.promises.rm(outputDir, { force: true, recursive: true }) |
| 47 | |
| 48 | if (packageSource) { |
| 49 | await copy({ |
| 50 | from: packageSource, // when using yarn pack and extracting it, it includes a folder called "package" |
| 51 | to: outputDir, |
| 52 | recursive: true, |
| 53 | parallelJobs: 20, |
| 54 | overwrite: true, |
| 55 | }) |
| 56 | } else { |
| 57 | await getPackedPackage('@prisma/client', outputDir) |
| 58 | } |
| 59 | |
| 60 | // TODO: use engine.getDmmf() |
| 61 | const dmmf = await getDMMF({ datamodel: schemas }) |
| 62 | |
| 63 | const schema = mergeSchemas({ schemas }) |
| 64 | |
| 65 | await generateClient({ |
| 66 | binaryPaths: {}, |
| 67 | datamodel: schema, |
| 68 | dmmf, |
| 69 | ...config, |
| 70 | outputDir, |
| 71 | schemaPath, |
| 72 | testMode: true, |
| 73 | copyRuntime: false, |
| 74 | runtimeSourcePath: path.join(__dirname, '../../runtime'), |
| 75 | generator: config.generators[0], |