@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,199 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { createInterface } from "node:readline/promises";
|
|
5
|
+
import { defineCommand } from "citty";
|
|
6
|
+
import { readConfig, defaultConfig, type UsalytConfig } from "../lib/config";
|
|
7
|
+
import {
|
|
8
|
+
buildInstallPlan,
|
|
9
|
+
UnknownItemsError,
|
|
10
|
+
UnsupportedItemError,
|
|
11
|
+
type InstallPlan,
|
|
12
|
+
} from "../lib/install";
|
|
13
|
+
import { printError } from "../lib/output";
|
|
14
|
+
import { UnsafePathError } from "../lib/paths";
|
|
15
|
+
import { loadRegistry, RegistryLoadError, type Registry } from "../lib/registry";
|
|
16
|
+
|
|
17
|
+
function normalizePositionals(values: unknown[]): string[] {
|
|
18
|
+
const names = values
|
|
19
|
+
.map((value) => String(value))
|
|
20
|
+
.flatMap((value) => value.split(/\s+/))
|
|
21
|
+
.map((value) => value.trim())
|
|
22
|
+
.filter(Boolean);
|
|
23
|
+
return [...new Set(names)];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function confirmInstall(command: string): Promise<boolean> {
|
|
27
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
28
|
+
try {
|
|
29
|
+
const answer = await rl.question(`Run "${command}" now? (y/N): `);
|
|
30
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
31
|
+
} finally {
|
|
32
|
+
rl.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function runPnpmAdd(root: string, dependencies: string[]): void {
|
|
37
|
+
const result = spawnSync("pnpm", ["add", ...dependencies], {
|
|
38
|
+
cwd: root,
|
|
39
|
+
stdio: "inherit",
|
|
40
|
+
});
|
|
41
|
+
if (result.error) {
|
|
42
|
+
printError(`could not run pnpm: ${result.error.message}`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (result.status !== 0) {
|
|
46
|
+
printError(`pnpm add exited with status ${String(result.status)}.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const addCommand = defineCommand({
|
|
51
|
+
meta: {
|
|
52
|
+
name: "add",
|
|
53
|
+
description: "Add registry components and their dependencies to your project",
|
|
54
|
+
},
|
|
55
|
+
args: {
|
|
56
|
+
components: {
|
|
57
|
+
type: "positional",
|
|
58
|
+
description: "Name of the component(s) to add",
|
|
59
|
+
required: false,
|
|
60
|
+
},
|
|
61
|
+
all: {
|
|
62
|
+
type: "boolean",
|
|
63
|
+
description: "Add every item in the registry",
|
|
64
|
+
alias: "a",
|
|
65
|
+
},
|
|
66
|
+
yes: {
|
|
67
|
+
type: "boolean",
|
|
68
|
+
description: "Confirm dependency installation without prompting",
|
|
69
|
+
alias: "y",
|
|
70
|
+
},
|
|
71
|
+
overwrite: {
|
|
72
|
+
type: "boolean",
|
|
73
|
+
description: "Replace existing files",
|
|
74
|
+
alias: "o",
|
|
75
|
+
},
|
|
76
|
+
install: {
|
|
77
|
+
type: "boolean",
|
|
78
|
+
description: "Run the printed pnpm add command when consent is confirmed",
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
async run({ args }) {
|
|
82
|
+
const root = process.cwd();
|
|
83
|
+
|
|
84
|
+
let registry: Registry;
|
|
85
|
+
try {
|
|
86
|
+
({ registry } = loadRegistry());
|
|
87
|
+
} catch (error) {
|
|
88
|
+
printError(error instanceof RegistryLoadError ? error.message : String(error));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const positionals = Array.isArray(args._) ? (args._ as unknown[]) : [];
|
|
93
|
+
const targets = args.all
|
|
94
|
+
? [...registry.items.map((item) => item.name)].sort()
|
|
95
|
+
: normalizePositionals(positionals);
|
|
96
|
+
|
|
97
|
+
if (targets.length === 0) {
|
|
98
|
+
printError(
|
|
99
|
+
"no components specified. Pass one or more component names or --all."
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let config: UsalytConfig;
|
|
105
|
+
try {
|
|
106
|
+
config = readConfig(root) ?? defaultConfig();
|
|
107
|
+
} catch (error) {
|
|
108
|
+
printError((error as Error).message);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let plan: InstallPlan;
|
|
113
|
+
try {
|
|
114
|
+
plan = buildInstallPlan({
|
|
115
|
+
registry,
|
|
116
|
+
config,
|
|
117
|
+
root,
|
|
118
|
+
targets,
|
|
119
|
+
overwrite: Boolean(args.overwrite),
|
|
120
|
+
});
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error instanceof UnknownItemsError) {
|
|
123
|
+
printError(`${error.message}.`);
|
|
124
|
+
console.error('Run "usalyt list" to see the available components.');
|
|
125
|
+
} else if (error instanceof UnsafePathError) {
|
|
126
|
+
printError(error.message);
|
|
127
|
+
} else if (error instanceof UnsupportedItemError) {
|
|
128
|
+
printError(error.message);
|
|
129
|
+
} else {
|
|
130
|
+
printError((error as Error).message);
|
|
131
|
+
}
|
|
132
|
+
console.error("No files were written.");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (plan.conflicts.length > 0) {
|
|
137
|
+
printError("refusing to overwrite existing files without --overwrite:");
|
|
138
|
+
for (const conflict of plan.conflicts) {
|
|
139
|
+
console.error(` - ${conflict}`);
|
|
140
|
+
}
|
|
141
|
+
console.error("No files were written.");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
for (const write of plan.writes) {
|
|
147
|
+
mkdirSync(path.dirname(write.destination), { recursive: true });
|
|
148
|
+
writeFileSync(write.destination, write.content, "utf8");
|
|
149
|
+
}
|
|
150
|
+
} catch (error) {
|
|
151
|
+
printError(
|
|
152
|
+
`could not write the planned files: ${(error as Error).message}. Some files may already have been written.`
|
|
153
|
+
);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const installedItems = new Set(plan.writes.map((write) => write.item)).size;
|
|
158
|
+
const dependencyCount = installedItems - targets.length;
|
|
159
|
+
|
|
160
|
+
console.log(
|
|
161
|
+
`Installed ${installedItems} item(s) (${targets.length} requested, ${dependencyCount} registry dependencies):`
|
|
162
|
+
);
|
|
163
|
+
for (const write of plan.writes) {
|
|
164
|
+
console.log(` + ${write.relativeDestination}`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (plan.dependencies.length === 0) {
|
|
168
|
+
console.log("No npm dependencies required.");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const installCommand = `pnpm add ${plan.dependencies.join(" ")}`;
|
|
173
|
+
console.log(`Dependencies: ${installCommand}`);
|
|
174
|
+
|
|
175
|
+
if (!args.install) {
|
|
176
|
+
console.log(
|
|
177
|
+
"Dependencies were not installed. Run the command above, or pass --install --yes to execute it."
|
|
178
|
+
);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!args.yes) {
|
|
183
|
+
if (process.stdin.isTTY && process.stdout.isTTY) {
|
|
184
|
+
if (await confirmInstall(installCommand)) {
|
|
185
|
+
runPnpmAdd(root, plan.dependencies);
|
|
186
|
+
} else {
|
|
187
|
+
console.log("Dependencies were not installed.");
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
console.log(
|
|
191
|
+
"Dependencies were not installed because consent was not confirmed. Pass --yes to execute the printed command."
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
runPnpmAdd(root, plan.dependencies);
|
|
198
|
+
},
|
|
199
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { existsSync, lstatSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createInterface } from "node:readline/promises";
|
|
3
|
+
import { defineCommand } from "citty";
|
|
4
|
+
import {
|
|
5
|
+
CONFIG_FILE,
|
|
6
|
+
configPathFor,
|
|
7
|
+
defaultConfig,
|
|
8
|
+
serializeConfig,
|
|
9
|
+
type UsalytConfig,
|
|
10
|
+
} from "../lib/config";
|
|
11
|
+
import { printError } from "../lib/output";
|
|
12
|
+
|
|
13
|
+
function aliasFromPath(value: string, stripExtension = false): string {
|
|
14
|
+
const normalized = value.trim().replace(/\\/g, "/").replace(/^\.?\//, "");
|
|
15
|
+
const withoutExtension = stripExtension
|
|
16
|
+
? normalized.replace(/\.(tsx?|jsx?)$/, "")
|
|
17
|
+
: normalized;
|
|
18
|
+
return `@/${withoutExtension}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function promptForConfig(defaults: UsalytConfig): Promise<UsalytConfig> {
|
|
22
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
23
|
+
try {
|
|
24
|
+
const defaultComponents = defaults.aliases.components.replace(/^@\//, "");
|
|
25
|
+
const defaultUtils = `${defaults.aliases.utils.replace(/^@\//, "")}.ts`;
|
|
26
|
+
|
|
27
|
+
const componentsAnswer = await rl.question(
|
|
28
|
+
`Components directory (relative to the project root) [${defaultComponents}]: `
|
|
29
|
+
);
|
|
30
|
+
const utilsAnswer = await rl.question(
|
|
31
|
+
`Utils file (relative to the project root) [${defaultUtils}]: `
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const componentsPath = componentsAnswer.trim() || defaultComponents;
|
|
35
|
+
const utilsPath = utilsAnswer.trim() || defaultUtils;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
...defaults,
|
|
39
|
+
aliases: {
|
|
40
|
+
components: aliasFromPath(componentsPath),
|
|
41
|
+
utils: aliasFromPath(utilsPath, true),
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
} finally {
|
|
45
|
+
rl.close();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const initCommand = defineCommand({
|
|
50
|
+
meta: {
|
|
51
|
+
name: "init",
|
|
52
|
+
description: "Create usalyt.json with the component and utility aliases",
|
|
53
|
+
},
|
|
54
|
+
args: {
|
|
55
|
+
yes: {
|
|
56
|
+
type: "boolean",
|
|
57
|
+
description: "Use the default aliases without prompting",
|
|
58
|
+
alias: "y",
|
|
59
|
+
},
|
|
60
|
+
overwrite: {
|
|
61
|
+
type: "boolean",
|
|
62
|
+
description: "Replace an existing usalyt.json",
|
|
63
|
+
alias: "o",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
async run({ args }) {
|
|
67
|
+
const root = process.cwd();
|
|
68
|
+
const configPath = configPathFor(root);
|
|
69
|
+
|
|
70
|
+
if (existsSync(configPath) && !args.overwrite) {
|
|
71
|
+
printError(
|
|
72
|
+
`${CONFIG_FILE} already exists at ${configPath}. Pass --overwrite to replace it.`
|
|
73
|
+
);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
if (lstatSync(configPath).isSymbolicLink()) {
|
|
79
|
+
printError(
|
|
80
|
+
`${CONFIG_FILE} at ${configPath} is a symbolic link. Remove the link and run init again.`
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// The configuration file does not exist yet.
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const defaults = defaultConfig();
|
|
89
|
+
const interactive =
|
|
90
|
+
!args.yes && Boolean(process.stdin.isTTY) && Boolean(process.stdout.isTTY);
|
|
91
|
+
const config = interactive ? await promptForConfig(defaults) : defaults;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
writeFileSync(configPath, serializeConfig(config), "utf8");
|
|
95
|
+
} catch (error) {
|
|
96
|
+
printError(`could not write ${configPath}: ${(error as Error).message}`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
console.log(`Created ${CONFIG_FILE} at ${configPath}`);
|
|
100
|
+
console.log(` components: ${config.aliases.components}`);
|
|
101
|
+
console.log(` utils: ${config.aliases.utils}`);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
@@ -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
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { defineCommand, runMain } from "citty";
|
|
3
|
+
import { addCommand } from "./commands/add";
|
|
4
|
+
import { initCommand } from "./commands/init";
|
|
5
|
+
import { listCommand } from "./commands/list";
|
|
6
|
+
|
|
7
|
+
const main = defineCommand({
|
|
8
|
+
meta: {
|
|
9
|
+
name: "usalyt",
|
|
10
|
+
version: "0.0.1",
|
|
11
|
+
description: "Usalyt component distribution CLI",
|
|
12
|
+
},
|
|
13
|
+
subCommands: {
|
|
14
|
+
init: initCommand,
|
|
15
|
+
add: addCommand,
|
|
16
|
+
list: listCommand,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
runMain(main);
|
|
@@ -0,0 +1,111 @@
|
|
|
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 UsalytConfig {
|
|
7
|
+
style?: string;
|
|
8
|
+
rsc?: boolean;
|
|
9
|
+
tsx?: boolean;
|
|
10
|
+
tailwind?: {
|
|
11
|
+
config?: string;
|
|
12
|
+
css?: string;
|
|
13
|
+
baseColor?: string;
|
|
14
|
+
};
|
|
15
|
+
aliases: {
|
|
16
|
+
components: string;
|
|
17
|
+
utils: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ConfigError extends Error {
|
|
22
|
+
constructor(message: string) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "ConfigError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Aliases are root-relative destinations. A leading `@/` or `~/` is accepted
|
|
30
|
+
* for readability and is equivalent to the consumer root. The CLI rejects
|
|
31
|
+
* absolute aliases and any alias that normalizes outside the root.
|
|
32
|
+
*/
|
|
33
|
+
export function defaultConfig(): UsalytConfig {
|
|
34
|
+
return {
|
|
35
|
+
style: "default",
|
|
36
|
+
rsc: true,
|
|
37
|
+
tsx: true,
|
|
38
|
+
tailwind: {
|
|
39
|
+
config: "",
|
|
40
|
+
css: "src/styles/globals.css",
|
|
41
|
+
baseColor: "neutral",
|
|
42
|
+
},
|
|
43
|
+
aliases: {
|
|
44
|
+
components: "@/src/components/ui",
|
|
45
|
+
utils: "@/src/lib/utils",
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function configPathFor(root: string): string {
|
|
51
|
+
return path.join(root, CONFIG_FILE);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
55
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function requireAlias(aliases: Record<string, unknown>, key: "components" | "utils"): string {
|
|
59
|
+
const value = aliases[key];
|
|
60
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
61
|
+
throw new ConfigError(`${CONFIG_FILE} must define a non-empty "aliases.${key}" string.`);
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Read `usalyt.json` from the consumer root, or return null when absent. */
|
|
67
|
+
export function readConfig(root: string): UsalytConfig | null {
|
|
68
|
+
const configPath = configPathFor(root);
|
|
69
|
+
if (!existsSync(configPath)) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let parsed: unknown;
|
|
74
|
+
try {
|
|
75
|
+
parsed = JSON.parse(readFileSync(configPath, "utf8"));
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throw new ConfigError(`${configPath} is not valid JSON: ${(error as Error).message}`);
|
|
78
|
+
}
|
|
79
|
+
if (!isRecord(parsed)) {
|
|
80
|
+
throw new ConfigError(`${configPath} must contain a JSON object.`);
|
|
81
|
+
}
|
|
82
|
+
if (!isRecord(parsed.aliases)) {
|
|
83
|
+
throw new ConfigError(`${configPath} must contain an "aliases" object.`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const defaults = defaultConfig();
|
|
87
|
+
return {
|
|
88
|
+
style: typeof parsed.style === "string" ? parsed.style : defaults.style,
|
|
89
|
+
rsc: typeof parsed.rsc === "boolean" ? parsed.rsc : defaults.rsc,
|
|
90
|
+
tsx: typeof parsed.tsx === "boolean" ? parsed.tsx : defaults.tsx,
|
|
91
|
+
tailwind: isRecord(parsed.tailwind)
|
|
92
|
+
? {
|
|
93
|
+
config:
|
|
94
|
+
typeof parsed.tailwind.config === "string" ? parsed.tailwind.config : defaults.tailwind!.config,
|
|
95
|
+
css: typeof parsed.tailwind.css === "string" ? parsed.tailwind.css : defaults.tailwind!.css,
|
|
96
|
+
baseColor:
|
|
97
|
+
typeof parsed.tailwind.baseColor === "string"
|
|
98
|
+
? parsed.tailwind.baseColor
|
|
99
|
+
: defaults.tailwind!.baseColor,
|
|
100
|
+
}
|
|
101
|
+
: defaults.tailwind,
|
|
102
|
+
aliases: {
|
|
103
|
+
components: requireAlias(parsed.aliases, "components"),
|
|
104
|
+
utils: requireAlias(parsed.aliases, "utils"),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function serializeConfig(config: UsalytConfig): string {
|
|
110
|
+
return `${JSON.stringify(config, null, 2)}\n`;
|
|
111
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
const IMPORT_PATTERN = /(\bfrom\s*|\bimport\s*)(["'])([^"'\n]+)\2/g;
|
|
4
|
+
|
|
5
|
+
const SOURCE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mts", ".cts", ".mjs", ".cjs"];
|
|
6
|
+
|
|
7
|
+
function resolveSourceFile(
|
|
8
|
+
sourcePath: string,
|
|
9
|
+
specifier: string,
|
|
10
|
+
knownSources: ReadonlySet<string>
|
|
11
|
+
): string | null {
|
|
12
|
+
if (!specifier.startsWith(".")) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const base = pathPosixNormalize(
|
|
17
|
+
pathPosixJoin(pathPosixDirname(sourcePath), specifier)
|
|
18
|
+
);
|
|
19
|
+
const candidates = [base];
|
|
20
|
+
for (const extension of SOURCE_EXTENSIONS) {
|
|
21
|
+
candidates.push(`${base}${extension}`);
|
|
22
|
+
}
|
|
23
|
+
for (const extension of SOURCE_EXTENSIONS) {
|
|
24
|
+
candidates.push(`${base}/index${extension}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return candidates.find((candidate) => knownSources.has(candidate)) ?? null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The registry stores POSIX-style paths on every platform.
|
|
31
|
+
function pathPosixNormalize(value: string): string {
|
|
32
|
+
return value.replace(/\\/g, "/").split("/").reduce<string[]>((segments, segment) => {
|
|
33
|
+
if (segment === "" || segment === ".") {
|
|
34
|
+
return segments;
|
|
35
|
+
}
|
|
36
|
+
if (segment === ".." && segments.length > 0 && segments[segments.length - 1] !== "..") {
|
|
37
|
+
segments.pop();
|
|
38
|
+
return segments;
|
|
39
|
+
}
|
|
40
|
+
segments.push(segment);
|
|
41
|
+
return segments;
|
|
42
|
+
}, []).join("/");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function pathPosixJoin(...parts: string[]): string {
|
|
46
|
+
return parts.filter((part) => part !== "").join("/");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function pathPosixDirname(value: string): string {
|
|
50
|
+
const index = value.lastIndexOf("/");
|
|
51
|
+
return index === -1 ? "" : value.slice(0, index);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function toImportSpecifier(fromFile: string, toFile: string): string {
|
|
55
|
+
const relative = path.relative(path.dirname(fromFile), toFile).split(path.sep).join("/");
|
|
56
|
+
let specifier = relative.replace(/\.(tsx?|jsx?|mts?|cts?|mjs?|cjs?)$/, "");
|
|
57
|
+
specifier = specifier.replace(/\/index$/, "");
|
|
58
|
+
if (!specifier.startsWith(".")) {
|
|
59
|
+
specifier = `./${specifier}`;
|
|
60
|
+
}
|
|
61
|
+
return specifier;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Rewrite registry-internal relative imports so references between installed
|
|
66
|
+
* files keep resolving at their configured destinations. Bare package imports
|
|
67
|
+
* are left untouched.
|
|
68
|
+
*/
|
|
69
|
+
export function rewriteRelativeImports(options: {
|
|
70
|
+
content: string;
|
|
71
|
+
sourcePath: string;
|
|
72
|
+
destPath: string;
|
|
73
|
+
sourceToDest: ReadonlyMap<string, string>;
|
|
74
|
+
}): string {
|
|
75
|
+
const { content, sourcePath, destPath, sourceToDest } = options;
|
|
76
|
+
const knownSources = new Set(sourceToDest.keys());
|
|
77
|
+
|
|
78
|
+
return content.replace(
|
|
79
|
+
IMPORT_PATTERN,
|
|
80
|
+
(match, prefix: string, quote: string, specifier: string) => {
|
|
81
|
+
const resolved = resolveSourceFile(sourcePath, specifier, knownSources);
|
|
82
|
+
if (!resolved) {
|
|
83
|
+
return match;
|
|
84
|
+
}
|
|
85
|
+
const targetDest = sourceToDest.get(resolved);
|
|
86
|
+
if (!targetDest || targetDest === destPath) {
|
|
87
|
+
return match;
|
|
88
|
+
}
|
|
89
|
+
return `${prefix}${quote}${toImportSpecifier(destPath, targetDest)}${quote}`;
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|