-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcli.js
executable file
·66 lines (56 loc) · 1.58 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
import parse from "node:path";
import process from "node:process";
import chalk from "chalk";
import { globby } from "globby";
import meow from "meow";
import { readJson, toOpenApi, writeYaml } from "./src/index.js";
const cli = meow(
`
Usage
$ mock-to-openapi <input> <output>
Options
--write, -w Write YAML output to files (default: true)
--verbose, -v Verbose output
Examples
$ mock-to-openapi ~/Downloads/*.json ~/Downloads/API
`,
{
importMeta: import.meta,
flags: {
verbose: { type: "boolean", shortFlag: "v" },
write: { type: "boolean", default: true, shortFlag: "w" },
},
},
);
// Check if input was set
if (cli.input.length === 0) {
cli.showHelp(2);
}
const verbose = (message) => {
if (cli.flags.verbose) {
console.log(chalk.gray(message));
}
};
// Search for json files
const files = await globby(cli.input[0], { expandDirectories: { extensions: ["json"] } });
// Process JSON files
if (files.length > 0) {
verbose(`Found ${files.length} JSON files`);
for (const file of files) {
verbose(`Processing ${file}`);
verbose(` - reading JSON data from ${file}`);
const data = await readJson(file);
const openapi = toOpenApi(data);
// Write output
if (cli.flags.write) {
const output = `${cli.input[1] || parse(file).dir}/${parse(file).name}.yaml`;
verbose(` - writing output to ${output}`);
await writeYaml(output, openapi);
}
}
console.log(chalk.green(`Done! ${files.length} JSON file(s) successfully processed`));
} else {
console.error(chalk.red("ERROR: I didn't find any json files"));
process.exit(1);
}