prisma-generator-express 1.65.2 → 1.67.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -3480
- package/dist/client/encodeQueryParams.js +3 -1
- package/dist/client/encodeQueryParams.js.map +1 -1
- package/dist/copy/misc.d.ts +1 -0
- package/dist/copy/misc.js +14 -0
- package/dist/copy/misc.js.map +1 -1
- package/dist/copy/operationDefinitions.js +69 -6
- package/dist/copy/operationDefinitions.js.map +1 -1
- package/dist/generators/generateFastifyHandler.d.ts +2 -2
- package/dist/generators/generateFastifyHandler.js +15 -5
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateHonoHandler.d.ts +2 -2
- package/dist/generators/generateHonoHandler.js +12 -5
- package/dist/generators/generateHonoHandler.js.map +1 -1
- package/dist/generators/generateImportPrismaStatement.d.ts +1 -0
- package/dist/generators/generateImportPrismaStatement.js +6 -0
- package/dist/generators/generateImportPrismaStatement.js.map +1 -1
- package/dist/generators/generateModelMetadata.js +20 -10
- package/dist/generators/generateModelMetadata.js.map +1 -1
- package/dist/generators/generateOperationCore.d.ts +3 -3
- package/dist/generators/generateOperationCore.js +51 -16
- package/dist/generators/generateOperationCore.js.map +1 -1
- package/dist/generators/generateQueryBuilderHelper.d.ts +1 -1
- package/dist/generators/generateQueryBuilderHelper.js +2 -4
- package/dist/generators/generateQueryBuilderHelper.js.map +1 -1
- package/dist/generators/generateRelationMeta.js +2 -2
- package/dist/generators/generateRelationMeta.js.map +1 -1
- package/dist/generators/generateRouteConfigType.d.ts +2 -2
- package/dist/generators/generateRouteConfigType.js +29 -11
- package/dist/generators/generateRouteConfigType.js.map +1 -1
- package/dist/generators/generateRouter.d.ts +7 -4
- package/dist/generators/generateRouter.js +107 -14
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.d.ts +7 -4
- package/dist/generators/generateRouterFastify.js +56 -28
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateRouterHono.d.ts +7 -4
- package/dist/generators/generateRouterHono.js +84 -53
- package/dist/generators/generateRouterHono.js.map +1 -1
- package/dist/generators/generateUnifiedDocs.d.ts +1 -1
- package/dist/generators/generateUnifiedDocs.js +15 -8
- package/dist/generators/generateUnifiedDocs.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.d.ts +2 -2
- package/dist/generators/generateUnifiedHandler.js +11 -3
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/guardOptions.js +12 -7
- package/dist/guardOptions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.js +22 -6
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/importExt.d.ts +1 -1
- package/dist/utils/pathCasing.d.ts +4 -0
- package/dist/utils/pathCasing.js +40 -0
- package/dist/utils/pathCasing.js.map +1 -0
- package/dist/utils/resolveImportStyle.js +13 -5
- package/dist/utils/resolveImportStyle.js.map +1 -1
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +16 -3
- package/src/bin.ts +1 -1
- package/src/client/encodeQueryParams.ts +39 -35
- package/src/constants.ts +1 -1
- package/src/copy/autoIncludePlanner.ts +93 -27
- package/src/copy/autoIncludePlannerGuarded.ts +171 -48
- package/src/copy/autoIncludeRuntimeGuarded.ts +25 -7
- package/src/copy/buildModelOpenApi.ts +153 -50
- package/src/copy/concurrency.ts +10 -8
- package/src/copy/errorMapper.ts +68 -19
- package/src/copy/guardHelpers.ts +30 -9
- package/src/copy/materializedCount.ts +17 -5
- package/src/copy/misc.ts +88 -68
- package/src/copy/ndjson.ts +72 -0
- package/src/copy/operationDefinitions.ts +99 -21
- package/src/copy/operationRuntime.ts +100 -12
- package/src/copy/pagination.ts +25 -11
- package/src/copy/parseQueryParams.ts +47 -48
- package/src/copy/projectionDefaults.ts +6 -4
- package/src/copy/routeConfig.express.ts +16 -4
- package/src/copy/routeConfig.fastify.ts +16 -2
- package/src/copy/routeConfig.hono.ts +19 -9
- package/src/copy/routeConfig.ts +174 -39
- package/src/copy/scalarTypes.ts +7 -2
- package/src/copy/sse.ts +72 -19
- package/src/generators/generateFastifyHandler.ts +24 -13
- package/src/generators/generateHonoHandler.ts +21 -13
- package/src/generators/generateImportPrismaStatement.ts +93 -81
- package/src/generators/generateModelMetadata.ts +24 -12
- package/src/generators/generateOperationCore.ts +81 -33
- package/src/generators/generateQueryBuilderHelper.ts +20 -4
- package/src/generators/generateRelationMeta.ts +23 -11
- package/src/generators/generateRouteConfigType.ts +39 -13
- package/src/generators/generateRouter.ts +114 -17
- package/src/generators/generateRouterFastify.ts +67 -31
- package/src/generators/generateRouterHono.ts +108 -60
- package/src/generators/generateUnifiedDocs.ts +29 -8
- package/src/generators/generateUnifiedHandler.ts +16 -8
- package/src/generators/generateUnifiedScalarUI.ts +1 -1
- package/src/guardOptions.ts +105 -92
- package/src/index.ts +46 -4
- package/src/utils/copyFiles.ts +199 -168
- package/src/utils/importExt.ts +2 -2
- package/src/utils/pathCasing.ts +42 -0
- package/src/utils/resolveImportStyle.ts +37 -15
- package/src/utils/strings.ts +2 -2
- package/src/utils/writeFileSafely.ts +6 -2
package/src/utils/copyFiles.ts
CHANGED
|
@@ -1,168 +1,199 @@
|
|
|
1
|
-
import { GeneratorOptions } from '@prisma/generator-helper'
|
|
2
|
-
import * as fs from 'fs'
|
|
3
|
-
import * as path from 'path'
|
|
4
|
-
import { Target } from '../constants'
|
|
5
|
-
import { ImportStyle } from './resolveImportStyle'
|
|
6
|
-
import { importExt } from './importExt'
|
|
7
|
-
|
|
8
|
-
const SHARED_FILES = [
|
|
9
|
-
'parseQueryParams.ts',
|
|
10
|
-
'buildModelOpenApi.ts',
|
|
11
|
-
'operationDefinitions.ts',
|
|
12
|
-
'misc.ts',
|
|
13
|
-
'routeConfig.ts',
|
|
14
|
-
'docsRenderer.ts',
|
|
15
|
-
'operationRuntime.ts',
|
|
16
|
-
'errorMapper.ts',
|
|
17
|
-
'sse.ts',
|
|
18
|
-
'pagination.ts',
|
|
19
|
-
'materializedCount.ts',
|
|
20
|
-
'guardHelpers.ts',
|
|
21
|
-
'scalarTypes.ts',
|
|
22
|
-
'concurrency.ts',
|
|
23
|
-
'projectionDefaults.ts',
|
|
24
|
-
'guardVariantRouting.ts',
|
|
25
|
-
'guardVariantError.ts',
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
const EXPRESS_ONLY_FILES = [
|
|
29
|
-
'autoIncludePlanner.ts',
|
|
30
|
-
'autoIncludePlannerGuarded.ts',
|
|
31
|
-
'autoIncludeRuntime.ts',
|
|
32
|
-
'autoIncludeRuntimeGuarded.ts',
|
|
33
|
-
'materializedRouter.ts',
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
1
|
+
import { GeneratorOptions } from '@prisma/generator-helper'
|
|
2
|
+
import * as fs from 'fs'
|
|
3
|
+
import * as path from 'path'
|
|
4
|
+
import { Target } from '../constants'
|
|
5
|
+
import { ImportStyle } from './resolveImportStyle'
|
|
6
|
+
import { importExt } from './importExt'
|
|
7
|
+
|
|
8
|
+
const SHARED_FILES = [
|
|
9
|
+
'parseQueryParams.ts',
|
|
10
|
+
'buildModelOpenApi.ts',
|
|
11
|
+
'operationDefinitions.ts',
|
|
12
|
+
'misc.ts',
|
|
13
|
+
'routeConfig.ts',
|
|
14
|
+
'docsRenderer.ts',
|
|
15
|
+
'operationRuntime.ts',
|
|
16
|
+
'errorMapper.ts',
|
|
17
|
+
'sse.ts',
|
|
18
|
+
'pagination.ts',
|
|
19
|
+
'materializedCount.ts',
|
|
20
|
+
'guardHelpers.ts',
|
|
21
|
+
'scalarTypes.ts',
|
|
22
|
+
'concurrency.ts',
|
|
23
|
+
'projectionDefaults.ts',
|
|
24
|
+
'guardVariantRouting.ts',
|
|
25
|
+
'guardVariantError.ts',
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
const EXPRESS_ONLY_FILES = [
|
|
29
|
+
'autoIncludePlanner.ts',
|
|
30
|
+
'autoIncludePlannerGuarded.ts',
|
|
31
|
+
'autoIncludeRuntime.ts',
|
|
32
|
+
'autoIncludeRuntimeGuarded.ts',
|
|
33
|
+
'materializedRouter.ts',
|
|
34
|
+
'ndjson.ts',
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
interface CopyFileOptions {
|
|
38
|
+
required?: boolean
|
|
39
|
+
importRewrites?: Array<{ from: string; to: string }>
|
|
40
|
+
destFilename?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolveTemplateDir(subpath: string): string {
|
|
44
|
+
const fromSrc = path.join(__dirname, '..', '..', 'src', subpath)
|
|
45
|
+
if (fs.existsSync(fromSrc)) return fromSrc
|
|
46
|
+
const fromDist = path.join(__dirname, '..', subpath)
|
|
47
|
+
if (fs.existsSync(fromDist)) return fromDist
|
|
48
|
+
throw new Error(
|
|
49
|
+
'Template directory "' +
|
|
50
|
+
subpath +
|
|
51
|
+
'" not found.\n Searched:\n ' +
|
|
52
|
+
fromSrc +
|
|
53
|
+
'\n ' +
|
|
54
|
+
fromDist +
|
|
55
|
+
'\n __dirname: ' +
|
|
56
|
+
__dirname,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function escapeRegex(str: string): string {
|
|
61
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const RELATIVE_IMPORT_RE =
|
|
65
|
+
/(\b(?:import|export)\b[^'";]*?\bfrom\s+|\bimport\s+|\bimport\s*\(\s*)(['"])(\.{1,2}\/[^'"\n]+)\2/g
|
|
66
|
+
|
|
67
|
+
const SKIP_EXTENSION_RE = /\.(m?[jt]sx?|json|css|svg|png|jpe?g|gif|webp|wasm)$/i
|
|
68
|
+
|
|
69
|
+
function applyExtension(content: string, style: ImportStyle): string {
|
|
70
|
+
if (style === 'none') return content
|
|
71
|
+
const ext = importExt(style)
|
|
72
|
+
if (!ext) return content
|
|
73
|
+
return content.replace(RELATIVE_IMPORT_RE, (_match, prefix, quote, spec) => {
|
|
74
|
+
if (SKIP_EXTENSION_RE.test(spec)) return prefix + quote + spec + quote
|
|
75
|
+
return prefix + quote + spec + ext + quote
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function copyFileSync(
|
|
80
|
+
srcDir: string,
|
|
81
|
+
destDir: string,
|
|
82
|
+
filename: string,
|
|
83
|
+
importStyle: ImportStyle,
|
|
84
|
+
options?: CopyFileOptions,
|
|
85
|
+
): string | null {
|
|
86
|
+
const srcPath = path.join(srcDir, filename)
|
|
87
|
+
const destFilename = options?.destFilename || filename
|
|
88
|
+
const destPath = path.join(destDir, destFilename)
|
|
89
|
+
|
|
90
|
+
if (!fs.existsSync(srcPath)) {
|
|
91
|
+
if (options?.required) return 'Required file not found: ' + srcPath
|
|
92
|
+
return null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const destDirPath = path.dirname(destPath)
|
|
96
|
+
if (!fs.existsSync(destDirPath))
|
|
97
|
+
fs.mkdirSync(destDirPath, { recursive: true })
|
|
98
|
+
|
|
99
|
+
let content = fs.readFileSync(srcPath, 'utf-8')
|
|
100
|
+
|
|
101
|
+
if (options?.importRewrites) {
|
|
102
|
+
for (const rewrite of options.importRewrites) {
|
|
103
|
+
if (rewrite.from === rewrite.to) continue
|
|
104
|
+
content = content.replace(
|
|
105
|
+
new RegExp('from [\'"]' + escapeRegex(rewrite.from) + '[\'"]', 'g'),
|
|
106
|
+
"from '" + rewrite.to + "'",
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const header =
|
|
112
|
+
'/**\n' +
|
|
113
|
+
' * Auto-generated by prisma-generator-express\n' +
|
|
114
|
+
' * DO NOT EDIT MANUALLY\n' +
|
|
115
|
+
' */\n\n'
|
|
116
|
+
|
|
117
|
+
if (!content.startsWith('/**')) content = header + content
|
|
118
|
+
|
|
119
|
+
content = applyExtension(content, importStyle)
|
|
120
|
+
|
|
121
|
+
fs.writeFileSync(destPath, content)
|
|
122
|
+
return null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function copyFiles(
|
|
126
|
+
options: GeneratorOptions,
|
|
127
|
+
target: Target,
|
|
128
|
+
importStyle: ImportStyle,
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
const outputPath = options.generator.output?.value
|
|
131
|
+
if (!outputPath) return
|
|
132
|
+
|
|
133
|
+
const copyBase = resolveTemplateDir('copy')
|
|
134
|
+
const errors: string[] = []
|
|
135
|
+
|
|
136
|
+
console.log(
|
|
137
|
+
' Copying utility files to: ' + outputPath + ' (target: ' + target + ')',
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
for (const file of SHARED_FILES) {
|
|
141
|
+
const err = copyFileSync(copyBase, outputPath, file, importStyle, {
|
|
142
|
+
required: true,
|
|
143
|
+
})
|
|
144
|
+
if (err) errors.push(err)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (target === 'express') {
|
|
148
|
+
for (const file of EXPRESS_ONLY_FILES) {
|
|
149
|
+
const err = copyFileSync(copyBase, outputPath, file, importStyle, {
|
|
150
|
+
required: true,
|
|
151
|
+
})
|
|
152
|
+
if (err) errors.push(err)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const targetConfigFile = 'routeConfig.' + target + '.ts'
|
|
157
|
+
const err = copyFileSync(
|
|
158
|
+
copyBase,
|
|
159
|
+
outputPath,
|
|
160
|
+
targetConfigFile,
|
|
161
|
+
importStyle,
|
|
162
|
+
{
|
|
163
|
+
required: true,
|
|
164
|
+
destFilename: 'routeConfig.target.ts',
|
|
165
|
+
},
|
|
166
|
+
)
|
|
167
|
+
if (err) errors.push(err)
|
|
168
|
+
|
|
169
|
+
const clientDir = path.join(outputPath, 'client')
|
|
170
|
+
if (!fs.existsSync(clientDir)) fs.mkdirSync(clientDir, { recursive: true })
|
|
171
|
+
|
|
172
|
+
const clientSrcDir = resolveTemplateDir('client')
|
|
173
|
+
const clientErr = copyFileSync(
|
|
174
|
+
clientSrcDir,
|
|
175
|
+
clientDir,
|
|
176
|
+
'encodeQueryParams.ts',
|
|
177
|
+
importStyle,
|
|
178
|
+
{
|
|
179
|
+
required: true,
|
|
180
|
+
importRewrites: [{ from: '../copy/misc', to: '../misc' }],
|
|
181
|
+
},
|
|
182
|
+
)
|
|
183
|
+
if (clientErr) errors.push(clientErr)
|
|
184
|
+
|
|
185
|
+
if (errors.length > 0) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
'Failed to copy ' +
|
|
188
|
+
errors.length +
|
|
189
|
+
' required file(s):\n' +
|
|
190
|
+
errors.map((e) => ' - ' + e).join('\n') +
|
|
191
|
+
'\n copyBase: ' +
|
|
192
|
+
copyBase +
|
|
193
|
+
'\n __dirname: ' +
|
|
194
|
+
__dirname,
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
console.log(' ✓ Utility files copied successfully')
|
|
199
|
+
}
|
package/src/utils/importExt.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type PathCase = 'lower' | 'kebab' | 'raw'
|
|
2
|
+
|
|
3
|
+
export function parsePathCase(raw: unknown): PathCase {
|
|
4
|
+
const value = String(raw ?? 'lower').toLowerCase()
|
|
5
|
+
if (value === 'lower' || value === 'kebab' || value === 'raw') return value
|
|
6
|
+
throw new Error(
|
|
7
|
+
`Invalid pathCase "${String(raw)}". Expected "lower", "kebab", or "raw".`,
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function toKebabCase(name: string): string {
|
|
12
|
+
let out = ''
|
|
13
|
+
for (let i = 0; i < name.length; i++) {
|
|
14
|
+
const ch = name[i]
|
|
15
|
+
const prev = i > 0 ? name[i - 1] : undefined
|
|
16
|
+
const next = name[i + 1]
|
|
17
|
+
const isUpper = ch >= 'A' && ch <= 'Z'
|
|
18
|
+
const prevIsLower = prev !== undefined && prev >= 'a' && prev <= 'z'
|
|
19
|
+
const prevIsDigit = prev !== undefined && prev >= '0' && prev <= '9'
|
|
20
|
+
const nextIsLower = next !== undefined && next >= 'a' && next <= 'z'
|
|
21
|
+
if (
|
|
22
|
+
isUpper &&
|
|
23
|
+
i > 0 &&
|
|
24
|
+
(prevIsLower ||
|
|
25
|
+
prevIsDigit ||
|
|
26
|
+
(prev !== undefined && prev >= 'A' && prev <= 'Z' && nextIsLower))
|
|
27
|
+
) {
|
|
28
|
+
out += '-'
|
|
29
|
+
}
|
|
30
|
+
out += ch.toLowerCase()
|
|
31
|
+
}
|
|
32
|
+
return out
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function modelPathSegment(
|
|
36
|
+
modelName: string,
|
|
37
|
+
pathCase: PathCase,
|
|
38
|
+
): string {
|
|
39
|
+
if (pathCase === 'raw') return modelName
|
|
40
|
+
if (pathCase === 'kebab') return toKebabCase(modelName)
|
|
41
|
+
return modelName.toLowerCase()
|
|
42
|
+
}
|
|
@@ -39,7 +39,8 @@ function stripJsonComments(input: string): string {
|
|
|
39
39
|
}
|
|
40
40
|
if (c === '/' && next === '*') {
|
|
41
41
|
i += 2
|
|
42
|
-
while (i < input.length && !(input[i] === '*' && input[i + 1] === '/'))
|
|
42
|
+
while (i < input.length && !(input[i] === '*' && input[i + 1] === '/'))
|
|
43
|
+
i++
|
|
43
44
|
i += 2
|
|
44
45
|
continue
|
|
45
46
|
}
|
|
@@ -75,7 +76,10 @@ function findUpwards(startDir: string, filename: string): string | null {
|
|
|
75
76
|
return result
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
function resolveExtendsTarget(
|
|
79
|
+
function resolveExtendsTarget(
|
|
80
|
+
extendsValue: string,
|
|
81
|
+
fromFile: string,
|
|
82
|
+
): string | null {
|
|
79
83
|
const fromDir = path.dirname(fromFile)
|
|
80
84
|
if (extendsValue.startsWith('.') || path.isAbsolute(extendsValue)) {
|
|
81
85
|
const direct = path.resolve(fromDir, extendsValue)
|
|
@@ -90,7 +94,9 @@ function resolveExtendsTarget(extendsValue: string, fromFile: string): string |
|
|
|
90
94
|
return require.resolve(extendsValue, { paths: [fromDir] })
|
|
91
95
|
} catch {
|
|
92
96
|
try {
|
|
93
|
-
return require.resolve(extendsValue + '/tsconfig.json', {
|
|
97
|
+
return require.resolve(extendsValue + '/tsconfig.json', {
|
|
98
|
+
paths: [fromDir],
|
|
99
|
+
})
|
|
94
100
|
} catch {
|
|
95
101
|
return null
|
|
96
102
|
}
|
|
@@ -102,7 +108,10 @@ type TsConfigRaw = {
|
|
|
102
108
|
compilerOptions?: Record<string, unknown>
|
|
103
109
|
}
|
|
104
110
|
|
|
105
|
-
function readTsConfig(
|
|
111
|
+
function readTsConfig(
|
|
112
|
+
filePath: string,
|
|
113
|
+
seen: Set<string> = new Set(),
|
|
114
|
+
): Record<string, unknown> {
|
|
106
115
|
const abs = path.resolve(filePath)
|
|
107
116
|
if (seen.has(abs)) return {}
|
|
108
117
|
seen.add(abs)
|
|
@@ -143,21 +152,25 @@ function readPackageType(startDir: string): string | null {
|
|
|
143
152
|
|
|
144
153
|
export function resolveImportStyle(options: GeneratorOptions): ImportStyle {
|
|
145
154
|
const config = (options.generator.config || {}) as Record<string, string>
|
|
146
|
-
const raw =
|
|
147
|
-
|
|
148
|
-
|
|
155
|
+
const raw =
|
|
156
|
+
typeof config.importStyle === 'string'
|
|
157
|
+
? config.importStyle.toLowerCase()
|
|
158
|
+
: 'auto'
|
|
149
159
|
|
|
150
160
|
if (!VALID_OVERRIDES.has(raw)) {
|
|
151
161
|
console.warn(
|
|
152
|
-
'[prisma-generator-express] Invalid importStyle "' +
|
|
153
|
-
|
|
162
|
+
'[prisma-generator-express] Invalid importStyle "' +
|
|
163
|
+
config.importStyle +
|
|
164
|
+
'". Expected one of: auto, none, js, ts. Falling back to auto.',
|
|
154
165
|
)
|
|
155
166
|
} else if (raw !== 'auto') {
|
|
156
167
|
return raw as ImportStyle
|
|
157
168
|
}
|
|
158
169
|
|
|
159
170
|
const outputPath = options.generator.output?.value
|
|
160
|
-
const schemaDir = options.schemaPath
|
|
171
|
+
const schemaDir = options.schemaPath
|
|
172
|
+
? path.dirname(options.schemaPath)
|
|
173
|
+
: process.cwd()
|
|
161
174
|
const startDir = outputPath ? path.dirname(outputPath) : schemaDir
|
|
162
175
|
|
|
163
176
|
const tsconfigPath =
|
|
@@ -171,12 +184,21 @@ export function resolveImportStyle(options: GeneratorOptions): ImportStyle {
|
|
|
171
184
|
if (compilerOptions.allowImportingTsExtensions === true) return 'ts'
|
|
172
185
|
|
|
173
186
|
const moduleVal = String(compilerOptions.module ?? '').toLowerCase()
|
|
174
|
-
const moduleResolution = String(
|
|
175
|
-
|
|
176
|
-
|
|
187
|
+
const moduleResolution = String(
|
|
188
|
+
compilerOptions.moduleResolution ?? '',
|
|
189
|
+
).toLowerCase()
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
NODE_MODULE_KEYWORDS.has(moduleVal) ||
|
|
193
|
+
NODE_MODULE_KEYWORDS.has(moduleResolution)
|
|
194
|
+
) {
|
|
177
195
|
return 'js'
|
|
178
196
|
}
|
|
179
|
-
if (
|
|
197
|
+
if (
|
|
198
|
+
moduleResolution === 'bundler' ||
|
|
199
|
+
moduleResolution === 'classic' ||
|
|
200
|
+
moduleResolution === 'node'
|
|
201
|
+
) {
|
|
180
202
|
return 'none'
|
|
181
203
|
}
|
|
182
204
|
|
|
@@ -184,4 +206,4 @@ export function resolveImportStyle(options: GeneratorOptions): ImportStyle {
|
|
|
184
206
|
if (pkgType === 'module') return 'js'
|
|
185
207
|
|
|
186
208
|
return 'none'
|
|
187
|
-
}
|
|
209
|
+
}
|
package/src/utils/strings.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const capitalize = (str: string) =>
|
|
2
|
-
str.charAt(0).toUpperCase() + str.slice(1)
|
|
1
|
+
export const capitalize = (str: string) =>
|
|
2
|
+
str.charAt(0).toUpperCase() + str.slice(1)
|
|
@@ -41,7 +41,11 @@ export async function writeFileSafely({
|
|
|
41
41
|
break
|
|
42
42
|
default:
|
|
43
43
|
if (!model) throw new Error('Model required for operation: ' + operation)
|
|
44
|
-
filePath = path.join(
|
|
44
|
+
filePath = path.join(
|
|
45
|
+
outputPath,
|
|
46
|
+
model.name,
|
|
47
|
+
`${model.name}${operation}.ts`,
|
|
48
|
+
)
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
const dirPath = path.dirname(filePath)
|
|
@@ -54,4 +58,4 @@ export async function writeFileSafely({
|
|
|
54
58
|
})
|
|
55
59
|
|
|
56
60
|
fs.writeFileSync(filePath, formattedContent)
|
|
57
|
-
}
|
|
61
|
+
}
|