@saltify/milky-generator 0.2.1 → 0.3.1

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 (3) hide show
  1. package/README.md +17 -6
  2. package/dist/cli.mjs +49 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -16,14 +16,10 @@ milkygen
16
16
 
17
17
  ## 命令
18
18
 
19
- ### 查看可用 generator
20
-
21
- ```
22
- milkygen list
23
- ```
24
-
25
19
  ### 生成内容
26
20
 
21
+ > 别名:`g`, `gen`
22
+
27
23
  ```
28
24
  milkygen generate <generator> [--output <file>] [--version <version>]
29
25
  ```
@@ -44,6 +40,21 @@ milkygen generate rust/serde --version 1.2.1
44
40
  - `latest`: 默认值,使用 npm `latest` 标签对应版本的协议定义。
45
41
  - 具体版本号: **只能使用 `1.2.1` 及以上版本**,因为协议定义在 `1.2.1` 版本中才正式发布。
46
42
  - `local`: 使用当前包依赖中的本地协议定义。不建议使用,因为可能与发布版本不一致。
43
+ - `--cdn`: 指定从哪个 CDN 获取协议定义,默认为 `unpkg`。可用值请使用 `milkygen list-cdns` 命令查看。
44
+
45
+ ### 查看可用 generator
46
+
47
+ > 别名:`l`
48
+
49
+ ```
50
+ milkygen list
51
+ ```
52
+
53
+ ### 查看可用 CDN
54
+
55
+ ```
56
+ milkygen list-cdns
57
+ ```
47
58
 
48
59
  ### 查看 CLI 版本
49
60
 
package/dist/cli.mjs CHANGED
@@ -29461,7 +29461,7 @@ var import_cjs = (/* @__PURE__ */ __commonJSMin(((exports) => {
29461
29461
  }
29462
29462
  });
29463
29463
  })))();
29464
- var version = "0.2.1";
29464
+ var version = "0.3.1";
29465
29465
  var description = "Milky codegen CLI";
29466
29466
  //#endregion
29467
29467
  //#region src/cli.ts
@@ -29499,11 +29499,24 @@ const generators = [
29499
29499
  generate: generateTypeScriptZodSpec
29500
29500
  }
29501
29501
  ];
29502
- async function resolveProtocolOfVersion(version) {
29503
- if (version === "latest") return await fetch("https://cdn.jsdelivr.net/npm/@saltify/milky-protocol/dist/protocol.json").then((res) => res.json());
29504
- else if (version === "local") return ir;
29502
+ const cdns = [
29503
+ {
29504
+ name: "unpkg",
29505
+ generateUrl: (packageName, version) => `https://unpkg.com/${packageName}@${version}/dist/protocol.json`
29506
+ },
29507
+ {
29508
+ name: "esmsh",
29509
+ generateUrl: (packageName, version) => `https://esm.sh/${packageName}@${version}/dist/protocol.json`
29510
+ },
29511
+ {
29512
+ name: "jsdelivr",
29513
+ generateUrl: (packageName, version) => `https://cdn.jsdelivr.net/npm/${packageName}@${version}/dist/protocol.json`
29514
+ }
29515
+ ];
29516
+ async function resolveProtocolOfVersion(version, cdn) {
29517
+ if (version === "local") return ir;
29505
29518
  else {
29506
- const response = await fetch(`https://cdn.jsdelivr.net/npm/@saltify/milky-protocol@${version}/dist/protocol.json`);
29519
+ const response = await fetch(cdn.generateUrl("@saltify/milky-protocol", version));
29507
29520
  if (!response.ok) throw new Error(`Failed to fetch protocol of version ${version}: ${response.status} ${response.statusText}`);
29508
29521
  return await response.json();
29509
29522
  }
@@ -29518,25 +29531,33 @@ function serializeOutput(output) {
29518
29531
  cmds: {
29519
29532
  generate: (0, import_cjs.command)({
29520
29533
  name: "generate",
29534
+ aliases: ["g", "gen"],
29521
29535
  description: "Generate a spec from Milky IR.",
29522
29536
  args: {
29523
29537
  generator: (0, import_cjs.positional)({
29538
+ displayName: "generator",
29524
29539
  type: import_cjs.string,
29525
- description: "The canonical name of the generator to run. Use \"list\" to see all available generators."
29540
+ description: "Name of the generator to run. Use \"list\" for all available generators."
29526
29541
  }),
29527
29542
  output: (0, import_cjs.option)({
29528
29543
  type: import_cjs.string,
29529
29544
  long: "output",
29530
29545
  short: "o",
29531
- description: "The output file path. If not specified, the generated spec will be printed to stdout.",
29546
+ description: "The output file path. The generator writes to stdout if not provided.",
29532
29547
  defaultValue: () => ""
29533
29548
  }),
29534
29549
  version: (0, import_cjs.option)({
29535
29550
  type: import_cjs.string,
29536
29551
  long: "version",
29537
29552
  short: "v",
29538
- description: "The version of the Milky IR to use. If not specified, the latest version (the version published with `latest` tag on npm) will be used.",
29553
+ description: "The version of the Milky IR to use. Latest stable version if not specified.",
29539
29554
  defaultValue: () => "latest"
29555
+ }),
29556
+ cdn: (0, import_cjs.option)({
29557
+ type: import_cjs.string,
29558
+ long: "cdn",
29559
+ description: `The CDN to fetch the protocol definition from. "${cdns[0].name}" if not specified.`,
29560
+ defaultValue: () => cdns[0].name
29540
29561
  })
29541
29562
  },
29542
29563
  handler: async (args) => {
@@ -29546,7 +29567,13 @@ function serializeOutput(output) {
29546
29567
  console.error("Use \"milkygen list\" to see all available generators.");
29547
29568
  process.exit(1);
29548
29569
  }
29549
- const protocol = await resolveProtocolOfVersion(args.version);
29570
+ const cdnSpec = cdns.find((c) => c.name === args.cdn);
29571
+ if (!cdnSpec) {
29572
+ console.error(`Unknown CDN: ${args.cdn}`);
29573
+ console.error(`Use "milkygen list-cdns" to see all supported CDNs.`);
29574
+ process.exit(1);
29575
+ }
29576
+ const protocol = await resolveProtocolOfVersion(args.version, cdnSpec);
29550
29577
  const content = serializeOutput(await spec.generate(protocol));
29551
29578
  if (args.output.length === 0) {
29552
29579
  process.stdout.write(content);
@@ -29563,6 +29590,7 @@ function serializeOutput(output) {
29563
29590
  }),
29564
29591
  list: (0, import_cjs.command)({
29565
29592
  name: "list",
29593
+ aliases: ["l"],
29566
29594
  description: "List all available generators.",
29567
29595
  args: {},
29568
29596
  handler: () => {
@@ -29572,8 +29600,20 @@ function serializeOutput(output) {
29572
29600
  });
29573
29601
  }
29574
29602
  }),
29603
+ "list-cdns": (0, import_cjs.command)({
29604
+ name: "list-cdns",
29605
+ description: "List all supported CDNs to fetch protocol definitions from.",
29606
+ args: {},
29607
+ handler: () => {
29608
+ console.log("Supported CDNs:");
29609
+ cdns.forEach((cdn) => {
29610
+ console.log(` - ${cdn.name}`);
29611
+ });
29612
+ }
29613
+ }),
29575
29614
  version: (0, import_cjs.command)({
29576
29615
  name: "version",
29616
+ aliases: ["v"],
29577
29617
  description: "Print the version of milkygen.",
29578
29618
  args: {},
29579
29619
  handler: () => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@saltify/milky-generator",
3
3
  "type": "module",
4
- "version": "0.2.1",
4
+ "version": "0.3.1",
5
5
  "description": "Milky codegen CLI",
6
6
  "bin": {
7
7
  "milkygen": "dist/cli.mjs"