@saltify/milky-generator 0.2.0 → 0.3.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/dist/cli.mjs +68 -17
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -58,9 +58,7 @@ function getApiTypeNames(endpoint) {
|
|
|
58
58
|
return {
|
|
59
59
|
pascalEndpoint,
|
|
60
60
|
inputName: `${pascalEndpoint}Input`,
|
|
61
|
-
outputName: `${pascalEndpoint}Output
|
|
62
|
-
requestName: `${pascalEndpoint}Request`,
|
|
63
|
-
responseName: `${pascalEndpoint}Response`
|
|
61
|
+
outputName: `${pascalEndpoint}Output`
|
|
64
62
|
};
|
|
65
63
|
}
|
|
66
64
|
function isSameField(first, second) {
|
|
@@ -24919,26 +24917,42 @@ function generateTypeScriptStaticSpec(ir) {
|
|
|
24919
24917
|
const typeNames = getApiTypeNames(api.endpoint);
|
|
24920
24918
|
l(`/** ${api.description} API 请求参数 */`);
|
|
24921
24919
|
if (api.requestFields !== void 0) {
|
|
24922
|
-
l(`export interface ${typeNames.
|
|
24920
|
+
l(`export interface ${typeNames.inputName} {`);
|
|
24923
24921
|
api.requestFields.forEach((field) => {
|
|
24924
24922
|
l(` /** ${field.description} */`);
|
|
24925
24923
|
l(` ${field.name}${field.isOptional ? "?" : ""}: ${getTypeScriptTypeProjection(field)};`);
|
|
24926
24924
|
});
|
|
24927
24925
|
l("}");
|
|
24928
|
-
} else l(`export type ${typeNames.
|
|
24926
|
+
} else l(`export type ${typeNames.inputName} = {};`);
|
|
24929
24927
|
l();
|
|
24930
24928
|
l(`/** ${api.description} API 响应数据 */`);
|
|
24931
24929
|
if (api.responseFields !== void 0) {
|
|
24932
|
-
l(`export interface ${typeNames.
|
|
24930
|
+
l(`export interface ${typeNames.outputName} {`);
|
|
24933
24931
|
api.responseFields.forEach((field) => {
|
|
24934
24932
|
l(` /** ${field.description} */`);
|
|
24935
24933
|
l(` ${field.name}${field.isOptional ? "?" : ""}: ${getTypeScriptTypeProjection(field)};`);
|
|
24936
24934
|
});
|
|
24937
24935
|
l("}");
|
|
24938
|
-
} else l(`export type ${typeNames.
|
|
24936
|
+
} else l(`export type ${typeNames.outputName} = {};`);
|
|
24939
24937
|
l();
|
|
24940
24938
|
});
|
|
24941
24939
|
});
|
|
24940
|
+
l("export interface ApiCategories {");
|
|
24941
|
+
ir.apiCategories.forEach((category) => {
|
|
24942
|
+
l(` /** ${category.name} */`);
|
|
24943
|
+
l(` ${category.key}: {`);
|
|
24944
|
+
category.apis.forEach((api) => {
|
|
24945
|
+
const typeNames = getApiTypeNames(api.endpoint);
|
|
24946
|
+
l(` /** ${api.description} */`);
|
|
24947
|
+
l(` ${api.endpoint}: {`);
|
|
24948
|
+
l(` request: ${typeNames.inputName};`);
|
|
24949
|
+
l(` response: ${typeNames.outputName};`);
|
|
24950
|
+
l(" };");
|
|
24951
|
+
});
|
|
24952
|
+
l(" };");
|
|
24953
|
+
});
|
|
24954
|
+
l("}");
|
|
24955
|
+
l();
|
|
24942
24956
|
return writer.toString();
|
|
24943
24957
|
}
|
|
24944
24958
|
//#endregion
|
|
@@ -25679,7 +25693,7 @@ const apiCategories = [
|
|
|
25679
25693
|
];
|
|
25680
25694
|
//#endregion
|
|
25681
25695
|
//#region ../protocol/package.json
|
|
25682
|
-
var version$1 = "1.2.1
|
|
25696
|
+
var version$1 = "1.2.1";
|
|
25683
25697
|
//#endregion
|
|
25684
25698
|
//#region ../protocol/src/index.ts
|
|
25685
25699
|
const ir = {
|
|
@@ -29447,7 +29461,7 @@ var import_cjs = (/* @__PURE__ */ __commonJSMin(((exports) => {
|
|
|
29447
29461
|
}
|
|
29448
29462
|
});
|
|
29449
29463
|
})))();
|
|
29450
|
-
var version = "0.
|
|
29464
|
+
var version = "0.3.0";
|
|
29451
29465
|
var description = "Milky codegen CLI";
|
|
29452
29466
|
//#endregion
|
|
29453
29467
|
//#region src/cli.ts
|
|
@@ -29485,11 +29499,24 @@ const generators = [
|
|
|
29485
29499
|
generate: generateTypeScriptZodSpec
|
|
29486
29500
|
}
|
|
29487
29501
|
];
|
|
29488
|
-
|
|
29489
|
-
|
|
29490
|
-
|
|
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;
|
|
29491
29518
|
else {
|
|
29492
|
-
const response = await fetch(
|
|
29519
|
+
const response = await fetch(cdn.generateUrl("@saltify/milky-protocol", version));
|
|
29493
29520
|
if (!response.ok) throw new Error(`Failed to fetch protocol of version ${version}: ${response.status} ${response.statusText}`);
|
|
29494
29521
|
return await response.json();
|
|
29495
29522
|
}
|
|
@@ -29507,22 +29534,29 @@ function serializeOutput(output) {
|
|
|
29507
29534
|
description: "Generate a spec from Milky IR.",
|
|
29508
29535
|
args: {
|
|
29509
29536
|
generator: (0, import_cjs.positional)({
|
|
29537
|
+
displayName: "generator",
|
|
29510
29538
|
type: import_cjs.string,
|
|
29511
|
-
description: "
|
|
29539
|
+
description: "Name of the generator to run. Use \"list\" for all available generators."
|
|
29512
29540
|
}),
|
|
29513
29541
|
output: (0, import_cjs.option)({
|
|
29514
29542
|
type: import_cjs.string,
|
|
29515
29543
|
long: "output",
|
|
29516
29544
|
short: "o",
|
|
29517
|
-
description: "The output file path.
|
|
29545
|
+
description: "The output file path. The generator writes to stdout if not provided.",
|
|
29518
29546
|
defaultValue: () => ""
|
|
29519
29547
|
}),
|
|
29520
29548
|
version: (0, import_cjs.option)({
|
|
29521
29549
|
type: import_cjs.string,
|
|
29522
29550
|
long: "version",
|
|
29523
29551
|
short: "v",
|
|
29524
|
-
description: "The version of the Milky IR to use.
|
|
29552
|
+
description: "The version of the Milky IR to use. Latest stable version if not specified.",
|
|
29525
29553
|
defaultValue: () => "latest"
|
|
29554
|
+
}),
|
|
29555
|
+
cdn: (0, import_cjs.option)({
|
|
29556
|
+
type: import_cjs.string,
|
|
29557
|
+
long: "cdn",
|
|
29558
|
+
description: `The CDN to fetch the protocol definition from. "${cdns[0].name}" if not specified.`,
|
|
29559
|
+
defaultValue: () => cdns[0].name
|
|
29526
29560
|
})
|
|
29527
29561
|
},
|
|
29528
29562
|
handler: async (args) => {
|
|
@@ -29532,7 +29566,13 @@ function serializeOutput(output) {
|
|
|
29532
29566
|
console.error("Use \"milkygen list\" to see all available generators.");
|
|
29533
29567
|
process.exit(1);
|
|
29534
29568
|
}
|
|
29535
|
-
const
|
|
29569
|
+
const cdnSpec = cdns.find((c) => c.name === args.cdn);
|
|
29570
|
+
if (!cdnSpec) {
|
|
29571
|
+
console.error(`Unknown CDN: ${args.cdn}`);
|
|
29572
|
+
console.error(`Use "milkygen list-cdns" to see all supported CDNs.`);
|
|
29573
|
+
process.exit(1);
|
|
29574
|
+
}
|
|
29575
|
+
const protocol = await resolveProtocolOfVersion(args.version, cdnSpec);
|
|
29536
29576
|
const content = serializeOutput(await spec.generate(protocol));
|
|
29537
29577
|
if (args.output.length === 0) {
|
|
29538
29578
|
process.stdout.write(content);
|
|
@@ -29558,6 +29598,17 @@ function serializeOutput(output) {
|
|
|
29558
29598
|
});
|
|
29559
29599
|
}
|
|
29560
29600
|
}),
|
|
29601
|
+
"list-cdns": (0, import_cjs.command)({
|
|
29602
|
+
name: "list-cdns",
|
|
29603
|
+
description: "List all supported CDNs to fetch protocol definitions from.",
|
|
29604
|
+
args: {},
|
|
29605
|
+
handler: () => {
|
|
29606
|
+
console.log("Supported CDNs:");
|
|
29607
|
+
cdns.forEach((cdn) => {
|
|
29608
|
+
console.log(` - ${cdn.name}`);
|
|
29609
|
+
});
|
|
29610
|
+
}
|
|
29611
|
+
}),
|
|
29561
29612
|
version: (0, import_cjs.command)({
|
|
29562
29613
|
name: "version",
|
|
29563
29614
|
description: "Print the version of milkygen.",
|