draig-car
Version:
Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL
145 lines (139 loc) • 3.85 kB
JavaScript
const path = require('path')
const yargs = require('yargs')
const u = require('./util')
const argv = yargs
.usage('Usage: $0 <init|repl|generate|serve|run>')
.command(
'init <project> <port> <dbClient> <dbName>',
'Create draig artifacts and YAML to initialize generation project',
yargs =>
yargs
.positional('project', {
description: 'API Project file name'
})
.positional('port', {
description: 'Port where the API will be reachable'
})
.positional('dbClient', {
description: 'Backend connector for the object relational mapper',
choices: ['mysql', 'sqlite3', 'pg', 'oracledb']
})
.positional('dbName', {
description:
"Name for the database (or file) name (use `XE' for oracledb)"
})
.option('generator', {
alias: 'g',
default: 'draig',
description: 'OpenAPI3 generator name (default: draig)'
})
.option('dbUserName', {
alias: 'u',
description: 'DB connection user'
})
.option('dbUserPassword', {
alias: 'p',
description: 'DB connection password'
})
.option('dbHost', {
alias: 'h',
default: 'localhost',
description: 'DB connection host'
}),
args => {
if (
args.dbClient !== 'sqlite3' &&
(!args.dbUserName || !args.dbUserPassword)
) {
yargs.showHelp()
console.log(
`\nSorry, ${args.dbClient} requires user and password to be specified`
)
process.exit(1)
}
if (
isNaN(args.port) ||
parseInt(args.port) < 1025 ||
parseInt(args.port) > 65535
) {
yargs.showHelp()
console.log('\nSorry, the port number is invalid (range 1024-65535)')
process.exit(1)
}
}
)
.command(
'repl [project-dir]',
'Open draig repl to begin generation or maintenance mode',
yargs =>
yargs.positional('project-dir', {
describe: 'Directory name where the api projects are generated'
})
)
.command(
'generate [project-dir]',
'Generate project with current configuration',
yargs =>
yargs
.positional('project-dir', {
describe: 'Directory name where the api projects are generated'
})
.option('noinstall', {
alias: 'i',
describe: "Don't execute install after generating",
type: 'boolean'
})
.option('template', {
alias: 't',
describe:
'Draig generator template folder to override default templates'
})
)
.command(
'clean',
'Clean (remove) all generated artifacts (including log files)',
)
.command(
'serve [project-dir]',
'Serve API from specified project-dir (if it is generated + installed)',
yargs =>
yargs.positional('project-dir', {
describe: 'Directory name where the api projects are generated'
})
)
.command(
'run <intcmd|list> [intargs..]',
'Open draig repl and run specified internal command with optional args',
yargs =>
yargs
.positional('intcmd', {
describe:
'Internal command to run (issue .help in repl or "run help")'
})
.positional('intargs', {
describe: 'Command args list'
})
)
.demandCommand(1, 'You need to provide one command to proceed')
.option('silent', {
alias: 's',
describe: 'Supress startup banner and initialization messages'
})
.completion('completion', (current, argv) => {
let commands = require('./commands')
if (argv._.includes('run'))
return Object.keys(commands).filter(c => commands[c].runnable)
return ['generate', 'clean', 'init', 'repl', 'run', 'serve']
})
.alias('?', 'help')
.help().argv
// Configure apiProject and projectDir based on current dir
if(!argv._.includes('init'))
u.configProject(argv)
// yargs should be done this already :-(
if (!['repl', 'init', 'generate', 'clean', 'serve', 'run'].includes(argv._[0])) {
yargs.showHelp()
console.error('\nInvalid command: %o', argv)
process.exit(0)
}
module.exports = argv