-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpaths.ts
291 lines (260 loc) · 8.28 KB
/
paths.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import fs from 'fs'
import path from 'path'
import fg from 'fast-glob'
import findUp from 'findup-sync'
import { getConfig } from './config'
export interface NodeTargetPaths {
base: string
dataMigrations: string
directives: string
db: string
dbSchema: string
src: string
functions: string
graphql: string
lib: string
generators: string
services: string
config: string
dist: string
types: string
}
export interface BrowserTargetPaths {
base: string
src: string
app: string
generators: string
index: string | null
routes: string
pages: string
components: string
layouts: string
config: string
webpack: string
postcss: string
storybookConfig: string
storybookPreviewConfig: string
dist: string
types: string
}
export interface Paths {
base: string
generated: {
base: string
schema: string
types: {
includes: string
mirror: string
}
prebuild: string
}
web: BrowserTargetPaths
api: NodeTargetPaths
scripts: string
}
export interface PagesDependency {
/** the variable to which the import is assigned */
importName: string
/** @alias importName */
const: string
/** absolute path without extension */
importPath: string
/** absolute path with extension */
path: string
/** const ${importName} = { ...data structure for async imports... } */
importStatement: string
}
const CONFIG_FILE_NAME = 'redwood.toml'
// TODO: Remove these.
const PATH_API_DIR_FUNCTIONS = 'api/src/functions'
const PATH_RW_SCRIPTS = 'scripts'
const PATH_API_DIR_GRAPHQL = 'api/src/graphql'
const PATH_API_DIR_CONFIG = 'api/src/config'
const PATH_API_DIR_LIB = 'api/src/lib'
const PATH_API_DIR_GENERATORS = 'api/generators'
const PATH_API_DIR_SERVICES = 'api/src/services'
const PATH_API_DIR_DIRECTIVES = 'api/src/directives'
const PATH_API_DIR_SRC = 'api/src'
const PATH_WEB_ROUTES = 'web/src/Routes' // .js|.tsx
const PATH_WEB_DIR_LAYOUTS = 'web/src/layouts/'
const PATH_WEB_DIR_PAGES = 'web/src/pages/'
const PATH_WEB_DIR_COMPONENTS = 'web/src/components'
const PATH_WEB_DIR_SRC = 'web/src'
const PATH_WEB_DIR_SRC_APP = 'web/src/App'
const PATH_WEB_DIR_SRC_INDEX = 'web/src/index' // .js|.tsx
const PATH_WEB_DIR_GENERATORS = 'web/generators'
const PATH_WEB_DIR_CONFIG = 'web/config'
const PATH_WEB_DIR_CONFIG_WEBPACK = 'web/config/webpack.config.js'
const PATH_WEB_DIR_CONFIG_POSTCSS = 'web/config/postcss.config.js'
const PATH_WEB_DIR_CONFIG_STORYBOOK_CONFIG = 'web/config/storybook.config.js'
const PATH_WEB_DIR_CONFIG_STORYBOOK_PREVIEW = 'web/config/storybook.preview.js'
const PATH_WEB_DIR_DIST = 'web/dist'
/**
* Search the parent directories for the Redwood configuration file.
*/
export const getConfigPath = (
cwd: string = process.env.RWJS_CWD ?? process.cwd()
): string => {
const configPath = findUp(CONFIG_FILE_NAME, { cwd })
if (!configPath) {
throw new Error(
`Could not find a "${CONFIG_FILE_NAME}" file, are you sure you're in a Redwood project?`
)
}
return configPath
}
/**
* The Redwood config file is used as an anchor for the base directory of a project.
*/
export const getBaseDir = (configPath: string = getConfigPath()): string => {
return path.dirname(configPath)
}
export const getBaseDirFromFile = (file: string) => {
return getBaseDir(getConfigPath(path.dirname(file)))
}
/**
* Use this to resolve files when the path to the file is known,
* but the extension is not.
*/
export const resolveFile = (
filePath: string,
extensions: string[] = ['.js', '.tsx', '.ts', '.jsx']
): string | null => {
for (const extension of extensions) {
const p = `${filePath}${extension}`
if (fs.existsSync(p)) {
return p
}
}
return null
}
/**
* Path constants that are relevant to a Redwood project.
*/
// TODO: Make this a proxy and make it lazy.
export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
const routes = resolveFile(path.join(BASE_DIR, PATH_WEB_ROUTES)) as string
const { schemaPath } = getConfig(getConfigPath(BASE_DIR)).api
const schemaDir = path.dirname(schemaPath)
const paths = {
base: BASE_DIR,
generated: {
base: path.join(BASE_DIR, '.redwood'),
schema: path.join(BASE_DIR, '.redwood/schema.graphql'),
types: {
includes: path.join(BASE_DIR, '.redwood/types/includes'),
mirror: path.join(BASE_DIR, '.redwood/types/mirror'),
},
prebuild: path.join(BASE_DIR, '.redwood/prebuild'),
},
scripts: path.join(BASE_DIR, PATH_RW_SCRIPTS),
api: {
base: path.join(BASE_DIR, 'api'),
dataMigrations: path.join(BASE_DIR, schemaDir, 'dataMigrations'),
db: path.join(BASE_DIR, schemaDir),
dbSchema: path.join(BASE_DIR, schemaPath),
functions: path.join(BASE_DIR, PATH_API_DIR_FUNCTIONS),
graphql: path.join(BASE_DIR, PATH_API_DIR_GRAPHQL),
lib: path.join(BASE_DIR, PATH_API_DIR_LIB),
generators: path.join(BASE_DIR, PATH_API_DIR_GENERATORS),
config: path.join(BASE_DIR, PATH_API_DIR_CONFIG),
services: path.join(BASE_DIR, PATH_API_DIR_SERVICES),
directives: path.join(BASE_DIR, PATH_API_DIR_DIRECTIVES),
src: path.join(BASE_DIR, PATH_API_DIR_SRC),
dist: path.join(BASE_DIR, 'api/dist'),
types: path.join(BASE_DIR, 'api/types'),
},
web: {
routes,
base: path.join(BASE_DIR, 'web'),
pages: path.join(BASE_DIR, PATH_WEB_DIR_PAGES),
components: path.join(BASE_DIR, PATH_WEB_DIR_COMPONENTS),
layouts: path.join(BASE_DIR, PATH_WEB_DIR_LAYOUTS),
src: path.join(BASE_DIR, PATH_WEB_DIR_SRC),
generators: path.join(BASE_DIR, PATH_WEB_DIR_GENERATORS),
app: resolveFile(path.join(BASE_DIR, PATH_WEB_DIR_SRC_APP)) as string,
index: resolveFile(path.join(BASE_DIR, PATH_WEB_DIR_SRC_INDEX)),
config: path.join(BASE_DIR, PATH_WEB_DIR_CONFIG),
webpack: path.join(BASE_DIR, PATH_WEB_DIR_CONFIG_WEBPACK),
postcss: path.join(BASE_DIR, PATH_WEB_DIR_CONFIG_POSTCSS),
storybookConfig: path.join(
BASE_DIR,
PATH_WEB_DIR_CONFIG_STORYBOOK_CONFIG
),
storybookPreviewConfig: path.join(
BASE_DIR,
PATH_WEB_DIR_CONFIG_STORYBOOK_PREVIEW
),
dist: path.join(BASE_DIR, PATH_WEB_DIR_DIST),
types: path.join(BASE_DIR, 'web/types'),
},
}
fs.mkdirSync(paths.generated.types.includes, { recursive: true })
fs.mkdirSync(paths.generated.types.mirror, { recursive: true })
return paths
}
/**
* Process the pages directory and return information useful for automated imports.
*
* Note: glob.sync returns posix style paths on Windows machines
* @deprecated I will write a seperate method that use `getFiles` instead. This
* is used by structure, babel auto-importer and the eslint plugin.
*/
export const processPagesDir = (
webPagesDir: string = getPaths().web.pages
): Array<PagesDependency> => {
const pagePaths = fg.sync('**/*Page.{js,jsx,ts,tsx}', {
cwd: webPagesDir,
ignore: ['node_modules'],
})
return pagePaths.map((pagePath) => {
const p = path.parse(pagePath)
const importName = p.dir.replace(/\//g, '')
const importPath = importStatementPath(
path.join(webPagesDir, p.dir, p.name)
)
const importStatement = `const ${importName} = { name: '${importName}', loader: import('${importPath}') }`
return {
importName,
const: importName,
importPath,
path: path.join(webPagesDir, pagePath),
importStatement,
}
})
}
/**
* Converts Windows-style paths to Posix-style
* C:\Users\Bob\dev\Redwood -> /c/Users/Bob/dev/Redwood
*
* The conversion only happens on Windows systems, and only for paths that are
* not already Posix-style
*
* @param path Filesystem path
*/
export const ensurePosixPath = (path: string) => {
let posixPath = path
if (process.platform === 'win32') {
if (/^[A-Z]:\\/.test(path)) {
const drive = path[0].toLowerCase()
posixPath = `/${drive}/${path.substring(3)}`
}
posixPath = posixPath.replace(/\\/g, '/')
}
return posixPath
}
/**
* Switches backslash to regular slash on Windows so the path works in
* import statements
* C:\Users\Bob\dev\Redwood\UserPage\UserPage ->
* C:/Users/Bob/dev/Redwood/UserPage/UserPage
*
* @param path Filesystem path
*/
export const importStatementPath = (path: string) => {
let importPath = path
if (process.platform === 'win32') {
importPath = importPath.replaceAll('\\', '/')
}
return importPath
}