Refactor scripts, add ability to load external scripts

This commit is contained in:
Jerko Steiner 2019-08-18 09:57:47 +07:00
parent b807e84539
commit 79c6d1a608
10 changed files with 116 additions and 41 deletions

View File

@ -25,8 +25,6 @@ export interface IContext {
} }
export interface ITeamService { export interface ITeamService {
jerko(params: string): number
create(params: ITeamCreateParams): Promise<ITeam> create(params: ITeamCreateParams): Promise<ITeam>
remove(params: ITeamRemoveParams): Promise<{id: number}> remove(params: ITeamRemoveParams): Promise<{id: number}>

View File

@ -1,31 +1,16 @@
#!/usr/bin/env node #!/usr/bin/env node
import * as commands from './commands'
import * as log from './log' import * as log from './log'
import {TCommand} from './TCommand' import {TCommand} from './TCommand'
import {argparse, arg} from '@rondo/argparse' import {argparse, arg} from '@rondo/argparse'
import {resolve} from './resolve'
const commandNames = Object.keys(commands).filter(cmd => !cmd.startsWith('_')) async function run(
args: any, commands: object, exit: (code: number) => void,
const {parse} = argparse({ ) {
help: arg('boolean', {alias: 'h'}), const p = './scripts'
debug: arg('boolean'), const module = await import(p)
command: arg('string', {
required: true,
positional: true,
choices: commandNames,
}),
args: arg('string[]', {
n: '*',
positional: true,
}),
})
type TArgs = ReturnType<typeof parse>
async function run(args: TArgs, exit: (code: number) => void) {
const commandName = args.command const commandName = args.command
if (!(commandName in commands)) { if (!(commandName in commands)) {
const c = commandNames
log.info( log.info(
'Invalid command! Use the --help argument to see a list of commands') 'Invalid command! Use the --help argument to see a list of commands')
exit(1) exit(1)
@ -39,10 +24,25 @@ async function start(
argv: string[] = process.argv.slice(1), argv: string[] = process.argv.slice(1),
exit = (code: number) => process.exit(code), exit = (code: number) => process.exit(code),
) { ) {
let args: TArgs | null = null const commands = await resolve()
const {parse} = argparse({
help: arg('boolean', {alias: 'h'}),
debug: arg('boolean'),
command: arg('string', {
required: true,
positional: true,
choices: Object.keys(commands).filter(c => !c.startsWith('_')),
}),
args: arg('string[]', {
n: '*',
positional: true,
}),
})
let args: ReturnType<typeof parse> | null = null
try { try {
args = parse(argv) args = parse(argv)
await run(args, exit) await run(args, commands, exit)
} catch (err) { } catch (err) {
log.error((args && args.debug ? err.stack : err.message)) log.error((args && args.debug ? err.stack : err.message))
exit(1) exit(1)

View File

@ -0,0 +1,20 @@
import * as commands from './scripts'
import {join} from 'path'
export async function resolve(cwd = process.cwd()) {
let extraScripts: object = {}
try {
extraScripts = await import(join(cwd, './src/scripts'))
} catch (err) {
try {
extraScripts = await import(join(cwd, './lib/scripts'))
} catch (err) {
// ignore
}
}
return {
...commands,
...extraScripts,
}
}

View File

@ -31,22 +31,52 @@ export async function build(...argv: string[]) {
await run(tsc, ['--build', path, ...watchArgs]) await run(tsc, ['--build', path, ...watchArgs])
} }
export async function test(...args: string[]) { export async function test(...argv: string[]) {
const {args} = argparse({
args: arg('string[]', {
n: '*',
positional: true,
}),
help: arg('boolean', {alias: 'h'}),
})
.parse(argv)
await run('jest', args) await run('jest', args)
} }
export async function exec(file: string) { export async function exec(...argv: string[]) {
const {parse} = argparse({
file: arg('string', {
required: true,
positional: true,
}),
args: arg('string[]', {
n: '*',
positional: true,
}),
help: arg('boolean', {alias: 'h'}),
})
const args = parse(argv)
const {file} = args
const command = file.endsWith('.ts') ? 'ts-node' : 'node' const command = file.endsWith('.ts') ? 'ts-node' : 'node'
const args = command === 'ts-node' ? const nodeArgs = command === 'ts-node' ?
[ [
'--files', '--files',
'--project', '--project',
findTsConfig(file), findTsConfig(file),
] : [] ] : []
await run(command, [...args, file]) await run(command, [...nodeArgs, file, ...args.args])
} }
export async function createMigration(project: string, name: string) { export async function createMigration(...argv: string[]) {
const args = argparse({
name: arg('string', {required: true, positional: true}),
project: arg('string', {default: '.'}),
help: arg('boolean', {alias: 'h'}),
})
.parse(argv)
const {name, project} = args
const typeorm = findNodeModules(project) const typeorm = findNodeModules(project)
.map(nm => p.join(nm, 'typeorm')) .map(nm => p.join(nm, 'typeorm'))
.find(t => fs.existsSync(t)) .find(t => fs.existsSync(t))
@ -70,7 +100,7 @@ function findTsConfig(file: string): string {
return '' return ''
} }
export async function browserify(path: string = '.') { async function browserify(path: string = '.') {
// mkdirSync(join(path, 'build'), {recursive: true}) // mkdirSync(join(path, 'build'), {recursive: true})
await run('browserify', [ await run('browserify', [
join(path, 'esm', 'index.js'), join(path, 'esm', 'index.js'),
@ -82,7 +112,7 @@ export async function browserify(path: string = '.') {
]) ])
} }
export async function uglify(path: string = '.') { async function uglify(path: string = '.') {
await run('uglifyjs', [ await run('uglifyjs', [
'--compress', '--compress',
'--mangle', '--mangle',
@ -93,13 +123,25 @@ export async function uglify(path: string = '.') {
]) ])
} }
export async function js(path: string = '.') { export async function js(...argv: string[]) {
const args = argparse({
path: arg('string', {positional: true, default: '.'}),
watch: arg('boolean', {alias: 'w'}),
help: arg('boolean', {alias: 'h'}),
})
.parse(argv)
const {path, watch} = args
return watch ? watchJs(path) : buildJs(path)
}
async function buildJs(path: string) {
await build(...['-p', path, '--esm']) await build(...['-p', path, '--esm'])
await browserify(path) await browserify(path)
await uglify(path) await uglify(path)
} }
export async function watchJs(path: string = '.') { async function watchJs(path: string) {
await run('watchify', [ await run('watchify', [
join(path, 'esm', 'index.js'), join(path, 'esm', 'index.js'),
// '-p', '[', 'tsify', '--project', path, ']', // '-p', '[', 'tsify', '--project', path, ']',
@ -111,7 +153,20 @@ export async function watchJs(path: string = '.') {
]) ])
} }
export async function css(path = '.') { export async function css(...argv: string[]) {
const args = argparse({
path: arg('string', {positional: true, default: '.'}),
watch: arg('boolean', {alias: 'w'}),
help: arg('boolean', {alias: 'h'}),
})
.parse(argv)
const {path, watch} = args
if (watch) {
await watchCss(path)
}
await run('node-sass', [ await run('node-sass', [
'--output-style', 'compressed', '--output-style', 'compressed',
'--output', join(path, 'build'), '--output', join(path, 'build'),
@ -119,7 +174,7 @@ export async function css(path = '.') {
]) ])
} }
export async function watchCss(path = '.') { async function watchCss(path = '.') {
await run('node-sass', [ await run('node-sass', [
join(path, 'scss'), join(path, 'scss'),
'--output', join(path, 'build'), '--output', join(path, 'build'),
@ -137,7 +192,13 @@ export async function watchCss(path = '.') {
]) ])
} }
export async function frontend(path = '.') { export async function frontend(...argv: string[]) {
const args = argparse({
path: arg('string', {positional: true, default: '.'}),
help: arg('boolean', {alias: 'h'}),
})
.parse(argv)
const {path} = args
await build(...['-p', path, '--esm']) await build(...['-p', path, '--esm'])
const promises = [ const promises = [
build(...['-p', path, '--watch', '--esm']), build(...['-p', path, '--watch', '--esm']),

View File

@ -18,10 +18,6 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
protected readonly permissions: IUserPermissions, protected readonly permissions: IUserPermissions,
) {} ) {}
jerko(params: string, context?: IContext): number {
return parseInt(params, 10)
}
async create(params: t.ITeamCreateParams, context: IContext) { async create(params: t.ITeamCreateParams, context: IContext) {
const {userId} = context const {userId} = context
const name = trim(params.name) const name = trim(params.name)