|
1 | 1 | #!/usr/bin/env node
|
2 |
| -"use strict"; |
| 2 | +'use strict'; |
3 | 3 |
|
4 |
| -const yargs = require("yargs"); |
5 |
| -const chalk = require("chalk"); |
6 |
| -const converter = require(".."); |
7 |
| -const helpText = require("./help-text.json"); |
| 4 | +const yargs = require('yargs'); |
| 5 | +const chalk = require('chalk'); |
| 6 | +const converter = require('../dist/cjs/index.js').default; |
| 7 | +const helpText = require('./help-text.json'); |
8 | 8 | const fs = require('fs');
|
9 | 9 | const readFileAsync = require('util').promisify(fs.readFile);
|
10 | 10 |
|
11 |
| -(function main () { |
12 |
| - let args = parseArgs(); |
13 |
| - let command = args.command; |
14 |
| - let file = args.file; |
15 |
| - let options = args.options; |
16 |
| - |
17 |
| - if (options.help) { |
18 |
| - // Show help text and exit |
19 |
| - console.log(getHelpText(command)); |
20 |
| - process.exit(0); |
21 |
| - } |
22 |
| - else if (command === "convert" && file) { |
23 |
| - // Convert the JSON Schema file |
24 |
| - convert(file, options); |
25 |
| - } |
26 |
| - else { |
27 |
| - // Invalid args. Show help text and exit with non-zero |
28 |
| - console.error("Error: Invalid arguments\n"); |
29 |
| - console.error(getHelpText(command)); |
30 |
| - process.exit(1); |
31 |
| - } |
32 |
| -}()); |
33 |
| - |
| 11 | +(async function main() { |
| 12 | + let args = parseArgs(); |
| 13 | + let command = args.command; |
| 14 | + let file = args.file; |
| 15 | + let options = args.options; |
| 16 | + |
| 17 | + if (options.help) { |
| 18 | + // Show help text and exit |
| 19 | + console.log(getHelpText(command)); |
| 20 | + process.exit(0); |
| 21 | + } else if (command === 'convert' && file) { |
| 22 | + // Convert the JSON Schema file |
| 23 | + await convert(file, options); |
| 24 | + } else { |
| 25 | + // Invalid args. Show help text and exit with non-zero |
| 26 | + console.error('Error: Invalid arguments\n'); |
| 27 | + console.error(getHelpText(command)); |
| 28 | + process.exit(1); |
| 29 | + } |
| 30 | +})(); |
34 | 31 |
|
35 | 32 | /**
|
36 | 33 | * Parses the command-line arguments
|
37 | 34 | *
|
38 | 35 | * @returns {object} - The parsed arguments
|
39 | 36 | */
|
40 |
| -function parseArgs () { |
41 |
| - // Configure the argument parser |
42 |
| - yargs |
43 |
| - .option("d", { |
44 |
| - alias: "dereference", |
45 |
| - type: "boolean", |
46 |
| - default: false, |
47 |
| - }) |
48 |
| - .option("h", { |
49 |
| - alias: "help", |
50 |
| - type: "boolean", |
51 |
| - }); |
52 |
| - |
53 |
| - // Show the version number on "--version" or "-v" |
54 |
| - yargs |
55 |
| - .version() |
56 |
| - .alias("v", "version"); |
57 |
| - |
58 |
| - // Disable the default "--help" behavior |
59 |
| - yargs.help(false); |
60 |
| - |
61 |
| - // Parse the command-line arguments |
62 |
| - let args = yargs.argv; |
63 |
| - |
64 |
| - // Normalize the parsed arguments |
65 |
| - let parsed = { |
66 |
| - command: args._[0], |
67 |
| - file: args._[1], |
68 |
| - options: { |
69 |
| - dereference: args.dereference, |
70 |
| - help: args.help, |
71 |
| - } |
72 |
| - }; |
73 |
| - |
74 |
| - return parsed; |
| 37 | +function parseArgs() { |
| 38 | + // Configure the argument parser |
| 39 | + yargs |
| 40 | + .option('d', { |
| 41 | + alias: 'dereference', |
| 42 | + type: 'boolean', |
| 43 | + default: false, |
| 44 | + }) |
| 45 | + .option('h', { |
| 46 | + alias: 'help', |
| 47 | + type: 'boolean', |
| 48 | + }); |
| 49 | + |
| 50 | + // Show the version number on "--version" or "-v" |
| 51 | + yargs.version().alias('v', 'version'); |
| 52 | + |
| 53 | + // Disable the default "--help" behavior |
| 54 | + yargs.help(false); |
| 55 | + |
| 56 | + // Parse the command-line arguments |
| 57 | + let args = yargs.argv; |
| 58 | + |
| 59 | + // Normalize the parsed arguments |
| 60 | + let parsed = { |
| 61 | + command: args._[0], |
| 62 | + file: args._[1], |
| 63 | + options: { |
| 64 | + dereference: args.dereference, |
| 65 | + help: args.help, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + return parsed; |
75 | 70 | }
|
76 | 71 |
|
77 |
| - |
78 | 72 | /**
|
79 | 73 | * Convert an JSON Schema to OpenAPI schema
|
80 | 74 | *
|
81 | 75 | * @param {string} file - The path of the file to convert
|
82 | 76 | * @param {object} options - Conversion options
|
83 | 77 | */
|
84 |
| -async function convert (file, options) { |
85 |
| - try { |
86 |
| - const schema = await readFileAsync(file, 'utf8'); |
87 |
| - const converted = await converter(JSON.parse(schema), options) |
88 |
| - console.log(JSON.stringify(converted)); |
89 |
| - } |
90 |
| - catch (error) { |
91 |
| - errorHandler(error); |
92 |
| - } |
| 78 | +async function convert(file, options) { |
| 79 | + try { |
| 80 | + const schema = await readFileAsync(file, 'utf8'); |
| 81 | + const converted = await converter(JSON.parse(schema), options); |
| 82 | + console.log(JSON.stringify(converted)); |
| 83 | + } catch (error) { |
| 84 | + errorHandler(error); |
| 85 | + } |
93 | 86 | }
|
94 | 87 |
|
95 |
| - |
96 | 88 | /**
|
97 | 89 | * Returns the help text for the specified command
|
98 | 90 | *
|
99 | 91 | * @param {string} [commandName] - The command to show help text for
|
100 | 92 | * @returns {string} - the help text
|
101 | 93 | */
|
102 |
| -function getHelpText (commandName) { |
103 |
| - let lines = helpText[commandName] || helpText.default; |
104 |
| - return lines.join("\n"); |
| 94 | +function getHelpText(commandName) { |
| 95 | + let lines = helpText[commandName] || helpText.default; |
| 96 | + return lines.join('\n'); |
105 | 97 | }
|
106 | 98 |
|
107 |
| - |
108 | 99 | /**
|
109 | 100 | * Writes error information to stderr and exits with a non-zero code
|
110 | 101 | *
|
111 | 102 | * @param {Error} err
|
112 | 103 | */
|
113 |
| -function errorHandler (err) { |
114 |
| - let errorMessage = process.env.DEBUG ? err.stack : err.message; |
115 |
| - console.error(chalk.red(errorMessage)); |
116 |
| - process.exit(1); |
| 104 | +function errorHandler(err) { |
| 105 | + let errorMessage = process.env.DEBUG ? err.stack : err.message; |
| 106 | + console.error(chalk.red(errorMessage)); |
| 107 | + process.exit(1); |
117 | 108 | }
|
0 commit comments