This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.ts
140 lines (119 loc) · 3.39 KB
/
config.ts
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
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
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
import * as fs from 'fs'
import * as path from 'path'
import * as ts from 'typescript'
import { importFile } from './helpers'
import { Config, InputConfig, ConfigWithInfo } from './types'
import { DEFAULT_META_SCHEMA_DIR, normalizeConfig } from './yogaDefaults'
import * as dotenv from 'dotenv'
/**
* Find a `prisma.yml` file if it exists
*/
export function findPrismaConfigFile(projectDir: string): string | null {
let definitionPath = path.join(projectDir, 'prisma.yml')
if (fs.existsSync(definitionPath)) {
return definitionPath
}
definitionPath = path.join(process.cwd(), 'prisma', 'prisma.yml')
if (fs.existsSync(definitionPath)) {
return definitionPath
}
return null
}
export function findConfigFile(
fileName: string,
opts: { required: true },
): string
export function findConfigFile(
fileName: string,
opts: { required: false },
): string | undefined
/**
* Find a config file
*/
export function findConfigFile(fileName: string, opts: { required: boolean }) {
const configPath = ts.findConfigFile(
/*searchPath*/ process.cwd(),
ts.sys.fileExists,
fileName,
)
if (!configPath) {
if (opts.required === true) {
throw new Error(`Could not find a valid '${fileName}'.`)
} else {
return undefined
}
}
return configPath
}
function findYogaConfigFile() {
const nodeEnv = process.env.NODE_ENV || 'dev'
let filePath = findConfigFile(`yoga.config.${nodeEnv}.ts`, {
required: false,
})
if (!filePath) {
filePath = findConfigFile('yoga.config.ts', { required: false })
}
return filePath
}
export function injectCustomEnvironmentVariables(env?: string) {
const nodeEnv = env || process.env.NODE_ENV
dotenv.config({
path: path.join(process.cwd(), nodeEnv ? `.env.${nodeEnv}` : '.env'),
})
}
function getDatamodelInfoDir(
yogaConfig: Config,
inputConfig: InputConfig,
projectDir: string,
): string | undefined {
if (!yogaConfig.prisma) {
return undefined
}
if (inputConfig.prisma && inputConfig.prisma.datamodelInfoPath) {
return path.join(projectDir, inputConfig.prisma.datamodelInfoPath)
}
return path.join(projectDir, DEFAULT_META_SCHEMA_DIR)
}
function getPrismaClientDir(
yogaConfig: Config,
projectDir: string,
): string | undefined {
if (!yogaConfig.prisma) {
return undefined
}
if (!yogaConfig.prisma.datamodelInfo.clientPath) {
throw new Error(
'Missing `clientPath` in generated `datamodelInfo`. Make sure to re-run nexus-prisma-generate@>=0.3.2',
)
}
return path.join(projectDir, yogaConfig.prisma.datamodelInfo.clientPath)
}
/**
* Dynamically imports a `yoga.config.ts` file
*/
export function importYogaConfig(
opts: { invalidate?: boolean; env?: string } = {
invalidate: false,
env: undefined,
},
): ConfigWithInfo {
injectCustomEnvironmentVariables(opts.env)
const yogaConfigPath = findYogaConfigFile()
const projectDir = path.dirname(
yogaConfigPath
? yogaConfigPath
: findConfigFile('package.json', { required: true }),
)
const inputConfig = yogaConfigPath
? importFile<InputConfig>(yogaConfigPath, 'default', opts.invalidate)
: {}
const yogaConfig = normalizeConfig(inputConfig, projectDir)
return {
yogaConfig,
yogaConfigPath,
inputConfig,
projectDir,
datamodelInfoDir: getDatamodelInfoDir(yogaConfig, inputConfig, projectDir),
prismaClientDir: getPrismaClientDir(yogaConfig, projectDir),
}
}