@stacksjs/defaults 0.75.712 → 0.75.717
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/ai/skills/stacks-dashboard/SKILL.md +20 -6
- package/ai/skills/stacks-dns/SKILL.md +17 -2
- package/ai/skills/stacks-native/CATALOG.md +7 -7
- package/app/Actions/Dashboard/Infrastructure/dns-overview.ts +2 -2
- package/app/Actions/Dashboard/Infrastructure/environment-file.ts +1 -12
- package/functions/dashboard-api.ts +5 -0
- package/ide/vscode/package.json +1 -1
- package/package.json +6 -5
- package/resources/components/Dashboard/Buddy/BuddyAssistant.stx +1 -1
- package/resources/components/Dashboard/DashboardSectionSidebar.stx +12 -3
- package/resources/components/Dashboard/Environment/EnvironmentDashboard.stx +88 -68
- package/resources/components/Dashboard/Infrastructure/DnsDashboard.stx +1 -0
- package/resources/components/Dashboard/Projects/GitHubIssuesDashboard.stx +165 -0
- package/resources/components/Dashboard/Settings/DashboardSettingsIntro.stx +10 -0
- package/resources/components/Dashboard/Settings/DashboardSettingsSearch.stx +32 -0
- package/resources/components/Dashboard/Settings/DnsRecordsSettings.stx +181 -0
- package/resources/components/Dashboard/Settings/MailSettingsDashboard.stx +7 -5
- package/resources/components/Dashboard/Settings/SettingsCodeEditor.stx +27 -0
- package/resources/components/Dashboard/Settings/SettingsDashboard.stx +187 -270
- package/resources/components/Dashboard/Settings/SettingsPicker.stx +87 -0
- package/resources/functions/dashboard/environment-value.ts +11 -0
- package/resources/functions/dashboard/page-availability.ts +1 -1
- package/resources/functions/dashboard/project-config-sections.ts +105 -0
- package/resources/functions/dashboard/section-landing.ts +1 -1
- package/resources/functions/dashboard/settings-panes.ts +26 -0
- package/resources/functions/dashboard/sidebar.ts +7 -7
- package/styles/dashboard-theme.css +281 -25
- package/views/dashboard/composables/dashboard-theme-bootstrap.ts +7 -0
- package/views/dashboard/composables/settings-dialog.ts +22 -0
- package/views/dashboard/composables/settings-window.ts +10 -4
- package/views/dashboard/composables/useProjectConfigEditor.ts +244 -0
- package/views/dashboard/composables/useSettingsWindow.ts +23 -1
- package/views/dashboard/github-issues.stx +18 -0
- package/views/dashboard/layouts/default.stx +1 -1
- package/views/dashboard/layouts/settings.stx +147 -10
- package/views/dashboard/settings/appearance.stx +2 -3
- package/views/dashboard/settings/billing.stx +2 -3
- package/views/dashboard/settings/config/[section].stx +22 -0
- package/views/dashboard/{environment/index.stx → settings/environment.stx} +1 -1
- package/views/dashboard/settings/index.stx +1 -3
- package/views/dashboard/settings/mail.stx +2 -3
- package/views/dashboard/settings/software-update.stx +2 -3
- package/resources/components/Dashboard/CodeEditor.stx +0 -73
- package/resources/components/Dashboard/Settings/DashboardSettingsFrame.stx +0 -101
- package/resources/functions/dashboard/config-io.ts +0 -401
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
<script server>
|
|
2
|
-
import { dashboardPageVisibility } from '@stacksjs/defaults/resources/functions/dashboard/page-availability'
|
|
3
|
-
|
|
4
|
-
const { pane } = defineProps<{ pane: 'general' | 'appearance' | 'mail' | 'billing' | 'software-update' }>()
|
|
5
|
-
|
|
6
|
-
const isDashboardPageAvailable = dashboardPageVisibility()
|
|
7
|
-
export const panes = ([
|
|
8
|
-
{ id: 'general', label: 'General', href: '/settings', icon: 'i-f7-gear-alt-fill', tone: 'grey', description: 'Manage this project’s configuration and environment bindings.', keywords: 'config project environment' },
|
|
9
|
-
{ id: 'appearance', label: 'Appearance', href: '/settings/appearance', icon: 'i-f7-circle-lefthalf-fill', tone: 'purple', description: 'Choose how the dashboard looks and which sections are shown in the sidebar.', keywords: 'theme color mode dark light sidebar display' },
|
|
10
|
-
{ id: 'software-update', label: 'Software Update', href: '/settings/software-update', icon: 'i-f7-arrow-down-circle-fill', tone: 'blue', description: 'Check for Stacks releases and update your projects through Buddy.', keywords: 'upgrade framework version install dependencies' },
|
|
11
|
-
{ id: 'mail', label: 'Mail', href: '/settings/mail', icon: 'i-f7-envelope-fill', tone: 'blue', description: 'Set the sender, delivery driver, and credentials for outgoing mail.', keywords: 'email smtp sender transport' },
|
|
12
|
-
{ id: 'billing', label: 'Billing', href: '/settings/billing', icon: 'i-f7-creditcard-fill', tone: 'green', description: 'View your subscription, payment methods, and transactions.', keywords: 'subscription payment transaction invoice' },
|
|
13
|
-
] as const).filter(item => isDashboardPageAvailable(item.href))
|
|
14
|
-
export const activePane = panes.find(item => item.id === pane) ?? panes[0]
|
|
15
|
-
defineClientPayload({ panes, activePane })
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
<script client>
|
|
19
|
-
import { dragSettingsWindow, isNativeSettingsWindow, returnToDashboard } from '@stacksjs/defaults/views/dashboard/composables/settings-window'
|
|
20
|
-
import { useSettingsWindow } from '@stacksjs/defaults/views/dashboard/composables/useSettingsWindow'
|
|
21
|
-
|
|
22
|
-
const settingsWindow = useSettingsWindow
|
|
23
|
-
const query = settingsWindow.query
|
|
24
|
-
const search = useRef<HTMLInputElement>('settingsSearch')
|
|
25
|
-
const native = state(isNativeSettingsWindow())
|
|
26
|
-
const entries = {{ panes }}
|
|
27
|
-
const selectedHref = {{ activePane.href }}
|
|
28
|
-
const matching = derived(() => {
|
|
29
|
-
const searchText = query().trim().toLowerCase()
|
|
30
|
-
return entries.filter(({ label, keywords }) => `${label.toLowerCase()} ${keywords}`.includes(searchText))
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
function matches(id: string): boolean {
|
|
34
|
-
return matching().some(item => item.id === id)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function openSearchResult(): void {
|
|
38
|
-
const [result, another] = matching()
|
|
39
|
-
if (result && !another && result.href !== selectedHref)
|
|
40
|
-
navigate(result.href)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
useEventListener('craft:ready', () => native.set(isNativeSettingsWindow()))
|
|
44
|
-
useEventListener('keydown', (event) => {
|
|
45
|
-
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'f') {
|
|
46
|
-
event.preventDefault()
|
|
47
|
-
search.current?.focus()
|
|
48
|
-
search.current?.select()
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
</script>
|
|
52
|
-
|
|
53
|
-
<div data-dashboard-settings class="dashboard-settings" :data-settings-native="String(native())">
|
|
54
|
-
<aside class="dashboard-settings-sidebar">
|
|
55
|
-
<div class="dashboard-settings-sidebar-chrome">
|
|
56
|
-
<SidebarHeader windowControls="native" />
|
|
57
|
-
<a href="/" class="dashboard-settings-back" @click="returnToDashboard($event)">
|
|
58
|
-
<span class="i-f7-chevron-left" aria-hidden="true"></span>
|
|
59
|
-
Dashboard
|
|
60
|
-
</a>
|
|
61
|
-
</div>
|
|
62
|
-
<form class="dashboard-settings-search" role="search" @submit.prevent="openSearchResult()">
|
|
63
|
-
<span class="i-f7-search" aria-hidden="true"></span>
|
|
64
|
-
<input x-model="query" ref="settingsSearch" type="search" placeholder="Search" aria-label="Search settings" autocomplete="off">
|
|
65
|
-
</form>
|
|
66
|
-
<nav aria-label="Settings">
|
|
67
|
-
<SettingsSidebarGroup>
|
|
68
|
-
@foreach(panes as item)
|
|
69
|
-
<a href="{{ item.href }}" data-stx-link class="dashboard-settings-nav-item" :hidden="!matches('{{ item.id }}')"
|
|
70
|
-
@if(item.id === activePane.id)aria-current="page"@endif>
|
|
71
|
-
<span class="dashboard-settings-tile" data-tone="{{ item.tone }}" aria-hidden="true">
|
|
72
|
-
<span class="{{ item.icon }}"></span>
|
|
73
|
-
</span>
|
|
74
|
-
{{ item.label }}
|
|
75
|
-
</a>
|
|
76
|
-
@endforeach
|
|
77
|
-
</SettingsSidebarGroup>
|
|
78
|
-
<p :if="matching().length === 0" class="dashboard-settings-search-empty" role="status">No matching settings</p>
|
|
79
|
-
</nav>
|
|
80
|
-
</aside>
|
|
81
|
-
|
|
82
|
-
<div class="dashboard-settings-detail">
|
|
83
|
-
<header class="dashboard-settings-toolbar" @mousedown="dragSettingsWindow($event)">
|
|
84
|
-
<div class="dashboard-settings-history" role="group" aria-label="Settings history">
|
|
85
|
-
<button type="button" aria-label="Back" :disabled="settingsWindow.index() === 0" @click="goBack()">
|
|
86
|
-
<span class="i-f7-chevron-left" aria-hidden="true"></span>
|
|
87
|
-
</button>
|
|
88
|
-
<button type="button" aria-label="Forward" :disabled="settingsWindow.index() >= settingsWindow.lastIndex()" @click="goForward()">
|
|
89
|
-
<span class="i-f7-chevron-right" aria-hidden="true"></span>
|
|
90
|
-
</button>
|
|
91
|
-
</div>
|
|
92
|
-
<h1>{{ activePane.label }}</h1>
|
|
93
|
-
</header>
|
|
94
|
-
<div class="dashboard-settings-pane" data-stx-scroll="settings">
|
|
95
|
-
<div class="dashboard-settings-inner">
|
|
96
|
-
<SettingsPaneHeader :title="activePane.label" :description="activePane.description" :icon="activePane.icon" :tone="activePane.tone" />
|
|
97
|
-
<slot />
|
|
98
|
-
</div>
|
|
99
|
-
</div>
|
|
100
|
-
</div>
|
|
101
|
-
</div>
|
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Read & write `config/*.ts` files from the dashboard.
|
|
3
|
-
*
|
|
4
|
-
* Why this exists
|
|
5
|
-
* ---------------
|
|
6
|
-
* Config files use `export default { ... } satisfies XConfig` and pull
|
|
7
|
-
* defaults from `env.*`. The settings page needs to:
|
|
8
|
-
* - List every config file
|
|
9
|
-
* - Show its currently-resolved values (env-substituted)
|
|
10
|
-
* - Persist edits back to the source `.ts` so they survive restarts
|
|
11
|
-
*
|
|
12
|
-
* Reading is straightforward: `await import(file)` returns the evaluated
|
|
13
|
-
* object. Writing is harder — we want to preserve comments, type imports,
|
|
14
|
-
* and the `env.*` references intact. Instead of regenerating the file,
|
|
15
|
-
* we mutate the source text in place: find the line containing
|
|
16
|
-
* `<key>: <oldValue>` and rewrite the value. This keeps the file
|
|
17
|
-
* round-trippable and human-readable.
|
|
18
|
-
*
|
|
19
|
-
* Edits supported (top-level scalar keys only):
|
|
20
|
-
* - string → string (single- or double-quoted)
|
|
21
|
-
* - number → number
|
|
22
|
-
* - boolean → boolean
|
|
23
|
-
*
|
|
24
|
-
* Edits NOT supported (the settings UI surfaces a read-only badge):
|
|
25
|
-
* - nested objects, arrays
|
|
26
|
-
* - keys whose current value is a function call or expression
|
|
27
|
-
* (`env.X ?? 'y'`) — those need to be edited via env, not config
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import { chmodSync, existsSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
31
|
-
import { randomUUID } from 'node:crypto'
|
|
32
|
-
import { basename, dirname, join, resolve } from 'node:path'
|
|
33
|
-
import { pathToFileURL } from 'node:url'
|
|
34
|
-
import process from 'node:process'
|
|
35
|
-
|
|
36
|
-
let projectRootCache: string | null = null
|
|
37
|
-
function projectRoot(): string {
|
|
38
|
-
if (projectRootCache) return projectRootCache
|
|
39
|
-
let dir = process.cwd()
|
|
40
|
-
for (let i = 0; i < 8; i++) {
|
|
41
|
-
if (existsSync(resolve(dir, 'config'))) {
|
|
42
|
-
projectRootCache = dir
|
|
43
|
-
return dir
|
|
44
|
-
}
|
|
45
|
-
const parent = resolve(dir, '..')
|
|
46
|
-
if (parent === dir) break
|
|
47
|
-
dir = parent
|
|
48
|
-
}
|
|
49
|
-
projectRootCache = process.cwd()
|
|
50
|
-
return projectRootCache
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function configDir(): string {
|
|
54
|
-
return resolve(projectRoot(), 'config')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface ConfigFileSummary {
|
|
58
|
-
/** Filename without extension, e.g. 'email'. */
|
|
59
|
-
name: string
|
|
60
|
-
/** Display title, e.g. 'Email'. */
|
|
61
|
-
title: string
|
|
62
|
-
/** Bytes - UI uses this to show "small / large config" hints. */
|
|
63
|
-
size: number
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Enumerate every `.ts` config file. Hidden / non-ts files are skipped. */
|
|
67
|
-
export function listConfigFiles(dir = configDir()): ConfigFileSummary[] {
|
|
68
|
-
if (!existsSync(dir))
|
|
69
|
-
throw new Error(`Could not list dashboard configuration: ${dir} does not exist`)
|
|
70
|
-
|
|
71
|
-
let entries: ReturnType<typeof readdirSync>
|
|
72
|
-
try {
|
|
73
|
-
entries = readdirSync(dir, { withFileTypes: true })
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
throw new Error(`Could not list dashboard configuration: ${error instanceof Error ? error.message : String(error)}`)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const out: ConfigFileSummary[] = []
|
|
80
|
-
for (const entry of entries) {
|
|
81
|
-
if (!entry.isFile() || !entry.name.endsWith('.ts') || entry.name.startsWith('.'))
|
|
82
|
-
continue
|
|
83
|
-
|
|
84
|
-
const path = join(dir, entry.name)
|
|
85
|
-
let size: number
|
|
86
|
-
try {
|
|
87
|
-
size = statSync(path).size
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
throw new Error(`Could not read dashboard configuration ${entry.name}: ${error instanceof Error ? error.message : String(error)}`)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const name = entry.name.replace(/\.ts$/, '')
|
|
94
|
-
out.push({ name, title: titleCase(name), size })
|
|
95
|
-
}
|
|
96
|
-
return out.sort((a, b) => a.name.localeCompare(b.name))
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Cache of imported config modules. We re-import on every call when the
|
|
101
|
-
* file's mtime has changed so the dashboard reflects edits immediately,
|
|
102
|
-
* but skip the cost when nothing has changed in a tight burst of reads.
|
|
103
|
-
*/
|
|
104
|
-
const moduleCache = new Map<string, { mtimeMs: number, value: any }>()
|
|
105
|
-
|
|
106
|
-
export interface ReadResult {
|
|
107
|
-
/** The fully-resolved default export, with env values substituted. */
|
|
108
|
-
values: any
|
|
109
|
-
/** Raw source text (used by the editor + the writer). */
|
|
110
|
-
source: string
|
|
111
|
-
/** Per-key metadata: what's editable, what's read-only and why. */
|
|
112
|
-
fields: ConfigField[]
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface ConfigField {
|
|
116
|
-
key: string
|
|
117
|
-
/** The currently-resolved value (post env substitution). */
|
|
118
|
-
value: any
|
|
119
|
-
/** 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null' */
|
|
120
|
-
type: string
|
|
121
|
-
/** True when this scalar can be safely round-tripped on write. */
|
|
122
|
-
editable: boolean
|
|
123
|
-
/** Human-readable reason when editable === false. */
|
|
124
|
-
reason?: string
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** Read one config file by name (e.g. 'email'). */
|
|
128
|
-
export async function readConfig(name: string): Promise<ReadResult | null> {
|
|
129
|
-
assertConfigName(name)
|
|
130
|
-
const path = join(configDir(), `${name}.ts`)
|
|
131
|
-
if (!existsSync(path)) return null
|
|
132
|
-
|
|
133
|
-
let mtimeMs: number
|
|
134
|
-
try {
|
|
135
|
-
mtimeMs = (await Bun.file(path).stat()).mtimeMs
|
|
136
|
-
}
|
|
137
|
-
catch (error) {
|
|
138
|
-
throw new Error(`Failed to stat config/${name}.ts: ${error instanceof Error ? error.message : String(error)}`)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Read the source as the source-of-truth for the field listing.
|
|
142
|
-
const source = readFileSync(path, 'utf8')
|
|
143
|
-
|
|
144
|
-
let modVal: any
|
|
145
|
-
const cached = moduleCache.get(path)
|
|
146
|
-
if (cached?.mtimeMs === mtimeMs) {
|
|
147
|
-
modVal = cached.value
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
try {
|
|
151
|
-
const moduleUrl = pathToFileURL(path)
|
|
152
|
-
moduleUrl.searchParams.set('dashboard-config-mtime', String(mtimeMs))
|
|
153
|
-
const mod = await import(moduleUrl.href)
|
|
154
|
-
modVal = mod?.default ?? mod
|
|
155
|
-
moduleCache.set(path, { mtimeMs, value: modVal })
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
throw new Error(`Failed to load config/${name}.ts: ${(err as Error).message}`)
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Literal overlays also cover filesystems whose timestamp precision is too
|
|
163
|
-
// coarse to distinguish two edits made in the same tick.
|
|
164
|
-
modVal = applySourceOverrides(modVal, source)
|
|
165
|
-
const fields = describeFields(modVal, source)
|
|
166
|
-
return { values: modVal, source, fields }
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Update a single top-level key in a config file. Returns the rewritten
|
|
171
|
-
* source on success, or throws when the key is non-editable (nested,
|
|
172
|
-
* env-backed, etc.) — caller should surface that to the UI.
|
|
173
|
-
*/
|
|
174
|
-
export interface UpdateResult {
|
|
175
|
-
ok: boolean
|
|
176
|
-
newValue: any
|
|
177
|
-
source: string
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export interface ConfigUpdate {
|
|
181
|
-
key: string
|
|
182
|
-
value: string | number | boolean
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export interface BatchUpdateResult {
|
|
186
|
-
ok: true
|
|
187
|
-
updates: ConfigUpdate[]
|
|
188
|
-
source: string
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export async function updateConfigKey(
|
|
192
|
-
name: string,
|
|
193
|
-
key: string,
|
|
194
|
-
newValue: string | number | boolean,
|
|
195
|
-
): Promise<UpdateResult> {
|
|
196
|
-
const result = await updateConfigKeys(name, [{ key, value: newValue }])
|
|
197
|
-
return { ok: true, newValue, source: result.source }
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Validate and apply a group of config edits as one filesystem operation.
|
|
202
|
-
* Every rewrite is prepared in memory first, so one invalid field prevents
|
|
203
|
-
* all fields from being persisted. The completed source is then swapped into
|
|
204
|
-
* place with a same-directory rename.
|
|
205
|
-
*/
|
|
206
|
-
export async function updateConfigKeys(
|
|
207
|
-
name: string,
|
|
208
|
-
updates: ConfigUpdate[],
|
|
209
|
-
): Promise<BatchUpdateResult> {
|
|
210
|
-
assertConfigName(name)
|
|
211
|
-
const path = join(configDir(), `${name}.ts`)
|
|
212
|
-
if (!existsSync(path))
|
|
213
|
-
throw new Error(`config/${name}.ts not found`)
|
|
214
|
-
|
|
215
|
-
const source = readFileSync(path, 'utf8')
|
|
216
|
-
const rewritten = rewriteConfigKeys(source, updates, `config/${name}.ts`)
|
|
217
|
-
atomicWrite(path, rewritten)
|
|
218
|
-
return { ok: true, updates, source: rewritten }
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Walk the source text for top-level scalar literal assignments and
|
|
223
|
-
* overlay them onto the given values object. This lets us reflect
|
|
224
|
-
* post-write edits without paying the cost (and risk) of re-importing
|
|
225
|
-
* the whole config module after every keystroke.
|
|
226
|
-
*/
|
|
227
|
-
function applySourceOverrides(values: any, source: string): any {
|
|
228
|
-
if (!values || typeof values !== 'object' || Array.isArray(values)) return values
|
|
229
|
-
const out: Record<string, any> = { ...values }
|
|
230
|
-
for (const key of Object.keys(out)) {
|
|
231
|
-
const re = scalarLiteralRegex(key)
|
|
232
|
-
const m = re.exec(source)
|
|
233
|
-
if (!m) continue
|
|
234
|
-
const literal = m[4]
|
|
235
|
-
const parsed = parseLiteral(literal)
|
|
236
|
-
if (parsed !== UNSUPPORTED) out[key] = parsed
|
|
237
|
-
}
|
|
238
|
-
return out
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const UNSUPPORTED = Symbol('unsupported')
|
|
242
|
-
function parseLiteral(literal: string): any {
|
|
243
|
-
if (literal === 'true') return true
|
|
244
|
-
if (literal === 'false') return false
|
|
245
|
-
if (literal === 'null') return null
|
|
246
|
-
if (/^-?\d+(?:\.\d+)?$/.test(literal)) return Number(literal)
|
|
247
|
-
if ((literal.startsWith('\'') && literal.endsWith('\'')) || (literal.startsWith('"') && literal.endsWith('"'))) {
|
|
248
|
-
return literal.slice(1, -1)
|
|
249
|
-
}
|
|
250
|
-
return UNSUPPORTED
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/* -------------------------------------------------------------------------- */
|
|
254
|
-
/* Internals */
|
|
255
|
-
/* -------------------------------------------------------------------------- */
|
|
256
|
-
|
|
257
|
-
function titleCase(name: string): string {
|
|
258
|
-
const acronyms: Record<string, string> = {
|
|
259
|
-
ai: 'AI',
|
|
260
|
-
cli: 'CLI',
|
|
261
|
-
cms: 'CMS',
|
|
262
|
-
dns: 'DNS',
|
|
263
|
-
saas: 'SaaS',
|
|
264
|
-
sms: 'SMS',
|
|
265
|
-
ui: 'UI',
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return name
|
|
269
|
-
.split(/[-_]/)
|
|
270
|
-
.map(segment => acronyms[segment.toLowerCase()] || segment.charAt(0).toUpperCase() + segment.slice(1))
|
|
271
|
-
.join(' ')
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function assertConfigName(name: string): void {
|
|
275
|
-
if (!/^[\w-]+$/.test(name))
|
|
276
|
-
throw new Error('Invalid config name')
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function describeFields(values: any, source: string): ConfigField[] {
|
|
280
|
-
if (!values || typeof values !== 'object' || Array.isArray(values)) return []
|
|
281
|
-
const out: ConfigField[] = []
|
|
282
|
-
for (const [key, value] of Object.entries(values)) {
|
|
283
|
-
const type = typeOf(value)
|
|
284
|
-
const editable = isScalar(type)
|
|
285
|
-
let reason: string | undefined
|
|
286
|
-
if (!editable) {
|
|
287
|
-
reason = type === 'object' || type === 'array'
|
|
288
|
-
? 'Nested values can be edited by hand in the file'
|
|
289
|
-
: 'Unsupported type'
|
|
290
|
-
}
|
|
291
|
-
else if (!hasScalarLiteral(source, key)) {
|
|
292
|
-
// The source value is a non-literal expression like `env.FOO ?? 'x'`.
|
|
293
|
-
// We can't safely rewrite it without losing the env fallback.
|
|
294
|
-
// Mark as read-only and tell the user to set the env var instead.
|
|
295
|
-
out.push({
|
|
296
|
-
key,
|
|
297
|
-
value,
|
|
298
|
-
type,
|
|
299
|
-
editable: false,
|
|
300
|
-
reason: 'Defined via env var or expression. Set the environment variable to change it.',
|
|
301
|
-
})
|
|
302
|
-
continue
|
|
303
|
-
}
|
|
304
|
-
out.push({ key, value, type, editable, reason })
|
|
305
|
-
}
|
|
306
|
-
return out
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function typeOf(v: any): string {
|
|
310
|
-
if (v === null) return 'null'
|
|
311
|
-
if (Array.isArray(v)) return 'array'
|
|
312
|
-
return typeof v
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function isScalar(t: string): boolean {
|
|
316
|
-
return t === 'string' || t === 'number' || t === 'boolean'
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Detects whether a top-level key in the export-default block is set
|
|
321
|
-
* directly to a scalar literal — `key: 'value'`, `key: 42`, `key: true`.
|
|
322
|
-
*
|
|
323
|
-
* The match is intentionally conservative: it only succeeds when the
|
|
324
|
-
* value following the colon is a single string/number/boolean literal,
|
|
325
|
-
* optionally followed by a comma. Anything more complex
|
|
326
|
-
* (`env.X ?? 'y'`, `[1, 2]`, `{ a: 1 }`, multi-line arrays, etc.)
|
|
327
|
-
* fails so we don't accidentally clobber an expression.
|
|
328
|
-
*/
|
|
329
|
-
function hasScalarLiteral(source: string, key: string): boolean {
|
|
330
|
-
const re = scalarLiteralRegex(key)
|
|
331
|
-
return re.test(source)
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function scalarLiteralRegex(key: string): RegExp {
|
|
335
|
-
const k = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
336
|
-
// Matches:
|
|
337
|
-
// <indent><key>: 'value' OR "value" OR 123 OR true/false/null
|
|
338
|
-
// followed by optional comma + line-ending. The line MUST not contain
|
|
339
|
-
// operators (??, ||, &&, +, ?), function calls, or sub-objects.
|
|
340
|
-
return new RegExp(
|
|
341
|
-
`^(\\s*)(${k})(\\s*:\\s*)('[^'\\n]*'|"[^"\\n]*"|-?\\d+(?:\\.\\d+)?|true|false|null)(\\s*,?\\s*)$`,
|
|
342
|
-
'm',
|
|
343
|
-
)
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
export function rewriteConfigKeys(
|
|
347
|
-
source: string,
|
|
348
|
-
updates: ConfigUpdate[],
|
|
349
|
-
label = 'config source',
|
|
350
|
-
): string {
|
|
351
|
-
if (updates.length === 0)
|
|
352
|
-
throw new Error('No configuration updates supplied')
|
|
353
|
-
|
|
354
|
-
const seen = new Set<string>()
|
|
355
|
-
let rewritten = source
|
|
356
|
-
for (const { key, value } of updates) {
|
|
357
|
-
if (!key || seen.has(key))
|
|
358
|
-
throw new Error(`Duplicate or empty configuration key "${key}"`)
|
|
359
|
-
seen.add(key)
|
|
360
|
-
|
|
361
|
-
const next = rewriteKey(rewritten, key, value)
|
|
362
|
-
if (!next)
|
|
363
|
-
throw new Error(`Cannot edit "${key}" in ${label}. The key is not a top-level scalar literal (likely env-backed or nested).`)
|
|
364
|
-
rewritten = next
|
|
365
|
-
}
|
|
366
|
-
return rewritten
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function rewriteKey(source: string, key: string, newValue: string | number | boolean): string | null {
|
|
370
|
-
const re = scalarLiteralRegex(key)
|
|
371
|
-
if (!re.test(source)) return null
|
|
372
|
-
const literal = serializeScalar(newValue)
|
|
373
|
-
return source.replace(re, (_full, indent, k, sep, _old, tail) => {
|
|
374
|
-
return `${indent}${k}${sep}${literal}${tail}`
|
|
375
|
-
})
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
function atomicWrite(path: string, source: string): void {
|
|
379
|
-
const tempPath = join(dirname(path), `.${basename(path)}.${randomUUID()}.tmp`)
|
|
380
|
-
try {
|
|
381
|
-
const mode = statSync(path).mode
|
|
382
|
-
writeFileSync(tempPath, source, { encoding: 'utf8', flag: 'wx', mode })
|
|
383
|
-
chmodSync(tempPath, mode)
|
|
384
|
-
renameSync(tempPath, path)
|
|
385
|
-
}
|
|
386
|
-
catch (error) {
|
|
387
|
-
if (existsSync(tempPath))
|
|
388
|
-
unlinkSync(tempPath)
|
|
389
|
-
throw error
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
function serializeScalar(v: string | number | boolean): string {
|
|
394
|
-
if (typeof v === 'string') {
|
|
395
|
-
// Prefer single quotes; fall back to double if the value contains a
|
|
396
|
-
// bare apostrophe (avoid escape gymnastics).
|
|
397
|
-
if (v.includes('\'') && !v.includes('"')) return `"${v}"`
|
|
398
|
-
return `'${v.replace(/'/g, '\\\'')}'`
|
|
399
|
-
}
|
|
400
|
-
return String(v)
|
|
401
|
-
}
|