@odougaraujo/brasa-ui 0.1.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.
Files changed (2) hide show
  1. package/dist/index.js +207 -0
  2. package/package.json +26 -0
package/dist/index.js ADDED
@@ -0,0 +1,207 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
5
+ import { resolve, dirname, join } from "path";
6
+ var REGISTRY_URL = "https://brasa-ui.vercel.app/r";
7
+ var CONFIG_FILE = "brasa.json";
8
+ var VERSION = "0.1.0";
9
+ function log(msg) {
10
+ console.log(` ${msg}`);
11
+ }
12
+ function success(msg) {
13
+ console.log(`\x1B[32m\u2713\x1B[0m ${msg}`);
14
+ }
15
+ function error(msg) {
16
+ console.error(`\x1B[31m\u2717\x1B[0m ${msg}`);
17
+ }
18
+ function warn(msg) {
19
+ console.log(`\x1B[33m!\x1B[0m ${msg}`);
20
+ }
21
+ function heading(msg) {
22
+ console.log(`
23
+ \x1B[1m${msg}\x1B[0m
24
+ `);
25
+ }
26
+ async function fetchJSON(url) {
27
+ const res = await fetch(url);
28
+ if (!res.ok) throw new Error(`Failed to fetch ${url}: ${res.status}`);
29
+ return res.json();
30
+ }
31
+ function loadConfig() {
32
+ const configPath = resolve(process.cwd(), CONFIG_FILE);
33
+ if (!existsSync(configPath)) return null;
34
+ return JSON.parse(readFileSync(configPath, "utf-8"));
35
+ }
36
+ function resolveAlias(alias) {
37
+ if (alias.startsWith("@/")) return alias.slice(2);
38
+ if (alias.startsWith("~/")) return alias.slice(2);
39
+ return alias;
40
+ }
41
+ async function init() {
42
+ heading("brasa.ui \u2014 init");
43
+ const configPath = resolve(process.cwd(), CONFIG_FILE);
44
+ if (existsSync(configPath)) {
45
+ warn("brasa.json already exists");
46
+ const existing = JSON.parse(readFileSync(configPath, "utf-8"));
47
+ log(` components: ${existing.aliases.components}`);
48
+ log(` utils: ${existing.aliases.utils}`);
49
+ return;
50
+ }
51
+ const hasSrc = existsSync(resolve(process.cwd(), "src"));
52
+ const base = hasSrc ? "src" : ".";
53
+ const config = {
54
+ aliases: {
55
+ components: `@/${base === "src" ? "" : ""}components/brasa`,
56
+ utils: `@/${base === "src" ? "" : ""}lib/brasa/utils`
57
+ }
58
+ };
59
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
60
+ success("Created brasa.json");
61
+ const componentsDir = resolve(process.cwd(), base, "components/brasa");
62
+ const utilsDir = resolve(process.cwd(), base, "lib/brasa/utils");
63
+ mkdirSync(componentsDir, { recursive: true });
64
+ mkdirSync(utilsDir, { recursive: true });
65
+ success(`Created ${componentsDir.replace(process.cwd() + "/", "")}/`);
66
+ success(`Created ${utilsDir.replace(process.cwd() + "/", "")}/`);
67
+ log("");
68
+ log("Now add components:");
69
+ log(" npx brasa-ui add cpf-input");
70
+ log(" npx brasa-ui add pix-payment");
71
+ log(" npx brasa-ui add cep-input");
72
+ }
73
+ async function add(names) {
74
+ if (names.length === 0) {
75
+ error("Specify at least one component: npx brasa-ui add cpf-input");
76
+ process.exit(1);
77
+ }
78
+ const config = loadConfig();
79
+ if (!config) {
80
+ error("No brasa.json found. Run `npx brasa-ui init` first.");
81
+ process.exit(1);
82
+ }
83
+ heading(`brasa.ui \u2014 adding ${names.join(", ")}`);
84
+ const index = await fetchJSON(`${REGISTRY_URL}/index.json`);
85
+ const available = new Set(index.map((c) => c.name));
86
+ for (const name of names) {
87
+ if (!available.has(name)) {
88
+ error(`Component "${name}" not found in registry`);
89
+ log("Available components:");
90
+ for (const c of index) {
91
+ log(` ${c.name} (${c.type})`);
92
+ }
93
+ process.exit(1);
94
+ }
95
+ }
96
+ const toInstall = /* @__PURE__ */ new Set();
97
+ const queue = [...names];
98
+ while (queue.length > 0) {
99
+ const name = queue.pop();
100
+ if (toInstall.has(name)) continue;
101
+ toInstall.add(name);
102
+ const entry = index.find((c) => c.name === name);
103
+ if (entry?.registryDependencies) {
104
+ for (const dep of entry.registryDependencies) {
105
+ if (!toInstall.has(dep)) queue.push(dep);
106
+ }
107
+ }
108
+ }
109
+ for (const name of toInstall) {
110
+ const entry = await fetchJSON(
111
+ `${REGISTRY_URL}/styles/default/${name}.json`
112
+ );
113
+ for (const file of entry.files) {
114
+ let targetPath = file.path;
115
+ if (targetPath.startsWith("lib/brasa/utils/")) {
116
+ const utilsBase = resolveAlias(config.aliases.utils);
117
+ const filename = targetPath.replace("lib/brasa/utils/", "");
118
+ targetPath = join(utilsBase, filename);
119
+ } else if (targetPath.startsWith("lib/brasa/components/")) {
120
+ const compBase = resolveAlias(config.aliases.components);
121
+ const rest = targetPath.replace("lib/brasa/components/", "");
122
+ targetPath = join(compBase, rest);
123
+ }
124
+ const fullPath = resolve(process.cwd(), targetPath);
125
+ let content = file.content;
126
+ content = content.replace(
127
+ /@\/lib\/brasa\/utils\//g,
128
+ config.aliases.utils.endsWith("/") ? config.aliases.utils : config.aliases.utils + "/"
129
+ );
130
+ if (existsSync(fullPath)) {
131
+ warn(`${targetPath} already exists, skipping`);
132
+ continue;
133
+ }
134
+ mkdirSync(dirname(fullPath), { recursive: true });
135
+ writeFileSync(fullPath, content);
136
+ success(targetPath);
137
+ }
138
+ }
139
+ log("");
140
+ success("Done! Components added to your project.");
141
+ }
142
+ async function list() {
143
+ heading("brasa.ui \u2014 available components");
144
+ const index = await fetchJSON(`${REGISTRY_URL}/index.json`);
145
+ const groups = {};
146
+ for (const c of index) {
147
+ (groups[c.type] ??= []).push(c);
148
+ }
149
+ for (const [type, components] of Object.entries(groups)) {
150
+ console.log(`\x1B[1m${type}\x1B[0m`);
151
+ for (const c of components) {
152
+ log(` ${c.name}`);
153
+ }
154
+ log("");
155
+ }
156
+ log(`${index.length} components available`);
157
+ log("Add with: npx brasa-ui add <name>");
158
+ }
159
+ function help() {
160
+ heading(`brasa-ui v${VERSION}`);
161
+ log("Brazilian UI components for React");
162
+ log("");
163
+ log("Commands:");
164
+ log(" init Set up brasa.ui in your project");
165
+ log(" add <name...> Add components to your project");
166
+ log(" list List available components");
167
+ log(" help Show this help");
168
+ log("");
169
+ log("Examples:");
170
+ log(" npx brasa-ui init");
171
+ log(" npx brasa-ui add cpf-input cep-input");
172
+ log(" npx brasa-ui add pix-payment");
173
+ log(" npx brasa-ui list");
174
+ }
175
+ var args = process.argv.slice(2);
176
+ var command = args[0];
177
+ switch (command) {
178
+ case "init":
179
+ init().catch((e) => {
180
+ error(e.message);
181
+ process.exit(1);
182
+ });
183
+ break;
184
+ case "add":
185
+ add(args.slice(1)).catch((e) => {
186
+ error(e.message);
187
+ process.exit(1);
188
+ });
189
+ break;
190
+ case "list":
191
+ case "ls":
192
+ list().catch((e) => {
193
+ error(e.message);
194
+ process.exit(1);
195
+ });
196
+ break;
197
+ case "help":
198
+ case "--help":
199
+ case "-h":
200
+ case void 0:
201
+ help();
202
+ break;
203
+ default:
204
+ error(`Unknown command: ${command}`);
205
+ help();
206
+ process.exit(1);
207
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@odougaraujo/brasa-ui",
3
+ "version": "0.1.0",
4
+ "description": "CLI to add brasa.ui components to your project",
5
+ "license": "MIT",
6
+ "author": "Douglas Araujo <douglasp.araujo96@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/odouglasaraujo/brasa-ui.git",
10
+ "directory": "packages/cli"
11
+ },
12
+ "keywords": ["brasa", "ui", "cli", "components", "brazil", "react"],
13
+ "type": "module",
14
+ "bin": {
15
+ "brasa-ui": "./dist/index.js"
16
+ },
17
+ "files": ["dist"],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "dev": "tsup --watch"
21
+ },
22
+ "devDependencies": {
23
+ "tsup": "^8",
24
+ "typescript": "^5"
25
+ }
26
+ }