-
Notifications
You must be signed in to change notification settings - Fork 365
/
frameworks-api.ts
72 lines (61 loc) · 2.12 KB
/
frameworks-api.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
import { access, mkdir, readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { mergeConfigs } from '@netlify/config'
import type { NetlifyOptions } from '../commands/types.js'
interface FrameworksAPIPath {
path: string
ensureExists: () => Promise<void>
exists: () => Promise<boolean>
}
export type FrameworksAPIPaths = ReturnType<typeof getFrameworksAPIPaths>
/**
* Returns an object containing the paths for all the operations of the
* Frameworks API. Each key maps to an object containing a `path` property with
* the path of the operation, an `exists` method that returns whether the path
* exists, and an `ensureExists` method that creates it in case it doesn't.
*/
export const getFrameworksAPIPaths = (basePath: string, packagePath?: string) => {
const root = resolve(basePath, packagePath || '', '.netlify/v1')
const edgeFunctions = resolve(root, 'edge-functions')
const paths = {
root,
config: resolve(root, 'config.json'),
functions: resolve(root, 'functions'),
edgeFunctions,
edgeFunctionsImportMap: resolve(edgeFunctions, 'import_map.json'),
blobs: resolve(root, 'blobs'),
}
return Object.entries(paths).reduce(
(acc, [name, path]) => ({
...acc,
[name]: {
path,
ensureExists: async () => {
await mkdir(path, { recursive: true })
},
exists: async () => {
try {
await access(path)
return true
} catch {
return false
}
},
},
}),
{} as Record<keyof typeof paths, FrameworksAPIPath>,
)
}
/**
* Merges a config object with any config options from the Frameworks API.
*/
export const getFrameworksAPIConfig = async (config: NetlifyOptions['config'], frameworksAPIConfigPath: string) => {
let frameworksAPIConfigFile: string | undefined
try {
frameworksAPIConfigFile = await readFile(frameworksAPIConfigPath, 'utf8')
} catch {
return config
}
const frameworksAPIConfig = JSON.parse(frameworksAPIConfigFile)
return mergeConfigs([frameworksAPIConfig, config], { concatenateArrays: true }) as NetlifyOptions['config']
}