@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
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usalyt/cli",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Usalyt component distribution CLI",
|
|
5
|
+
"bin": {
|
|
6
|
+
"usalyt": "./dist/index.mjs"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"citty": "^0.2.2",
|
|
11
|
+
"picocolors": "^1.1.1",
|
|
12
|
+
"@usalyt/registry": "^0.0.2"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^26.1.1",
|
|
16
|
+
"tsdown": "^0.22.14",
|
|
17
|
+
"typescript": "^6.0.0"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/Usalyt/ui.git",
|
|
23
|
+
"directory": "packages/cli"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/Usalyt/ui",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/Usalyt/ui/issues"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"src",
|
|
38
|
+
"tsdown.config.ts",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"NOTICE",
|
|
42
|
+
"THIRD_PARTY_NOTICES.md",
|
|
43
|
+
"CHANGELOG.md"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"test": "pnpm build && node --test tests/cli.test.mjs"
|
|
48
|
+
}
|
|
6
49
|
}
|
|
@@ -0,0 +1,229 @@
|
|
|
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 {
|
|
16
|
+
detectPackageManager,
|
|
17
|
+
formatInstallArgs,
|
|
18
|
+
formatInstallCommand,
|
|
19
|
+
readInstalledDependencies,
|
|
20
|
+
type PackageManager,
|
|
21
|
+
} from "../lib/project";
|
|
22
|
+
import { loadRegistry, RegistryLoadError, type Registry } from "../lib/registry";
|
|
23
|
+
|
|
24
|
+
function normalizePositionals(values: unknown[]): string[] {
|
|
25
|
+
const names = values
|
|
26
|
+
.map((value) => String(value))
|
|
27
|
+
.flatMap((value) => value.split(/\s+/))
|
|
28
|
+
.map((value) => value.trim())
|
|
29
|
+
.filter(Boolean);
|
|
30
|
+
return [...new Set(names)];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function confirmInstall(command: string): Promise<boolean> {
|
|
34
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
35
|
+
try {
|
|
36
|
+
const answer = await rl.question(`Run "${command}" now? (y/N): `);
|
|
37
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
38
|
+
} finally {
|
|
39
|
+
rl.close();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function runPackageInstall(
|
|
44
|
+
root: string,
|
|
45
|
+
pm: PackageManager,
|
|
46
|
+
dependencies: string[]
|
|
47
|
+
): void {
|
|
48
|
+
const result = spawnSync(pm, formatInstallArgs(pm, dependencies), {
|
|
49
|
+
cwd: root,
|
|
50
|
+
stdio: "inherit",
|
|
51
|
+
});
|
|
52
|
+
if (result.error) {
|
|
53
|
+
printError(`could not run ${pm}: ${result.error.message}`);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (result.status !== 0) {
|
|
57
|
+
printError(`${pm} exited with status ${String(result.status)}.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const addCommand = defineCommand({
|
|
62
|
+
meta: {
|
|
63
|
+
name: "add",
|
|
64
|
+
description: "Add registry components and their dependencies to your project",
|
|
65
|
+
},
|
|
66
|
+
args: {
|
|
67
|
+
components: {
|
|
68
|
+
type: "positional",
|
|
69
|
+
description: "Name of the component(s) to add",
|
|
70
|
+
required: false,
|
|
71
|
+
},
|
|
72
|
+
all: {
|
|
73
|
+
type: "boolean",
|
|
74
|
+
description: "Add every item in the registry",
|
|
75
|
+
alias: "a",
|
|
76
|
+
},
|
|
77
|
+
yes: {
|
|
78
|
+
type: "boolean",
|
|
79
|
+
description: "Confirm dependency installation without prompting",
|
|
80
|
+
alias: "y",
|
|
81
|
+
},
|
|
82
|
+
overwrite: {
|
|
83
|
+
type: "boolean",
|
|
84
|
+
description: "Replace existing files",
|
|
85
|
+
alias: "o",
|
|
86
|
+
},
|
|
87
|
+
install: {
|
|
88
|
+
type: "boolean",
|
|
89
|
+
description: "Install missing npm dependencies using the detected package manager",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
async run({ args }) {
|
|
93
|
+
const root = process.cwd();
|
|
94
|
+
|
|
95
|
+
let registry: Registry;
|
|
96
|
+
try {
|
|
97
|
+
({ registry } = loadRegistry());
|
|
98
|
+
} catch (error) {
|
|
99
|
+
printError(error instanceof RegistryLoadError ? error.message : String(error));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const positionals = Array.isArray(args._) ? (args._ as unknown[]) : [];
|
|
104
|
+
const targets = args.all
|
|
105
|
+
? [...registry.items.map((item) => item.name)].sort()
|
|
106
|
+
: normalizePositionals(positionals);
|
|
107
|
+
|
|
108
|
+
if (targets.length === 0) {
|
|
109
|
+
printError(
|
|
110
|
+
"no components specified. Pass one or more component names or --all."
|
|
111
|
+
);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let config: UsalytConfig;
|
|
116
|
+
try {
|
|
117
|
+
config = readConfig(root) ?? defaultConfig();
|
|
118
|
+
} catch (error) {
|
|
119
|
+
printError((error as Error).message);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let plan: InstallPlan;
|
|
124
|
+
try {
|
|
125
|
+
plan = buildInstallPlan({
|
|
126
|
+
registry,
|
|
127
|
+
config,
|
|
128
|
+
root,
|
|
129
|
+
targets,
|
|
130
|
+
overwrite: Boolean(args.overwrite),
|
|
131
|
+
});
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (error instanceof UnknownItemsError) {
|
|
134
|
+
printError(`${error.message}.`);
|
|
135
|
+
console.error('Run "usalyt list" to see the available components.');
|
|
136
|
+
} else if (error instanceof UnsafePathError) {
|
|
137
|
+
printError(error.message);
|
|
138
|
+
} else if (error instanceof UnsupportedItemError) {
|
|
139
|
+
printError(error.message);
|
|
140
|
+
} else {
|
|
141
|
+
printError((error as Error).message);
|
|
142
|
+
}
|
|
143
|
+
console.error("No files were written.");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (plan.conflicts.length > 0) {
|
|
148
|
+
printError("refusing to overwrite existing files without --overwrite:");
|
|
149
|
+
for (const conflict of plan.conflicts) {
|
|
150
|
+
console.error(` - ${conflict}`);
|
|
151
|
+
}
|
|
152
|
+
console.error("No files were written.");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
for (const write of plan.writes) {
|
|
158
|
+
mkdirSync(path.dirname(write.destination), { recursive: true });
|
|
159
|
+
writeFileSync(write.destination, write.content, "utf8");
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
printError(
|
|
163
|
+
`could not write the planned files: ${(error as Error).message}. Some files may already have been written.`
|
|
164
|
+
);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const installedItems = new Set(plan.writes.map((write) => write.item)).size;
|
|
169
|
+
const requestedInstalled = targets.filter((t) =>
|
|
170
|
+
plan.writes.some((write) => write.item === t)
|
|
171
|
+
).length;
|
|
172
|
+
const dependencyCount = Math.max(0, installedItems - requestedInstalled);
|
|
173
|
+
|
|
174
|
+
console.log(
|
|
175
|
+
`Installed ${installedItems} item(s) (${requestedInstalled} requested, ${dependencyCount} registry dependencies):`
|
|
176
|
+
);
|
|
177
|
+
for (const write of plan.writes) {
|
|
178
|
+
console.log(` + ${write.relativeDestination}`);
|
|
179
|
+
}
|
|
180
|
+
if (plan.skipped.length > 0) {
|
|
181
|
+
console.log(`Skipped ${plan.skipped.length} existing dependency file(s):`);
|
|
182
|
+
for (const skipped of plan.skipped) {
|
|
183
|
+
console.log(` = ${skipped.relativeDestination}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const installedPackages = readInstalledDependencies(root);
|
|
188
|
+
const missingDependencies = plan.dependencies.filter(
|
|
189
|
+
(dep) => !installedPackages.has(dep)
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
if (missingDependencies.length === 0) {
|
|
193
|
+
console.log(
|
|
194
|
+
plan.dependencies.length === 0
|
|
195
|
+
? "No npm dependencies required."
|
|
196
|
+
: "All required npm dependencies are already installed."
|
|
197
|
+
);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const pm = detectPackageManager(root);
|
|
202
|
+
const installCommand = formatInstallCommand(pm, missingDependencies);
|
|
203
|
+
console.log(`Dependencies: ${installCommand}`);
|
|
204
|
+
|
|
205
|
+
if (!args.install) {
|
|
206
|
+
console.log(
|
|
207
|
+
"Dependencies were not installed. Run the command above, or pass --install --yes to execute it."
|
|
208
|
+
);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!args.yes) {
|
|
213
|
+
if (process.stdin.isTTY && process.stdout.isTTY) {
|
|
214
|
+
if (await confirmInstall(installCommand)) {
|
|
215
|
+
runPackageInstall(root, pm, missingDependencies);
|
|
216
|
+
} else {
|
|
217
|
+
console.log("Dependencies were not installed.");
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
console.log(
|
|
221
|
+
"Dependencies were not installed because consent was not confirmed. Pass --yes to execute the printed command."
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
runPackageInstall(root, pm, missingDependencies);
|
|
228
|
+
},
|
|
229
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { defineCommand } from "citty";
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
import { defaultConfig, readConfig, type UsalytConfig } from "../lib/config";
|
|
5
|
+
import { buildInstallPlan, UnknownItemsError } from "../lib/install";
|
|
6
|
+
import { printError } from "../lib/output";
|
|
7
|
+
import { loadRegistry, RegistryLoadError, type Registry } from "../lib/registry";
|
|
8
|
+
|
|
9
|
+
function renderLineDiff(localText: string, registryText: string): string[] {
|
|
10
|
+
const localLines = localText.split(/\r?\n/);
|
|
11
|
+
const registryLines = registryText.split(/\r?\n/);
|
|
12
|
+
const max = Math.max(localLines.length, registryLines.length);
|
|
13
|
+
const diff: string[] = [];
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < max; i += 1) {
|
|
16
|
+
const localLine = localLines[i];
|
|
17
|
+
const registryLine = registryLines[i];
|
|
18
|
+
if (localLine === registryLine) continue;
|
|
19
|
+
if (localLine !== undefined) {
|
|
20
|
+
diff.push(pc.red(`- L${i + 1}: ${localLine}`));
|
|
21
|
+
}
|
|
22
|
+
if (registryLine !== undefined) {
|
|
23
|
+
diff.push(pc.green(`+ L${i + 1}: ${registryLine}`));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return diff;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const diffCommand = defineCommand({
|
|
30
|
+
meta: {
|
|
31
|
+
name: "diff",
|
|
32
|
+
description: "Compare local installed components against the packaged registry",
|
|
33
|
+
},
|
|
34
|
+
args: {
|
|
35
|
+
components: {
|
|
36
|
+
type: "positional",
|
|
37
|
+
description: "Component name(s) to compare (defaults to all installed items)",
|
|
38
|
+
required: false,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
run({ args }) {
|
|
42
|
+
const root = process.cwd();
|
|
43
|
+
|
|
44
|
+
let registry: Registry;
|
|
45
|
+
try {
|
|
46
|
+
({ registry } = loadRegistry());
|
|
47
|
+
} catch (error) {
|
|
48
|
+
printError(error instanceof RegistryLoadError ? error.message : String(error));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let config: UsalytConfig;
|
|
53
|
+
try {
|
|
54
|
+
config = readConfig(root) ?? defaultConfig();
|
|
55
|
+
} catch (error) {
|
|
56
|
+
printError((error as Error).message);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const positionals = Array.isArray(args._)
|
|
61
|
+
? (args._ as unknown[]).map((v) => String(v).trim()).filter(Boolean)
|
|
62
|
+
: [];
|
|
63
|
+
const targets =
|
|
64
|
+
positionals.length > 0
|
|
65
|
+
? [...new Set(positionals)]
|
|
66
|
+
: registry.items.map((item) => item.name);
|
|
67
|
+
|
|
68
|
+
let plan;
|
|
69
|
+
try {
|
|
70
|
+
plan = buildInstallPlan({
|
|
71
|
+
registry,
|
|
72
|
+
config,
|
|
73
|
+
root,
|
|
74
|
+
targets,
|
|
75
|
+
overwrite: true,
|
|
76
|
+
});
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof UnknownItemsError) {
|
|
79
|
+
printError(`${error.message}.`);
|
|
80
|
+
} else {
|
|
81
|
+
printError((error as Error).message);
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const targetSet = new Set(targets);
|
|
87
|
+
const inspected = plan.writes.filter((entry) => targetSet.has(entry.item));
|
|
88
|
+
|
|
89
|
+
let checked = 0;
|
|
90
|
+
let changed = 0;
|
|
91
|
+
|
|
92
|
+
for (const file of inspected) {
|
|
93
|
+
if (!existsSync(file.destination)) {
|
|
94
|
+
if (positionals.length > 0) {
|
|
95
|
+
console.log(`${pc.yellow("missing")} ${file.relativeDestination}`);
|
|
96
|
+
changed += 1;
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
checked += 1;
|
|
102
|
+
const localContent = readFileSync(file.destination, "utf8");
|
|
103
|
+
if (localContent === file.content) {
|
|
104
|
+
if (positionals.length > 0) {
|
|
105
|
+
console.log(`${pc.green("up to date")} ${file.relativeDestination}`);
|
|
106
|
+
}
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
changed += 1;
|
|
111
|
+
console.log(`${pc.yellow("modified")} ${file.relativeDestination}`);
|
|
112
|
+
const lines = renderLineDiff(localContent, file.content);
|
|
113
|
+
for (const line of lines.slice(0, 40)) {
|
|
114
|
+
console.log(` ${line}`);
|
|
115
|
+
}
|
|
116
|
+
if (lines.length > 40) {
|
|
117
|
+
console.log(` ... (${lines.length - 40} more diff lines)`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (checked === 0 && positionals.length === 0) {
|
|
122
|
+
console.log("No installed Usalyt registry files found.");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (changed === 0) {
|
|
127
|
+
console.log(`All ${checked} checked file(s) are up to date.`);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
});
|
|
@@ -0,0 +1,271 @@
|
|
|
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
|
+
import { UnsafePathError } from "../lib/paths";
|
|
13
|
+
import {
|
|
14
|
+
detectCssEntry,
|
|
15
|
+
detectTsconfigPathMapping,
|
|
16
|
+
injectThemeStylesheet,
|
|
17
|
+
type TsconfigPathMapping,
|
|
18
|
+
} from "../lib/project";
|
|
19
|
+
import { loadRegistry } from "../lib/registry";
|
|
20
|
+
import {
|
|
21
|
+
applyThemePresetToCss,
|
|
22
|
+
encodeThemePreset,
|
|
23
|
+
hasThemeFlags,
|
|
24
|
+
isDefaultThemeConfig,
|
|
25
|
+
resolveThemeFromFlags,
|
|
26
|
+
ThemeOptionError,
|
|
27
|
+
toUsalytThemeConfig,
|
|
28
|
+
type ThemePresetConfig,
|
|
29
|
+
} from "../lib/theme";
|
|
30
|
+
|
|
31
|
+
function aliasFromPath(
|
|
32
|
+
value: string,
|
|
33
|
+
stripExtension = false,
|
|
34
|
+
tsconfigPaths?: TsconfigPathMapping | null
|
|
35
|
+
): string {
|
|
36
|
+
const normalized = value.trim().replace(/\\/g, "/").replace(/^\.?\//, "");
|
|
37
|
+
const withoutExtension = stripExtension
|
|
38
|
+
? normalized.replace(/\.(tsx?|jsx?)$/, "")
|
|
39
|
+
: normalized;
|
|
40
|
+
if (tsconfigPaths && tsconfigPaths.targetDir !== "") {
|
|
41
|
+
if (withoutExtension.startsWith(`${tsconfigPaths.targetDir}/`)) {
|
|
42
|
+
return `${tsconfigPaths.prefix}${withoutExtension.slice(tsconfigPaths.targetDir.length + 1)}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const prefix = tsconfigPaths?.prefix ?? "@/";
|
|
46
|
+
return `${prefix}${withoutExtension}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function promptForConfig(
|
|
50
|
+
defaults: UsalytConfig,
|
|
51
|
+
detectedCss: string,
|
|
52
|
+
tsconfigPaths: TsconfigPathMapping | null
|
|
53
|
+
): Promise<UsalytConfig> {
|
|
54
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
55
|
+
try {
|
|
56
|
+
const defaultComponents = defaults.aliases.components.replace(/^@\//, "");
|
|
57
|
+
const defaultUtils = `${defaults.aliases.utils.replace(/^@\//, "")}.ts`;
|
|
58
|
+
|
|
59
|
+
const componentsAnswer = await rl.question(
|
|
60
|
+
`Components directory (relative to the project root) [${defaultComponents}]: `
|
|
61
|
+
);
|
|
62
|
+
const utilsAnswer = await rl.question(
|
|
63
|
+
`Utils file (relative to the project root) [${defaultUtils}]: `
|
|
64
|
+
);
|
|
65
|
+
const cssAnswer = await rl.question(
|
|
66
|
+
`Global Tailwind CSS file (relative to the project root) [${detectedCss}]: `
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const componentsPath = componentsAnswer.trim() || defaultComponents;
|
|
70
|
+
const utilsPath = utilsAnswer.trim() || defaultUtils;
|
|
71
|
+
const cssPath = cssAnswer.trim() || detectedCss;
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
...defaults,
|
|
75
|
+
tailwind: {
|
|
76
|
+
...defaults.tailwind,
|
|
77
|
+
css: cssPath,
|
|
78
|
+
},
|
|
79
|
+
aliases: {
|
|
80
|
+
components: aliasFromPath(componentsPath, false, tsconfigPaths),
|
|
81
|
+
utils: aliasFromPath(utilsPath, true, tsconfigPaths),
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
} finally {
|
|
85
|
+
rl.close();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const initCommand = defineCommand({
|
|
90
|
+
meta: {
|
|
91
|
+
name: "init",
|
|
92
|
+
description: "Create usalyt.json and set up the Usalyt Tailwind v4 theme tokens",
|
|
93
|
+
},
|
|
94
|
+
args: {
|
|
95
|
+
yes: {
|
|
96
|
+
type: "boolean",
|
|
97
|
+
description: "Use detected defaults without prompting",
|
|
98
|
+
alias: "y",
|
|
99
|
+
},
|
|
100
|
+
overwrite: {
|
|
101
|
+
type: "boolean",
|
|
102
|
+
description: "Replace an existing usalyt.json and theme stylesheet",
|
|
103
|
+
alias: "o",
|
|
104
|
+
},
|
|
105
|
+
"no-theme": {
|
|
106
|
+
type: "boolean",
|
|
107
|
+
description: "Skip writing usalyt-theme.css and updating the Tailwind CSS entry",
|
|
108
|
+
},
|
|
109
|
+
preset: {
|
|
110
|
+
type: "string",
|
|
111
|
+
description: "Named theme preset (e.g. linear, console-dense, minimal-light) or Theme Studio URL/query",
|
|
112
|
+
},
|
|
113
|
+
base: {
|
|
114
|
+
type: "string",
|
|
115
|
+
description: "Surface base palette (linear, zinc, slate, stone)",
|
|
116
|
+
},
|
|
117
|
+
accent: {
|
|
118
|
+
type: "string",
|
|
119
|
+
description: "Primary accent (lavender, cobalt, emerald, amber, crimson, monochrome, or #RRGGBB)",
|
|
120
|
+
},
|
|
121
|
+
radius: {
|
|
122
|
+
type: "string",
|
|
123
|
+
description: "Border radius scale (none, sm, md, lg, xl)",
|
|
124
|
+
},
|
|
125
|
+
density: {
|
|
126
|
+
type: "string",
|
|
127
|
+
description: "Spacing density scale (compact, default, comfortable)",
|
|
128
|
+
},
|
|
129
|
+
font: {
|
|
130
|
+
type: "string",
|
|
131
|
+
description: "Sans font stack (inter, system, geist, jakarta)",
|
|
132
|
+
},
|
|
133
|
+
mode: {
|
|
134
|
+
type: "string",
|
|
135
|
+
description: "Default theme polarity (dark, light)",
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
async run({ args }) {
|
|
139
|
+
const root = process.cwd();
|
|
140
|
+
const configPath = configPathFor(root);
|
|
141
|
+
|
|
142
|
+
let resolvedTheme: ThemePresetConfig;
|
|
143
|
+
try {
|
|
144
|
+
resolvedTheme = resolveThemeFromFlags({
|
|
145
|
+
preset: args.preset,
|
|
146
|
+
base: args.base,
|
|
147
|
+
accent: args.accent,
|
|
148
|
+
radius: args.radius,
|
|
149
|
+
density: args.density,
|
|
150
|
+
font: args.font,
|
|
151
|
+
mode: args.mode,
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (error instanceof ThemeOptionError) {
|
|
155
|
+
printError(error.message);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (existsSync(configPath) && !args.overwrite) {
|
|
162
|
+
printError(
|
|
163
|
+
`${CONFIG_FILE} already exists at ${configPath}. Pass --overwrite to replace it.`
|
|
164
|
+
);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
if (lstatSync(configPath).isSymbolicLink()) {
|
|
170
|
+
printError(
|
|
171
|
+
`${CONFIG_FILE} at ${configPath} is a symbolic link. Remove the link and run init again.`
|
|
172
|
+
);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
} catch {
|
|
176
|
+
// The configuration file does not exist yet.
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const defaults = defaultConfig();
|
|
180
|
+
const detectedCss = detectCssEntry(root);
|
|
181
|
+
const tsconfigPaths = detectTsconfigPathMapping(root);
|
|
182
|
+
const interactive =
|
|
183
|
+
!args.yes && Boolean(process.stdin.isTTY) && Boolean(process.stdout.isTTY);
|
|
184
|
+
|
|
185
|
+
const themeCustomized =
|
|
186
|
+
hasThemeFlags({
|
|
187
|
+
preset: args.preset,
|
|
188
|
+
base: args.base,
|
|
189
|
+
accent: args.accent,
|
|
190
|
+
radius: args.radius,
|
|
191
|
+
density: args.density,
|
|
192
|
+
font: args.font,
|
|
193
|
+
mode: args.mode,
|
|
194
|
+
}) || !isDefaultThemeConfig(resolvedTheme);
|
|
195
|
+
|
|
196
|
+
const baseConfig: UsalytConfig = {
|
|
197
|
+
...defaults,
|
|
198
|
+
tailwind: {
|
|
199
|
+
...defaults.tailwind,
|
|
200
|
+
css: detectedCss,
|
|
201
|
+
baseColor: themeCustomized ? resolvedTheme.base : defaults.tailwind?.baseColor,
|
|
202
|
+
},
|
|
203
|
+
...(themeCustomized ? { theme: toUsalytThemeConfig(resolvedTheme) } : {}),
|
|
204
|
+
aliases: tsconfigPaths
|
|
205
|
+
? {
|
|
206
|
+
components: aliasFromPath("src/components/ui", false, tsconfigPaths),
|
|
207
|
+
utils: aliasFromPath("src/lib/utils", true, tsconfigPaths),
|
|
208
|
+
}
|
|
209
|
+
: defaults.aliases,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const promptedConfig = interactive
|
|
213
|
+
? await promptForConfig(defaults, detectedCss, tsconfigPaths)
|
|
214
|
+
: baseConfig;
|
|
215
|
+
const config: UsalytConfig = themeCustomized
|
|
216
|
+
? {
|
|
217
|
+
...promptedConfig,
|
|
218
|
+
tailwind: {
|
|
219
|
+
...promptedConfig.tailwind,
|
|
220
|
+
baseColor: resolvedTheme.base,
|
|
221
|
+
},
|
|
222
|
+
theme: toUsalytThemeConfig(resolvedTheme),
|
|
223
|
+
}
|
|
224
|
+
: promptedConfig;
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
writeFileSync(configPath, serializeConfig(config), "utf8");
|
|
228
|
+
} catch (error) {
|
|
229
|
+
printError(`could not write ${configPath}: ${(error as Error).message}`);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
console.log(`Created ${CONFIG_FILE} at ${configPath}`);
|
|
233
|
+
console.log(` components: ${config.aliases.components}`);
|
|
234
|
+
console.log(` utils: ${config.aliases.utils}`);
|
|
235
|
+
if (config.tailwind?.css) {
|
|
236
|
+
console.log(` css: ${config.tailwind.css}`);
|
|
237
|
+
}
|
|
238
|
+
if (themeCustomized) {
|
|
239
|
+
console.log(` preset: ${encodeThemePreset(resolvedTheme)}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (args["no-theme"]) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
const { registry } = loadRegistry();
|
|
248
|
+
const themeItem = registry.items.find((item) => item.name === "theme");
|
|
249
|
+
const themeContent = themeItem?.files[0]?.content;
|
|
250
|
+
if (themeContent && config.tailwind?.css) {
|
|
251
|
+
const finalThemeCss = applyThemePresetToCss(themeContent, resolvedTheme);
|
|
252
|
+
const result = injectThemeStylesheet({
|
|
253
|
+
root,
|
|
254
|
+
cssEntryRelative: config.tailwind.css,
|
|
255
|
+
themeCss: finalThemeCss,
|
|
256
|
+
overwriteTheme: Boolean(args.overwrite),
|
|
257
|
+
});
|
|
258
|
+
console.log(` theme: ${result.themeFile}`);
|
|
259
|
+
if (result.cssUpdated) {
|
|
260
|
+
console.log(` updated: ${result.cssEntry}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
} catch (error) {
|
|
264
|
+
if (error instanceof UnsafePathError) {
|
|
265
|
+
printError(error.message);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
printError(`could not set up theme stylesheet: ${(error as Error).message}`);
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
});
|