Skip to content

Commit

Permalink
transform-control
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Oct 23, 2024
1 parent 4be1ea5 commit d8c9268
Show file tree
Hide file tree
Showing 38 changed files with 1,451 additions and 366 deletions.
188 changes: 188 additions & 0 deletions bin/run-apidef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#!/usr/bin/env node

const Path = require('node:path')
const { statSync } = require('node:fs')
const { parseArgs } = require('node:util')

const { Gubu, Fault } = require('gubu')
const { Aontu, Context } = require('aontu')


const Pkg = require('../package.json')

const { ApiDef } = require('../dist/apidef.js')


let DEBUG = false
let CONSOLE = console


try {
let options = resolveOptions()

if(options.debug) {
DEBUG = true
}

if(options.version) {
version()
}

if(options.help) {
help()
}

if(options.version || options.help) {
exit()
}

options = validateOptions(options)

generate(options)

}
catch(err) {
handleError(err)
}



function exit(err) {
let code = 0
if(err) {
code = 1
}
process.exit(code)
}


function generate(options) {
const apidef = new ApiDef({
debug: options.debug
})

const spec = {
def: options.def,
kind: 'openapi-3',
model: Path.join(options.folder,'model/api.jsonic'),
meta: { name: options.name },
}

if(options.watch) {
apidef.watch(spec)
}
else {
apidef.generate(spec)
}

}



function resolveOptions() {

const args = parseArgs({
allowPositionals: true,
options: {
folder: {
type: 'string',
short: 'f',
default: '',
},

def: {
type: 'string',
short: 'd',
default: '',
},

watch: {
type: 'boolean',
short: 'w',
},

debug: {
type: 'boolean',
short: 'g',
},

help: {
type: 'boolean',
short: 'h',
},

version: {
type: 'boolean',
short: 'v',
},

}
})

const options = {
name: args.positionals[0],
folder: '' === args.values.folder ? args.positionals[0] : args.values.folder,
def: args.values.def,
watch: !!args.values.watch,
debug: !!args.values.debug,
help: !!args.values.help,
version: !!args.values.version,
}

return options
}


function validateOptions(rawOptions) {
const optShape = Gubu({
name: Fault('The first argument should be the project name.', String),
folder: String,
def: '',
watch: Boolean,
debug: Boolean,
help: Boolean,
version: Boolean,
})

const err = []
const options = optShape(rawOptions,{err})

if(err[0]) {
throw new Error(err[0].text)
}

if('' !== options.def) {
options.def = Path.resolve(options.def)
const stat = statSync(options.def, {throwIfNoEntry:false})
if(null == stat) {
throw new Error('Definition file not found: '+options.def)
}
}

return options
}


function handleError(err) {
CONSOLE.log('Voxgig SDK Generator Error:')

if(DEBUG) {
CONSOLE.log(err)
}
else {
CONSOLE.log(err.message)
}

exit(err)
}


function version() {
CONSOLE.log(Pkg.version)
}


function help() {
const s = 'TODO'
CONSOLE.log(s)
}
1 change: 1 addition & 0 deletions dist-test/apidef.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist-test/apidef.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion dist/apidef.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
type ApiDefOptions = {
fs?: any;
debug?: boolean;
};
type ApiDefSpec = {
def: string;
model: string;
kind: string;
meta: Record<string, any>;
};
declare function ApiDef(opts?: ApiDefOptions): {
watch: (spec: any) => Promise<void>;
generate: (spec: any) => Promise<{
generate: (spec: ApiDefSpec) => Promise<{
ok: boolean;
model: {
main: {
Expand Down
Loading

0 comments on commit d8c9268

Please sign in to comment.