1
- import { spawn } from 'child_process'
2
- import { carefullyWriteFile } from './utils.js'
1
+ import { carefullyWriteFile , getPackageJSON , spawnAsync } from './utils.js'
3
2
import BaseCommand from '../base-command.js'
4
3
import path from 'path'
5
4
import fs from 'fs/promises'
6
5
import inquirer from 'inquirer'
6
+ import { NETLIFY_NEON_PACKAGE_NAME } from './constants.js'
7
7
8
8
export const initDrizzle = async ( command : BaseCommand ) => {
9
9
if ( ! command . project . root ) {
@@ -29,12 +29,9 @@ export const initDrizzle = async (command: BaseCommand) => {
29
29
}
30
30
31
31
const packageJsonPath = path . resolve ( command . project . root , 'package.json' )
32
+ const packageJson = getPackageJSON ( command . workingDir )
32
33
33
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
34
- const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) )
35
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
36
34
packageJson . scripts = {
37
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
38
35
...( packageJson . scripts ?? { } ) ,
39
36
...packageJsonScripts ,
40
37
}
@@ -59,15 +56,14 @@ export const initDrizzle = async (command: BaseCommand) => {
59
56
}
60
57
}
61
58
62
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
63
- if ( ! Object . keys ( packageJson ?. devDependencies ?? { } ) . includes ( 'drizzle-kit' ) ) {
59
+ if ( ! Object . keys ( packageJson . devDependencies ?? { } ) . includes ( 'drizzle-kit' ) ) {
64
60
await spawnAsync ( command . project . packageManager ?. installCommand ?? 'npm install' , [ 'drizzle-kit@latest' , '-D' ] , {
65
61
stdio : 'inherit' ,
66
62
shell : true ,
67
63
} )
68
64
}
69
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
70
- if ( ! Object . keys ( packageJson ? .dependencies ?? { } ) . includes ( 'drizzle-orm' ) ) {
65
+
66
+ if ( ! Object . keys ( packageJson . dependencies ?? { } ) . includes ( 'drizzle-orm' ) ) {
71
67
await spawnAsync ( command . project . packageManager ?. installCommand ?? 'npm install' , [ 'drizzle-orm@latest' ] , {
72
68
stdio : 'inherit' ,
73
69
shell : true ,
@@ -94,7 +90,7 @@ export const posts = pgTable('posts', {
94
90
content: text().notNull().default('')
95
91
});`
96
92
97
- const dbIndex = `import { neon } from '@netlify/neon ';
93
+ const dbIndex = `import { neon } from '${ NETLIFY_NEON_PACKAGE_NAME } ';
98
94
import { drizzle } from 'drizzle-orm/neon-http';
99
95
100
96
import * as schema from './schema';
@@ -109,17 +105,3 @@ const packageJsonScripts = {
109
105
'db:migrate' : 'netlify dev:exec drizzle-kit migrate' ,
110
106
'db:studio' : 'netlify dev:exec drizzle-kit studio' ,
111
107
}
112
-
113
- const spawnAsync = ( command : string , args : string [ ] , options : Parameters < typeof spawn > [ 2 ] ) : Promise < number > => {
114
- return new Promise ( ( resolve , reject ) => {
115
- const child = spawn ( command , args , options )
116
- child . on ( 'error' , reject )
117
- child . on ( 'exit' , ( code ) => {
118
- if ( code === 0 ) {
119
- resolve ( code )
120
- }
121
- const errorMessage = code ? `Process exited with code ${ code . toString ( ) } ` : 'Process exited with no code'
122
- reject ( new Error ( errorMessage ) )
123
- } )
124
- } )
125
- }
0 commit comments