@ucdjs/cli 0.1.0 → 0.1.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.
@@ -4,14 +4,14 @@ import yargs from "yargs-parser";
4
4
 
5
5
  //#region package.json
6
6
  var name = "@ucdjs/cli";
7
- var version = "0.1.0";
7
+ var version = "0.1.2";
8
8
  var type = "module";
9
9
  var author = {
10
10
  "name": "Lucas Nørgård",
11
11
  "email": "lucasnrgaard@gmail.com",
12
12
  "url": "https://luxass.dev"
13
13
  };
14
- var packageManager = "pnpm@10.8.0";
14
+ var packageManager = "pnpm@10.10.0";
15
15
  var license = "MIT";
16
16
  var homepage = "https://github.com/ucdjs/ucd";
17
17
  var repository = {
@@ -30,6 +30,8 @@ var scripts = {
30
30
  };
31
31
  var dependencies = {
32
32
  "@luxass/unicode-utils": "catalog:prod",
33
+ "@luxass/utils": "catalog:prod",
34
+ "@ucdjs/schema-gen": "workspace:*",
33
35
  "farver": "catalog:prod",
34
36
  "yargs-parser": "catalog:prod"
35
37
  };
@@ -63,7 +65,7 @@ var package_default = {
63
65
 
64
66
  //#endregion
65
67
  //#region src/cli-utils.ts
66
- const SUPPORTED_COMMANDS = new Set(["generate"]);
68
+ const SUPPORTED_COMMANDS = new Set(["generate", "codegen"]);
67
69
  /**
68
70
  * Resolves the CLI command based on the provided arguments.
69
71
  *
@@ -129,7 +131,7 @@ async function runCommand(cmd, flags) {
129
131
  headline: "A CLI for working with the Unicode Character Database (UCD).",
130
132
  usage: "[command] [...flags]",
131
133
  tables: {
132
- "Commands": [["generate", "Generate UCD data files."]],
134
+ "Commands": [["generate", "Generate UCD data files."], ["codegen", "Generate TypeScript code from UCD data."]],
133
135
  "Global Flags": [
134
136
  ["--force", "Force the operation to run, even if it's not needed."],
135
137
  ["--version", "Show the version number and exit."],
@@ -142,7 +144,7 @@ async function runCommand(cmd, flags) {
142
144
  console.log(` ${bgGreen(black(` ucd `))} ${green(`v${package_default.version ?? "x.y.z"}`)}`);
143
145
  break;
144
146
  case "generate": {
145
- const { runGenerate } = await import("./generate-Hwn8O33w.js");
147
+ const { runGenerate } = await import("./generate-Z7bYnm_v.js");
146
148
  const versions = flags._.slice(3);
147
149
  await runGenerate({
148
150
  versions,
@@ -150,14 +152,23 @@ async function runCommand(cmd, flags) {
150
152
  });
151
153
  break;
152
154
  }
155
+ case "codegen": {
156
+ const { runCodegenRoot } = await import("./root-BT7DL0Ms.js");
157
+ const subcommand = flags._[3]?.toString() ?? "";
158
+ await runCodegenRoot(subcommand, { flags });
159
+ break;
160
+ }
153
161
  default: throw new Error(`Error running ${cmd} -- no command found.`);
154
162
  }
155
163
  }
156
164
  function parseFlags(args) {
157
165
  return yargs(args, {
158
166
  configuration: { "parse-positional-numbers": false },
159
- string: ["output-dir"],
160
- default: { "output-dir": "./data" }
167
+ string: [
168
+ "output-dir",
169
+ "input-dir",
170
+ "output-file"
171
+ ]
161
172
  });
162
173
  }
163
174
  async function runCLI(args) {
package/dist/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- import { runCLI } from "./cli-utils-Cy8lSvR2.js";
1
+ import { runCLI } from "./cli-utils-DyOk9RI0.js";
2
2
  import process from "node:process";
3
3
 
4
4
  //#region src/cli.ts
@@ -0,0 +1,98 @@
1
+ import { printHelp } from "./cli-utils-DyOk9RI0.js";
2
+ import { existsSync } from "node:fs";
3
+ import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { RawDataFile } from "@luxass/unicode-utils/data-files";
6
+ import { toKebabCase } from "@luxass/utils";
7
+ import { generateFields } from "@ucdjs/schema-gen";
8
+
9
+ //#region src/cmd/codegen/fields.ts
10
+ async function runFieldCodegen({ inputPath, flags }) {
11
+ if (flags?.help || flags?.h) {
12
+ printHelp({
13
+ headline: "Generate Unicode Data Files",
14
+ commandName: "ucd codegen fields",
15
+ usage: "<input> [...flags]",
16
+ tables: { Flags: [
17
+ ["--openai-key (-k)", "The OpenAI API key to use. (can also be set using OPENAI_API_KEY env var)"],
18
+ ["--output-dir", "Specify the output directory for generated files (defaults to .codegen)"],
19
+ ["--bundle <filename>", "Combine all generated files into a single file, if no filename is provided, the default is index.ts"],
20
+ ["--help (-h)", "See all available flags."]
21
+ ] }
22
+ });
23
+ return;
24
+ }
25
+ const openaiKey = flags.openaiKey || process.env.OPENAI_API_KEY;
26
+ if (!openaiKey) {
27
+ console.error("No OpenAI API key provided. Please provide an OpenAI API key.");
28
+ return;
29
+ }
30
+ if (inputPath == null) {
31
+ console.error("No input path provided. Please provide an input path.");
32
+ return;
33
+ }
34
+ const resolvedInputPath = path.resolve(inputPath);
35
+ if (!existsSync(resolvedInputPath)) {
36
+ console.error(`invalid input path: ${inputPath}. Please provide a valid input path.`);
37
+ return;
38
+ }
39
+ const outputDir = flags.outputDir || process.cwd();
40
+ await mkdir(outputDir, { recursive: true });
41
+ const shouldBundle = typeof flags.bundle === "string" || flags.bundle === true;
42
+ const bundleFileName = typeof flags.bundle === "string" ? flags.bundle : "index.ts";
43
+ const isDirectory = (await stat(resolvedInputPath)).isDirectory();
44
+ const files = [];
45
+ if (isDirectory) {
46
+ const dir = await readdir(resolvedInputPath, {
47
+ withFileTypes: true,
48
+ recursive: true
49
+ });
50
+ for (const file of dir) if (file.isFile()) {
51
+ if (!file.name.endsWith(".txt")) continue;
52
+ files.push(path.join(file.parentPath, file.name));
53
+ }
54
+ } else files.push(resolvedInputPath);
55
+ const promises = files.map(async (filePath) => {
56
+ const content = await readFile(filePath, "utf-8");
57
+ const datafile = new RawDataFile(content);
58
+ if (datafile.heading == null) {
59
+ console.error(`heading for file ${filePath} is null. Skipping file.`);
60
+ return null;
61
+ }
62
+ const code = await generateFields({
63
+ datafile,
64
+ apiKey: openaiKey
65
+ });
66
+ if (code == null) {
67
+ console.error(`Error generating fields for file: ${filePath}`);
68
+ return null;
69
+ }
70
+ if (!shouldBundle) {
71
+ const fileName = toKebabCase(path.basename(filePath).replace(/\.txt$/, "")).toLowerCase();
72
+ await writeFile(path.join(outputDir, `${fileName}.ts`), code, "utf-8");
73
+ }
74
+ return {
75
+ code,
76
+ fileName: filePath.replace(`${process.cwd()}/`, "")
77
+ };
78
+ });
79
+ const generatedCode = await Promise.all(promises);
80
+ if (!shouldBundle) {
81
+ console.log(`Generated fields for ${files.length} files in ${outputDir}`);
82
+ return;
83
+ }
84
+ let bundledCode = `// This file is generated by ucd codegen. Do not edit this file directly.\n\n`;
85
+ for (const { fileName, code } of generatedCode.filter((obj) => obj != null)) {
86
+ bundledCode += `//#region ${fileName}\n`;
87
+ bundledCode += code;
88
+ bundledCode += `\n//#endregion\n`;
89
+ bundledCode += "\n\n";
90
+ }
91
+ let bundlePath = path.resolve(path.join(outputDir, bundleFileName));
92
+ if (path.extname(bundleFileName) === "") bundlePath = path.join(outputDir, `${bundleFileName}.ts`);
93
+ await writeFile(bundlePath, bundledCode, "utf-8");
94
+ console.log(`Generated bundled fields in ${bundlePath}`);
95
+ }
96
+
97
+ //#endregion
98
+ export { runFieldCodegen };
@@ -1,12 +1,13 @@
1
- import { printHelp } from "./cli-utils-Cy8lSvR2.js";
1
+ import { printHelp } from "./cli-utils-DyOk9RI0.js";
2
2
  import { green, yellow } from "farver/fast";
3
3
  import { mkdir, writeFile } from "node:fs/promises";
4
4
  import path, { dirname, join } from "node:path";
5
+ import { UNICODE_VERSIONS_WITH_UCD } from "@luxass/unicode-utils";
5
6
 
6
7
  //#region src/cmd/generate.ts
7
8
  const BASE_URL = "https://unicode-proxy.ucdjs.dev/proxy";
8
9
  async function runGenerate({ versions: providedVersions, flags }) {
9
- if (flags?.help || flags?.h || providedVersions.length === 0) {
10
+ if (flags?.help || flags?.h) {
10
11
  printHelp({
11
12
  headline: "Generate Unicode Data Files",
12
13
  commandName: "ucd generate",
@@ -15,6 +16,16 @@ async function runGenerate({ versions: providedVersions, flags }) {
15
16
  });
16
17
  return;
17
18
  }
19
+ if (providedVersions.length === 0) {
20
+ console.error("No versions provided. Please provide at least one version.");
21
+ return;
22
+ }
23
+ if (providedVersions[0] === "all") providedVersions = UNICODE_VERSIONS_WITH_UCD.map((v) => v.version);
24
+ const invalidVersions = providedVersions.filter((version) => !UNICODE_VERSIONS_WITH_UCD.find((v) => v.version === version));
25
+ if (invalidVersions.length > 0) {
26
+ console.error(`Invalid version(s) provided: ${invalidVersions.join(", ")}. Please provide valid Unicode versions.`);
27
+ return;
28
+ }
18
29
  const outputDir = flags.outputDir ?? path.join(process.cwd(), "data");
19
30
  await mkdir(outputDir, { recursive: true });
20
31
  async function processDirectory(entry, basePath, version, baseOutputDir, downloadedFiles) {
@@ -0,0 +1,33 @@
1
+ import { printHelp } from "./cli-utils-DyOk9RI0.js";
2
+
3
+ //#region src/cmd/codegen/root.ts
4
+ const CODEGEN_SUBCOMMANDS = ["fields"];
5
+ function isValidSubcommand(subcommand) {
6
+ return CODEGEN_SUBCOMMANDS.includes(subcommand);
7
+ }
8
+ async function runCodegenRoot(subcommand, { flags }) {
9
+ if (!isValidSubcommand(subcommand) || !isValidSubcommand(subcommand) && (flags?.help || flags?.h)) {
10
+ printHelp({
11
+ commandName: "ucd codegen",
12
+ usage: "[command] [...flags]",
13
+ tables: {
14
+ Commands: [["fields", "Generate fields for the Unicode data files."]],
15
+ Flags: [["--help (-h)", "See all available flags."]]
16
+ }
17
+ });
18
+ return;
19
+ }
20
+ if (subcommand === "fields") {
21
+ const { runFieldCodegen } = await import("./fields-BqikCPbE.js");
22
+ const inputPath = flags._.slice(4)?.toString() ?? "";
23
+ await runFieldCodegen({
24
+ inputPath,
25
+ flags
26
+ });
27
+ return;
28
+ }
29
+ throw new Error(`Invalid subcommand: ${subcommand}`);
30
+ }
31
+
32
+ //#endregion
33
+ export { runCodegenRoot };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ucdjs/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Lucas Nørgård",
@@ -25,17 +25,19 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@luxass/unicode-utils": "^0.0.0",
28
+ "@luxass/unicode-utils": "^0.4.1",
29
+ "@luxass/utils": "^1.4.0",
29
30
  "farver": "^0.4.2",
30
- "yargs-parser": "^21.1.1"
31
+ "yargs-parser": "^21.1.1",
32
+ "@ucdjs/schema-gen": "0.1.0"
31
33
  },
32
34
  "devDependencies": {
33
35
  "@luxass/eslint-config": "^4.18.1",
34
36
  "@types/yargs-parser": "^21.0.3",
35
- "eslint": "^9.23.0",
37
+ "eslint": "^9.25.1",
36
38
  "publint": "^0.3.12",
37
39
  "tsdown": "v0.9.3",
38
- "typescript": "^5.8.2",
40
+ "typescript": "^5.8.3",
39
41
  "vitest-testdirs": "^3.0.1"
40
42
  },
41
43
  "publishConfig": {