@sdxc/kit 0.0.0-stage → 2026.10.17
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/LICENSE.md +21 -0
- package/README.md +93 -2
- package/changelog.jsonl +1097 -0
- package/config.schema.json +89 -0
- package/dist/author.d.ts +36 -0
- package/dist/author.js +107 -0
- package/dist/bin.d.ts +9 -0
- package/dist/bin.js +10 -0
- package/dist/cli.d.ts +33 -0
- package/dist/cli.js +58 -0
- package/dist/commands/create.d.ts +20 -0
- package/dist/commands/create.js +169 -0
- package/dist/commands/init.d.ts +16 -0
- package/dist/commands/init.js +27 -0
- package/dist/commands/make.d.ts +335 -0
- package/dist/commands/make.js +152 -0
- package/dist/commands/run.d.ts +23 -0
- package/dist/commands/run.js +53 -0
- package/dist/commands/ui/add.d.ts +23 -0
- package/dist/commands/ui/add.js +50 -0
- package/dist/commands/ui/diff.d.ts +20 -0
- package/dist/commands/ui/diff.js +28 -0
- package/dist/commands/ui/list.d.ts +24 -0
- package/dist/commands/ui/list.js +45 -0
- package/dist/commands/ui/remove.d.ts +22 -0
- package/dist/commands/ui/remove.js +48 -0
- package/dist/commands/ui/update.d.ts +23 -0
- package/dist/commands/ui/update.js +37 -0
- package/dist/commands/upgrade.d.ts +30 -0
- package/dist/commands/upgrade.js +184 -0
- package/dist/config.d.ts +81 -0
- package/dist/config.js +157 -0
- package/dist/contract.d.ts +161 -0
- package/dist/contract.js +308 -0
- package/dist/create/github.d.ts +63 -0
- package/dist/create/github.js +172 -0
- package/dist/create/target.d.ts +31 -0
- package/dist/create/target.js +56 -0
- package/dist/create/templates.d.ts +69 -0
- package/dist/create/templates.js +251 -0
- package/dist/dependencies.d.ts +34 -0
- package/dist/dependencies.js +76 -0
- package/dist/diff.d.ts +14 -0
- package/dist/diff.js +103 -0
- package/dist/format.d.ts +21 -0
- package/dist/format.js +40 -0
- package/dist/init.d.ts +40 -0
- package/dist/init.js +100 -0
- package/dist/lock.d.ts +74 -0
- package/dist/lock.js +214 -0
- package/dist/make/declare.d.ts +54 -0
- package/dist/make/declare.js +598 -0
- package/dist/make/generate.d.ts +62 -0
- package/dist/make/generate.js +160 -0
- package/dist/make/imports.d.ts +43 -0
- package/dist/make/imports.js +105 -0
- package/dist/make/index.d.ts +43 -0
- package/dist/make/index.js +140 -0
- package/dist/make/kinds.d.ts +40 -0
- package/dist/make/kinds.js +96 -0
- package/dist/make/migrations.d.ts +41 -0
- package/dist/make/migrations.js +116 -0
- package/dist/make/names.d.ts +31 -0
- package/dist/make/names.js +47 -0
- package/dist/make/stubs.d.ts +44 -0
- package/dist/make/stubs.js +89 -0
- package/dist/middleware.d.ts +44 -0
- package/dist/middleware.js +49 -0
- package/dist/package-manager.d.ts +41 -0
- package/dist/package-manager.js +80 -0
- package/dist/project.d.ts +19 -0
- package/dist/project.js +42 -0
- package/dist/releases.d.ts +40 -0
- package/dist/releases.js +110 -0
- package/dist/testing.d.ts +60 -0
- package/dist/testing.js +148 -0
- package/dist/ui/commands.d.ts +114 -0
- package/dist/ui/commands.js +432 -0
- package/dist/ui/imports.d.ts +46 -0
- package/dist/ui/imports.js +160 -0
- package/dist/ui/layout.d.ts +48 -0
- package/dist/ui/layout.js +138 -0
- package/dist/ui/registry.d.ts +58 -0
- package/dist/ui/registry.js +82 -0
- package/dist/upgrade/changelog.d.ts +50 -0
- package/dist/upgrade/changelog.js +69 -0
- package/dist/upgrade/project.d.ts +62 -0
- package/dist/upgrade/project.js +132 -0
- package/dist/upgrade/prompt.d.ts +51 -0
- package/dist/upgrade/prompt.js +227 -0
- package/dist/upgrade/registry.d.ts +33 -0
- package/dist/upgrade/registry.js +50 -0
- package/lock.schema.json +60 -0
- package/package.json +44 -5
- package/registry/ui/accordion.json +15 -0
- package/registry/ui/alert-dialog.json +20 -0
- package/registry/ui/alert.json +15 -0
- package/registry/ui/aspect-ratio.json +13 -0
- package/registry/ui/attachment.json +15 -0
- package/registry/ui/avatar.json +15 -0
- package/registry/ui/badge.json +13 -0
- package/registry/ui/breadcrumbs.json +15 -0
- package/registry/ui/bubble.json +13 -0
- package/registry/ui/button.json +16 -0
- package/registry/ui/calendar.json +16 -0
- package/registry/ui/card.json +18 -0
- package/registry/ui/carousel.json +15 -0
- package/registry/ui/chart.json +13 -0
- package/registry/ui/checkbox-group.json +13 -0
- package/registry/ui/checkbox.json +16 -0
- package/registry/ui/color-area.json +13 -0
- package/registry/ui/color-field.json +20 -0
- package/registry/ui/color-picker.json +17 -0
- package/registry/ui/color-slider.json +13 -0
- package/registry/ui/color-swatch-picker.json +15 -0
- package/registry/ui/color-swatch.json +13 -0
- package/registry/ui/color-wheel.json +13 -0
- package/registry/ui/combobox.json +15 -0
- package/registry/ui/command.json +13 -0
- package/registry/ui/confirm.json +19 -0
- package/registry/ui/context-menu.json +16 -0
- package/registry/ui/date-field.json +19 -0
- package/registry/ui/date-picker.json +15 -0
- package/registry/ui/date-range-picker.json +16 -0
- package/registry/ui/description.json +13 -0
- package/registry/ui/dialog.json +19 -0
- package/registry/ui/disclosure.json +18 -0
- package/registry/ui/drawer.json +18 -0
- package/registry/ui/drop-indicator.json +13 -0
- package/registry/ui/drop-zone.json +13 -0
- package/registry/ui/empty.json +15 -0
- package/registry/ui/field-error.json +13 -0
- package/registry/ui/file-trigger.json +13 -0
- package/registry/ui/form.json +21 -0
- package/registry/ui/grid-list.json +18 -0
- package/registry/ui/group.json +13 -0
- package/registry/ui/header.json +13 -0
- package/registry/ui/heading-scope.json +20 -0
- package/registry/ui/heading.json +15 -0
- package/registry/ui/highlight.json +16 -0
- package/registry/ui/hover-card.json +13 -0
- package/registry/ui/image-placeholder.json +16 -0
- package/registry/ui/input.json +13 -0
- package/registry/ui/item.json +13 -0
- package/registry/ui/keyboard.json +13 -0
- package/registry/ui/label.json +13 -0
- package/registry/ui/link-button.json +16 -0
- package/registry/ui/link.json +13 -0
- package/registry/ui/listbox.json +15 -0
- package/registry/ui/logo.json +15 -0
- package/registry/ui/marker.json +13 -0
- package/registry/ui/menu.json +19 -0
- package/registry/ui/menubar.json +13 -0
- package/registry/ui/message-scroller.json +16 -0
- package/registry/ui/message.json +13 -0
- package/registry/ui/meter.json +13 -0
- package/registry/ui/modal.json +15 -0
- package/registry/ui/nav-link.json +13 -0
- package/registry/ui/navigation-menu.json +15 -0
- package/registry/ui/number-field.json +15 -0
- package/registry/ui/otp-field.json +15 -0
- package/registry/ui/overlay-arrow.json +13 -0
- package/registry/ui/pagination.json +16 -0
- package/registry/ui/popover.json +13 -0
- package/registry/ui/progress-bar.json +13 -0
- package/registry/ui/radio-group.json +13 -0
- package/registry/ui/range-calendar.json +16 -0
- package/registry/ui/resizable.json +13 -0
- package/registry/ui/scroll-area.json +13 -0
- package/registry/ui/search-field.json +16 -0
- package/registry/ui/section.json +13 -0
- package/registry/ui/select.json +13 -0
- package/registry/ui/selection-indicator.json +13 -0
- package/registry/ui/sentinel-row.json +16 -0
- package/registry/ui/separator.json +13 -0
- package/registry/ui/shared-element.json +13 -0
- package/registry/ui/sheet.json +15 -0
- package/registry/ui/shortcut-list.json +18 -0
- package/registry/ui/sidebar.json +20 -0
- package/registry/ui/skeleton.json +13 -0
- package/registry/ui/slider.json +16 -0
- package/registry/ui/spinner.json +13 -0
- package/registry/ui/switch.json +16 -0
- package/registry/ui/table.json +16 -0
- package/registry/ui/tabs.json +17 -0
- package/registry/ui/tag-group.json +18 -0
- package/registry/ui/text-field.json +19 -0
- package/registry/ui/text.json +13 -0
- package/registry/ui/textarea.json +13 -0
- package/registry/ui/time-field.json +19 -0
- package/registry/ui/toast.json +16 -0
- package/registry/ui/toggle-button.json +17 -0
- package/registry/ui/toolbar.json +13 -0
- package/registry/ui/tooltip.json +16 -0
- package/registry/ui/tree.json +18 -0
- package/registry/ui/typeset.json +16 -0
- package/registry/ui/youtube-embed.json +16 -0
- package/stubs/command.stub +16 -0
- package/stubs/command.test.stub +21 -0
- package/stubs/controller.api.stub +21 -0
- package/stubs/controller.api.test.stub +18 -0
- package/stubs/controller.form.stub +63 -0
- package/stubs/controller.form.test.stub +61 -0
- package/stubs/controller.stub +24 -0
- package/stubs/controller.test.stub +18 -0
- package/stubs/email.stub +26 -0
- package/stubs/email.test.stub +21 -0
- package/stubs/factory.stub +13 -0
- package/stubs/initializer.stub +9 -0
- package/stubs/initializer.test.stub +16 -0
- package/stubs/job.cron.stub +16 -0
- package/stubs/job.stub +15 -0
- package/stubs/job.test.stub +18 -0
- package/stubs/mcp.resource.stub +15 -0
- package/stubs/mcp.stub +13 -0
- package/stubs/mcp.test.stub +18 -0
- package/stubs/middleware.stub +16 -0
- package/stubs/middleware.test.stub +16 -0
- package/stubs/migration.stub +1 -0
- package/stubs/model.stub +33 -0
- package/stubs/model.test.stub +18 -0
- package/stubs/policy.stub +16 -0
- package/stubs/policy.test.stub +20 -0
- package/stubs/service.stub +17 -0
- package/stubs/service.test.stub +17 -0
- package/stubs/validator.stub +17 -0
- package/stubs/validator.test.stub +25 -0
- package/templates/app/.env.example +16 -0
- package/templates/app/README.md +84 -0
- package/templates/app/app/commands/about.test.ts +29 -0
- package/templates/app/app/commands/about.ts +34 -0
- package/templates/app/app/commands/db/console.ts +6 -0
- package/templates/app/app/commands/db/migrate.test.ts +30 -0
- package/templates/app/app/commands/db/migrate.ts +55 -0
- package/templates/app/app/commands/index.ts +31 -0
- package/templates/app/app/commands/typegen.test.ts +17 -0
- package/templates/app/app/commands/typegen.ts +16 -0
- package/templates/app/app/http/controllers/api/health.tsx +15 -0
- package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
- package/templates/app/app/http/controllers/home.tsx +30 -0
- package/templates/app/app/http/controllers/mcp.tsx +16 -0
- package/templates/app/app/http/controllers/not-found.tsx +30 -0
- package/templates/app/app/http/controllers/robots.test.ts +18 -0
- package/templates/app/app/http/controllers/robots.ts +24 -0
- package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
- package/templates/app/app/http/controllers/sitemap.ts +25 -0
- package/templates/app/app/http/middleware/api.ts +10 -0
- package/templates/app/app/http/middleware/page.test.ts +40 -0
- package/templates/app/app/http/middleware/page.ts +49 -0
- package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
- package/templates/app/app/http/middleware/server-timing.ts +42 -0
- package/templates/app/app/initializers/assets.ts +11 -0
- package/templates/app/app/initializers/captcha.ts +11 -0
- package/templates/app/app/initializers/cli.ts +75 -0
- package/templates/app/app/initializers/database.test.ts +18 -0
- package/templates/app/app/initializers/database.ts +18 -0
- package/templates/app/app/initializers/dispatcher.ts +36 -0
- package/templates/app/app/initializers/honeypot.ts +11 -0
- package/templates/app/app/initializers/i18n.test.ts +16 -0
- package/templates/app/app/initializers/i18n.ts +28 -0
- package/templates/app/app/initializers/local-database.test.ts +33 -0
- package/templates/app/app/initializers/local-database.ts +53 -0
- package/templates/app/app/initializers/logger.ts +10 -0
- package/templates/app/app/initializers/mail.test.ts +27 -0
- package/templates/app/app/initializers/mail.ts +11 -0
- package/templates/app/app/initializers/mcp.ts +16 -0
- package/templates/app/app/initializers/openapi.ts +21 -0
- package/templates/app/app/initializers/router.test.ts +61 -0
- package/templates/app/app/initializers/router.tsx +111 -0
- package/templates/app/app/initializers/seo.ts +15 -0
- package/templates/app/app/initializers/session.test.ts +34 -0
- package/templates/app/app/initializers/session.ts +29 -0
- package/templates/app/app/initializers/spam.ts +10 -0
- package/templates/app/app/jobs/index.ts +10 -0
- package/templates/app/app/mcp/resources/index.ts +9 -0
- package/templates/app/app/mcp/tools/index.ts +10 -0
- package/templates/app/app/models/index.ts +10 -0
- package/templates/app/app/services/migrations.test.ts +45 -0
- package/templates/app/app/services/migrations.ts +60 -0
- package/templates/app/app/test/cloudflare-workers.ts +25 -0
- package/templates/app/bootstrap/browser.ts +27 -0
- package/templates/app/bootstrap/cli.ts +9 -0
- package/templates/app/bootstrap/worker.ts +31 -0
- package/templates/app/cloudflare.config.ts +56 -0
- package/templates/app/config/constants.ts +19 -0
- package/templates/app/config/problems.ts +24 -0
- package/templates/app/config/security-policy.ts +34 -0
- package/templates/app/config/security-txt.ts +28 -0
- package/templates/app/database/migrations/.gitkeep +0 -0
- package/templates/app/gitignore +9 -0
- package/templates/app/package.json +68 -0
- package/templates/app/resources/layouts/document.tsx +79 -0
- package/templates/app/resources/locales/en.json +10 -0
- package/templates/app/routes/api.ts +29 -0
- package/templates/app/routes/web.ts +14 -0
- package/templates/app/tsconfig.json +40 -0
- package/templates/app/vite.config.ts +44 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What each `sdxc ui` command does, as plans over the project: `add`, `update` and `remove`
|
|
3
|
+
* answer every file they would write, edit or delete and the lock entries they would record,
|
|
4
|
+
* so `--dry-run` prints exactly what applying the same plan writes.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
import { failure, isFailure, success } from "@sdxc/result";
|
|
13
|
+
import { compare } from "@sdxc/semver";
|
|
14
|
+
import { missingDependencies } from "../dependencies.js";
|
|
15
|
+
import { unifiedDiff } from "../diff.js";
|
|
16
|
+
import { formatFiles } from "../format.js";
|
|
17
|
+
import { readLock, removeUIEntry, renderLock, setUIEntry, uiEntries, uiEntry, writeLock, } from "../lock.js";
|
|
18
|
+
import { lockFileAtOrAbove, packageManager } from "../package-manager.js";
|
|
19
|
+
import { copyPath, pointAtCopies, pointBack, projectSources, readProjectFile } from "./imports.js";
|
|
20
|
+
import { readLayout } from "./layout.js";
|
|
21
|
+
import { readRegistry, withDependencies } from "./registry.js";
|
|
22
|
+
/**
|
|
23
|
+
* Reads the installed registry, the lock file and the copies' layout of the project at `root`.
|
|
24
|
+
*
|
|
25
|
+
* @param packageRoot - The `@sdxc/kit` package holding the registry.
|
|
26
|
+
*/
|
|
27
|
+
export async function loadContext(project, packageRoot) {
|
|
28
|
+
let { root, config } = project;
|
|
29
|
+
let registry = await readRegistry(packageRoot);
|
|
30
|
+
if (isFailure(registry))
|
|
31
|
+
return registry;
|
|
32
|
+
let lock = await readLock(root, config);
|
|
33
|
+
if (isFailure(lock))
|
|
34
|
+
return lock;
|
|
35
|
+
let layout = await readLayout(root, config);
|
|
36
|
+
if (isFailure(layout))
|
|
37
|
+
return layout;
|
|
38
|
+
return success({ root, config, registry: registry.data, lock: lock.data, layout: layout.data });
|
|
39
|
+
}
|
|
40
|
+
/** The hash a lock entry records for a file's content: SHA-256, base64, `sha256-` prefixed. */
|
|
41
|
+
export function hashContent(content) {
|
|
42
|
+
return `sha256-${createHash("sha256").update(content).digest("base64")}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Every component in the registry, and every copy the registry no longer holds, sorted by
|
|
46
|
+
* name. A copy is edited when a file it recorded is missing or hashes differently, and behind
|
|
47
|
+
* when the component changed in a release after the one it came from.
|
|
48
|
+
*/
|
|
49
|
+
export async function listComponents(context) {
|
|
50
|
+
let names = new Set([
|
|
51
|
+
...context.registry.entries.keys(),
|
|
52
|
+
...uiEntries(context.lock).map((entry) => entry.name),
|
|
53
|
+
]);
|
|
54
|
+
let rows = [];
|
|
55
|
+
for (let name of [...names].sort()) {
|
|
56
|
+
let entry = context.registry.entries.get(name);
|
|
57
|
+
let copied = uiEntry(context.lock, name);
|
|
58
|
+
let copy = null;
|
|
59
|
+
if (copied !== undefined) {
|
|
60
|
+
copy = {
|
|
61
|
+
release: copied.release,
|
|
62
|
+
edited: await isEdited(context, copied),
|
|
63
|
+
behind: entry !== undefined && compare(copied.release, entry.changedIn) < 0,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
rows.push({ name, changedIn: entry?.changedIn ?? null, copy });
|
|
67
|
+
}
|
|
68
|
+
return rows;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Copies `names` (and, with `withDeps`, every component they reach) into `ui.directory`, with
|
|
72
|
+
* their tests under `ui.tests`, and points every project file's imports of them at the copies.
|
|
73
|
+
* A component already copied is reported and left alone; a file in the way stops the plan.
|
|
74
|
+
*/
|
|
75
|
+
export async function planAdd(context, names, options) {
|
|
76
|
+
let unknown = unknownNames(context, names);
|
|
77
|
+
if (unknown !== null)
|
|
78
|
+
return failure(unknown);
|
|
79
|
+
let plan = emptyPlan();
|
|
80
|
+
let wanted = options.withDeps ? withDependencies(context.registry, names) : [...new Set(names)];
|
|
81
|
+
let added = [];
|
|
82
|
+
for (let name of wanted) {
|
|
83
|
+
let entry = context.registry.entries.get(name);
|
|
84
|
+
if (entry === undefined)
|
|
85
|
+
continue;
|
|
86
|
+
if (uiEntry(context.lock, name) !== undefined) {
|
|
87
|
+
if (names.includes(name)) {
|
|
88
|
+
plan.report.push(`${name} is already copied; sdxc ui update ${name} replaces it`);
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
added.push(entry);
|
|
93
|
+
}
|
|
94
|
+
let copies = copiedComponents(context, added);
|
|
95
|
+
for (let entry of added) {
|
|
96
|
+
let files = {};
|
|
97
|
+
for (let file of registryFiles(entry, context.config.ui.tests)) {
|
|
98
|
+
let path = copyPath(context.layout, file.path);
|
|
99
|
+
let existing = await readProjectFile(context.root, path);
|
|
100
|
+
if (existing !== null) {
|
|
101
|
+
return failure(new Error(`${path} already exists and the lock file does not list it; move it first`));
|
|
102
|
+
}
|
|
103
|
+
let content = pointAtCopies(path, file.content, copies, context.layout);
|
|
104
|
+
if (isFailure(content))
|
|
105
|
+
return content;
|
|
106
|
+
plan.files.set(path, { before: null, after: content.data.source });
|
|
107
|
+
files[file.path] = null;
|
|
108
|
+
}
|
|
109
|
+
plan.record.push({ name: entry.name, release: context.registry.release, files });
|
|
110
|
+
plan.report.push(`Copied ${entry.name} to ${copyPath(context.layout, entry.source.path)}`);
|
|
111
|
+
}
|
|
112
|
+
let rewritten = await rewriteProject(context, plan, (file, source) => pointAtCopies(file, source, copiedComponents(context, added, false), context.layout));
|
|
113
|
+
if (isFailure(rewritten))
|
|
114
|
+
return rewritten;
|
|
115
|
+
return success(plan);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The difference between the copy of `name` and the installed version, as a unified diff from
|
|
119
|
+
* the installed file to the local one, or an empty string when they match.
|
|
120
|
+
*/
|
|
121
|
+
export async function diffComponent(context, name) {
|
|
122
|
+
let copied = uiEntry(context.lock, name);
|
|
123
|
+
if (copied === undefined)
|
|
124
|
+
return failure(new Error(`${name} is not copied; sdxc ui add ${name}`));
|
|
125
|
+
let entry = context.registry.entries.get(name);
|
|
126
|
+
if (entry === undefined) {
|
|
127
|
+
return failure(new Error(`Release ${context.registry.release} no longer ships ${name}`));
|
|
128
|
+
}
|
|
129
|
+
let copies = copiedComponents(context, []);
|
|
130
|
+
let output = "";
|
|
131
|
+
for (let file of registryFiles(entry, copied.files[entry.test?.path ?? ""] !== undefined)) {
|
|
132
|
+
let path = copyPath(context.layout, file.path);
|
|
133
|
+
let installed = pointAtCopies(path, file.content, copies, context.layout);
|
|
134
|
+
if (isFailure(installed))
|
|
135
|
+
return installed;
|
|
136
|
+
let local = await readProjectFile(context.root, path);
|
|
137
|
+
output += unifiedDiff(path, installed.data.source, local);
|
|
138
|
+
}
|
|
139
|
+
return success(output);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Replaces each file of each copy whose hash still matches the lock with the installed version.
|
|
143
|
+
* An edited file is replaced only when `overwrite` answers yes for it, and otherwise left alone
|
|
144
|
+
* and reported; the entry moves to the installed release only when every file was replaced.
|
|
145
|
+
*/
|
|
146
|
+
export async function planUpdate(context, names, options) {
|
|
147
|
+
let unknown = uncopiedNames(context, names);
|
|
148
|
+
if (unknown !== null)
|
|
149
|
+
return failure(unknown);
|
|
150
|
+
let plan = emptyPlan();
|
|
151
|
+
let copies = copiedComponents(context, []);
|
|
152
|
+
for (let name of new Set(names)) {
|
|
153
|
+
let copied = uiEntry(context.lock, name);
|
|
154
|
+
let entry = context.registry.entries.get(name);
|
|
155
|
+
if (copied === undefined)
|
|
156
|
+
continue;
|
|
157
|
+
if (entry === undefined) {
|
|
158
|
+
plan.report.push(`Release ${context.registry.release} no longer ships ${name}; left as it is`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
let withTest = context.config.ui.tests || (entry.test !== null && entry.test.path in copied.files);
|
|
162
|
+
let files = { ...copied.files };
|
|
163
|
+
let skipped = 0;
|
|
164
|
+
let replaced = 0;
|
|
165
|
+
for (let file of registryFiles(entry, withTest)) {
|
|
166
|
+
let path = copyPath(context.layout, file.path);
|
|
167
|
+
let local = await readProjectFile(context.root, path);
|
|
168
|
+
let recorded = copied.files[file.path];
|
|
169
|
+
let untouched = local === null || (recorded !== undefined && hashContent(local) === recorded);
|
|
170
|
+
if (!untouched) {
|
|
171
|
+
let overwrite = await options.overwrite(path);
|
|
172
|
+
if (isFailure(overwrite))
|
|
173
|
+
return overwrite;
|
|
174
|
+
if (!overwrite.data) {
|
|
175
|
+
skipped += 1;
|
|
176
|
+
plan.report.push(`${path} is edited; left as it is (--force replaces it)`);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
let content = pointAtCopies(path, file.content, copies, context.layout);
|
|
181
|
+
if (isFailure(content))
|
|
182
|
+
return content;
|
|
183
|
+
files[file.path] = null;
|
|
184
|
+
if (content.data.source === local) {
|
|
185
|
+
files[file.path] = hashContent(local);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
plan.files.set(path, { before: local, after: content.data.source });
|
|
189
|
+
replaced += 1;
|
|
190
|
+
}
|
|
191
|
+
let release = skipped === 0 ? context.registry.release : copied.release;
|
|
192
|
+
plan.record.push({ name, release, files });
|
|
193
|
+
plan.report.push(replaced === 0 && skipped === 0
|
|
194
|
+
? `${name} already matches release ${context.registry.release}`
|
|
195
|
+
: `Updated ${replaced} file${replaced === 1 ? "" : "s"} of ${name}`);
|
|
196
|
+
}
|
|
197
|
+
return success(plan);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Deletes the copies of `names`, drops their lock entries and points every project file's
|
|
201
|
+
* imports of them back at `@sdxc/ui/<name>`, folded into an existing `@sdxc/ui` import.
|
|
202
|
+
*/
|
|
203
|
+
export async function planRemove(context, names) {
|
|
204
|
+
let unknown = uncopiedNames(context, names);
|
|
205
|
+
if (unknown !== null)
|
|
206
|
+
return failure(unknown);
|
|
207
|
+
let plan = emptyPlan();
|
|
208
|
+
let removed = [];
|
|
209
|
+
for (let name of new Set(names)) {
|
|
210
|
+
let copied = uiEntry(context.lock, name);
|
|
211
|
+
if (copied === undefined)
|
|
212
|
+
continue;
|
|
213
|
+
for (let file of Object.keys(copied.files)) {
|
|
214
|
+
let path = copyPath(context.layout, file);
|
|
215
|
+
let local = await readProjectFile(context.root, path);
|
|
216
|
+
if (local !== null)
|
|
217
|
+
plan.files.set(path, { before: local, after: null });
|
|
218
|
+
}
|
|
219
|
+
plan.forget.push(name);
|
|
220
|
+
removed.push({ name, exports: context.registry.entries.get(name)?.exports ?? [] });
|
|
221
|
+
plan.report.push(`Removed ${name}`);
|
|
222
|
+
}
|
|
223
|
+
let rewritten = await rewriteProject(context, plan, (file, source) => {
|
|
224
|
+
let result = pointBack(file, source, removed, context.layout);
|
|
225
|
+
if (isFailure(result))
|
|
226
|
+
return result;
|
|
227
|
+
return success({ source: result.data, unhandled: [] });
|
|
228
|
+
});
|
|
229
|
+
if (isFailure(rewritten))
|
|
230
|
+
return rewritten;
|
|
231
|
+
return success(plan);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Writes and deletes the plan's files as a task counting each one, runs `format` over what it
|
|
235
|
+
* wrote, then records the lock entries with each new file hashed as it ended up. A failed
|
|
236
|
+
* `format` is reported, and the hashes are then of the files as written.
|
|
237
|
+
*/
|
|
238
|
+
export async function applyPlan(ctx, context, plan) {
|
|
239
|
+
let report = [...plan.report];
|
|
240
|
+
let written = [];
|
|
241
|
+
if (plan.files.size > 0) {
|
|
242
|
+
let applied = await ctx.task("Writing files", async (task) => {
|
|
243
|
+
task.total(plan.files.size);
|
|
244
|
+
for (let [path, change] of plan.files) {
|
|
245
|
+
let target = join(context.root, path);
|
|
246
|
+
if (change.after === null) {
|
|
247
|
+
await rm(target, { force: true });
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
await mkdir(dirname(target), { recursive: true });
|
|
251
|
+
await writeFile(target, change.after);
|
|
252
|
+
written.push(path);
|
|
253
|
+
}
|
|
254
|
+
task.advance();
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
if (isFailure(applied))
|
|
258
|
+
return applied;
|
|
259
|
+
}
|
|
260
|
+
let formatted = await formatFiles(ctx, context.config.format, written, context.root);
|
|
261
|
+
if (isFailure(formatted)) {
|
|
262
|
+
report.push(`${formatted.error.message}; the lock file records them as written`);
|
|
263
|
+
}
|
|
264
|
+
let lock = await settleLock(context, plan, async (path) => readProjectFile(context.root, path));
|
|
265
|
+
if (isFailure(lock))
|
|
266
|
+
return lock;
|
|
267
|
+
let saved = await writeLock(context.root, lock.data);
|
|
268
|
+
if (isFailure(saved))
|
|
269
|
+
return saved;
|
|
270
|
+
return success(report);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Lists the `@sdxc/*` packages the plan's files import in `package.json`, then prints the plan's
|
|
274
|
+
* diff on stdout and its report on stderr under `--dry-run`, or applies it and prints its report.
|
|
275
|
+
*/
|
|
276
|
+
export async function finishPlan(ctx, context, plan, dryRun) {
|
|
277
|
+
let listed = await listImportedPackages(context.root, plan);
|
|
278
|
+
if (isFailure(listed))
|
|
279
|
+
return listed;
|
|
280
|
+
if (dryRun) {
|
|
281
|
+
let diff = await renderPlan(context, plan);
|
|
282
|
+
if (isFailure(diff))
|
|
283
|
+
return diff;
|
|
284
|
+
ctx.stdout(diff.data);
|
|
285
|
+
for (let line of plan.report)
|
|
286
|
+
ctx.stderr(`${line}\n`);
|
|
287
|
+
return success(undefined);
|
|
288
|
+
}
|
|
289
|
+
let report = await applyPlan(ctx, context, plan);
|
|
290
|
+
if (isFailure(report))
|
|
291
|
+
return report;
|
|
292
|
+
for (let line of report.data)
|
|
293
|
+
ctx.stdout(`${line}\n`);
|
|
294
|
+
return success(undefined);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Adds to `plan` the `package.json` edit listing every `@sdxc/*` package the files it writes
|
|
298
|
+
* import and the project lacks, at the project's release, with a report line naming them and
|
|
299
|
+
* the install to run.
|
|
300
|
+
*/
|
|
301
|
+
async function listImportedPackages(root, plan) {
|
|
302
|
+
let files = [...plan.files].flatMap(([path, change]) => change.after === null ? [] : [{ path, content: change.after }]);
|
|
303
|
+
let edit = await missingDependencies(root, files);
|
|
304
|
+
if (isFailure(edit))
|
|
305
|
+
return edit;
|
|
306
|
+
if (edit.data === null)
|
|
307
|
+
return success(undefined);
|
|
308
|
+
plan.files.set(edit.data.path, { before: edit.data.before, after: edit.data.after });
|
|
309
|
+
let manager = await packageManager({
|
|
310
|
+
declared: undefined,
|
|
311
|
+
findLockFile: lockFileAtOrAbove(root),
|
|
312
|
+
});
|
|
313
|
+
let added = edit.data.added.map(({ name, version }) => `${name}@${version}`).join(", ");
|
|
314
|
+
plan.report.push(`Added ${added} to package.json; run ${manager.install.join(" ")}`);
|
|
315
|
+
return success(undefined);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* The diff of every file the plan touches and of the lock file, which is what applying it
|
|
319
|
+
* writes before `format` runs; the lock's hashes are of the files as the CLI writes them.
|
|
320
|
+
*/
|
|
321
|
+
export async function renderPlan(context, plan) {
|
|
322
|
+
let output = "";
|
|
323
|
+
for (let [path, change] of [...plan.files].sort(([a], [b]) => (a < b ? -1 : 1))) {
|
|
324
|
+
output += unifiedDiff(path, change.before, change.after);
|
|
325
|
+
}
|
|
326
|
+
let lock = await settleLock(context, plan, async (path) => {
|
|
327
|
+
let change = plan.files.get(path);
|
|
328
|
+
return change === undefined ? readProjectFile(context.root, path) : change.after;
|
|
329
|
+
});
|
|
330
|
+
if (isFailure(lock))
|
|
331
|
+
return lock;
|
|
332
|
+
let before = renderLock(context.lock);
|
|
333
|
+
let after = renderLock(lock.data);
|
|
334
|
+
if (isFailure(before))
|
|
335
|
+
return before;
|
|
336
|
+
if (isFailure(after))
|
|
337
|
+
return after;
|
|
338
|
+
output += unifiedDiff(context.lock.path, before.data === "" ? null : before.data, after.data === "" ? null : after.data);
|
|
339
|
+
return success(output);
|
|
340
|
+
}
|
|
341
|
+
/** The lock once the plan's entries are recorded, hashing each pending file through `read`. */
|
|
342
|
+
async function settleLock(context, plan, read) {
|
|
343
|
+
let lock = context.lock;
|
|
344
|
+
for (let name of plan.forget)
|
|
345
|
+
lock = removeUIEntry(lock, name);
|
|
346
|
+
for (let pending of plan.record) {
|
|
347
|
+
let files = {};
|
|
348
|
+
for (let [file, hash] of Object.entries(pending.files)) {
|
|
349
|
+
if (hash !== null) {
|
|
350
|
+
files[file] = hash;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
let content = await read(copyPath(context.layout, file));
|
|
354
|
+
if (content === null)
|
|
355
|
+
return failure(new Error(`${file} was not written`));
|
|
356
|
+
files[file] = hashContent(content);
|
|
357
|
+
}
|
|
358
|
+
let entry = { kind: "ui", name: pending.name, release: pending.release, files };
|
|
359
|
+
lock = setUIEntry(lock, entry);
|
|
360
|
+
}
|
|
361
|
+
return success(lock);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Runs `transform` over every project source outside the plan's own files, adding each file it
|
|
365
|
+
* changes to the plan and reporting each import it could not rewrite.
|
|
366
|
+
*/
|
|
367
|
+
async function rewriteProject(context, plan, transform) {
|
|
368
|
+
let sources = await projectSources(context.root);
|
|
369
|
+
if (isFailure(sources))
|
|
370
|
+
return sources;
|
|
371
|
+
for (let file of sources.data) {
|
|
372
|
+
if (plan.files.has(file))
|
|
373
|
+
continue;
|
|
374
|
+
let source = await readProjectFile(context.root, file);
|
|
375
|
+
if (source === null)
|
|
376
|
+
continue;
|
|
377
|
+
let rewritten = transform(file, source);
|
|
378
|
+
if (isFailure(rewritten))
|
|
379
|
+
return failure(new Error(`${file}: ${rewritten.error.message}`));
|
|
380
|
+
if (rewritten.data.source !== source) {
|
|
381
|
+
plan.files.set(file, { before: source, after: rewritten.data.source });
|
|
382
|
+
}
|
|
383
|
+
for (let location of rewritten.data.unhandled) {
|
|
384
|
+
plan.report.push(`${location} imports @sdxc/ui as a whole; import the copied components' names from their copies by hand`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return success(undefined);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Every copy the rewrite points imports at: the lock's copies plus `added`, with the names the
|
|
391
|
+
* registry says each exports. `includeLocked: false` answers `added` alone.
|
|
392
|
+
*/
|
|
393
|
+
function copiedComponents(context, added, includeLocked = true) {
|
|
394
|
+
let names = new Set(added.map((entry) => entry.name));
|
|
395
|
+
if (includeLocked)
|
|
396
|
+
for (let entry of uiEntries(context.lock))
|
|
397
|
+
names.add(entry.name);
|
|
398
|
+
return [...names].flatMap((name) => {
|
|
399
|
+
let entry = context.registry.entries.get(name);
|
|
400
|
+
return entry === undefined ? [] : [{ name, exports: entry.exports }];
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/** The entry's source, and its test when `withTest` and it has one. */
|
|
404
|
+
function registryFiles(entry, withTest) {
|
|
405
|
+
return withTest && entry.test !== null ? [entry.source, entry.test] : [entry.source];
|
|
406
|
+
}
|
|
407
|
+
/** Whether any file the lock recorded for the copy is missing or no longer hashes the same. */
|
|
408
|
+
async function isEdited(context, copied) {
|
|
409
|
+
for (let [file, hash] of Object.entries(copied.files)) {
|
|
410
|
+
let content = await readProjectFile(context.root, copyPath(context.layout, file));
|
|
411
|
+
if (content === null || hashContent(content) !== hash)
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
/** A failure naming every one of `names` the registry does not ship, or `null`. */
|
|
417
|
+
function unknownNames(context, names) {
|
|
418
|
+
let unknown = names.filter((name) => !context.registry.entries.has(name));
|
|
419
|
+
if (unknown.length === 0)
|
|
420
|
+
return null;
|
|
421
|
+
return new Error(`Release ${context.registry.release} ships no component named ${unknown.join(", ")}; sdxc ui list shows every one`);
|
|
422
|
+
}
|
|
423
|
+
/** A failure naming every one of `names` the lock file does not list as copied, or `null`. */
|
|
424
|
+
function uncopiedNames(context, names) {
|
|
425
|
+
let missing = names.filter((name) => uiEntry(context.lock, name) === undefined);
|
|
426
|
+
if (missing.length === 0)
|
|
427
|
+
return null;
|
|
428
|
+
return new Error(`Not copied: ${missing.join(", ")}; sdxc ui list shows the copies`);
|
|
429
|
+
}
|
|
430
|
+
function emptyPlan() {
|
|
431
|
+
return { files: new Map(), record: [], forget: [], report: [] };
|
|
432
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app-wide import rewrite behind `sdxc ui`: every TypeScript and JavaScript file in the
|
|
3
|
+
* project imports a copied component from its copy, and from `@sdxc/ui/<name>` again once the
|
|
4
|
+
* copy is removed. Edits go through `@sdxc/codemod`, so only the import declarations change.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { CopyLayout } from "./layout.js";
|
|
11
|
+
/** The specifier that exports every component. */
|
|
12
|
+
export declare const UI_ROOT = "@sdxc/ui";
|
|
13
|
+
/** A copied component as the rewrite needs it: its name and every name its module exports. */
|
|
14
|
+
export interface CopiedComponent {
|
|
15
|
+
name: string;
|
|
16
|
+
exports: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
/** A file's text after the rewrite, with the imports it could not split by name. */
|
|
19
|
+
export interface Rewritten {
|
|
20
|
+
source: string;
|
|
21
|
+
/** `<file>:<line>` of each default or namespace import of `@sdxc/ui` left as it was. */
|
|
22
|
+
unhandled: string[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Every source file in the project at `root`, relative to it with `/` separators and sorted,
|
|
26
|
+
* outside installed packages and build output.
|
|
27
|
+
*/
|
|
28
|
+
export declare function projectSources(root: string): Promise<Result<string[], Error>>;
|
|
29
|
+
/** Reads a project file, `null` when it does not exist. */
|
|
30
|
+
export declare function readProjectFile(root: string, path: string): Promise<string | null>;
|
|
31
|
+
/**
|
|
32
|
+
* `source` importing each of `copies` from its copy: `@sdxc/ui/<name>` is replaced, and the
|
|
33
|
+
* names a copy exports move off `@sdxc/ui` into an import of the copy. A default or namespace
|
|
34
|
+
* import of `@sdxc/ui` stays and is reported, since it cannot be split by name.
|
|
35
|
+
*
|
|
36
|
+
* @param file - The file's path relative to the project root, which decides relative specifiers.
|
|
37
|
+
*/
|
|
38
|
+
export declare function pointAtCopies(file: string, source: string, copies: readonly CopiedComponent[], layout: CopyLayout): Result<Rewritten, Error>;
|
|
39
|
+
/**
|
|
40
|
+
* `source` importing each of `removed` from the package again: an import of a copy is folded
|
|
41
|
+
* into the file's `@sdxc/ui` declaration when it has one, and points at `@sdxc/ui/<name>`
|
|
42
|
+
* otherwise.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pointBack(file: string, source: string, removed: readonly CopiedComponent[], layout: CopyLayout): Result<string, Error>;
|
|
45
|
+
/** `path` joined under the copies' folder, as a project-relative path. */
|
|
46
|
+
export declare function copyPath(layout: CopyLayout, file: string): string;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app-wide import rewrite behind `sdxc ui`: every TypeScript and JavaScript file in the
|
|
3
|
+
* project imports a copied component from its copy, and from `@sdxc/ui/<name>` again once the
|
|
4
|
+
* copy is removed. Edits go through `@sdxc/codemod`, so only the import declarations change.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
10
|
+
import { join, posix } from "node:path";
|
|
11
|
+
import { findModuleReferences, moveImports, rewriteSpecifiers } from "@sdxc/codemod";
|
|
12
|
+
import { isFailure, success, wrap } from "@sdxc/result";
|
|
13
|
+
import { copySpecifier, namesCopy } from "./layout.js";
|
|
14
|
+
/** The specifier that exports every component. */
|
|
15
|
+
export const UI_ROOT = "@sdxc/ui";
|
|
16
|
+
/** Extensions of the files the rewrite reads. */
|
|
17
|
+
const SOURCE_FILE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
18
|
+
/** Folders that hold installed packages, build output or tool state, never project sources. */
|
|
19
|
+
const SKIPPED_DIRECTORIES = new Set([
|
|
20
|
+
"node_modules",
|
|
21
|
+
".git",
|
|
22
|
+
"dist",
|
|
23
|
+
"build",
|
|
24
|
+
"coverage",
|
|
25
|
+
".wrangler",
|
|
26
|
+
".output",
|
|
27
|
+
".cache",
|
|
28
|
+
".turbo",
|
|
29
|
+
".next",
|
|
30
|
+
".vercel",
|
|
31
|
+
".netlify",
|
|
32
|
+
]);
|
|
33
|
+
/**
|
|
34
|
+
* Every source file in the project at `root`, relative to it with `/` separators and sorted,
|
|
35
|
+
* outside installed packages and build output.
|
|
36
|
+
*/
|
|
37
|
+
export async function projectSources(root) {
|
|
38
|
+
return wrap(async () => {
|
|
39
|
+
let found = [];
|
|
40
|
+
let walk = async (directory) => {
|
|
41
|
+
let entries = await readdir(join(root, directory), { withFileTypes: true });
|
|
42
|
+
for (let entry of entries) {
|
|
43
|
+
let path = directory === "" ? entry.name : `${directory}/${entry.name}`;
|
|
44
|
+
if (entry.isDirectory()) {
|
|
45
|
+
if (!SKIPPED_DIRECTORIES.has(entry.name))
|
|
46
|
+
await walk(path);
|
|
47
|
+
}
|
|
48
|
+
else if (entry.isFile() && SOURCE_FILE.test(entry.name)) {
|
|
49
|
+
found.push(path);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
await walk("");
|
|
54
|
+
return found.sort();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** Reads a project file, `null` when it does not exist. */
|
|
58
|
+
export async function readProjectFile(root, path) {
|
|
59
|
+
let text = await wrap(() => readFile(join(root, path), "utf8"));
|
|
60
|
+
return isFailure(text) ? null : text.data;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* `source` importing each of `copies` from its copy: `@sdxc/ui/<name>` is replaced, and the
|
|
64
|
+
* names a copy exports move off `@sdxc/ui` into an import of the copy. A default or namespace
|
|
65
|
+
* import of `@sdxc/ui` stays and is reported, since it cannot be split by name.
|
|
66
|
+
*
|
|
67
|
+
* @param file - The file's path relative to the project root, which decides relative specifiers.
|
|
68
|
+
*/
|
|
69
|
+
export function pointAtCopies(file, source, copies, layout) {
|
|
70
|
+
if (copies.length === 0 || !source.includes(UI_ROOT))
|
|
71
|
+
return success({ source, unhandled: [] });
|
|
72
|
+
let current = source;
|
|
73
|
+
let unsplit = false;
|
|
74
|
+
for (let copy of copies) {
|
|
75
|
+
let target = copySpecifier(layout, file, copy.name);
|
|
76
|
+
let renamed = rewriteSpecifiers(file, current, (reference) => reference.specifier === `${UI_ROOT}/${copy.name}` ? target : null);
|
|
77
|
+
if (isFailure(renamed))
|
|
78
|
+
return renamed;
|
|
79
|
+
current = renamed.data.source;
|
|
80
|
+
if (copy.exports.length === 0)
|
|
81
|
+
continue;
|
|
82
|
+
let moved = moveImports(file, current, { from: UI_ROOT, names: [...copy.exports], to: target });
|
|
83
|
+
if (isFailure(moved))
|
|
84
|
+
return moved;
|
|
85
|
+
current = moved.data.source;
|
|
86
|
+
if (moved.data.unhandled.length > 0)
|
|
87
|
+
unsplit = true;
|
|
88
|
+
}
|
|
89
|
+
if (!unsplit)
|
|
90
|
+
return success({ source: current, unhandled: [] });
|
|
91
|
+
let references = findModuleReferences(file, current);
|
|
92
|
+
if (isFailure(references))
|
|
93
|
+
return references;
|
|
94
|
+
let reports = references.data
|
|
95
|
+
.filter((reference) => reference.specifier === UI_ROOT && !splittable(reference))
|
|
96
|
+
.map((reference) => `${file}:${lineOf(current, reference.range.start)}`);
|
|
97
|
+
return success({ source: current, unhandled: reports });
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `source` importing each of `removed` from the package again: an import of a copy is folded
|
|
101
|
+
* into the file's `@sdxc/ui` declaration when it has one, and points at `@sdxc/ui/<name>`
|
|
102
|
+
* otherwise.
|
|
103
|
+
*/
|
|
104
|
+
export function pointBack(file, source, removed, layout) {
|
|
105
|
+
let current = source;
|
|
106
|
+
for (let copy of removed) {
|
|
107
|
+
if (!current.includes(copy.name))
|
|
108
|
+
continue;
|
|
109
|
+
let references = findModuleReferences(file, current);
|
|
110
|
+
if (isFailure(references))
|
|
111
|
+
return references;
|
|
112
|
+
let specifiers = new Set(references.data
|
|
113
|
+
.filter((reference) => namesCopy(layout, file, reference.specifier, copy.name))
|
|
114
|
+
.map((reference) => reference.specifier));
|
|
115
|
+
if (specifiers.size === 0)
|
|
116
|
+
continue;
|
|
117
|
+
let hasRoot = references.data.some((reference) => (reference.kind === "import" || reference.kind === "export") &&
|
|
118
|
+
reference.specifier === UI_ROOT &&
|
|
119
|
+
reference.braces !== null);
|
|
120
|
+
for (let specifier of specifiers) {
|
|
121
|
+
if (hasRoot && copy.exports.length > 0) {
|
|
122
|
+
let folded = moveImports(file, current, {
|
|
123
|
+
from: specifier,
|
|
124
|
+
names: [...copy.exports],
|
|
125
|
+
to: UI_ROOT,
|
|
126
|
+
});
|
|
127
|
+
if (isFailure(folded))
|
|
128
|
+
return folded;
|
|
129
|
+
current = folded.data.source;
|
|
130
|
+
}
|
|
131
|
+
let renamed = rewriteSpecifiers(file, current, (reference) => reference.specifier === specifier ? `${UI_ROOT}/${copy.name}` : null);
|
|
132
|
+
if (isFailure(renamed))
|
|
133
|
+
return renamed;
|
|
134
|
+
current = renamed.data.source;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return success(current);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Whether a declaration can be split by name: no default or namespace binding, and no
|
|
141
|
+
* `export *`. A side-effect import binds nothing and counts as splittable.
|
|
142
|
+
*/
|
|
143
|
+
function splittable(reference) {
|
|
144
|
+
if (reference.kind === "export" && reference.braces === null)
|
|
145
|
+
return false;
|
|
146
|
+
return reference.names.every((name) => name.imported !== "default" && name.imported !== "*");
|
|
147
|
+
}
|
|
148
|
+
/** The 1-based line of `offset` in `source`. */
|
|
149
|
+
function lineOf(source, offset) {
|
|
150
|
+
let line = 1;
|
|
151
|
+
for (let index = 0; index < offset; index++) {
|
|
152
|
+
if (source.charCodeAt(index) === 10)
|
|
153
|
+
line += 1;
|
|
154
|
+
}
|
|
155
|
+
return line;
|
|
156
|
+
}
|
|
157
|
+
/** `path` joined under the copies' folder, as a project-relative path. */
|
|
158
|
+
export function copyPath(layout, file) {
|
|
159
|
+
return posix.join(layout.directory, file);
|
|
160
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where copied components live and how the project spells an import of one: the folder from
|
|
3
|
+
* `ui.directory`, the prefix from `ui.alias` or the tsconfig `paths` entry covering that folder,
|
|
4
|
+
* and the extension from the tsconfig's module resolution.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Config } from "../config.js";
|
|
11
|
+
/** How the project imports its copies. */
|
|
12
|
+
export interface CopyLayout {
|
|
13
|
+
/** The copies' folder relative to the project root, with `/` separators. */
|
|
14
|
+
directory: string;
|
|
15
|
+
/** The import prefix for the folder, or `null` when files import copies relatively. */
|
|
16
|
+
alias: string | null;
|
|
17
|
+
/** `.js` under `node16` and `nodenext` resolution, which require it; empty otherwise. */
|
|
18
|
+
extension: "" | ".js";
|
|
19
|
+
}
|
|
20
|
+
/** The `compilerOptions` the layout reads, merged down an `extends` chain. */
|
|
21
|
+
export interface CompilerOptions {
|
|
22
|
+
paths?: Record<string, string[]>;
|
|
23
|
+
/** The directory `paths` targets resolve against, absolute. */
|
|
24
|
+
pathsBase?: string;
|
|
25
|
+
moduleResolution?: string;
|
|
26
|
+
module?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The layout of the project at `root`: `ui.alias` when configured, otherwise inferred from the
|
|
30
|
+
* root `tsconfig.json`. A project without a tsconfig imports copies relatively, without an
|
|
31
|
+
* extension.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readLayout(root: string, config: Config): Promise<Result<CopyLayout, Error>>;
|
|
34
|
+
/**
|
|
35
|
+
* The import prefix a tsconfig `paths` entry gives `directory`: the `<prefix>/*` entry whose
|
|
36
|
+
* target folder contains it, the deepest such target when several do.
|
|
37
|
+
*/
|
|
38
|
+
export declare function inferAlias(root: string, directory: string, options: CompilerOptions): string | null;
|
|
39
|
+
/**
|
|
40
|
+
* The specifier `file` (relative to the project root) imports the copy of `name` with: relative
|
|
41
|
+
* inside the copies' folder and without an alias, the alias elsewhere.
|
|
42
|
+
*/
|
|
43
|
+
export declare function copySpecifier(layout: CopyLayout, file: string, name: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Whether `specifier`, imported from `file`, names the copy of `name`: through the alias or a
|
|
46
|
+
* relative path, with or without an extension, however the project spelled it.
|
|
47
|
+
*/
|
|
48
|
+
export declare function namesCopy(layout: CopyLayout, file: string, specifier: string, name: string): boolean;
|