Automatically print help
This commit is contained in:
parent
10ca503e59
commit
a057ca97d5
@ -8,6 +8,8 @@ export type TArgType<T extends TArgTypeName> =
|
||||
? boolean
|
||||
: never
|
||||
|
||||
export let exit = () => process.exit()
|
||||
|
||||
export interface IArgParam<T extends TArgTypeName> {
|
||||
alias?: string
|
||||
description?: string
|
||||
|
||||
22
packages/scripts/src/argparse.ts
Normal file
22
packages/scripts/src/argparse.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export {arg} from '@rondo/argparse'
|
||||
import {info} from './log'
|
||||
import {
|
||||
argparse as configure, IArgsConfig, isHelp, TArgs
|
||||
} from '@rondo/argparse'
|
||||
|
||||
export let exit = () => process.exit()
|
||||
|
||||
export function argparse<T extends IArgsConfig>(config: T) {
|
||||
const parser = configure(config)
|
||||
return {
|
||||
...parser,
|
||||
parse(args: string[]): TArgs<T> {
|
||||
const result = parser.parse(args)
|
||||
if ('help' in config && isHelp(args)) {
|
||||
info(parser.help())
|
||||
exit()
|
||||
}
|
||||
return result
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
import * as fs from 'fs'
|
||||
import * as log from '../log'
|
||||
import * as p from 'path'
|
||||
import {argparse, arg} from '@rondo/argparse'
|
||||
import {argparse, arg} from '../argparse'
|
||||
import {findNodeModules} from '../modules'
|
||||
import {join} from 'path'
|
||||
import {run} from '../run'
|
||||
@ -22,16 +23,9 @@ export async function build(...argv: string[]) {
|
||||
alias: 'w',
|
||||
description: 'Watch for changes',
|
||||
}),
|
||||
help: arg('boolean', {
|
||||
alias: 'h',
|
||||
description: 'Print help message',
|
||||
}),
|
||||
help: arg('boolean', {alias: 'h'}),
|
||||
})
|
||||
const args = parse(argv)
|
||||
if (args.help) {
|
||||
console.log('Usage: rondo build ' + help())
|
||||
return
|
||||
}
|
||||
const path = args.esm ? join(args.project, 'tsconfig.esm.json') : args.project
|
||||
const watchArgs = args.watch ? ['--watch', '--preserveWatchOutput'] : []
|
||||
await run(tsc, ['--build', path, ...watchArgs])
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
export async function help() {
|
||||
console.log('Usage help')
|
||||
}
|
||||
@ -1,3 +1,2 @@
|
||||
export * from './help'
|
||||
export * from './build'
|
||||
export * from './newlib'
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import {argparse, arg, isHelp} from '@rondo/argparse'
|
||||
import {run} from '../run'
|
||||
import * as fs from 'fs'
|
||||
import * as log from '../log'
|
||||
import * as path from 'path'
|
||||
import {argparse, arg} from '../argparse'
|
||||
import {run} from '../run'
|
||||
|
||||
export async function newlib(...argv: string[]) {
|
||||
const {parse, help} = argparse({
|
||||
@ -13,15 +14,11 @@ export async function newlib(...argv: string[]) {
|
||||
}),
|
||||
// frontend: arg('boolean', {alias: 'f'}),
|
||||
})
|
||||
if (isHelp(argv)) {
|
||||
console.log('Usage: rondo newlib ' + help())
|
||||
return
|
||||
}
|
||||
const args = parse(argv)
|
||||
|
||||
const destDir = path.join('./packages', args.name)
|
||||
|
||||
console.log('mkdir %s', destDir)
|
||||
log.info('mkdir %s', destDir)
|
||||
fs.mkdirSync(destDir)
|
||||
|
||||
const libraryName = `${args.namespace}/${args.name}`
|
||||
@ -31,17 +28,17 @@ export async function newlib(...argv: string[]) {
|
||||
const src = file
|
||||
const dest = path.join(destDir, path.relative(templateDir, file))
|
||||
if (dest === path.join(destDir, 'package.json')) {
|
||||
console.log('Add %s', dest)
|
||||
log.info('Add %s', dest)
|
||||
const libPkg = JSON.parse(fs.readFileSync(src, 'utf8'))
|
||||
libPkg.name = libraryName
|
||||
fs.writeFileSync(dest, JSON.stringify(libPkg, null, ' '))
|
||||
} else {
|
||||
console.log('Copy %s', src)
|
||||
log.info('Copy %s', src)
|
||||
fs.copyFileSync(src, dest)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Update main package.json')
|
||||
log.info('Update main package.json')
|
||||
const pkgFile = path.join(process.cwd(), 'package.json')
|
||||
const pkg = require(pkgFile)
|
||||
pkg.dependencies = pkg.dependencies || {}
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
import * as commands from './commands'
|
||||
import * as log from './log'
|
||||
import {TCommand} from './TCommand'
|
||||
|
||||
async function run(...argv: string[]) {
|
||||
const commandName = argv[0]
|
||||
if (!(commandName in commands)) {
|
||||
const c = Object.keys(commands).filter(cmd => !cmd.startsWith('_'))
|
||||
console.log(
|
||||
`Available commands:\n\n${c.join('\n')}`)
|
||||
log.info(`Available commands:\n\n${c.join('\n')}`)
|
||||
return
|
||||
}
|
||||
const command = (commands as any)[commandName] as TCommand
|
||||
@ -17,7 +17,7 @@ async function run(...argv: string[]) {
|
||||
if (typeof require !== 'undefined' && require.main === module) {
|
||||
run(...process.argv.slice(2))
|
||||
.catch(err => {
|
||||
console.log('> ' + err.message)
|
||||
log.error('> ' + err.message)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
12
packages/scripts/src/log.ts
Normal file
12
packages/scripts/src/log.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import {format} from 'util'
|
||||
|
||||
const stdout: NodeJS.WriteStream = process.stdout
|
||||
const stderr: NodeJS.WriteStream = process.stderr
|
||||
|
||||
export function error(message: string, ...values: any[]) {
|
||||
stderr.write(format(message + '\n', ...values))
|
||||
}
|
||||
|
||||
export function info(message: string, ...values: any[]) {
|
||||
stdout.write(format(message + '\n', ...values))
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user