@naminc/vnui 0.0.0-stage → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ngo Dinh Nam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
- # Temporary Holding Version
1
+ # vnui CLI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Source installer for [vn/ui](https://github.com/naminc/vnui). It copies editable React and TypeScript components, helpers, styles and local data into your app.
4
+
5
+ The npm release is pending. Until it is published, clone the repository, run `pnpm install --frozen-lockfile`, then run `npm pack` in `packages/cli`. Install the resulting archive as a development dependency in a React 19 TypeScript project. Run `npx --no-install vnui init` and `npx --no-install vnui add phone-input`. `vnui list` shows all eight components.
6
+
7
+ The npm package name is `@naminc/vnui`; the installed command is still `vnui`. The unscoped `vnui` name was rejected by npm because it is too similar to an existing package. After a verified npm release, run `npx @naminc/vnui@latest init` and `npx @naminc/vnui@latest add phone-input` instead. Use `--cwd` to select the consumer project. Existing edited files require interactive confirmation or `--overwrite`.
8
+
9
+ The consumer must support CSS imports and target ES2020 or later. VietQR installs `qrcode` and its TypeScript declarations when required. The library validates formats and produces payment instructions locally. It does not verify account ownership or completed payments. See the [full documentation](https://github.com/naminc/vnui/blob/main/README.md) and [data notices](https://github.com/naminc/vnui/blob/main/THIRD_PARTY.md).
package/bin.mjs ADDED
@@ -0,0 +1,275 @@
1
+ #!/usr/bin/env node
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { createRequire } from "node:module";
5
+ import { fileURLToPath } from "node:url";
6
+ import { createInterface } from "node:readline/promises";
7
+ import { parse } from "jsonc-parser";
8
+ import spawn from "cross-spawn";
9
+ const registry = fileURLToPath(new URL("./registry/", import.meta.url));
10
+ function json(file) {
11
+ const errors = [];
12
+ const value = parse(fs.readFileSync(file, "utf8"), errors, {
13
+ allowTrailingComma: true,
14
+ });
15
+ if (errors.length) throw Error("Invalid JSON in " + file);
16
+ return value;
17
+ }
18
+ function inside(root, target) {
19
+ const rel = path.relative(root, target);
20
+ if (
21
+ !rel ||
22
+ rel === ".." ||
23
+ rel.startsWith(".." + path.sep) ||
24
+ path.isAbsolute(rel)
25
+ )
26
+ throw Error("Target outside project: " + target);
27
+ let current = root;
28
+ for (const part of rel.split(path.sep)) {
29
+ current = path.join(current, part);
30
+ if (fs.lstatSync(current, { throwIfNoEntry: false })?.isSymbolicLink())
31
+ throw Error("Symlink in installation path: " + current);
32
+ }
33
+ return target;
34
+ }
35
+ function outputDir(root) {
36
+ const tsFile = path.join(root, "tsconfig.json");
37
+ const shadcn = path.join(root, "components.json");
38
+ const alias = fs.existsSync(shadcn)
39
+ ? json(shadcn).aliases?.components
40
+ : undefined;
41
+ let ts;
42
+ try {
43
+ ts = createRequire(tsFile)("typescript");
44
+ } catch {
45
+ if (alias)
46
+ throw Error(
47
+ "Install TypeScript in this project to resolve its component alias.",
48
+ );
49
+ }
50
+ let paths = json(tsFile).compilerOptions?.paths ?? {},
51
+ base = root;
52
+ if (ts) {
53
+ const result = ts.getParsedCommandLineOfConfigFile(
54
+ tsFile,
55
+ {},
56
+ {
57
+ ...ts.sys,
58
+ onUnRecoverableConfigFileDiagnostic: () => {
59
+ throw Error("Cannot parse tsconfig.json");
60
+ },
61
+ },
62
+ );
63
+ paths = result?.options.paths ?? {};
64
+ base = result?.options.baseUrl ?? result?.options.pathsBasePath ?? root;
65
+ }
66
+ const target =
67
+ alias ??
68
+ Object.keys(paths)
69
+ .find((k) => k === "@/*" || k === "~/*")
70
+ ?.replace("*", "components");
71
+ if (target) {
72
+ for (const key of Object.keys(paths).sort((a, b) => b.length - a.length)) {
73
+ const wildcard = key.endsWith("/*");
74
+ const prefix = wildcard ? key.slice(0, -1) : key;
75
+ if ((wildcard && target.startsWith(prefix)) || target === key) {
76
+ const mapped = paths[key][0].replace(
77
+ "*",
78
+ wildcard ? target.slice(prefix.length) : "",
79
+ );
80
+ return path.join(path.resolve(base, mapped), "vnui");
81
+ }
82
+ }
83
+ throw Error(
84
+ "Cannot resolve components alias " + target + " in tsconfig.json",
85
+ );
86
+ }
87
+ return path.join(
88
+ root,
89
+ fs.existsSync(path.join(root, "src")) ? "src" : "",
90
+ "components",
91
+ "vnui",
92
+ );
93
+ }
94
+ function manager(root, pkg) {
95
+ const supported = ["pnpm", "npm", "yarn", "bun"];
96
+ const specified = pkg.packageManager?.split("@")[0];
97
+ if (specified && !supported.includes(specified))
98
+ throw Error("Unsupported package manager: " + specified);
99
+ if (specified) return specified;
100
+ let dir = root;
101
+ while (true) {
102
+ for (const [file, pm] of [
103
+ ["pnpm-lock.yaml", "pnpm"],
104
+ ["yarn.lock", "yarn"],
105
+ ["bun.lock", "bun"],
106
+ ["bun.lockb", "bun"],
107
+ ["package-lock.json", "npm"],
108
+ ])
109
+ if (fs.existsSync(path.join(dir, file))) return pm;
110
+ const parent = path.dirname(dir);
111
+ if (parent === dir) break;
112
+ dir = parent;
113
+ }
114
+ return "npm";
115
+ }
116
+ function install(root, pm, deps, dev) {
117
+ if (!deps.length) return;
118
+ const args = [
119
+ pm === "npm" ? "install" : "add",
120
+ ...(dev ? ["-D"] : []),
121
+ ...deps,
122
+ ];
123
+ const result = spawn.sync(pm, args, { cwd: root, stdio: "inherit" });
124
+ if (result.error || result.status !== 0)
125
+ throw Error("Dependency installation failed. Check " + pm + " and retry.");
126
+ }
127
+ async function main() {
128
+ const args = process.argv.slice(2);
129
+ let root = process.cwd();
130
+ const cwd = args.indexOf("--cwd");
131
+ if (cwd >= 0) {
132
+ if (!args[cwd + 1]) throw Error("Provide a directory after --cwd");
133
+ root = path.resolve(args[cwd + 1]);
134
+ args.splice(cwd, 2);
135
+ }
136
+ const force = args.includes("--overwrite");
137
+ if (force) args.splice(args.indexOf("--overwrite"), 1);
138
+ const [cmd, ...names] = args;
139
+ if (!cmd || cmd === "--help") {
140
+ console.log(
141
+ "vnui init | list | add <component...> [--cwd <directory>] [--overwrite]",
142
+ );
143
+ return;
144
+ }
145
+ if (!fs.existsSync(path.join(registry, "registry.json")))
146
+ throw Error("Build the local registry first with pnpm registry.");
147
+ const items = json(path.join(registry, "registry.json")).items;
148
+ if (cmd === "list") {
149
+ console.log(items.map((i) => i.name).join("\n"));
150
+ return;
151
+ }
152
+ if (!["init", "add"].includes(cmd)) throw Error("Unknown command: " + cmd);
153
+ root = fs.realpathSync(root);
154
+ const pkg = json(path.join(root, "package.json"));
155
+ const available = {
156
+ ...pkg.devDependencies,
157
+ ...pkg.dependencies,
158
+ ...pkg.peerDependencies,
159
+ };
160
+ if (!available.react || !fs.existsSync(path.join(root, "tsconfig.json")))
161
+ throw Error(
162
+ "Choose a React TypeScript project with package.json and tsconfig.json.",
163
+ );
164
+ const config = path.join(root, "vnui.json");
165
+ inside(root, config);
166
+ if (cmd === "init") {
167
+ if (fs.existsSync(config)) {
168
+ console.log("vnui.json already exists.");
169
+ return;
170
+ }
171
+ const dir = inside(root, outputDir(root));
172
+ fs.writeFileSync(
173
+ config,
174
+ JSON.stringify(
175
+ { directory: path.relative(root, dir).split(path.sep).join("/") },
176
+ null,
177
+ 2,
178
+ ) + "\n",
179
+ { flag: "wx" },
180
+ );
181
+ console.log("Initialized " + config);
182
+ return;
183
+ }
184
+ if (!names.length) throw Error("Choose a component. See vnui list.");
185
+ if (!fs.existsSync(config)) throw Error("Run vnui init first.");
186
+ const dest = inside(root, path.resolve(root, json(config).directory));
187
+ const files = new Map(),
188
+ deps = new Set(),
189
+ dev = new Set();
190
+ for (const n of names) {
191
+ if (!items.some((i) => i.name === n))
192
+ throw Error("Unknown component: " + n);
193
+ const item = json(path.join(registry, n + ".json"));
194
+ for (const file of item.files)
195
+ files.set(inside(root, path.resolve(dest, file.path)), file.content);
196
+ for (const d of item.dependencies ?? []) deps.add(d);
197
+ for (const d of item.devDependencies ?? []) dev.add(d);
198
+ }
199
+ const lock = path.join(root, ".vnui-install.lock");
200
+ const fd = fs.openSync(lock, "wx");
201
+ try {
202
+ const pending = [],
203
+ conflicts = [];
204
+ for (const [file, content] of files) {
205
+ const old = fs.existsSync(file) ? fs.readFileSync(file) : null;
206
+ if (old?.toString("utf8") === content) continue;
207
+ if (old) conflicts.push(path.relative(root, file));
208
+ pending.push({ file, content, old });
209
+ }
210
+ if (conflicts.length && !force) {
211
+ console.error("Existing files: " + conflicts.join(", "));
212
+ if (!process.stdin.isTTY)
213
+ throw Error("Use --overwrite to confirm replacing these files.");
214
+ const prompt = createInterface({
215
+ input: process.stdin,
216
+ output: process.stdout,
217
+ });
218
+ let yes;
219
+ try {
220
+ yes = await prompt.question("Replace listed files? [y/N] ");
221
+ } finally {
222
+ prompt.close();
223
+ }
224
+ if (!/^y(es)?$/i.test(yes)) throw Error("Installation cancelled.");
225
+ }
226
+ const pm = manager(root, pkg);
227
+ const missing = (ds) =>
228
+ [...ds].filter((d) => !available[d.slice(0, d.lastIndexOf("@"))]);
229
+ install(root, pm, missing(deps), false);
230
+ install(root, pm, missing(dev), true);
231
+ const written = [];
232
+ try {
233
+ for (const row of pending) {
234
+ inside(root, row.file);
235
+ fs.mkdirSync(path.dirname(row.file), { recursive: true });
236
+ if (row.old) {
237
+ if (!fs.readFileSync(row.file).equals(row.old))
238
+ throw Error("File changed during installation: " + row.file);
239
+ written.push(row);
240
+ fs.writeFileSync(row.file, row.content);
241
+ } else {
242
+ const handle = fs.openSync(row.file, "wx");
243
+ written.push(row);
244
+ try {
245
+ fs.writeFileSync(handle, row.content);
246
+ } finally {
247
+ fs.closeSync(handle);
248
+ }
249
+ }
250
+ }
251
+ } catch (e) {
252
+ for (const r of written.reverse()) {
253
+ if (r.old) fs.writeFileSync(r.file, r.old);
254
+ else fs.unlinkSync(r.file);
255
+ }
256
+ throw e;
257
+ }
258
+ console.log(
259
+ "Installed " +
260
+ names.join(", ") +
261
+ " in " +
262
+ path.relative(root, dest) +
263
+ " (" +
264
+ pending.length +
265
+ " files written).",
266
+ );
267
+ } finally {
268
+ fs.closeSync(fd);
269
+ fs.unlinkSync(lock);
270
+ }
271
+ }
272
+ main().catch((e) => {
273
+ console.error("vnui: " + e.message);
274
+ process.exitCode = 1;
275
+ });
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@naminc/vnui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Install editable Vietnamese React components.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/naminc/vnui.git",
9
+ "directory": "packages/cli"
10
+ },
11
+ "homepage": "https://github.com/naminc/vnui#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/naminc/vnui/issues"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "type": "module",
19
+ "engines": {
20
+ "node": ">=22.14"
21
+ },
22
+ "bin": {
23
+ "vnui": "bin.mjs"
24
+ },
25
+ "scripts": {
26
+ "prepack": "node ../../scripts/registry.mjs"
27
+ },
28
+ "files": [
29
+ "bin.mjs",
30
+ "registry",
31
+ "LICENSE",
32
+ "README.md"
33
+ ],
34
+ "dependencies": {
35
+ "jsonc-parser": "3.3.1",
36
+ "cross-spawn": "7.0.6"
37
+ }
38
+ }