@nhatnguyet/cli 0.0.0-stage → 0.2.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/src/cli.ts ADDED
@@ -0,0 +1,191 @@
1
+ #!/usr/bin/env node
2
+ // Lệnh dòng lệnh `nhatnguyet`: gọi lớp API tiếng Anh của các gói @nhatnguyet
3
+ // rồi in ra terminal (render.ts) hoặc JSON (--json).
4
+ //
5
+ // Command line for the Nhat Nguyet libraries.
6
+
7
+ import { solarTerms, toLunar, toSolar } from "@nhatnguyet/am-lich";
8
+ import type { Lang } from "@nhatnguyet/am-lich";
9
+ import { castZiWei, decadeLuck, findPatterns, yearlyLuck, ziWeiSvg } from "@nhatnguyet/tu-vi";
10
+ import { baZiSvg, castBaZi } from "@nhatnguyet/bat-tu";
11
+ import { numerology, numerologySvg } from "@nhatnguyet/than-so-hoc";
12
+ import { writeFileSync } from "node:fs";
13
+ import { festivals } from "@nhatnguyet/le-hoi";
14
+ import { renderBaZi, renderFestivals, renderLunar, renderNumerology, renderSolarTerms, renderZiWei } from "./render.ts";
15
+
16
+ // Gói không phụ thuộc @types/node: khai đúng phần process đang dùng.
17
+ declare const process: {
18
+ argv: string[];
19
+ env: Record<string, string | undefined>;
20
+ stdout: { isTTY?: boolean; columns?: number; write(s: string): boolean };
21
+ stderr: { write(s: string): boolean };
22
+ exitCode?: number;
23
+ };
24
+
25
+ /** Version printed by --version. */
26
+ export const VERSION = "0.1.0";
27
+
28
+ export const HELP = `nhatnguyet: Zi Wei Dou Shu, BaZi, lunar calendar, numerology, Vietnamese festivals
29
+
30
+ Usage:
31
+ nhatnguyet ziwei <YYYY-MM-DD> <HH:MM> <male|female> [--name "..."] [--year 2026]
32
+ nhatnguyet bazi <YYYY-MM-DD> [HH:MM] <male|female>
33
+ nhatnguyet lunar <YYYY-MM-DD> [--country VN|CN|KR|JP|...]
34
+ nhatnguyet solar <lunar YYYY-MM-DD> [--leap] [--country ...]
35
+ nhatnguyet terms <year> [--country ...]
36
+ nhatnguyet numerology "<full name>" <YYYY-MM-DD> [--today YYYY-MM-DD]
37
+ nhatnguyet festivals <year>
38
+
39
+ Options:
40
+ --lang en|vi|zh language of the result (default en)
41
+ --json print JSON instead of text
42
+ --svg <file> also write the chart as SVG (ziwei, bazi, numerology)
43
+ --theme dark dark SVG
44
+ --no-color no ANSI colours (also when NO_COLOR is set or output is not a terminal)
45
+ -h, --help this help
46
+ -v, --version version
47
+
48
+ Times are clock times; the Tý double hour runs 23:00 to 00:59. Dates and times
49
+ are read as Vietnam time (UTC+7) unless --country says otherwise (lunar, solar,
50
+ terms). Docs: https://nhatnguyet.org/ma-nguon-mo`;
51
+
52
+ const ALIAS: Readonly<Record<string, string>> = {
53
+ ziwei: "ziwei", "zi-wei": "ziwei", tuvi: "ziwei", "tu-vi": "ziwei",
54
+ bazi: "bazi", "ba-zi": "bazi", battu: "bazi", "bat-tu": "bazi",
55
+ lunar: "lunar", amlich: "lunar", "am-lich": "lunar",
56
+ solar: "solar", duong: "solar",
57
+ terms: "terms", "solar-terms": "terms", tietkhi: "terms",
58
+ numerology: "numerology", "than-so-hoc": "numerology",
59
+ festivals: "festivals", "le-hoi": "festivals", lehoi: "festivals",
60
+ };
61
+
62
+ /** Parsed command line. */
63
+ export interface Args {
64
+ command: string;
65
+ positional: string[];
66
+ flags: Record<string, string | boolean>;
67
+ }
68
+
69
+ /** Split argv (without node and script) into command, positional arguments and flags. */
70
+ export function parseArgs(argv: string[]): Args {
71
+ const positional: string[] = [];
72
+ const flags: Record<string, string | boolean> = {};
73
+ const coGiaTri = new Set(["lang", "country", "name", "year", "today", "tz", "svg", "theme"]);
74
+ for (let i = 0; i < argv.length; i++) {
75
+ const a = argv[i];
76
+ if (a === "-h") flags.help = true;
77
+ else if (a === "-v") flags.version = true;
78
+ else if (a.startsWith("--")) {
79
+ const eq = a.indexOf("=");
80
+ const k = eq > 0 ? a.slice(2, eq) : a.slice(2);
81
+ if (eq > 0) flags[k] = a.slice(eq + 1);
82
+ else if (coGiaTri.has(k)) {
83
+ if (i + 1 >= argv.length) throw new Error(`--${k} needs a value`);
84
+ flags[k] = argv[++i];
85
+ } else flags[k] = true;
86
+ } else positional.push(a);
87
+ }
88
+ const lenh = positional.shift() ?? "";
89
+ return { command: ALIAS[lenh.toLowerCase()] ?? lenh, positional, flags };
90
+ }
91
+
92
+ const GIOI = /^(male|female|m|f|nam|nữ|nu)$/i;
93
+
94
+ /** Where `run` writes files (the SVG of --svg); tests pass their own. */
95
+ export interface RunEnv {
96
+ color: boolean;
97
+ width: number;
98
+ writeFile?: (path: string, data: string) => void;
99
+ }
100
+
101
+ /** Run one command; returns the text to print (no trailing newline). */
102
+ export function run(argv: string[], env: RunEnv): string {
103
+ const { command, positional: p, flags } = parseArgs(argv);
104
+ if (flags.version) return VERSION;
105
+ if (flags.help || command === "" || command === "help") return HELP;
106
+ const lang = (typeof flags.lang === "string" ? flags.lang : "en") as Lang;
107
+ const json = flags.json === true;
108
+ const opts = { color: env.color && flags.color !== false && flags["no-color"] !== true, width: env.width };
109
+ const country = typeof flags.country === "string" ? flags.country : undefined;
110
+ const can = (n: number, mau: string) => {
111
+ if (p.length < n) throw new Error(`usage: nhatnguyet ${mau}`);
112
+ };
113
+ const dumpJson = (x: unknown) => JSON.stringify(x, (k, v) => (k === "raw" ? undefined : v), 2);
114
+ const theme = flags.theme === "dark" ? ("dark" as const) : ("light" as const);
115
+ const ghiSvg = (ve: () => string) => {
116
+ if (typeof flags.svg !== "string") return;
117
+ (env.writeFile ?? writeFileSync)(flags.svg, ve() + "\n");
118
+ };
119
+
120
+ switch (command) {
121
+ case "ziwei": {
122
+ can(3, "ziwei <YYYY-MM-DD> <HH:MM> <male|female>");
123
+ const year = typeof flags.year === "string" ? Number(flags.year) : undefined;
124
+ const chart = castZiWei({ date: p[0], time: p[1], gender: p[2], name: typeof flags.name === "string" ? flags.name : undefined, lang, viewYear: year });
125
+ const extra = { decades: decadeLuck(chart), year: yearlyLuck(chart), patterns: findPatterns(chart) };
126
+ ghiSvg(() => ziWeiSvg(chart, { theme }));
127
+ if (json) return dumpJson({ chart, ...extra });
128
+ return renderZiWei(chart, extra, opts);
129
+ }
130
+ case "bazi": {
131
+ can(2, "bazi <YYYY-MM-DD> [HH:MM] <male|female>");
132
+ const coGio = p.length >= 3 || !GIOI.test(p[1]);
133
+ if (coGio) can(3, "bazi <YYYY-MM-DD> [HH:MM] <male|female>");
134
+ const chart = castBaZi({ date: p[0], time: coGio ? p[1] : null, gender: coGio ? p[2] : p[1], lang });
135
+ ghiSvg(() => baZiSvg(chart, { theme, name: typeof flags.name === "string" ? flags.name : "" }));
136
+ return json ? dumpJson(chart) : renderBaZi(chart, opts);
137
+ }
138
+ case "lunar": {
139
+ can(1, "lunar <YYYY-MM-DD> [--country CN]");
140
+ const r = toLunar(p[0], { country, lang });
141
+ return json ? dumpJson(r) : renderLunar(r, opts);
142
+ }
143
+ case "solar": {
144
+ can(1, "solar <lunar YYYY-MM-DD> [--leap]");
145
+ const k = /^(-?\d+)-(\d+)-(\d+)$/.exec(p[0]);
146
+ if (!k) throw new Error("lunar date must look like YYYY-MM-DD");
147
+ const r = toSolar({ year: Number(k[1]), month: Number(k[2]), day: Number(k[3]), leap: flags.leap === true }, { country });
148
+ return json ? dumpJson(r) : r.date;
149
+ }
150
+ case "terms": {
151
+ can(1, "terms <year>");
152
+ const y = Number(p[0]);
153
+ const t = solarTerms(y, { country, lang });
154
+ return json ? dumpJson(t) : renderSolarTerms(y, t, lang, opts);
155
+ }
156
+ case "numerology": {
157
+ can(2, 'numerology "<full name>" <YYYY-MM-DD>');
158
+ const r = numerology({ name: p[0], birthDate: p[1], today: typeof flags.today === "string" ? flags.today : undefined, lang });
159
+ ghiSvg(() => numerologySvg(r, { theme }));
160
+ return json ? dumpJson(r) : renderNumerology(r, opts);
161
+ }
162
+ case "festivals": {
163
+ can(1, "festivals <year>");
164
+ const y = Number(p[0]);
165
+ const list = festivals(y, { lang });
166
+ return json ? dumpJson(list) : renderFestivals(y, list, lang, opts);
167
+ }
168
+ }
169
+ throw new Error(`unknown command "${command}". Try: nhatnguyet --help`);
170
+ }
171
+
172
+ function main(): void {
173
+ const out = process.stdout;
174
+ const env = process.env;
175
+ const ep = env.FORCE_COLOR !== undefined && env.FORCE_COLOR !== "0";
176
+ const color = env.NO_COLOR === undefined && (ep || (!!out.isTTY && env.TERM !== "dumb"));
177
+ const width = out.isTTY && out.columns ? Math.min(out.columns, 140) : 88;
178
+ try {
179
+ out.write(run(process.argv.slice(2), { color, width }) + "\n");
180
+ } catch (e) {
181
+ process.stderr.write("nhatnguyet: " + (e instanceof Error ? e.message : String(e)) + "\n");
182
+ process.exitCode = 1;
183
+ }
184
+ }
185
+
186
+ // Chạy khi được gọi như chương trình (không chạy khi test nhập mô-đun).
187
+ const laChuongTrinh = (() => {
188
+ const a = process.argv[1] ?? "";
189
+ return /(?:^|[\\/])(?:cli\.(?:ts|js)|nhatnguyet)$/.test(a) || a.includes("/.bin/") || a.includes("@nhatnguyet/cli");
190
+ })();
191
+ if (laChuongTrinh) main();
package/src/node.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ // Gói không phụ thuộc @types/node: khai đúng phần node:fs mà cli.ts dùng.
2
+ declare module "node:fs" {
3
+ export function writeFileSync(path: string, data: string): void;
4
+ }