@yunarch/config-web 0.7.4 → 0.7.5
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as n,b as a,c as
|
|
2
|
+
import{a as n,b as a,c as w}from"../chunk-66TS4QAV.js";import{writeFile as B}from"fs/promises";import{styleText as o}from"util";import F from"@inquirer/confirm";async function P(e,t){await n("npx",["openapi-typescript-codegen","--input",e,"--output",t,"--client","fetch"],{shell:!0})}import{writeFile as b}from"fs/promises";var x=`
|
|
3
3
|
import {
|
|
4
4
|
http as mswHttp,
|
|
5
5
|
type DefaultBodyType,
|
|
@@ -122,22 +122,22 @@ export function http<P extends keyof Paths, M extends Methods<P>>(
|
|
|
122
122
|
}
|
|
123
123
|
return handlers[method as keyof typeof handlers](uri, resolver, options);
|
|
124
124
|
}
|
|
125
|
-
`;async function T(e){await b(`${e}/openapi-msw-http.ts`,
|
|
126
|
-
${
|
|
125
|
+
`;async function T(e){await b(`${e}/openapi-msw-http.ts`,x)}import{readFile as C,writeFile as $}from"fs/promises";async function O(e,t){await n("npx",["openapi-typescript",e,"-o",t],{shell:!0});let s=await C(t,"utf8");await $(t,`/* eslint-disable -- Autogenerated file */
|
|
126
|
+
${s}`)}import{existsSync as S}from"fs";import{mkdir as D}from"fs/promises";import h from"path";async function k(e){if(h.extname(e)!=="")throw new Error("Output must be a directory.");let t=process.cwd(),s=h.resolve(e),d=s.startsWith(t)?s:h.resolve(t,h.relative(h.parse(e).root,e));return S(d)||await a({name:"Generating output directory",command:async()=>{await D(d,{recursive:!0})}}),d}import{existsSync as M}from"fs";import{readFile as v}from"fs/promises";async function I(e){if(!e.endsWith(".json"))throw new Error(`Input file must be a JSON file: ${e}`);if(e.startsWith("http"))try{let{stdout:t}=await n("curl",["-s",e,"--fail"],{encoding:"utf8"});return t}catch{throw new Error(`Failed to fetch remote OpenAPI file: ${e}`)}if(!M(e))throw new Error(`Input file does not exist: ${e}`);return await v(e,"utf8")}async function A(e){if(!e.endsWith(".json"))throw new Error(`Output file must be a JSON file: ${e}`);return M(e)?await v(e,"utf8"):!1}async function R(e,t){return await a({name:"Reading OpenAPI schemas",command:async()=>{let[s,r]=await Promise.all([I(e),A(t)]);return[JSON.stringify(JSON.parse(s)),r?JSON.stringify(JSON.parse(r)):!1]}})}w().name("openapi-sync").description("A CLI tool to convert OpenAPI 3.0/3.1 schemas to TypeScript types and create type-safe fetching based on a openapi file and keep them in sync.").requiredOption("-i, --input <path>","The input (local or remote) openapi schema (JSON).").requiredOption("-o, --output <folder>","The output folder to save the generated models and openapi schema and type definitions.").option("-y, --yes","Skip confirmation prompts and proceed with defaults.").option("-f, --force-gen","Force generation of typescript schemas and fetching code even if the input and output schemas are identical.").option("--include-msw-utils","Include MSW mocking utilities based on the generated typescript types.").option("--post-script <script>","A package.json script to run after the code generation.").option("--verify-openapi-sync","Verifies that the generated output is up to date with the input (e.g., in CI) to catch outdated or mismatched output without making changes.").addHelpText("after",`
|
|
127
127
|
Example usage:
|
|
128
|
-
${
|
|
129
|
-
`).action(async({input:e,output:
|
|
128
|
+
${o("dim","$")} ${o("cyan","openapi-sync")} ${o("green","-i")} ${o("yellow","./openapi.json")} ${o("green","-o")} ${o("yellow","./src/api/gen")} ${o("green","--include-msw-utils")}
|
|
129
|
+
`).action(async({input:e,output:t,yes:s,forceGen:r,verifyOpenapiSync:d,includeMswUtils:E,postScript:f})=>{try{console.log(o("magenta",`
|
|
130
130
|
\u{1F680} openapi-sync
|
|
131
|
-
`));let
|
|
131
|
+
`));let p=await k(t),m=`${p}/openapi.json`,H=`${p}/schema.d.ts`,[u,g]=await R(e,m),l=u!==g;d&&(console.log(l?o("yellow",`
|
|
132
132
|
\u26A0\uFE0F Local and remote schemas does not match!
|
|
133
|
-
`):
|
|
133
|
+
`):o("green",`
|
|
134
134
|
\u2705 Local and remote schemas match!
|
|
135
|
-
`)),process.exit(l?1:0)),g===!1&&await a({name:"Creating local schema",command:B(
|
|
135
|
+
`)),process.exit(l?1:0)),g===!1&&await a({name:"Creating local schema",command:B(m,u)}),!l&&!r?(console.log(o("blue",`
|
|
136
136
|
No updates required.
|
|
137
|
-
`)),process.exit(0)):l&&(console.log(
|
|
137
|
+
`)),process.exit(0)):l&&(console.log(o("yellow",`
|
|
138
138
|
\u26A0\uFE0F Local and remote schemas does not match!
|
|
139
|
-
`)),
|
|
139
|
+
`)),s||await F({message:"Do you want to use the remote schema? (y/n)?"})?await a({name:"Replacing local schema with input schema",command:B(m,u)}):(console.log(o("yellow",`
|
|
140
140
|
\u26A0\uFE0F Sync remote schemas skipped.
|
|
141
|
-
`)),
|
|
141
|
+
`)),r||process.exit(0))),await a({name:"Generating code from OpenAPI schema",command:async()=>{let y=[O(m,H),P(m,p)];E&&y.push(T(p)),await Promise.all(y)}}),f&&await a({name:"Running post script",command:async()=>{try{await(typeof Bun<"u"?n("bun",["run",f]):n("node",["--run",f]))}catch{await n("npm",["run",f],{shell:!0})}}}),console.log(o("green",`
|
|
142
142
|
\u2705 openapi-sync process completed!
|
|
143
|
-
`))}catch(
|
|
143
|
+
`))}catch(p){console.error(p),process.exit(1)}}).parseAsync(process.argv);
|