-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathConfig.ts
More file actions
193 lines (176 loc) · 5.86 KB
/
Copy pathConfig.ts
File metadata and controls
193 lines (176 loc) · 5.86 KB
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
import {CloudAuthView} from 'alinea/cloud/view/CloudAuth'
import {MediaFile, MediaLibrary} from 'alinea/core/media/MediaTypes'
import type {Preview} from 'alinea/core/Preview'
import type {en} from 'alinea/translations'
import type {Auth} from './Auth.js'
import {getWorkspace} from './Internal.js'
import {Root} from './Root.js'
import {Schema} from './Schema.js'
import {getScope} from './Scope.js'
import {Type} from './Type.js'
import {isValidIdentifier} from './util/Identifiers.js'
import {entries, values} from './util/Objects.js'
import * as paths from './util/Paths.js'
import {Workspace, type WorkspaceInternal} from './Workspace.js'
/** Configuration options */
export interface Config {
/** A schema describing the types of entries */
schema: Schema
/** A record containing workspace configurations */
workspaces: Record<string, Workspace>
/** A url which will be embedded in the dashboard for live previews */
preview?: Preview
/** Every edit will pass through a draft status before being published */
enableDrafts?: boolean
/** The interval in seconds at which the frontend will poll for updates */
syncInterval?: number
/** The base url of the application */
baseUrl?: string | {development?: string; production?: string}
/** The url of the handler endpoint */
handlerUrl?: string
/** The folder where public assets are stored, defaults to /public */
publicDir?: string
/** Filename of the generated dashboard */
dashboardFile?: string
/** Supported interface languages */
interfaceLanguages?: Record<string, typeof en>
auth?: Auth.View
}
export namespace Config {
export function baseUrl(
config: Config,
env = process.env.NODE_ENV ?? 'production'
) {
const result =
typeof config.baseUrl === 'object'
? (config.baseUrl as Record<string, string>)[env]
: config.baseUrl
if (!result) return result
if (result.includes('://')) return result
return `https://${result}`
}
export function mainWorkspace(config: Config): WorkspaceInternal {
const key = Object.keys(config.workspaces)[0]
return getWorkspace(config.workspaces[key])
}
export function type(config: Config, name: string): Type | undefined {
return config.schema[name]
}
export function rootContains(
config: Config,
root: Root,
childType: Type
): boolean {
const allowed = Root.contains(root)
if (allowed.length === 0) return false
const scope = getScope(config)
const typeName = scope.nameOf(childType)
for (const type of allowed) {
if (typeof type === 'string') {
if (type === typeName) return true
} else {
const name = scope.nameOf(type)
if (name === typeName) return true
}
}
return false
}
export function typeContains(
config: Config,
parentType: Type,
childType: Type
): boolean {
const allowed = Type.contains(parentType)
if (allowed.length === 0) return false
const scope = getScope(config)
const typeName = scope.nameOf(childType)
for (const type of allowed) {
if (typeof type === 'string') {
if (type === typeName) return true
} else {
const name = scope.nameOf(type)
if (name === typeName) return true
}
}
return false
}
export function hasAuth(config: Config): boolean {
return Boolean(config.auth)
}
export function multipleWorkspaces(config: Config): boolean {
return Object.keys(config.workspaces).length > 1
}
export function contentDir(config: Config) {
const workspace = mainWorkspace(config)
if (multipleWorkspaces(config)) return paths.dirname(workspace.source)
return workspace.source
}
export function filePath(
config: Config,
workspace: string,
root: string,
locale: string | null,
...rest: Array<string>
) {
const hasMultipleWorkspaces = multipleWorkspaces(config)
let result = ''
if (hasMultipleWorkspaces) result = paths.join(result, workspace)
result = paths.join(result, root)
if (locale) result = paths.join(result, locale.toLowerCase())
return paths.join(result, ...rest)
}
export function validate(config: Config) {
Schema.validate(config.schema)
const all = entries(config.workspaces)
let sourceDir: string
for (const [key, workspace] of all) {
if (!isValidIdentifier(key))
throw new Error(
`Invalid Workspace name "${key}", use only a-z, A-Z, 0-9, and _`
)
Workspace.validate(workspace, config.schema)
if (all.length > 1) {
const source = getWorkspace(workspace).source
const dir = paths.dirname(source)
const name = paths.basename(source)
if (name !== key)
throw new Error(
`Workspace "${key}" source directory "${name}" must be named "${key}"`
)
sourceDir ??= dir
if (sourceDir !== dir)
throw new Error(
`Workspaces "${key}" must be a directory of "${sourceDir}"`
)
}
}
}
export function referencedViews(config: Config): Array<string> {
return Schema.referencedViews(config.schema).concat(
values(config.workspaces).flatMap(Workspace.referencedViews)
)
}
}
const normalized = new WeakSet()
/** Create a new config instance */
export function createConfig<Definition extends Config>(
definition: Definition
) {
if (normalized.has(definition)) return definition
if (definition.schema.MediaFile && definition.schema.MediaFile !== MediaFile)
throw new Error(`"MediaFile" is a reserved Type name`)
if (
definition.schema.MediaLibrary &&
definition.schema.MediaLibrary !== MediaLibrary
)
throw new Error(`"MediaLibrary" is a reserved Type name`)
const res = {
auth: CloudAuthView,
...definition,
publicDir: definition.publicDir ?? '/public',
schema: {...definition.schema, MediaLibrary, MediaFile}
}
Config.validate(res)
normalized.add(res)
return res
}