Skip to content

Commit 5a33a49

Browse files
authored
Update deps, swap tiny-glob for fast-glob (#963)
1 parent 49cdb5c commit 5a33a49

File tree

4 files changed

+298
-945
lines changed

4 files changed

+298
-945
lines changed

bin/cli.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22

3-
import fs from "fs";
4-
import path from "path";
5-
import glob from "tiny-glob";
3+
import fs from "node:fs";
4+
import { URL } from "node:url";
5+
import glob from "fast-glob";
66
import parser from "yargs-parser";
77
import openapiTS from "../dist/index.js";
88

@@ -35,6 +35,8 @@ Options
3535

3636
const OUTPUT_FILE = "FILE";
3737
const OUTPUT_STDOUT = "STDOUT";
38+
const CWD = new URL(`file://${process.cwd()}/`);
39+
const EXT_RE = /\.[^.]+$/i;
3840

3941
const timeStart = process.hrtime();
4042

@@ -114,11 +116,11 @@ async function generateSchema(pathToSpec) {
114116

115117
// output
116118
if (output === OUTPUT_FILE) {
117-
let outputFilePath = path.resolve(process.cwd(), flags.output); // note: may be directory
119+
let outputFilePath = new URL(flags.output, CWD); // note: may be directory
118120
const isDir = fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isDirectory();
119121
if (isDir) {
120-
const filename = pathToSpec.replace(new RegExp(`${path.extname(pathToSpec)}$`), ".ts");
121-
outputFilePath = path.join(outputFilePath, filename);
122+
const filename = pathToSpec.replace(EXT_RE, ".ts");
123+
outputFilePath = new URL(filename, outputFilePath);
122124
}
123125

124126
fs.writeFileSync(outputFilePath, result, "utf8");
@@ -141,6 +143,9 @@ async function main() {
141143
}
142144

143145
let output = flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOUT
146+
let outputFile = new URL(flags.output, CWD);
147+
let outputDir = new URL(".", outputFile);
148+
144149
const pathToSpec = input;
145150

146151
if (output === OUTPUT_FILE) {
@@ -155,20 +160,20 @@ async function main() {
155160

156161
// handle remote schema, exit
157162
if (/^https?:\/\//.test(pathToSpec)) {
158-
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(path.dirname(flags.output), { recursive: true });
163+
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(outputDir, { recursive: true });
159164
await generateSchema(pathToSpec);
160165
return;
161166
}
162167

163168
// handle stdin schema, exit
164169
if (pathToSpec === "-") {
165-
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(path.dirname(flags.output), { recursive: true });
170+
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(outputDir, { recursive: true });
166171
await generateSchema(process.stdin);
167172
return;
168173
}
169174

170175
// handle local schema(s)
171-
const inputSpecPaths = await glob(pathToSpec, { filesOnly: true });
176+
const inputSpecPaths = await glob(pathToSpec);
172177
const isGlob = inputSpecPaths.length > 1;
173178

174179
// error: no matches for glob
@@ -177,20 +182,15 @@ async function main() {
177182
}
178183

179184
// error: tried to glob output to single file
180-
if (isGlob && output === OUTPUT_FILE && fs.existsSync(flags.output) && fs.lstatSync(flags.output).isFile()) {
185+
if (isGlob && output === OUTPUT_FILE && fs.existsSync(outputDir) && fs.lstatSync(outputDir).isFile()) {
181186
errorAndExit(`❌ Expected directory for --output if using glob patterns. Received "${flags.output}".`);
182187
}
183188

184189
// generate schema(s) in parallel
185190
await Promise.all(
186191
inputSpecPaths.map(async (specPath) => {
187192
if (flags.output !== "." && output === OUTPUT_FILE) {
188-
let outputDir = path.resolve(process.cwd(), flags.output);
189-
if (isGlob) {
190-
outputDir = path.resolve(outputDir, path.dirname(specPath)); // globs: use output dir + spec dir
191-
} else {
192-
outputDir = path.dirname(outputDir); // single files: just use output parent dir
193-
}
193+
if (isGlob) outputDir = new URL(specPath, outputDir);
194194
fs.mkdirSync(outputDir, { recursive: true }); // recursively make parent dirs
195195
}
196196
await generateSchema(specPath);

package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,27 @@
4646
"version": "npm run build"
4747
},
4848
"dependencies": {
49+
"fast-glob": "^3.2.12",
4950
"js-yaml": "^4.1.0",
5051
"mime": "^3.0.0",
5152
"prettier": "^2.7.1",
52-
"tiny-glob": "^0.2.9",
53-
"undici": "^5.10.0",
53+
"undici": "^5.12.0",
5454
"yargs-parser": "^21.1.1"
5555
},
5656
"devDependencies": {
5757
"@types/js-yaml": "^4.0.5",
5858
"@types/mime": "^2.0.3",
59-
"@types/node": "^17.0.45",
59+
"@types/node": "^18.11.7",
6060
"@types/prettier": "^2.7.1",
61-
"@typescript-eslint/eslint-plugin": "^5.38.1",
62-
"@typescript-eslint/parser": "^5.38.1",
63-
"@vitest/coverage-c8": "^0.23.4",
64-
"del-cli": "^4.0.1",
61+
"@typescript-eslint/eslint-plugin": "^5.41.0",
62+
"@typescript-eslint/parser": "^5.41.0",
63+
"@vitest/coverage-c8": "^0.24.3",
64+
"del-cli": "^5.0.0",
6565
"eol": "^0.9.1",
66-
"eslint": "^8.24.0",
66+
"eslint": "^8.26.0",
6767
"eslint-config-prettier": "^8.5.0",
6868
"eslint-plugin-prettier": "^4.2.1",
69-
"nyc": "^15.1.0",
7069
"typescript": "^4.8.4",
71-
"vitest": "^0.23.4"
70+
"vitest": "^0.24.3"
7271
}
7372
}

0 commit comments

Comments
 (0)