@usalyt/cli 0.0.0-stage → 0.0.2
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 +23 -0
- package/LICENSE +21 -0
- package/NOTICE +31 -0
- package/README.md +255 -2
- package/THIRD_PARTY_NOTICES.md +446 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +2068 -0
- package/package.json +46 -3
- package/src/commands/add.ts +229 -0
- package/src/commands/diff.ts +130 -0
- package/src/commands/init.ts +271 -0
- package/src/commands/list.ts +54 -0
- package/src/commands/theme.ts +194 -0
- package/src/index.ts +25 -0
- package/src/lib/config.ts +141 -0
- package/src/lib/imports.ts +118 -0
- package/src/lib/install.ts +297 -0
- package/src/lib/output.ts +7 -0
- package/src/lib/paths.ts +125 -0
- package/src/lib/project.ts +218 -0
- package/src/lib/registry.ts +120 -0
- package/src/lib/theme.ts +161 -0
- package/tsdown.config.ts +9 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import pc from "picocolors";
|
|
3
|
+
import { printError } from "../lib/output";
|
|
4
|
+
import { loadRegistry, type Registry } from "../lib/registry";
|
|
5
|
+
|
|
6
|
+
export const listCommand = defineCommand({
|
|
7
|
+
meta: {
|
|
8
|
+
name: "list",
|
|
9
|
+
description: "List the items in the packaged Usalyt registry",
|
|
10
|
+
},
|
|
11
|
+
args: {
|
|
12
|
+
json: {
|
|
13
|
+
type: "boolean",
|
|
14
|
+
description: "Print machine-readable JSON",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
run({ args }) {
|
|
18
|
+
let registry: Registry;
|
|
19
|
+
try {
|
|
20
|
+
({ registry } = loadRegistry());
|
|
21
|
+
} catch (error) {
|
|
22
|
+
printError((error as Error).message);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (args.json) {
|
|
27
|
+
const payload = {
|
|
28
|
+
name: registry.name,
|
|
29
|
+
version: registry.version,
|
|
30
|
+
count: registry.items.length,
|
|
31
|
+
items: registry.items.map((item) => ({
|
|
32
|
+
name: item.name,
|
|
33
|
+
type: item.type,
|
|
34
|
+
title: item.title,
|
|
35
|
+
description: item.description,
|
|
36
|
+
license: item.license,
|
|
37
|
+
dependencies: item.dependencies,
|
|
38
|
+
registryDependencies: item.registryDependencies,
|
|
39
|
+
})),
|
|
40
|
+
};
|
|
41
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
console.log(
|
|
46
|
+
`${registry.name} registry ${registry.version}: ${registry.items.length} items`
|
|
47
|
+
);
|
|
48
|
+
for (const item of registry.items) {
|
|
49
|
+
console.log(` ${pc.bold(item.name)} (${item.type}) - ${item.description}`);
|
|
50
|
+
}
|
|
51
|
+
console.log("");
|
|
52
|
+
console.log("Add components with: usalyt add <component>");
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { defineCommand } from "citty";
|
|
3
|
+
import {
|
|
4
|
+
CONFIG_FILE,
|
|
5
|
+
ConfigError,
|
|
6
|
+
configPathFor,
|
|
7
|
+
readConfig,
|
|
8
|
+
serializeConfig,
|
|
9
|
+
} from "../lib/config";
|
|
10
|
+
import { printError } from "../lib/output";
|
|
11
|
+
import { UnsafePathError } from "../lib/paths";
|
|
12
|
+
import { detectCssEntry, injectThemeStylesheet } from "../lib/project";
|
|
13
|
+
import { loadRegistry } from "../lib/registry";
|
|
14
|
+
import {
|
|
15
|
+
applyThemePresetToCss,
|
|
16
|
+
DEFAULT_THEME_CONFIG,
|
|
17
|
+
encodeThemePreset,
|
|
18
|
+
hasThemeFlags,
|
|
19
|
+
NAMED_THEME_PRESETS,
|
|
20
|
+
resolveThemeFromFlags,
|
|
21
|
+
ThemeOptionError,
|
|
22
|
+
toUsalytThemeConfig,
|
|
23
|
+
type ThemePresetConfig,
|
|
24
|
+
} from "../lib/theme";
|
|
25
|
+
|
|
26
|
+
const applyThemeCommand = defineCommand({
|
|
27
|
+
meta: {
|
|
28
|
+
name: "apply",
|
|
29
|
+
description: "Apply a theme preset or token overrides to usalyt-theme.css",
|
|
30
|
+
},
|
|
31
|
+
args: {
|
|
32
|
+
preset: {
|
|
33
|
+
type: "string",
|
|
34
|
+
description:
|
|
35
|
+
"Named theme preset (e.g. linear, console-dense, minimal-light) or Theme Studio URL/query",
|
|
36
|
+
},
|
|
37
|
+
base: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Surface base palette (linear, zinc, slate, stone)",
|
|
40
|
+
},
|
|
41
|
+
accent: {
|
|
42
|
+
type: "string",
|
|
43
|
+
description:
|
|
44
|
+
"Primary accent (lavender, cobalt, emerald, amber, crimson, monochrome, or #RRGGBB)",
|
|
45
|
+
},
|
|
46
|
+
radius: {
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "Border radius scale (none, sm, md, lg, xl)",
|
|
49
|
+
},
|
|
50
|
+
density: {
|
|
51
|
+
type: "string",
|
|
52
|
+
description: "Spacing density scale (compact, default, comfortable)",
|
|
53
|
+
},
|
|
54
|
+
font: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "Sans font stack (inter, system, geist, jakarta)",
|
|
57
|
+
},
|
|
58
|
+
mode: {
|
|
59
|
+
type: "string",
|
|
60
|
+
description: "Default theme polarity (dark, light)",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
async run({ args }) {
|
|
64
|
+
const root = process.cwd();
|
|
65
|
+
|
|
66
|
+
let config;
|
|
67
|
+
try {
|
|
68
|
+
config = readConfig(root);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (error instanceof ConfigError) {
|
|
71
|
+
printError(error.message);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!config) {
|
|
78
|
+
printError(
|
|
79
|
+
`No ${CONFIG_FILE} found in ${root}. Run \`usalyt init\` first.`
|
|
80
|
+
);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const flags = {
|
|
85
|
+
preset: args.preset,
|
|
86
|
+
base: args.base,
|
|
87
|
+
accent: args.accent,
|
|
88
|
+
radius: args.radius,
|
|
89
|
+
density: args.density,
|
|
90
|
+
font: args.font,
|
|
91
|
+
mode: args.mode,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (!hasThemeFlags(flags)) {
|
|
95
|
+
printError(
|
|
96
|
+
"Pass --preset <name|query|url> or at least one token flag (--base, --accent, --radius, --density, --font, --mode)."
|
|
97
|
+
);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const currentStart: ThemePresetConfig = config.theme?.preset
|
|
102
|
+
? resolveThemeFromFlags({ preset: config.theme.preset }, DEFAULT_THEME_CONFIG)
|
|
103
|
+
: DEFAULT_THEME_CONFIG;
|
|
104
|
+
|
|
105
|
+
let resolvedTheme: ThemePresetConfig;
|
|
106
|
+
try {
|
|
107
|
+
resolvedTheme = resolveThemeFromFlags(flags, currentStart);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (error instanceof ThemeOptionError) {
|
|
110
|
+
printError(error.message);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const cssEntry = config.tailwind?.css || detectCssEntry(root);
|
|
117
|
+
try {
|
|
118
|
+
const { registry } = loadRegistry();
|
|
119
|
+
const themeItem = registry.items.find((item) => item.name === "theme");
|
|
120
|
+
const themeContent = themeItem?.files[0]?.content;
|
|
121
|
+
if (!themeContent) {
|
|
122
|
+
printError("The packaged registry does not contain a theme stylesheet.");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const finalThemeCss = applyThemePresetToCss(themeContent, resolvedTheme);
|
|
127
|
+
const result = injectThemeStylesheet({
|
|
128
|
+
root,
|
|
129
|
+
cssEntryRelative: cssEntry,
|
|
130
|
+
themeCss: finalThemeCss,
|
|
131
|
+
overwriteTheme: true,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const updatedConfig = {
|
|
135
|
+
...config,
|
|
136
|
+
tailwind: {
|
|
137
|
+
...config.tailwind,
|
|
138
|
+
css: cssEntry,
|
|
139
|
+
baseColor: resolvedTheme.base,
|
|
140
|
+
},
|
|
141
|
+
theme: toUsalytThemeConfig(resolvedTheme),
|
|
142
|
+
};
|
|
143
|
+
writeFileSync(configPathFor(root), serializeConfig(updatedConfig), "utf8");
|
|
144
|
+
|
|
145
|
+
const presetCode = encodeThemePreset(resolvedTheme);
|
|
146
|
+
console.log(`Applied theme preset "${presetCode}"`);
|
|
147
|
+
console.log(` theme: ${result.themeFile}`);
|
|
148
|
+
console.log(` config: ${CONFIG_FILE}`);
|
|
149
|
+
if (result.cssUpdated) {
|
|
150
|
+
console.log(` updated: ${result.cssEntry}`);
|
|
151
|
+
}
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof UnsafePathError) {
|
|
154
|
+
printError(error.message);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
printError(`could not apply theme preset: ${(error as Error).message}`);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const listThemesCommand = defineCommand({
|
|
163
|
+
meta: {
|
|
164
|
+
name: "list",
|
|
165
|
+
description: "List available curated theme presets",
|
|
166
|
+
},
|
|
167
|
+
args: {
|
|
168
|
+
json: {
|
|
169
|
+
type: "boolean",
|
|
170
|
+
description: "Output theme presets as JSON",
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
run({ args }) {
|
|
174
|
+
if (args.json) {
|
|
175
|
+
console.log(JSON.stringify({ presets: NAMED_THEME_PRESETS }, null, 2));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
console.log("Available Usalyt theme presets:");
|
|
179
|
+
for (const preset of NAMED_THEME_PRESETS) {
|
|
180
|
+
console.log(` ${preset.id.padEnd(18)} ${preset.description}`);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
export const themeCommand = defineCommand({
|
|
186
|
+
meta: {
|
|
187
|
+
name: "theme",
|
|
188
|
+
description: "List or apply Usalyt theme presets and design token overrides",
|
|
189
|
+
},
|
|
190
|
+
subCommands: {
|
|
191
|
+
apply: applyThemeCommand,
|
|
192
|
+
list: listThemesCommand,
|
|
193
|
+
},
|
|
194
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { defineCommand, runMain } from "citty";
|
|
3
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
4
|
+
import { addCommand } from "./commands/add";
|
|
5
|
+
import { diffCommand } from "./commands/diff";
|
|
6
|
+
import { initCommand } from "./commands/init";
|
|
7
|
+
import { listCommand } from "./commands/list";
|
|
8
|
+
import { themeCommand } from "./commands/theme";
|
|
9
|
+
|
|
10
|
+
const main = defineCommand({
|
|
11
|
+
meta: {
|
|
12
|
+
name: "usalyt",
|
|
13
|
+
version: packageJson.version,
|
|
14
|
+
description: "Usalyt component distribution CLI",
|
|
15
|
+
},
|
|
16
|
+
subCommands: {
|
|
17
|
+
init: initCommand,
|
|
18
|
+
add: addCommand,
|
|
19
|
+
diff: diffCommand,
|
|
20
|
+
list: listCommand,
|
|
21
|
+
theme: themeCommand,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
runMain(main);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const CONFIG_FILE = "usalyt.json";
|
|
5
|
+
|
|
6
|
+
export interface UsalytThemeConfig {
|
|
7
|
+
preset?: string;
|
|
8
|
+
base?: string;
|
|
9
|
+
accent?: string;
|
|
10
|
+
radius?: string;
|
|
11
|
+
density?: string;
|
|
12
|
+
font?: string;
|
|
13
|
+
defaultMode?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface UsalytConfig {
|
|
17
|
+
style?: string;
|
|
18
|
+
rsc?: boolean;
|
|
19
|
+
tsx?: boolean;
|
|
20
|
+
tailwind?: {
|
|
21
|
+
config?: string;
|
|
22
|
+
css?: string;
|
|
23
|
+
baseColor?: string;
|
|
24
|
+
};
|
|
25
|
+
theme?: UsalytThemeConfig;
|
|
26
|
+
aliases: {
|
|
27
|
+
components: string;
|
|
28
|
+
blocks?: string;
|
|
29
|
+
utils: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class ConfigError extends Error {
|
|
34
|
+
constructor(message: string) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "ConfigError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Aliases are root-relative destinations. A leading `@/` or `~/` is accepted
|
|
42
|
+
* for readability and is equivalent to the consumer root. The CLI rejects
|
|
43
|
+
* absolute aliases and any alias that normalizes outside the root.
|
|
44
|
+
*/
|
|
45
|
+
export function defaultConfig(): UsalytConfig {
|
|
46
|
+
return {
|
|
47
|
+
style: "default",
|
|
48
|
+
rsc: true,
|
|
49
|
+
tsx: true,
|
|
50
|
+
tailwind: {
|
|
51
|
+
config: "",
|
|
52
|
+
css: "src/styles/globals.css",
|
|
53
|
+
baseColor: "neutral",
|
|
54
|
+
},
|
|
55
|
+
aliases: {
|
|
56
|
+
components: "@/src/components/ui",
|
|
57
|
+
utils: "@/src/lib/utils",
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function configPathFor(root: string): string {
|
|
63
|
+
return path.join(root, CONFIG_FILE);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
67
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function requireAlias(aliases: Record<string, unknown>, key: "components" | "utils"): string {
|
|
71
|
+
const value = aliases[key];
|
|
72
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
73
|
+
throw new ConfigError(`${CONFIG_FILE} must define a non-empty "aliases.${key}" string.`);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Read `usalyt.json` from the consumer root, or return null when absent. */
|
|
79
|
+
export function readConfig(root: string): UsalytConfig | null {
|
|
80
|
+
const configPath = configPathFor(root);
|
|
81
|
+
if (!existsSync(configPath)) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let parsed: unknown;
|
|
86
|
+
try {
|
|
87
|
+
parsed = JSON.parse(readFileSync(configPath, "utf8"));
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new ConfigError(`${configPath} is not valid JSON: ${(error as Error).message}`);
|
|
90
|
+
}
|
|
91
|
+
if (!isRecord(parsed)) {
|
|
92
|
+
throw new ConfigError(`${configPath} must contain a JSON object.`);
|
|
93
|
+
}
|
|
94
|
+
if (!isRecord(parsed.aliases)) {
|
|
95
|
+
throw new ConfigError(`${configPath} must contain an "aliases" object.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const defaults = defaultConfig();
|
|
99
|
+
const parsedTheme = isRecord(parsed.theme)
|
|
100
|
+
? {
|
|
101
|
+
...(typeof parsed.theme.preset === "string" ? { preset: parsed.theme.preset } : {}),
|
|
102
|
+
...(typeof parsed.theme.base === "string" ? { base: parsed.theme.base } : {}),
|
|
103
|
+
...(typeof parsed.theme.accent === "string" ? { accent: parsed.theme.accent } : {}),
|
|
104
|
+
...(typeof parsed.theme.radius === "string" ? { radius: parsed.theme.radius } : {}),
|
|
105
|
+
...(typeof parsed.theme.density === "string" ? { density: parsed.theme.density } : {}),
|
|
106
|
+
...(typeof parsed.theme.font === "string" ? { font: parsed.theme.font } : {}),
|
|
107
|
+
...(typeof parsed.theme.defaultMode === "string"
|
|
108
|
+
? { defaultMode: parsed.theme.defaultMode }
|
|
109
|
+
: {}),
|
|
110
|
+
}
|
|
111
|
+
: undefined;
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
style: typeof parsed.style === "string" ? parsed.style : defaults.style,
|
|
115
|
+
rsc: typeof parsed.rsc === "boolean" ? parsed.rsc : defaults.rsc,
|
|
116
|
+
tsx: typeof parsed.tsx === "boolean" ? parsed.tsx : defaults.tsx,
|
|
117
|
+
tailwind: isRecord(parsed.tailwind)
|
|
118
|
+
? {
|
|
119
|
+
config:
|
|
120
|
+
typeof parsed.tailwind.config === "string" ? parsed.tailwind.config : defaults.tailwind!.config,
|
|
121
|
+
css: typeof parsed.tailwind.css === "string" ? parsed.tailwind.css : defaults.tailwind!.css,
|
|
122
|
+
baseColor:
|
|
123
|
+
typeof parsed.tailwind.baseColor === "string"
|
|
124
|
+
? parsed.tailwind.baseColor
|
|
125
|
+
: defaults.tailwind!.baseColor,
|
|
126
|
+
}
|
|
127
|
+
: defaults.tailwind,
|
|
128
|
+
...(parsedTheme && Object.keys(parsedTheme).length > 0 ? { theme: parsedTheme } : {}),
|
|
129
|
+
aliases: {
|
|
130
|
+
components: requireAlias(parsed.aliases, "components"),
|
|
131
|
+
...(typeof parsed.aliases.blocks === "string" && parsed.aliases.blocks.trim() !== ""
|
|
132
|
+
? { blocks: parsed.aliases.blocks }
|
|
133
|
+
: {}),
|
|
134
|
+
utils: requireAlias(parsed.aliases, "utils"),
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function serializeConfig(config: UsalytConfig): string {
|
|
140
|
+
return `${JSON.stringify(config, null, 2)}\n`;
|
|
141
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { TsconfigPathMapping } from "./project";
|
|
3
|
+
|
|
4
|
+
const IMPORT_PATTERN = /(\bfrom\s*|\bimport\s*)(["'])([^"'\n]+)\2/g;
|
|
5
|
+
|
|
6
|
+
const SOURCE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mts", ".cts", ".mjs", ".cjs"];
|
|
7
|
+
|
|
8
|
+
function resolveSourceFile(
|
|
9
|
+
sourcePath: string,
|
|
10
|
+
specifier: string,
|
|
11
|
+
knownSources: ReadonlySet<string>
|
|
12
|
+
): string | null {
|
|
13
|
+
if (!specifier.startsWith(".")) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const base = pathPosixNormalize(
|
|
18
|
+
pathPosixJoin(pathPosixDirname(sourcePath), specifier)
|
|
19
|
+
);
|
|
20
|
+
const candidates = [base];
|
|
21
|
+
for (const extension of SOURCE_EXTENSIONS) {
|
|
22
|
+
candidates.push(`${base}${extension}`);
|
|
23
|
+
}
|
|
24
|
+
for (const extension of SOURCE_EXTENSIONS) {
|
|
25
|
+
candidates.push(`${base}/index${extension}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return candidates.find((candidate) => knownSources.has(candidate)) ?? null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The registry stores POSIX-style paths on every platform.
|
|
32
|
+
function pathPosixNormalize(value: string): string {
|
|
33
|
+
return value.replace(/\\/g, "/").split("/").reduce<string[]>((segments, segment) => {
|
|
34
|
+
if (segment === "" || segment === ".") {
|
|
35
|
+
return segments;
|
|
36
|
+
}
|
|
37
|
+
if (segment === ".." && segments.length > 0 && segments[segments.length - 1] !== "..") {
|
|
38
|
+
segments.pop();
|
|
39
|
+
return segments;
|
|
40
|
+
}
|
|
41
|
+
segments.push(segment);
|
|
42
|
+
return segments;
|
|
43
|
+
}, []).join("/");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pathPosixJoin(...parts: string[]): string {
|
|
47
|
+
return parts.filter((part) => part !== "").join("/");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function pathPosixDirname(value: string): string {
|
|
51
|
+
const index = value.lastIndexOf("/");
|
|
52
|
+
return index === -1 ? "" : value.slice(0, index);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function toImportSpecifier(
|
|
56
|
+
fromFile: string,
|
|
57
|
+
toFile: string,
|
|
58
|
+
root?: string,
|
|
59
|
+
tsconfigPaths?: TsconfigPathMapping | null
|
|
60
|
+
): string {
|
|
61
|
+
if (root && tsconfigPaths) {
|
|
62
|
+
const relToRoot = path.relative(root, toFile).split(path.sep).join("/");
|
|
63
|
+
const baseDir = tsconfigPaths.targetDir;
|
|
64
|
+
const withinTarget =
|
|
65
|
+
baseDir === ""
|
|
66
|
+
? !relToRoot.startsWith("..")
|
|
67
|
+
: relToRoot.startsWith(`${baseDir}/`);
|
|
68
|
+
|
|
69
|
+
if (withinTarget) {
|
|
70
|
+
const subPath = baseDir === "" ? relToRoot : relToRoot.slice(baseDir.length + 1);
|
|
71
|
+
const stripped = subPath
|
|
72
|
+
.replace(/\.(tsx?|jsx?|mts?|cts?|mjs?|cjs?)$/, "")
|
|
73
|
+
.replace(/\/index$/, "");
|
|
74
|
+
return `${tsconfigPaths.prefix}${stripped}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const relative = path.relative(path.dirname(fromFile), toFile).split(path.sep).join("/");
|
|
79
|
+
let specifier = relative.replace(/\.(tsx?|jsx?|mts?|cts?|mjs?|cjs?)$/, "");
|
|
80
|
+
specifier = specifier.replace(/\/index$/, "");
|
|
81
|
+
if (!specifier.startsWith(".")) {
|
|
82
|
+
specifier = `./${specifier}`;
|
|
83
|
+
}
|
|
84
|
+
return specifier;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Rewrite registry-internal relative imports so references between installed
|
|
89
|
+
* files keep resolving at their configured destinations. Uses the consumer's
|
|
90
|
+
* tsconfig path alias when available, falling back to relative paths. Bare
|
|
91
|
+
* package imports are left untouched.
|
|
92
|
+
*/
|
|
93
|
+
export function rewriteRelativeImports(options: {
|
|
94
|
+
content: string;
|
|
95
|
+
sourcePath: string;
|
|
96
|
+
destPath: string;
|
|
97
|
+
sourceToDest: ReadonlyMap<string, string>;
|
|
98
|
+
root?: string;
|
|
99
|
+
tsconfigPaths?: TsconfigPathMapping | null;
|
|
100
|
+
}): string {
|
|
101
|
+
const { content, sourcePath, destPath, sourceToDest, root, tsconfigPaths } = options;
|
|
102
|
+
const knownSources = new Set(sourceToDest.keys());
|
|
103
|
+
|
|
104
|
+
return content.replace(
|
|
105
|
+
IMPORT_PATTERN,
|
|
106
|
+
(match, prefix: string, quote: string, specifier: string) => {
|
|
107
|
+
const resolved = resolveSourceFile(sourcePath, specifier, knownSources);
|
|
108
|
+
if (!resolved) {
|
|
109
|
+
return match;
|
|
110
|
+
}
|
|
111
|
+
const targetDest = sourceToDest.get(resolved);
|
|
112
|
+
if (!targetDest || targetDest === destPath) {
|
|
113
|
+
return match;
|
|
114
|
+
}
|
|
115
|
+
return `${prefix}${quote}${toImportSpecifier(destPath, targetDest, root, tsconfigPaths)}${quote}`;
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|