Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions args.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const argv = require('yargs-parser')
const parseArgs = require('./lib/parse-args')
const { requireModule } = require('./util')
const { loadEnvQuitely } = require('./env-loader')

Expand All @@ -21,42 +21,55 @@ const DEFAULT_ARGUMENTS = {
commonPrefix: false
}

module.exports = function parseArgs (args) {
const CLI_OPTIONS = {
port: { type: 'string', short: 'p' },
'inspect-port': { type: 'string' },
'body-limit': { type: 'string' },
'plugin-timeout': { type: 'string', short: 'T' },
'close-grace-delay': { type: 'string', short: 'g' },
'trust-proxy-hop': { type: 'string' },
'log-level': { type: 'string', short: 'l' },
address: { type: 'string', short: 'a' },
socket: { type: 'string', short: 's' },
prefix: { type: 'string', short: 'x' },
'ignore-watch': { type: 'string' },
'logging-module': { type: 'string', short: 'L' },
'debug-host': { type: 'string' },
lang: { type: 'string' },
require: { type: 'string', short: 'r' },
import: { type: 'string', short: 'i' },
config: { type: 'string', short: 'c' },
method: { type: 'string' },
'trust-proxy-ips': { type: 'string' },
'follow-watch': { type: 'string' },
'pretty-logs': { type: 'boolean', short: 'P' },
options: { type: 'boolean', short: 'o' },
watch: { type: 'boolean', short: 'w' },
'verbose-watch': { type: 'boolean', short: 'V' },
debug: { type: 'boolean', short: 'd' },
standardlint: { type: 'boolean' },
'common-prefix': { type: 'boolean' },
'include-hooks': { type: 'boolean' },
'trust-proxy-enabled': { type: 'boolean' },
help: { type: 'boolean', short: 'h' },
'debug-port': { type: 'string', short: 'I' }
}

module.exports = function parseCliArgs (args) {
loadEnvQuitely()
const commandLineArguments = argv(args, {
configuration: {
'populate--': true
},
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay', 'trust-proxy-hop'],
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'import', 'config', 'method', 'trust-proxy-ips', 'follow-watch'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks', 'trust-proxy-enabled'],
const commandLineArguments = parseArgs(args, {
populateRest: true,
envPrefix: 'FASTIFY_',
alias: {
port: ['p'],
socket: ['s'],
help: ['h'],
config: ['c'],
options: ['o'],
address: ['a'],
watch: ['w'],
prefix: ['x'],
require: ['r'],
import: ['i'],
debug: ['d'],
'debug-port': ['I'],
'log-level': ['l'],
'pretty-logs': ['P'],
'plugin-timeout': ['T'],
'close-grace-delay': ['g'],
'logging-module': ['L'],
'verbose-watch': ['V']
}
tokenize: true,
coerceNumbers: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay', 'trust-proxy-hop', 'debug-port'],
options: CLI_OPTIONS
})

const configFileOptions = commandLineArguments.config ? requireModule(commandLineArguments.config) : undefined

const additionalArgs = commandLineArguments['--'] || []
const { _, ...pluginOptions } = argv(additionalArgs)
const pluginParsed = parseArgs(additionalArgs, { options: {}, strict: false })
const { _, ...pluginOptions } = pluginParsed
const ignoreWatchArg = commandLineArguments.ignoreWatch || configFileOptions?.ignoreWatch || ''
const followWatchArg = commandLineArguments.followWatch || configFileOptions?.followWatch || ''
let ignoreWatch = `${DEFAULT_IGNORE} ${ignoreWatchArg}`.trim()
Expand All @@ -76,6 +89,7 @@ module.exports = function parseArgs (args) {
return {
_: parsedArgs._,
'--': additionalArgs,
help: parsedArgs.help,
port: parsedArgs.port,
bodyLimit: parsedArgs.bodyLimit,
pluginTimeout: parsedArgs.pluginTimeout,
Expand Down
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

const path = require('node:path')
const commist = require('commist')()
const argv = require('yargs-parser')(process.argv)
const { parseArgs } = require('node:util')
const argv = parseArgs({ args: process.argv.slice(2), strict: false, allowPositionals: true }).values
const help = require('help-me')({
// the default
dir: path.join(path.dirname(require.main.filename), 'help')
Expand Down
9 changes: 7 additions & 2 deletions eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('node:path')
const generify = require('generify')
const argv = require('yargs-parser')
const parseArgs = require('./lib/parse-args')
const log = require('./log')

function eject (dir, template) {
Expand All @@ -19,7 +19,12 @@ function eject (dir, template) {
}

function cli (args) {
const opts = argv(args)
const opts = parseArgs(args, {
options: {
lang: { type: 'string' },
esm: { type: 'boolean' }
}
})

let template
if (opts.lang === 'ts' || opts.lang === 'typescript') {
Expand Down
8 changes: 6 additions & 2 deletions generate-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { existsSync } = require('node:fs')
const path = require('node:path')
const chalk = require('chalk')
const generify = require('generify')
const argv = require('yargs-parser')
const parseArgs = require('./lib/parse-args')
const cliPkg = require('./package')
const { execSync } = require('node:child_process')
const { promisify } = require('node:util')
Expand Down Expand Up @@ -79,7 +79,11 @@ async function generate (dir, template) {
}

function cli (args) {
const opts = argv(args)
const opts = parseArgs(args, {
options: {
integrate: { type: 'boolean' }
}
})
const dir = opts._[0]

if (dir && existsSync(dir)) {
Expand Down
8 changes: 6 additions & 2 deletions generate-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { readFileSync, existsSync } = require('node:fs')
const path = require('node:path')
const generify = require('generify')
const argv = require('yargs-parser')
const parseArgs = require('./lib/parse-args')
const { execSync } = require('node:child_process')
const log = require('./log')

Expand Down Expand Up @@ -86,7 +86,11 @@ function showHelp () {
}

function cli (args) {
const opts = argv(args)
const opts = parseArgs(args, {
options: {
help: { type: 'boolean' }
}
})

const dir = process.cwd()

Expand Down
8 changes: 6 additions & 2 deletions generate-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'

const parseArgs = require('./args')
const argv = require('yargs-parser')
const parseArgsLib = require('./lib/parse-args')
const log = require('./log')
const {
exit,
Expand All @@ -26,7 +26,11 @@ function loadModules (opts) {

async function generateSwagger (args) {
const opts = parseArgs(args)
const extraOpts = argv(args)
const extraOpts = parseArgsLib(args, {
options: {
yaml: { type: 'boolean' }
}
})
if (opts.help) {
return showHelpForCommand('generate-swagger')
}
Expand Down
11 changes: 9 additions & 2 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
const path = require('node:path')
const chalk = require('chalk')
const generify = require('generify')
const argv = require('yargs-parser')
const parseArgs = require('./lib/parse-args')
const cliPkg = require('./package')
const { execSync } = require('node:child_process')
const log = require('./log')
Expand Down Expand Up @@ -134,7 +134,14 @@ function generate (dir, template) {
}

function cli (args) {
const opts = argv(args)
const opts = parseArgs(args, {
options: {
lang: { type: 'string' },
esm: { type: 'boolean' },
standardlint: { type: 'boolean' },
integrate: { type: 'boolean' }
}
})
const dir = opts._[0]

if (dir && existsSync(dir)) {
Expand Down
Loading
Loading