@astrale-os/cli 1.0.0-beta.12 → 1.0.0-beta.13
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/dist/astrale.js +15432 -14745
- package/dist/types/admin/contract.d.ts +26 -0
- package/dist/types/admin/instance/client.d.ts +2 -4
- package/dist/types/admin/instance/model.d.ts +3 -0
- package/package.json +1 -1
- package/src/admin/.spec/architecture.md +12 -5
- package/src/admin/__tests__/fixture.ts +19 -95
- package/src/admin/catalog/.spec/api.d.ts +0 -2
- package/src/admin/catalog/.spec/architecture.md +5 -4
- package/src/admin/catalog/__tests__/client.test.ts +136 -33
- package/src/admin/catalog/client.ts +38 -61
- package/src/admin/contract.ts +46 -0
- package/src/admin/instance/.spec/api.d.ts +4 -8
- package/src/admin/instance/.spec/architecture.md +7 -7
- package/src/admin/instance/__tests__/client.test.ts +138 -31
- package/src/admin/instance/client.ts +32 -37
- package/src/admin/instance/model.ts +3 -0
- package/src/commands/__tests__/call.test.ts +34 -0
- package/src/commands/__tests__/read-commands.test.ts +27 -0
- package/src/commands/__tests__/token-ttl.test.ts +49 -4
- package/src/commands/call.ts +28 -3
- package/src/commands/query.ts +8 -2
- package/src/commands/token.ts +34 -20
- package/src/commands/ui/__tests__/commands.test.ts +129 -0
- package/src/commands/ui/add.ts +51 -0
- package/src/commands/ui/doctor.ts +13 -0
- package/src/commands/ui/init.ts +38 -0
- package/src/commands/ui/list.ts +26 -0
- package/src/commands/ui/preset-apply.ts +19 -0
- package/src/commands/ui/preset-list.ts +12 -0
- package/src/commands/ui/shared.ts +25 -0
- package/src/lib/__tests__/binary.test.ts +16 -1
- package/src/lib/binary.ts +22 -5
- package/src/lib/proc.ts +7 -2
- package/src/program/.spec/api.d.ts +1 -0
- package/src/program/__tests__/program.test.ts +32 -2
- package/src/program/build.ts +23 -1
- package/src/program/command.ts +1 -0
- package/src/program/registry.ts +2 -1
- package/src/ui/.spec/api.d.ts +14 -0
- package/src/ui/.spec/architecture.md +10 -0
- package/src/ui/.spec/laws.ts +36 -0
- package/src/ui/.spec/layout.ts +16 -0
- package/src/ui/__tests__/ui.test.ts +542 -0
- package/src/ui/index.ts +13 -0
- package/src/ui/lock.ts +87 -0
- package/src/ui/model.ts +83 -0
- package/src/ui/operations.ts +539 -0
- package/src/ui/project.ts +146 -0
- package/src/ui/release.ts +267 -0
- package/src/ui/runner.ts +18 -0
- package/studio/server/agent/harness/gateway/token.test.ts +1 -1
- package/studio/server/agent/harness/gateway/token.ts +3 -3
- package/dist/types/admin/binding.d.ts +0 -19
- package/src/admin/__tests__/binding.test.ts +0 -31
- package/src/admin/binding.ts +0 -98
package/src/ui/model.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AstraleError } from '../errors'
|
|
2
|
+
|
|
3
|
+
export const UI_PACKAGE = '@astrale-os/ui'
|
|
4
|
+
export const UI_REPOSITORY = 'astrale-os/ui'
|
|
5
|
+
export const UI_LOCK_FILE = 'astrale-ui.lock.json'
|
|
6
|
+
export const UI_PRESETS = ['astrale', 'compact', 'expressive'] as const
|
|
7
|
+
|
|
8
|
+
export type UiPreset = (typeof UI_PRESETS)[number]
|
|
9
|
+
export type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun'
|
|
10
|
+
|
|
11
|
+
export type UiCompatibility = {
|
|
12
|
+
version: 1
|
|
13
|
+
shadcn: string
|
|
14
|
+
base: 'base'
|
|
15
|
+
style: 'nova'
|
|
16
|
+
baseUi: string
|
|
17
|
+
react: string
|
|
18
|
+
tailwind: string
|
|
19
|
+
presets: UiPreset[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type UiRegistryFile = {
|
|
23
|
+
path: string
|
|
24
|
+
type: string
|
|
25
|
+
target?: string
|
|
26
|
+
content?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type UiRegistryItem = {
|
|
30
|
+
name: string
|
|
31
|
+
type: string
|
|
32
|
+
title?: string
|
|
33
|
+
description?: string
|
|
34
|
+
dependencies?: string[]
|
|
35
|
+
files: UiRegistryFile[]
|
|
36
|
+
meta: { canonicalAddress: string; ownership?: string }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type UiRegistry = { name: string; homepage?: string; items: UiRegistryItem[] }
|
|
40
|
+
|
|
41
|
+
export type UiLockItem = {
|
|
42
|
+
address: string
|
|
43
|
+
sourceDigest: string
|
|
44
|
+
files: Record<string, string>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type UiLock = {
|
|
48
|
+
$schema: string
|
|
49
|
+
version: 1
|
|
50
|
+
package: { name: typeof UI_PACKAGE; version: string }
|
|
51
|
+
registry: { repository: typeof UI_REPOSITORY; ref: string; commit: string }
|
|
52
|
+
tooling: { shadcn: string; base: 'base'; style: 'nova'; baseUi: string }
|
|
53
|
+
preset: UiPreset
|
|
54
|
+
items: Record<string, UiLockItem>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type UiRelease = {
|
|
58
|
+
version: string
|
|
59
|
+
ref: string
|
|
60
|
+
commit: string
|
|
61
|
+
compatibility: UiCompatibility
|
|
62
|
+
registry: UiRegistry
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class UiError extends AstraleError {
|
|
66
|
+
constructor(
|
|
67
|
+
code:
|
|
68
|
+
| 'UI_PROJECT_UNSUPPORTED'
|
|
69
|
+
| 'UI_CONFIG_MISSING'
|
|
70
|
+
| 'UI_REGISTRY_UNAVAILABLE'
|
|
71
|
+
| 'UI_ITEM_NOT_FOUND'
|
|
72
|
+
| 'UI_ITEM_CONFLICT'
|
|
73
|
+
| 'UI_LOCAL_CHANGES'
|
|
74
|
+
| 'UI_DEPENDENCY_INSTALL_FAILED'
|
|
75
|
+
| 'UI_LOCK_INVALID'
|
|
76
|
+
| 'UI_TOOL_FAILED',
|
|
77
|
+
message: string,
|
|
78
|
+
hint?: string,
|
|
79
|
+
options?: ErrorOptions,
|
|
80
|
+
) {
|
|
81
|
+
super(code, message, hint, options)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { access, lstat, mkdir, readFile, realpath, rm, writeFile } from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { digest, parseUiLock, readUiLock } from './lock'
|
|
5
|
+
import {
|
|
6
|
+
UI_PACKAGE,
|
|
7
|
+
UI_PRESETS,
|
|
8
|
+
UiError,
|
|
9
|
+
type UiLock,
|
|
10
|
+
type UiPreset,
|
|
11
|
+
type UiRegistryItem,
|
|
12
|
+
type UiRelease,
|
|
13
|
+
} from './model'
|
|
14
|
+
import {
|
|
15
|
+
assertSupportedUiProject,
|
|
16
|
+
discoverUiProject,
|
|
17
|
+
projectRelative,
|
|
18
|
+
type UiProject,
|
|
19
|
+
} from './project'
|
|
20
|
+
import {
|
|
21
|
+
readUiRegistryItem,
|
|
22
|
+
readUiReleaseSnapshot,
|
|
23
|
+
registryItemUrl,
|
|
24
|
+
resolveUiRelease,
|
|
25
|
+
} from './release'
|
|
26
|
+
import { defaultUiRunner, shadcnInvocation, type UiRunner } from './runner'
|
|
27
|
+
|
|
28
|
+
type Dependencies = { fetcher?: typeof fetch; runner?: UiRunner }
|
|
29
|
+
|
|
30
|
+
async function exists(target: string): Promise<boolean> {
|
|
31
|
+
return access(target).then(
|
|
32
|
+
() => true,
|
|
33
|
+
() => false,
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function readOptional(target: string): Promise<string | undefined> {
|
|
38
|
+
return readFile(target, 'utf8').catch(() => undefined)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function manifestDependencies(manifest: Record<string, unknown>): Record<string, string> {
|
|
42
|
+
return { ...(manifest.dependencies as Record<string, string> | undefined) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type InitUiOptions = {
|
|
46
|
+
path?: string
|
|
47
|
+
preset?: UiPreset
|
|
48
|
+
version?: string
|
|
49
|
+
dryRun?: boolean
|
|
50
|
+
force?: boolean
|
|
51
|
+
install?: boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function initUi(
|
|
55
|
+
options: InitUiOptions,
|
|
56
|
+
dependencies: Dependencies = {},
|
|
57
|
+
): Promise<Record<string, unknown>> {
|
|
58
|
+
const project = await discoverUiProject(options.path)
|
|
59
|
+
assertSupportedUiProject(project)
|
|
60
|
+
const preset = options.preset ?? 'astrale'
|
|
61
|
+
if (!UI_PRESETS.includes(preset)) {
|
|
62
|
+
throw new UiError('UI_PROJECT_UNSUPPORTED', 'Unknown UI preset: ' + preset)
|
|
63
|
+
}
|
|
64
|
+
if ((await exists(project.uiLockPath)) && !options.force) {
|
|
65
|
+
const lock = await readUiLock(project.uiLockPath)
|
|
66
|
+
const css = (await readOptional(project.cssPath)) ?? ''
|
|
67
|
+
const components = await readOptional(project.componentsPath)
|
|
68
|
+
.then((value) => (value ? (JSON.parse(value) as Record<string, unknown>) : undefined))
|
|
69
|
+
.catch(() => undefined)
|
|
70
|
+
const requestedVersion = options.version?.replace(/^v/u, '')
|
|
71
|
+
const desired =
|
|
72
|
+
(!requestedVersion || requestedVersion === lock.package.version) &&
|
|
73
|
+
(!options.preset || options.preset === lock.preset) &&
|
|
74
|
+
css.includes(UI_PACKAGE + '/theme.css') &&
|
|
75
|
+
css.includes(UI_PACKAGE + '/presets/' + lock.preset + '.css') &&
|
|
76
|
+
components?.style === 'base-nova'
|
|
77
|
+
if (!desired) {
|
|
78
|
+
throw new UiError(
|
|
79
|
+
'UI_ITEM_CONFLICT',
|
|
80
|
+
'Existing Astrale UI initialization differs from the requested state.',
|
|
81
|
+
'Run astrale ui doctor, then repeat init with --force after reviewing the changes.',
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
return { status: 'unchanged', root: project.root, lock }
|
|
85
|
+
}
|
|
86
|
+
const release = await resolveUiRelease(options.version, dependencies.fetcher)
|
|
87
|
+
const cssRelative = projectRelative(project, project.cssPath)
|
|
88
|
+
const plan = {
|
|
89
|
+
status: options.dryRun ? 'planned' : 'initialized',
|
|
90
|
+
root: project.root,
|
|
91
|
+
package: UI_PACKAGE + '@' + release.version,
|
|
92
|
+
manager: project.manager,
|
|
93
|
+
preset,
|
|
94
|
+
files: [
|
|
95
|
+
'package.json',
|
|
96
|
+
cssRelative,
|
|
97
|
+
'components.json',
|
|
98
|
+
'astrale-ui.lock.json',
|
|
99
|
+
...(project.lockPath ? [projectRelative(project, project.lockPath)] : []),
|
|
100
|
+
],
|
|
101
|
+
tooling: {
|
|
102
|
+
shadcn: release.compatibility.shadcn,
|
|
103
|
+
base: release.compatibility.base,
|
|
104
|
+
style: release.compatibility.style,
|
|
105
|
+
baseUi: release.compatibility.baseUi,
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
if (options.dryRun) return plan
|
|
109
|
+
|
|
110
|
+
const mutationPaths = [
|
|
111
|
+
project.packageJsonPath,
|
|
112
|
+
project.cssPath,
|
|
113
|
+
project.componentsPath,
|
|
114
|
+
project.uiLockPath,
|
|
115
|
+
...(project.lockPath ? [project.lockPath] : []),
|
|
116
|
+
]
|
|
117
|
+
const snapshots = new Map<string, string | undefined>()
|
|
118
|
+
for (const target of mutationPaths) snapshots.set(target, await readOptional(target))
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const manifest = { ...project.packageJson }
|
|
122
|
+
manifest.dependencies = {
|
|
123
|
+
...manifestDependencies(manifest),
|
|
124
|
+
[UI_PACKAGE]: release.version,
|
|
125
|
+
}
|
|
126
|
+
await writeJson(project.packageJsonPath, manifest)
|
|
127
|
+
|
|
128
|
+
const currentCss = (await readOptional(project.cssPath)) ?? ''
|
|
129
|
+
const imports = [
|
|
130
|
+
"@import '" + UI_PACKAGE + "/theme.css';",
|
|
131
|
+
"@import '" + UI_PACKAGE + '/presets/' + preset + ".css';",
|
|
132
|
+
]
|
|
133
|
+
const withoutAstrale = currentCss
|
|
134
|
+
.replace(
|
|
135
|
+
/^@import\s+['"]@astrale-os\/ui\/(?:theme\.css|presets\/[a-z-]+\.css)['"];?\s*$/gmu,
|
|
136
|
+
'',
|
|
137
|
+
)
|
|
138
|
+
.trimStart()
|
|
139
|
+
await mkdir(path.dirname(project.cssPath), { recursive: true })
|
|
140
|
+
await writeFile(project.cssPath, imports.join('\n') + '\n\n' + withoutAstrale, 'utf8')
|
|
141
|
+
|
|
142
|
+
const components = JSON.parse((await readOptional(project.componentsPath)) ?? '{}') as Record<
|
|
143
|
+
string,
|
|
144
|
+
unknown
|
|
145
|
+
>
|
|
146
|
+
await writeJson(project.componentsPath, {
|
|
147
|
+
$schema: 'https://ui.shadcn.com/schema.json',
|
|
148
|
+
...components,
|
|
149
|
+
style: 'base-nova',
|
|
150
|
+
rsc: components.rsc ?? false,
|
|
151
|
+
tsx: components.tsx ?? true,
|
|
152
|
+
tailwind: {
|
|
153
|
+
config: '',
|
|
154
|
+
baseColor: 'neutral',
|
|
155
|
+
cssVariables: true,
|
|
156
|
+
prefix: '',
|
|
157
|
+
...(components.tailwind as Record<string, unknown> | undefined),
|
|
158
|
+
css: cssRelative,
|
|
159
|
+
},
|
|
160
|
+
aliases: {
|
|
161
|
+
components: '@/components',
|
|
162
|
+
utils: '@/lib/utils',
|
|
163
|
+
ui: '@/components/ui',
|
|
164
|
+
lib: '@/lib',
|
|
165
|
+
hooks: '@/hooks',
|
|
166
|
+
...(components.aliases as Record<string, unknown> | undefined),
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
if (options.install !== false) {
|
|
171
|
+
const result = await (dependencies.runner ?? defaultUiRunner)(
|
|
172
|
+
project.manager,
|
|
173
|
+
['install'],
|
|
174
|
+
project.root,
|
|
175
|
+
)
|
|
176
|
+
if (result.code !== 0) {
|
|
177
|
+
throw new UiError(
|
|
178
|
+
'UI_DEPENDENCY_INSTALL_FAILED',
|
|
179
|
+
project.manager + ' install failed.',
|
|
180
|
+
result.stderr.trim() || undefined,
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const lock: UiLock = {
|
|
186
|
+
$schema:
|
|
187
|
+
'https://raw.githubusercontent.com/astrale-os/ui/' +
|
|
188
|
+
release.ref +
|
|
189
|
+
'/schemas/ui-lock.schema.json',
|
|
190
|
+
version: 1,
|
|
191
|
+
package: { name: UI_PACKAGE, version: release.version },
|
|
192
|
+
registry: {
|
|
193
|
+
repository: 'astrale-os/ui',
|
|
194
|
+
ref: release.ref,
|
|
195
|
+
commit: release.commit,
|
|
196
|
+
},
|
|
197
|
+
tooling: {
|
|
198
|
+
shadcn: release.compatibility.shadcn,
|
|
199
|
+
base: 'base',
|
|
200
|
+
style: 'nova',
|
|
201
|
+
baseUi: release.compatibility.baseUi,
|
|
202
|
+
},
|
|
203
|
+
preset,
|
|
204
|
+
items: {},
|
|
205
|
+
}
|
|
206
|
+
await writeJson(project.uiLockPath, lock)
|
|
207
|
+
return { ...plan, lock }
|
|
208
|
+
} catch (error) {
|
|
209
|
+
for (const [target, value] of snapshots) {
|
|
210
|
+
if (value !== undefined) await writeFile(target, value, 'utf8')
|
|
211
|
+
else await rm(target, { force: true })
|
|
212
|
+
}
|
|
213
|
+
throw error
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function listUi(
|
|
218
|
+
query: string | undefined,
|
|
219
|
+
options: { type?: string; limit?: number; version?: string },
|
|
220
|
+
dependencies: Dependencies = {},
|
|
221
|
+
): Promise<UiRegistryItem[]> {
|
|
222
|
+
const release = await resolveUiRelease(options.version, dependencies.fetcher)
|
|
223
|
+
const needle = query?.trim().toLowerCase()
|
|
224
|
+
return release.registry.items
|
|
225
|
+
.filter((item) => {
|
|
226
|
+
if (options.type && !item.meta.canonicalAddress.startsWith(options.type + '/')) return false
|
|
227
|
+
if (!needle) return true
|
|
228
|
+
return [item.meta.canonicalAddress, item.name, item.title, item.description]
|
|
229
|
+
.filter(Boolean)
|
|
230
|
+
.some((value) => String(value).toLowerCase().includes(needle))
|
|
231
|
+
})
|
|
232
|
+
.slice(0, options.limit ?? 100)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function listLockedUi(
|
|
236
|
+
input: string | undefined,
|
|
237
|
+
dependencies: Dependencies = {},
|
|
238
|
+
): Promise<UiRegistryItem[]> {
|
|
239
|
+
const project = await discoverUiProject(input)
|
|
240
|
+
const { release } = await lockedRelease(project, dependencies.fetcher)
|
|
241
|
+
return release.registry.items
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function lockedRelease(
|
|
245
|
+
project: UiProject,
|
|
246
|
+
fetcher?: typeof fetch,
|
|
247
|
+
): Promise<{ lock: UiLock; release: UiRelease }> {
|
|
248
|
+
const lock = await readUiLock(project.uiLockPath)
|
|
249
|
+
const release = await readUiReleaseSnapshot(
|
|
250
|
+
{
|
|
251
|
+
version: lock.package.version,
|
|
252
|
+
ref: lock.registry.ref,
|
|
253
|
+
commit: lock.registry.commit,
|
|
254
|
+
},
|
|
255
|
+
fetcher,
|
|
256
|
+
)
|
|
257
|
+
return { lock, release }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export async function addUi(
|
|
261
|
+
addresses: string[],
|
|
262
|
+
options: { project?: string; dryRun?: boolean; overwrite?: boolean; yes?: boolean },
|
|
263
|
+
dependencies: Dependencies = {},
|
|
264
|
+
): Promise<Record<string, unknown>> {
|
|
265
|
+
if (addresses.length === 0) {
|
|
266
|
+
throw new UiError('UI_ITEM_NOT_FOUND', 'No UI item was provided.')
|
|
267
|
+
}
|
|
268
|
+
const project = await discoverUiProject(options.project)
|
|
269
|
+
const { lock, release } = await lockedRelease(project, dependencies.fetcher)
|
|
270
|
+
const items = addresses.map((address) => findItem(release, address))
|
|
271
|
+
const itemDocuments = await Promise.all(
|
|
272
|
+
items.map((item) => readUiRegistryItem(release, item, dependencies.fetcher)),
|
|
273
|
+
)
|
|
274
|
+
if (options.overwrite && !options.yes) {
|
|
275
|
+
throw new UiError(
|
|
276
|
+
'UI_LOCAL_CHANGES',
|
|
277
|
+
'Replacing installed UI source requires explicit confirmation.',
|
|
278
|
+
'Review the locally edited files, then repeat with both --overwrite and --yes.',
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
await rejectLocalChanges(project, lock, items, options.overwrite === true)
|
|
282
|
+
const targets = (
|
|
283
|
+
await Promise.all(
|
|
284
|
+
items.flatMap((item) =>
|
|
285
|
+
item.files
|
|
286
|
+
.filter((file) => file.target)
|
|
287
|
+
.map((file) => assertSafePlannedTarget(project, file.target!)),
|
|
288
|
+
),
|
|
289
|
+
)
|
|
290
|
+
).filter((target, index, all) => all.indexOf(target) === index)
|
|
291
|
+
const invocation = shadcnInvocation(project.manager, release.compatibility.shadcn, [
|
|
292
|
+
'add',
|
|
293
|
+
...items.map((item) => registryItemUrl(release, item.name)),
|
|
294
|
+
'--cwd',
|
|
295
|
+
project.root,
|
|
296
|
+
...(options.dryRun ? ['--dry-run'] : []),
|
|
297
|
+
...(options.overwrite ? ['--overwrite'] : []),
|
|
298
|
+
...(options.yes ? ['--yes'] : []),
|
|
299
|
+
])
|
|
300
|
+
const snapshots = new Map<string, string | undefined>()
|
|
301
|
+
if (!options.dryRun) {
|
|
302
|
+
for (const target of [project.packageJsonPath, project.lockPath!, ...targets]) {
|
|
303
|
+
snapshots.set(target, await readOptional(target))
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
let result: Awaited<ReturnType<UiRunner>>
|
|
307
|
+
try {
|
|
308
|
+
result = await (dependencies.runner ?? defaultUiRunner)(
|
|
309
|
+
invocation.file,
|
|
310
|
+
invocation.args,
|
|
311
|
+
project.root,
|
|
312
|
+
)
|
|
313
|
+
if (result.code !== 0) {
|
|
314
|
+
throw new UiError(
|
|
315
|
+
'UI_TOOL_FAILED',
|
|
316
|
+
'The pinned shadcn operation failed.',
|
|
317
|
+
result.stderr.trim(),
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
if (!options.dryRun) {
|
|
321
|
+
for (const target of targets) {
|
|
322
|
+
if (!(await exists(target))) {
|
|
323
|
+
throw new UiError('UI_TOOL_FAILED', 'The pinned shadcn operation omitted an item file.')
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
} catch (error) {
|
|
328
|
+
for (const [target, previous] of snapshots) {
|
|
329
|
+
if (previous === undefined) await rm(target, { force: true })
|
|
330
|
+
else await writeFile(target, previous, 'utf8')
|
|
331
|
+
}
|
|
332
|
+
throw error
|
|
333
|
+
}
|
|
334
|
+
if (options.dryRun) {
|
|
335
|
+
return {
|
|
336
|
+
status: 'planned',
|
|
337
|
+
items: items.map((item) => item.meta.canonicalAddress),
|
|
338
|
+
sources: itemDocuments.map((item) => ({
|
|
339
|
+
address: item.meta.canonicalAddress,
|
|
340
|
+
dependencies: item.dependencies ?? [],
|
|
341
|
+
files: item.files.map((file) => file.target).filter(Boolean),
|
|
342
|
+
})),
|
|
343
|
+
command: [invocation.file, ...invocation.args],
|
|
344
|
+
output: result.stdout,
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
for (const [index, item] of items.entries()) {
|
|
350
|
+
const itemDocument = itemDocuments[index]!
|
|
351
|
+
const files: Record<string, string> = {}
|
|
352
|
+
for (const file of item.files) {
|
|
353
|
+
if (!file.target) continue
|
|
354
|
+
const target = await safeTarget(project, file.target)
|
|
355
|
+
files[projectRelative(project, target)] = digest(await readFile(target))
|
|
356
|
+
}
|
|
357
|
+
lock.items[item.meta.canonicalAddress] = {
|
|
358
|
+
address: item.meta.canonicalAddress,
|
|
359
|
+
sourceDigest: digest(JSON.stringify(itemDocument)),
|
|
360
|
+
files,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
await writeJson(project.uiLockPath, lock)
|
|
364
|
+
} catch (error) {
|
|
365
|
+
for (const [target, previous] of snapshots) {
|
|
366
|
+
if (previous === undefined) await rm(target, { force: true })
|
|
367
|
+
else await writeFile(target, previous, 'utf8')
|
|
368
|
+
}
|
|
369
|
+
throw error
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
status: 'installed',
|
|
373
|
+
items: items.map((item) => item.meta.canonicalAddress),
|
|
374
|
+
files: Object.fromEntries(
|
|
375
|
+
items.map((item) => [
|
|
376
|
+
item.meta.canonicalAddress,
|
|
377
|
+
lock.items[item.meta.canonicalAddress]?.files,
|
|
378
|
+
]),
|
|
379
|
+
),
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export async function doctorUi(
|
|
384
|
+
input?: string,
|
|
385
|
+
): Promise<{ healthy: boolean; checks: Array<{ check: string; ok: boolean; detail?: string }> }> {
|
|
386
|
+
const project = await discoverUiProject(input)
|
|
387
|
+
const checks: Array<{ check: string; ok: boolean; detail?: string }> = []
|
|
388
|
+
let lock: UiLock | undefined
|
|
389
|
+
try {
|
|
390
|
+
lock = parseUiLock(JSON.parse(await readFile(project.uiLockPath, 'utf8')))
|
|
391
|
+
checks.push({ check: 'lock', ok: true })
|
|
392
|
+
} catch (error) {
|
|
393
|
+
checks.push({
|
|
394
|
+
check: 'lock',
|
|
395
|
+
ok: false,
|
|
396
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
const dependencies = manifestDependencies(project.packageJson)
|
|
400
|
+
checks.push({
|
|
401
|
+
check: 'package',
|
|
402
|
+
ok: typeof dependencies[UI_PACKAGE] === 'string',
|
|
403
|
+
detail: dependencies[UI_PACKAGE],
|
|
404
|
+
})
|
|
405
|
+
const css = (await readOptional(project.cssPath)) ?? ''
|
|
406
|
+
checks.push({ check: 'theme', ok: css.includes(UI_PACKAGE + '/theme.css') })
|
|
407
|
+
checks.push({
|
|
408
|
+
check: 'preset',
|
|
409
|
+
ok: UI_PRESETS.some((preset) => css.includes(UI_PACKAGE + '/presets/' + preset + '.css')),
|
|
410
|
+
})
|
|
411
|
+
checks.push({ check: 'components', ok: await exists(project.componentsPath) })
|
|
412
|
+
if (lock) {
|
|
413
|
+
for (const item of Object.values(lock.items)) {
|
|
414
|
+
for (const [file, expected] of Object.entries(item.files)) {
|
|
415
|
+
const actual = await readFile(path.join(project.root, file))
|
|
416
|
+
.then(digest)
|
|
417
|
+
.catch(() => '')
|
|
418
|
+
checks.push({ check: 'item:' + item.address + ':' + file, ok: actual === expected })
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return { healthy: checks.every((check) => check.ok), checks }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export async function applyPreset(
|
|
426
|
+
preset: UiPreset,
|
|
427
|
+
options: { project?: string; dryRun?: boolean },
|
|
428
|
+
): Promise<Record<string, unknown>> {
|
|
429
|
+
if (!UI_PRESETS.includes(preset)) {
|
|
430
|
+
throw new UiError('UI_ITEM_NOT_FOUND', 'Unknown preset: ' + preset)
|
|
431
|
+
}
|
|
432
|
+
const project = await discoverUiProject(options.project)
|
|
433
|
+
const lock = await readUiLock(project.uiLockPath)
|
|
434
|
+
const css = (await readOptional(project.cssPath)) ?? ''
|
|
435
|
+
const next = css.replace(
|
|
436
|
+
/@astrale-os\/ui\/presets\/[a-z-]+\.css/gu,
|
|
437
|
+
'@astrale-os/ui/presets/' + preset + '.css',
|
|
438
|
+
)
|
|
439
|
+
if (next === css && !css.includes('@astrale-os/ui/presets/')) {
|
|
440
|
+
throw new UiError('UI_CONFIG_MISSING', 'No Astrale preset import was found.')
|
|
441
|
+
}
|
|
442
|
+
if (!options.dryRun) {
|
|
443
|
+
await writeFile(project.cssPath, next, 'utf8')
|
|
444
|
+
lock.preset = preset
|
|
445
|
+
await writeJson(project.uiLockPath, lock)
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
status: options.dryRun ? 'planned' : 'applied',
|
|
449
|
+
preset,
|
|
450
|
+
file: projectRelative(project, project.cssPath),
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function findItem(release: UiRelease, address: string): UiRegistryItem {
|
|
455
|
+
const normalized = address.replace(/^@astrale\//u, '')
|
|
456
|
+
const exact = release.registry.items.find(
|
|
457
|
+
(item) => item.meta.canonicalAddress === normalized || item.name === normalized,
|
|
458
|
+
)
|
|
459
|
+
if (exact) return exact
|
|
460
|
+
const shorthand = release.registry.items.filter((item) =>
|
|
461
|
+
item.meta.canonicalAddress.endsWith('/' + normalized),
|
|
462
|
+
)
|
|
463
|
+
if (shorthand.length === 1) return shorthand[0]!
|
|
464
|
+
throw new UiError(
|
|
465
|
+
'UI_ITEM_NOT_FOUND',
|
|
466
|
+
shorthand.length > 1
|
|
467
|
+
? 'UI item "' + address + '" is ambiguous.'
|
|
468
|
+
: 'UI item "' + address + '" was not found.',
|
|
469
|
+
shorthand.length > 1
|
|
470
|
+
? 'Use one of: ' + shorthand.map((item) => item.meta.canonicalAddress).join(', ')
|
|
471
|
+
: 'Run astrale ui list to discover canonical addresses.',
|
|
472
|
+
)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async function rejectLocalChanges(
|
|
476
|
+
project: UiProject,
|
|
477
|
+
lock: UiLock,
|
|
478
|
+
items: UiRegistryItem[],
|
|
479
|
+
overwrite: boolean,
|
|
480
|
+
): Promise<void> {
|
|
481
|
+
if (overwrite) return
|
|
482
|
+
for (const item of items) {
|
|
483
|
+
const installed = lock.items[item.meta.canonicalAddress]
|
|
484
|
+
if (!installed) continue
|
|
485
|
+
for (const [file, expected] of Object.entries(installed.files)) {
|
|
486
|
+
const actual = await readFile(path.join(project.root, file))
|
|
487
|
+
.then(digest)
|
|
488
|
+
.catch(() => '')
|
|
489
|
+
if (actual !== expected) {
|
|
490
|
+
throw new UiError(
|
|
491
|
+
'UI_LOCAL_CHANGES',
|
|
492
|
+
'Installed UI file has local changes: ' + file,
|
|
493
|
+
'Review the file, then repeat add with explicit --overwrite --yes.',
|
|
494
|
+
)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
async function safeTarget(project: UiProject, relative: string): Promise<string> {
|
|
501
|
+
if (path.isAbsolute(relative) || relative.split(/[\\/]/u).includes('..')) {
|
|
502
|
+
throw new UiError('UI_LOCK_INVALID', 'Unsafe registry target: ' + relative)
|
|
503
|
+
}
|
|
504
|
+
const target = path.resolve(project.root, relative)
|
|
505
|
+
projectRelative(project, target)
|
|
506
|
+
const parent = await realpath(path.dirname(target))
|
|
507
|
+
const root = await realpath(project.root)
|
|
508
|
+
if (parent !== root && !parent.startsWith(root + path.sep)) {
|
|
509
|
+
throw new UiError('UI_LOCK_INVALID', 'Registry target escapes through a symlink: ' + relative)
|
|
510
|
+
}
|
|
511
|
+
if ((await exists(target)) && (await lstat(target)).isSymbolicLink()) {
|
|
512
|
+
throw new UiError('UI_LOCK_INVALID', 'Registry target is a symlink: ' + relative)
|
|
513
|
+
}
|
|
514
|
+
return target
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
async function assertSafePlannedTarget(project: UiProject, relative: string): Promise<string> {
|
|
518
|
+
if (path.isAbsolute(relative) || relative.split(/[\\/]/u).includes('..')) {
|
|
519
|
+
throw new UiError('UI_LOCK_INVALID', 'Unsafe registry target: ' + relative)
|
|
520
|
+
}
|
|
521
|
+
const target = path.resolve(project.root, relative)
|
|
522
|
+
projectRelative(project, target)
|
|
523
|
+
const root = await realpath(project.root)
|
|
524
|
+
const physicalTarget = path.resolve(root, path.relative(project.root, target))
|
|
525
|
+
let current = root
|
|
526
|
+
for (const segment of path.relative(root, physicalTarget).split(path.sep)) {
|
|
527
|
+
current = path.join(current, segment)
|
|
528
|
+
if (!(await exists(current))) break
|
|
529
|
+
if ((await lstat(current)).isSymbolicLink()) {
|
|
530
|
+
throw new UiError('UI_LOCK_INVALID', 'Registry target traverses a symlink: ' + relative)
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return target
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
async function writeJson(target: string, value: unknown): Promise<void> {
|
|
537
|
+
await mkdir(path.dirname(target), { recursive: true })
|
|
538
|
+
await writeFile(target, JSON.stringify(value, null, 2) + '\n', 'utf8')
|
|
539
|
+
}
|