@usalyt/cli 0.0.0-stage → 0.0.1
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/CHANGELOG.md +15 -0
- package/LICENSE +674 -0
- package/NOTICE +32 -0
- package/README.md +241 -2
- package/THIRD_PARTY_NOTICES.md +446 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +653 -0
- package/package.json +46 -3
- package/src/commands/add.ts +199 -0
- package/src/commands/init.ts +103 -0
- package/src/commands/list.ts +54 -0
- package/src/index.ts +20 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/imports.ts +92 -0
- package/src/lib/install.ts +225 -0
- package/src/lib/output.ts +7 -0
- package/src/lib/paths.ts +99 -0
- package/src/lib/registry.ts +120 -0
- package/tsdown.config.ts +9 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { lstatSync, realpathSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { UsalytConfig } from "./config";
|
|
4
|
+
import { rewriteRelativeImports } from "./imports";
|
|
5
|
+
import { assertWithinRoot, isWithinRoot, resolveAliasWithinRoot, UnsafePathError } from "./paths";
|
|
6
|
+
import type { Registry, RegistryItem } from "./registry";
|
|
7
|
+
|
|
8
|
+
export class UnknownItemsError extends Error {
|
|
9
|
+
readonly names: string[];
|
|
10
|
+
|
|
11
|
+
constructor(names: string[]) {
|
|
12
|
+
super(`unknown component(s): ${names.join(", ")}`);
|
|
13
|
+
this.name = "UnknownItemsError";
|
|
14
|
+
this.names = names;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class UnsupportedItemError extends Error {
|
|
19
|
+
constructor(message: string) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "UnsupportedItemError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PlannedWrite {
|
|
26
|
+
item: string;
|
|
27
|
+
destination: string;
|
|
28
|
+
relativeDestination: string;
|
|
29
|
+
content: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface InstallPlan {
|
|
33
|
+
writes: PlannedWrite[];
|
|
34
|
+
dependencies: string[];
|
|
35
|
+
conflicts: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const SAFE_ITEM_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
39
|
+
const SAFE_FILE_BASENAME = /^[A-Za-z0-9._-]+$/;
|
|
40
|
+
const REWRITE_EXTENSIONS = /\.(tsx?|jsx?|mts?|cts?)$/;
|
|
41
|
+
|
|
42
|
+
function collectRegistryDependencies(
|
|
43
|
+
targets: string[],
|
|
44
|
+
itemsByName: ReadonlyMap<string, RegistryItem>
|
|
45
|
+
): string[] {
|
|
46
|
+
const planned = new Set<string>();
|
|
47
|
+
const ordered: string[] = [];
|
|
48
|
+
|
|
49
|
+
const visit = (name: string): void => {
|
|
50
|
+
if (planned.has(name)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const item = itemsByName.get(name);
|
|
54
|
+
if (!item) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
planned.add(name);
|
|
58
|
+
for (const dependency of [...item.registryDependencies].sort()) {
|
|
59
|
+
visit(dependency);
|
|
60
|
+
}
|
|
61
|
+
ordered.push(name);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
for (const target of targets) {
|
|
65
|
+
visit(target);
|
|
66
|
+
}
|
|
67
|
+
return ordered;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function relativeDisplayPath(root: string, destination: string): string {
|
|
71
|
+
return path.relative(root, destination).split(path.sep).join("/");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function extensionOf(alias: string): string {
|
|
75
|
+
return path.extname(alias);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Build every destination and its rewritten content before touching the
|
|
80
|
+
* filesystem. Unknown items, unsafe destinations, directory collisions, and
|
|
81
|
+
* conflicts are all detected here so a rejected request writes nothing.
|
|
82
|
+
*/
|
|
83
|
+
export function buildInstallPlan(options: {
|
|
84
|
+
registry: Registry;
|
|
85
|
+
config: UsalytConfig;
|
|
86
|
+
root: string;
|
|
87
|
+
targets: string[];
|
|
88
|
+
overwrite: boolean;
|
|
89
|
+
}): InstallPlan {
|
|
90
|
+
const { registry, config, root, targets, overwrite } = options;
|
|
91
|
+
const itemsByName = new Map(registry.items.map((item) => [item.name, item]));
|
|
92
|
+
const unknown = targets.filter((target) => !itemsByName.has(target));
|
|
93
|
+
if (unknown.length > 0) {
|
|
94
|
+
throw new UnknownItemsError(unknown);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const plannedNames = collectRegistryDependencies(targets, itemsByName);
|
|
98
|
+
const componentsDir = resolveAliasWithinRoot(root, config.aliases.components, "components");
|
|
99
|
+
|
|
100
|
+
const utilsAlias = config.aliases.utils;
|
|
101
|
+
let utilsDestination = resolveAliasWithinRoot(root, utilsAlias, "utils");
|
|
102
|
+
const utilsExtension = extensionOf(utilsAlias);
|
|
103
|
+
if (utilsExtension === "") {
|
|
104
|
+
utilsDestination = `${utilsDestination}.ts`;
|
|
105
|
+
assertWithinRoot(root, utilsDestination, "utils");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const sourceToDest = new Map<string, string>();
|
|
109
|
+
const destinationOwner = new Map<string, string>();
|
|
110
|
+
|
|
111
|
+
for (const name of plannedNames) {
|
|
112
|
+
const item = itemsByName.get(name)!;
|
|
113
|
+
if (!SAFE_ITEM_NAME.test(item.name)) {
|
|
114
|
+
throw new UnsupportedItemError(
|
|
115
|
+
`Registry item "${item.name}" has an unsafe name and cannot be installed.`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const file of item.files) {
|
|
120
|
+
const baseName = path.posix.basename(file.path);
|
|
121
|
+
if (!SAFE_FILE_BASENAME.test(baseName) || baseName === "." || baseName === "..") {
|
|
122
|
+
throw new UnsupportedItemError(
|
|
123
|
+
`Registry item "${item.name}" contains an unsafe file path: ${file.path}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let destination: string;
|
|
128
|
+
if (file.path.startsWith("lib/")) {
|
|
129
|
+
if (item.name !== "utils") {
|
|
130
|
+
throw new UnsupportedItemError(
|
|
131
|
+
`Registry item "${item.name}" targets the shared lib directory, which is reserved for the "utils" item.`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
destination = utilsDestination;
|
|
135
|
+
} else {
|
|
136
|
+
destination = path.join(componentsDir, item.name, baseName);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
assertWithinRoot(root, destination, `"${item.name}"`);
|
|
140
|
+
|
|
141
|
+
const owner = destinationOwner.get(destination);
|
|
142
|
+
if (owner !== undefined && owner !== file.path) {
|
|
143
|
+
throw new UnsupportedItemError(
|
|
144
|
+
`Registry files "${owner}" and "${file.path}" resolve to the same destination: ${destination}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
destinationOwner.set(destination, file.path);
|
|
148
|
+
sourceToDest.set(file.path, destination);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const conflicts: string[] = [];
|
|
153
|
+
for (const destination of destinationOwner.keys()) {
|
|
154
|
+
let entry;
|
|
155
|
+
try {
|
|
156
|
+
entry = lstatSync(destination);
|
|
157
|
+
} catch {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (entry.isSymbolicLink()) {
|
|
162
|
+
let realDestination: string;
|
|
163
|
+
try {
|
|
164
|
+
realDestination = realpathSync(destination);
|
|
165
|
+
} catch {
|
|
166
|
+
throw new UnsupportedItemError(
|
|
167
|
+
`Cannot write "${destination}" because it is a symbolic link with a missing target.`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const realRoot = realpathSync(root);
|
|
171
|
+
if (!isWithinRoot(realRoot, realDestination)) {
|
|
172
|
+
throw new UnsafePathError(
|
|
173
|
+
destinationOwner.get(destination) ?? "destination",
|
|
174
|
+
destination,
|
|
175
|
+
`The destination "${destination}" resolves outside the consumer root ${root} through a symbolic link.`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (entry.isDirectory()) {
|
|
181
|
+
throw new UnsupportedItemError(
|
|
182
|
+
`Cannot write "${destination}" because a directory already exists at that path.`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (!overwrite) {
|
|
186
|
+
conflicts.push(relativeDisplayPath(root, destination));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const writes: PlannedWrite[] = [];
|
|
191
|
+
for (const name of plannedNames) {
|
|
192
|
+
const item = itemsByName.get(name)!;
|
|
193
|
+
for (const file of item.files) {
|
|
194
|
+
const destination = sourceToDest.get(file.path)!;
|
|
195
|
+
const content = REWRITE_EXTENSIONS.test(file.path)
|
|
196
|
+
? rewriteRelativeImports({
|
|
197
|
+
content: file.content,
|
|
198
|
+
sourcePath: file.path,
|
|
199
|
+
destPath: destination,
|
|
200
|
+
sourceToDest,
|
|
201
|
+
})
|
|
202
|
+
: file.content;
|
|
203
|
+
writes.push({
|
|
204
|
+
item: item.name,
|
|
205
|
+
destination,
|
|
206
|
+
relativeDestination: relativeDisplayPath(root, destination),
|
|
207
|
+
content,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const dependencies = new Set<string>();
|
|
213
|
+
for (const name of plannedNames) {
|
|
214
|
+
const item = itemsByName.get(name)!;
|
|
215
|
+
for (const dependency of item.dependencies) {
|
|
216
|
+
dependencies.add(dependency);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
writes,
|
|
222
|
+
dependencies: [...dependencies].sort(),
|
|
223
|
+
conflicts: conflicts.sort(),
|
|
224
|
+
};
|
|
225
|
+
}
|
package/src/lib/paths.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export class UnsafePathError extends Error {
|
|
5
|
+
readonly destination: string;
|
|
6
|
+
readonly alias: string;
|
|
7
|
+
|
|
8
|
+
constructor(alias: string, destination: string, message: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "UnsafePathError";
|
|
11
|
+
this.alias = alias;
|
|
12
|
+
this.destination = destination;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/;
|
|
17
|
+
|
|
18
|
+
/** Strip the `@/` or `~/` prefix and return the root-relative alias path. */
|
|
19
|
+
export function aliasToRelativePath(alias: string, label: string): string {
|
|
20
|
+
if (typeof alias !== "string" || alias.trim() === "") {
|
|
21
|
+
throw new UnsafePathError(alias, "", `The ${label} alias must be a non-empty string.`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const original = alias.trim();
|
|
25
|
+
let value = original;
|
|
26
|
+
if (value.startsWith("@/") || value.startsWith("~/")) {
|
|
27
|
+
value = value.slice(2);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (value.includes("\0")) {
|
|
31
|
+
throw new UnsafePathError(
|
|
32
|
+
original,
|
|
33
|
+
"",
|
|
34
|
+
`The ${label} alias contains a null byte and cannot be used.`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (path.isAbsolute(value) || WINDOWS_ABSOLUTE.test(value)) {
|
|
38
|
+
throw new UnsafePathError(
|
|
39
|
+
original,
|
|
40
|
+
value,
|
|
41
|
+
`The ${label} alias "${original}" is absolute. Aliases must resolve inside the consumer root.`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return value === "" ? "." : value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function isWithinRoot(root: string, destination: string): boolean {
|
|
49
|
+
const relative = path.relative(root, destination);
|
|
50
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolve an existing ancestor through `realpath` and append the missing
|
|
55
|
+
* segments, so a symlinked parent that points outside the root is detected
|
|
56
|
+
* before any directory or file is created.
|
|
57
|
+
*/
|
|
58
|
+
function realpathAllowMissing(target: string): string {
|
|
59
|
+
let current = target;
|
|
60
|
+
const missing: string[] = [];
|
|
61
|
+
while (!existsSync(current)) {
|
|
62
|
+
const parent = path.dirname(current);
|
|
63
|
+
if (parent === current) {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
missing.unshift(path.basename(current));
|
|
67
|
+
current = parent;
|
|
68
|
+
}
|
|
69
|
+
const base = existsSync(current) ? realpathSync(current) : current;
|
|
70
|
+
return path.join(base, ...missing);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function assertWithinRoot(root: string, destination: string, label: string): void {
|
|
74
|
+
if (!isWithinRoot(root, destination)) {
|
|
75
|
+
throw new UnsafePathError(
|
|
76
|
+
label,
|
|
77
|
+
destination,
|
|
78
|
+
`The ${label} destination "${destination}" resolves outside the consumer root ${root}.`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const realRoot = realpathSync(root);
|
|
83
|
+
const realDestination = realpathAllowMissing(destination);
|
|
84
|
+
if (!isWithinRoot(realRoot, realDestination)) {
|
|
85
|
+
throw new UnsafePathError(
|
|
86
|
+
label,
|
|
87
|
+
destination,
|
|
88
|
+
`The ${label} destination "${destination}" resolves outside the consumer root ${root} through a symbolic link.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Resolve a configured alias against the consumer root and enforce containment. */
|
|
94
|
+
export function resolveAliasWithinRoot(root: string, alias: string, label: string): string {
|
|
95
|
+
const relative = aliasToRelativePath(alias, label);
|
|
96
|
+
const destination = path.resolve(root, relative);
|
|
97
|
+
assertWithinRoot(root, destination, label);
|
|
98
|
+
return destination;
|
|
99
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
export interface RegistryFile {
|
|
7
|
+
path: string;
|
|
8
|
+
type: string;
|
|
9
|
+
content: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RegistryItem {
|
|
13
|
+
name: string;
|
|
14
|
+
type: string;
|
|
15
|
+
title: string;
|
|
16
|
+
description: string;
|
|
17
|
+
license: string;
|
|
18
|
+
dependencies: string[];
|
|
19
|
+
devDependencies: string[];
|
|
20
|
+
registryDependencies: string[];
|
|
21
|
+
dependencyVersions: Record<string, string>;
|
|
22
|
+
files: RegistryFile[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Registry {
|
|
26
|
+
name: string;
|
|
27
|
+
version: string;
|
|
28
|
+
items: RegistryItem[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class RegistryLoadError extends Error {
|
|
32
|
+
constructor(message: string) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "RegistryLoadError";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const require = createRequire(import.meta.url);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Resolve the registry through Node package resolution so the CLI works from
|
|
42
|
+
* any working directory once `@usalyt/registry` is installed. The relative
|
|
43
|
+
* candidates are a development-only fallback for the monorepo layout and are
|
|
44
|
+
* never based on the current working directory.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveRegistryPath(): string {
|
|
47
|
+
try {
|
|
48
|
+
return require.resolve("@usalyt/registry/registry.json");
|
|
49
|
+
} catch {
|
|
50
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
51
|
+
const candidates = [
|
|
52
|
+
path.resolve(here, "../../registry/registry.json"),
|
|
53
|
+
path.resolve(here, "../registry/registry.json"),
|
|
54
|
+
];
|
|
55
|
+
for (const candidate of candidates) {
|
|
56
|
+
if (existsSync(candidate)) {
|
|
57
|
+
return candidate;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw new RegistryLoadError(
|
|
61
|
+
"Could not locate the packaged @usalyt/registry/registry.json. " +
|
|
62
|
+
"Reinstall @usalyt/cli so its @usalyt/registry dependency is present, then retry."
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
68
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function validateRegistry(value: unknown, registryPath: string): Registry {
|
|
72
|
+
if (!isRecord(value)) {
|
|
73
|
+
throw new RegistryLoadError(`Registry at ${registryPath} is not a JSON object.`);
|
|
74
|
+
}
|
|
75
|
+
if (typeof value.name !== "string" || typeof value.version !== "string") {
|
|
76
|
+
throw new RegistryLoadError(`Registry at ${registryPath} is missing "name" or "version".`);
|
|
77
|
+
}
|
|
78
|
+
if (!Array.isArray(value.items) || value.items.length === 0) {
|
|
79
|
+
throw new RegistryLoadError(`Registry at ${registryPath} contains no items.`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (const item of value.items) {
|
|
83
|
+
if (!isRecord(item) || typeof item.name !== "string" || typeof item.type !== "string") {
|
|
84
|
+
throw new RegistryLoadError(`Registry at ${registryPath} contains an invalid item.`);
|
|
85
|
+
}
|
|
86
|
+
if (!Array.isArray(item.files) || item.files.length === 0) {
|
|
87
|
+
throw new RegistryLoadError(`Registry item "${item.name}" contains no files.`);
|
|
88
|
+
}
|
|
89
|
+
for (const file of item.files) {
|
|
90
|
+
if (!isRecord(file) || typeof file.path !== "string" || typeof file.content !== "string") {
|
|
91
|
+
throw new RegistryLoadError(`Registry item "${item.name}" contains an invalid file entry.`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return value as unknown as Registry;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function loadRegistry(): { registry: Registry; registryPath: string } {
|
|
100
|
+
const registryPath = resolveRegistryPath();
|
|
101
|
+
let raw: string;
|
|
102
|
+
try {
|
|
103
|
+
raw = readFileSync(registryPath, "utf8");
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw new RegistryLoadError(
|
|
106
|
+
`Could not read the packaged registry at ${registryPath}: ${(error as Error).message}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let parsed: unknown;
|
|
111
|
+
try {
|
|
112
|
+
parsed = JSON.parse(raw);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw new RegistryLoadError(
|
|
115
|
+
`The packaged registry at ${registryPath} is not valid JSON: ${(error as Error).message}`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return { registry: validateRegistry(parsed, registryPath), registryPath };
|
|
120
|
+
}
|