stera-ui 0.6.0 → 0.7.0

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.
@@ -4,8 +4,10 @@ import {
4
4
  findConfigPath,
5
5
  installDependencies,
6
6
  loadConfig,
7
- writeSteraUiCss
8
- } from "./chunk-2Y64FOD3.js";
7
+ migrateLegacyStyles,
8
+ writeComponentFiles,
9
+ writeStyles
10
+ } from "./chunk-5NLD52O5.js";
9
11
  import {
10
12
  ComponentNotFoundError,
11
13
  createSpinner,
@@ -18,112 +20,7 @@ import {
18
20
  } from "./chunk-KGMF6UW4.js";
19
21
 
20
22
  // src/commands/add.ts
21
- import path3 from "path";
22
-
23
- // src/utils/write-files.ts
24
- import fs from "fs";
25
- import path2 from "path";
26
- import { confirm } from "@inquirer/prompts";
27
-
28
- // src/utils/resolve-paths.ts
29
23
  import path from "path";
30
- import { loadConfig as loadConfig2, createMatchPath } from "tsconfig-paths";
31
- var TYPE_TO_ALIAS = {
32
- "registry:ui": "ui",
33
- "registry:lib": "lib",
34
- "registry:hook": "hooks",
35
- "registry:component": "components"
36
- };
37
- var matcherCache = /* @__PURE__ */ new Map();
38
- function getTsConfigMatcher(projectRoot) {
39
- let matcher = matcherCache.get(projectRoot);
40
- if (matcher !== void 0) return matcher;
41
- const tsConfig = loadConfig2(projectRoot);
42
- matcher = tsConfig.resultType === "success" && Object.keys(tsConfig.paths).length > 0 ? {
43
- paths: tsConfig.paths,
44
- match: createMatchPath(tsConfig.absoluteBaseUrl, tsConfig.paths)
45
- } : null;
46
- matcherCache.set(projectRoot, matcher);
47
- return matcher;
48
- }
49
- function resolveAlias(alias, projectRoot) {
50
- const matcher = getTsConfigMatcher(projectRoot);
51
- if (matcher) {
52
- const hasMatchingPrefix = Object.keys(matcher.paths).some((key) => {
53
- const prefix = key.replace(/\/\*$/, "");
54
- return alias === prefix || alias.startsWith(`${prefix}/`);
55
- });
56
- if (hasMatchingPrefix) {
57
- const resolved = matcher.match(alias, void 0, () => true, [
58
- ".ts",
59
- ".tsx"
60
- ]);
61
- if (resolved) {
62
- return path.relative(projectRoot, resolved);
63
- }
64
- }
65
- }
66
- throw new Error(
67
- `Could not resolve alias "${alias}" from tsconfig.json. Ensure compilerOptions.paths defines a mapping for it (e.g. "${alias}/*"), then re-run. See https://ui.stera.sh/docs/installation.`
68
- );
69
- }
70
- function resolveOutputPath(config, projectRoot, file) {
71
- const alias = TYPE_TO_ALIAS[file.type];
72
- if (alias) {
73
- const dir = resolveAlias(config.aliases[alias], projectRoot);
74
- const filename = path.basename(file.path);
75
- return path.join(projectRoot, dir, filename);
76
- }
77
- if (file.type === "registry:style") {
78
- const cssDir = path.dirname(config.css);
79
- const filename = path.basename(file.path);
80
- return path.join(projectRoot, cssDir, filename);
81
- }
82
- return path.join(projectRoot, file.path);
83
- }
84
-
85
- // src/utils/write-files.ts
86
- async function confirmOverwrite(relativePath) {
87
- if (!process.stdin.isTTY) {
88
- console.log(
89
- ` Skipping ${relativePath} (already exists, use --overwrite to replace)`
90
- );
91
- return false;
92
- }
93
- return confirm({
94
- message: `${relativePath} already exists. Overwrite?`,
95
- default: false
96
- });
97
- }
98
- async function writeComponentFiles(items, config, projectRoot, options = {}) {
99
- const written = [];
100
- const skipped = [];
101
- for (const item of items) {
102
- for (const file of item.files ?? []) {
103
- const outputPath = resolveOutputPath(config, projectRoot, file);
104
- const relativePath = path2.relative(projectRoot, outputPath);
105
- if (fs.existsSync(outputPath)) {
106
- const existing = fs.readFileSync(outputPath, "utf-8");
107
- if (existing === file.content) {
108
- skipped.push({ path: relativePath, reason: "unchanged" });
109
- continue;
110
- }
111
- if (!options.overwrite) {
112
- const confirmed = await confirmOverwrite(relativePath);
113
- if (!confirmed) {
114
- skipped.push({ path: relativePath, reason: "declined" });
115
- continue;
116
- }
117
- }
118
- }
119
- const dir = path2.dirname(outputPath);
120
- fs.mkdirSync(dir, { recursive: true });
121
- fs.writeFileSync(outputPath, file.content ?? "", "utf-8");
122
- written.push(relativePath);
123
- }
124
- }
125
- return { written, skipped };
126
- }
127
24
 
128
25
  // src/utils/transform.ts
129
26
  function applyTransforms(content, ctx, transforms2) {
@@ -197,7 +94,7 @@ var transformImports = (content, ctx) => {
197
94
  // src/commands/add.ts
198
95
  var transforms = [transformImports];
199
96
  async function add(components, options) {
200
- const cwd = options.cwd ? path3.resolve(options.cwd) : process.cwd();
97
+ const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
201
98
  const configPath = findConfigPath(cwd);
202
99
  if (!configPath) {
203
100
  console.error(
@@ -210,7 +107,7 @@ async function add(components, options) {
210
107
  console.error(` ${CROSS} Failed to parse ${CONFIG_FILE}.`);
211
108
  process.exit(1);
212
109
  }
213
- const projectRoot = path3.dirname(configPath);
110
+ const projectRoot = path.dirname(configPath);
214
111
  if (components.includes("globals")) {
215
112
  if (components.length > 1) {
216
113
  console.error(
@@ -223,15 +120,29 @@ async function add(components, options) {
223
120
  }
224
121
  const spinner2 = createSpinner("Refreshing base styles");
225
122
  let result;
123
+ let migrated = null;
226
124
  try {
227
- result = await writeSteraUiCss(config, projectRoot);
125
+ migrated = await migrateLegacyStyles(config, projectRoot);
126
+ result = await writeStyles(config, projectRoot, {
127
+ overwrite: options.overwrite
128
+ });
228
129
  } catch (err) {
229
130
  spinner2.fail("Failed to refresh base styles");
230
131
  throw err;
231
132
  }
232
- spinner2.succeed(
233
- `Refreshed ${path3.relative(projectRoot, result.steraUiPath)}`
234
- );
133
+ spinner2.succeed(`Refreshed ${result.written.length} style files`);
134
+ if (migrated) {
135
+ console.log(
136
+ ` ${CHECK} Migrated ${path.relative(projectRoot, migrated)} to ui/`
137
+ );
138
+ }
139
+ for (const target of result.written) {
140
+ console.log(` ${CHECK} ${target}`);
141
+ }
142
+ for (const file of result.skipped) {
143
+ const reason = file.reason === "unchanged" ? "unchanged" : "kept your version";
144
+ console.log(` ${dim(`- ${file.path} (${reason})`)}`);
145
+ }
235
146
  console.log("");
236
147
  return;
237
148
  }
@@ -0,0 +1,376 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ createSpinner,
4
+ fetchRegistryItem
5
+ } from "./chunk-KKUJRJ2N.js";
6
+
7
+ // src/utils/resolve-config.ts
8
+ import fs from "fs";
9
+ import path from "path";
10
+ var CONFIG_FILE = "stera.config.json";
11
+ var LEGACY_CONFIG_FILE = "components.json";
12
+ function migrateConfig(raw) {
13
+ if (!("version" in raw) && "tailwind" in raw) {
14
+ const legacy = raw;
15
+ return {
16
+ version: 1,
17
+ css: legacy.tailwind.css || "src/styles/globals.css",
18
+ aliases: legacy.aliases
19
+ };
20
+ }
21
+ return raw;
22
+ }
23
+ var warnedLegacyFilename = false;
24
+ function warnLegacyFilenameOnce(legacyPath) {
25
+ if (warnedLegacyFilename) return;
26
+ warnedLegacyFilename = true;
27
+ console.warn(
28
+ ` \u26A0 Found ${LEGACY_CONFIG_FILE}. Rename to ${CONFIG_FILE} \u2014 legacy filename support will be removed in a future release.
29
+ (${legacyPath})`
30
+ );
31
+ }
32
+ function findConfigPath(cwd) {
33
+ let dir = path.resolve(cwd);
34
+ while (true) {
35
+ const configPath = path.join(dir, CONFIG_FILE);
36
+ if (fs.existsSync(configPath)) return configPath;
37
+ const legacyPath = path.join(dir, LEGACY_CONFIG_FILE);
38
+ if (fs.existsSync(legacyPath)) return legacyPath;
39
+ const parent = path.dirname(dir);
40
+ if (parent === dir) return null;
41
+ dir = parent;
42
+ }
43
+ }
44
+ function loadConfig(cwd) {
45
+ const configPath = findConfigPath(cwd);
46
+ if (!configPath) return null;
47
+ if (path.basename(configPath) === LEGACY_CONFIG_FILE) {
48
+ warnLegacyFilenameOnce(configPath);
49
+ }
50
+ try {
51
+ const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
52
+ return migrateConfig(raw);
53
+ } catch {
54
+ return null;
55
+ }
56
+ }
57
+
58
+ // src/utils/install-deps.ts
59
+ import fs2 from "fs";
60
+ import path2 from "path";
61
+ import { detect } from "@antfu/ni";
62
+ import { execa } from "execa";
63
+ async function detectPackageManager(cwd) {
64
+ const detected = await detect({ programmatic: true, cwd });
65
+ if (detected === "pnpm@6" || detected === "pnpm") return "pnpm";
66
+ if (detected === "yarn@berry" || detected === "yarn") return "yarn";
67
+ if (detected === "bun") return "bun";
68
+ if (detected === "npm") return "npm";
69
+ const userAgent = process.env.npm_config_user_agent || "";
70
+ if (userAgent.startsWith("pnpm")) return "pnpm";
71
+ if (userAgent.startsWith("yarn")) return "yarn";
72
+ if (userAgent.startsWith("bun")) return "bun";
73
+ return "npm";
74
+ }
75
+ function getInstallArgs(pm, deps) {
76
+ switch (pm) {
77
+ case "pnpm":
78
+ return ["pnpm", ["add", ...deps]];
79
+ case "yarn":
80
+ return ["yarn", ["add", ...deps]];
81
+ case "bun":
82
+ return ["bun", ["add", ...deps]];
83
+ default:
84
+ return ["npm", ["install", ...deps]];
85
+ }
86
+ }
87
+ async function installDependencies(deps, cwd) {
88
+ if (deps.length === 0) return;
89
+ const pkgPath = path2.join(cwd, "package.json");
90
+ let existing = /* @__PURE__ */ new Set();
91
+ if (fs2.existsSync(pkgPath)) {
92
+ try {
93
+ const pkg = JSON.parse(fs2.readFileSync(pkgPath, "utf-8"));
94
+ existing = new Set(
95
+ Object.keys({ ...pkg.dependencies, ...pkg.devDependencies })
96
+ );
97
+ } catch {
98
+ }
99
+ }
100
+ const toInstall = deps.filter((dep) => !existing.has(dep));
101
+ if (toInstall.length === 0) return;
102
+ const pm = await detectPackageManager(cwd);
103
+ const [cmd, args] = getInstallArgs(pm, toInstall);
104
+ const spinner = createSpinner(`Installing with ${pm}`);
105
+ try {
106
+ await execa(cmd, args, { cwd });
107
+ spinner.succeed(`Installed with ${pm}`);
108
+ } catch (error) {
109
+ spinner.fail(`Install failed`);
110
+ if (error instanceof Error && "stdout" in error && typeof error.stdout === "string" && error.stdout.includes("ERR_PNPM_ADDING_TO_ROOT")) {
111
+ throw new Error(
112
+ `Cannot install dependencies at a pnpm workspace root.
113
+ Run stera-ui init from within a specific workspace package instead,
114
+ or use "pnpm add -w <package>" manually to install at the root.`
115
+ );
116
+ }
117
+ throw error;
118
+ }
119
+ }
120
+
121
+ // src/utils/patch-css-vars.ts
122
+ import fs3 from "fs";
123
+ function patchCssVariables(filePath, patches) {
124
+ const content = fs3.readFileSync(filePath, "utf-8");
125
+ const lines = content.split("\n");
126
+ const patched = [];
127
+ const compiled = Object.entries(patches).map(([prop, newValue]) => ({
128
+ prop,
129
+ newValue,
130
+ regex: new RegExp(`(\\s*${escapeRegex(prop)}:\\s*).+?(;)`)
131
+ }));
132
+ for (let i = 0; i < lines.length; i++) {
133
+ for (const { prop, newValue, regex } of compiled) {
134
+ if (regex.test(lines[i])) {
135
+ lines[i] = lines[i].replace(regex, `$1${newValue}$2`);
136
+ if (!patched.includes(prop)) patched.push(prop);
137
+ }
138
+ }
139
+ }
140
+ if (patched.length > 0) {
141
+ fs3.writeFileSync(filePath, lines.join("\n"), "utf-8");
142
+ }
143
+ return patched;
144
+ }
145
+ function removeCssImport(filePath, importPath) {
146
+ const content = fs3.readFileSync(filePath, "utf-8");
147
+ const pattern = new RegExp(
148
+ `@import\\s+['"]${escapeRegex(importPath)}['"]\\s*;?\\n?`
149
+ );
150
+ if (!pattern.test(content)) return false;
151
+ fs3.writeFileSync(filePath, content.replace(pattern, ""), "utf-8");
152
+ return true;
153
+ }
154
+ function escapeRegex(str) {
155
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
156
+ }
157
+
158
+ // src/utils/css-imports.ts
159
+ import fs4 from "fs/promises";
160
+ async function insertImportLine(filePath, importStatement) {
161
+ const content = await fs4.readFile(filePath, "utf-8");
162
+ const importPath = importStatement.match(/["']([^"']+)["']/)?.[1];
163
+ if (importPath) {
164
+ const escaped = importPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
165
+ if (new RegExp(`@import\\s+["']${escaped}["']`).test(content)) return;
166
+ }
167
+ const lines = content.split("\n");
168
+ let lastImportIndex = -1;
169
+ for (let i = 0; i < lines.length; i++) {
170
+ if (lines[i].trim().startsWith("@import")) {
171
+ lastImportIndex = i;
172
+ }
173
+ }
174
+ if (lastImportIndex >= 0) {
175
+ lines.splice(lastImportIndex + 1, 0, importStatement);
176
+ } else {
177
+ lines.unshift(importStatement);
178
+ }
179
+ await fs4.writeFile(filePath, lines.join("\n"), "utf-8");
180
+ }
181
+
182
+ // src/utils/write-files.ts
183
+ import fs5 from "fs";
184
+ import path4 from "path";
185
+ import { confirm } from "@inquirer/prompts";
186
+
187
+ // src/utils/resolve-paths.ts
188
+ import path3 from "path";
189
+ import { loadConfig as loadConfig2, createMatchPath } from "tsconfig-paths";
190
+ var TYPE_TO_ALIAS = {
191
+ "registry:ui": "ui",
192
+ "registry:lib": "lib",
193
+ "registry:hook": "hooks",
194
+ "registry:component": "components"
195
+ };
196
+ var matcherCache = /* @__PURE__ */ new Map();
197
+ function getTsConfigMatcher(projectRoot) {
198
+ let matcher = matcherCache.get(projectRoot);
199
+ if (matcher !== void 0) return matcher;
200
+ const tsConfig = loadConfig2(projectRoot);
201
+ matcher = tsConfig.resultType === "success" && Object.keys(tsConfig.paths).length > 0 ? {
202
+ paths: tsConfig.paths,
203
+ match: createMatchPath(tsConfig.absoluteBaseUrl, tsConfig.paths)
204
+ } : null;
205
+ matcherCache.set(projectRoot, matcher);
206
+ return matcher;
207
+ }
208
+ function resolveAlias(alias, projectRoot) {
209
+ const matcher = getTsConfigMatcher(projectRoot);
210
+ if (matcher) {
211
+ const hasMatchingPrefix = Object.keys(matcher.paths).some((key) => {
212
+ const prefix = key.replace(/\/\*$/, "");
213
+ return alias === prefix || alias.startsWith(`${prefix}/`);
214
+ });
215
+ if (hasMatchingPrefix) {
216
+ const resolved = matcher.match(alias, void 0, () => true, [
217
+ ".ts",
218
+ ".tsx"
219
+ ]);
220
+ if (resolved) {
221
+ return path3.relative(projectRoot, resolved);
222
+ }
223
+ }
224
+ }
225
+ throw new Error(
226
+ `Could not resolve alias "${alias}" from tsconfig.json. Ensure compilerOptions.paths defines a mapping for it (e.g. "${alias}/*"), then re-run. See https://ui.stera.sh/docs/installation.`
227
+ );
228
+ }
229
+ function resolveOutputPath(config, projectRoot, file) {
230
+ const alias = TYPE_TO_ALIAS[file.type];
231
+ if (alias) {
232
+ const dir = resolveAlias(config.aliases[alias], projectRoot);
233
+ const filename = path3.basename(file.path);
234
+ return path3.join(projectRoot, dir, filename);
235
+ }
236
+ if (file.type === "registry:style") {
237
+ const cssDir = path3.dirname(config.css);
238
+ const filename = path3.basename(file.path);
239
+ return path3.join(projectRoot, cssDir, filename);
240
+ }
241
+ return path3.join(projectRoot, file.path);
242
+ }
243
+
244
+ // src/utils/write-files.ts
245
+ async function confirmOverwrite(relativePath) {
246
+ if (!process.stdin.isTTY) {
247
+ console.log(
248
+ ` Skipping ${relativePath} (already exists, use --overwrite to replace)`
249
+ );
250
+ return false;
251
+ }
252
+ return confirm({
253
+ message: `${relativePath} already exists. Overwrite?`,
254
+ default: false
255
+ });
256
+ }
257
+ async function writeComponentFiles(items, config, projectRoot, options = {}) {
258
+ const written = [];
259
+ const skipped = [];
260
+ for (const item of items) {
261
+ for (const file of item.files ?? []) {
262
+ const outputPath = resolveOutputPath(config, projectRoot, file);
263
+ const relativePath = path4.relative(projectRoot, outputPath);
264
+ if (fs5.existsSync(outputPath)) {
265
+ const existing = fs5.readFileSync(outputPath, "utf-8");
266
+ if (existing === file.content) {
267
+ skipped.push({ path: relativePath, reason: "unchanged" });
268
+ continue;
269
+ }
270
+ if (!options.overwrite) {
271
+ const confirmed = await confirmOverwrite(relativePath);
272
+ if (!confirmed) {
273
+ skipped.push({ path: relativePath, reason: "declined" });
274
+ continue;
275
+ }
276
+ }
277
+ }
278
+ const dir = path4.dirname(outputPath);
279
+ fs5.mkdirSync(dir, { recursive: true });
280
+ fs5.writeFileSync(outputPath, file.content ?? "", "utf-8");
281
+ written.push(relativePath);
282
+ }
283
+ }
284
+ return { written, skipped };
285
+ }
286
+
287
+ // src/utils/write-styles.ts
288
+ import fs6 from "fs/promises";
289
+ import { existsSync } from "fs";
290
+ import path5 from "path";
291
+ var INDEX_TARGET = "ui/index.css";
292
+ var TYPOGRAPHY_TARGET = "ui/typography.css";
293
+ var PROTECTED_TARGETS = /* @__PURE__ */ new Set([TYPOGRAPHY_TARGET, "ui/colors.css"]);
294
+ var LEGACY_FILENAME = "stera-ui.css";
295
+ var LEGACY_IMPORT = "./stera-ui.css";
296
+ function partitionImports(content) {
297
+ const lines = content.split("\n");
298
+ const hoisted = [];
299
+ const kept = [];
300
+ for (const line of lines) {
301
+ const specifier = line.trim().match(/^@import\s+["'](.+?)["']/)?.[1];
302
+ if (specifier === void 0) {
303
+ kept.push(line);
304
+ } else if (specifier.startsWith("./") || specifier.startsWith("../")) {
305
+ kept.push(line);
306
+ } else if (specifier !== "tailwindcss") {
307
+ hoisted.push(line.trim());
308
+ }
309
+ }
310
+ const cleaned = kept.join("\n").replace(/\n{3,}/g, "\n\n").replace(/^\n+/, "");
311
+ return { content: cleaned, hoisted };
312
+ }
313
+ async function migrateLegacyStyles(config, cwd) {
314
+ const globalsPath = path5.resolve(cwd, config.css);
315
+ const legacyPath = path5.join(path5.dirname(globalsPath), LEGACY_FILENAME);
316
+ if (!existsSync(legacyPath)) return null;
317
+ await fs6.rm(legacyPath);
318
+ if (existsSync(globalsPath)) {
319
+ removeCssImport(globalsPath, LEGACY_IMPORT);
320
+ await insertImportLine(globalsPath, `@import "./${INDEX_TARGET}";`);
321
+ }
322
+ return legacyPath;
323
+ }
324
+ async function writeStyles(config, cwd, options = {}) {
325
+ const item = await fetchRegistryItem("globals");
326
+ const cssDir = path5.dirname(path5.resolve(cwd, config.css));
327
+ const written = [];
328
+ const skipped = [];
329
+ let extraImports = [];
330
+ for (const file of item.files ?? []) {
331
+ const target = file.target ?? path5.basename(file.path);
332
+ const outputPath = path5.join(cssDir, target);
333
+ let content = file.content ?? "";
334
+ if (target === INDEX_TARGET) {
335
+ const partitioned = partitionImports(content);
336
+ content = partitioned.content;
337
+ extraImports = partitioned.hoisted;
338
+ }
339
+ if (PROTECTED_TARGETS.has(target) && existsSync(outputPath)) {
340
+ const existing = await fs6.readFile(outputPath, "utf-8");
341
+ if (existing === content) {
342
+ skipped.push({ path: target, reason: "unchanged" });
343
+ continue;
344
+ }
345
+ if (!options.overwrite && !await confirmOverwrite(target)) {
346
+ skipped.push({ path: target, reason: "declined" });
347
+ continue;
348
+ }
349
+ }
350
+ await fs6.mkdir(path5.dirname(outputPath), { recursive: true });
351
+ await fs6.writeFile(outputPath, content, "utf-8");
352
+ written.push(target);
353
+ }
354
+ return {
355
+ indexPath: path5.join(cssDir, INDEX_TARGET),
356
+ typographyPath: path5.join(cssDir, TYPOGRAPHY_TARGET),
357
+ extraImports,
358
+ dependencies: item.dependencies ?? [],
359
+ written,
360
+ skipped
361
+ };
362
+ }
363
+
364
+ export {
365
+ CONFIG_FILE,
366
+ LEGACY_CONFIG_FILE,
367
+ findConfigPath,
368
+ loadConfig,
369
+ installDependencies,
370
+ patchCssVariables,
371
+ insertImportLine,
372
+ writeComponentFiles,
373
+ TYPOGRAPHY_TARGET,
374
+ migrateLegacyStyles,
375
+ writeStyles
376
+ };