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 {
jerko(params: string): number
create(params: ITeamCreateParams): Promise<ITeam>
remove(params: ITeamRemoveParams): Promise<{id: number}>

View File

@ -1,31 +1,16 @@
#!/usr/bin/env node
import * as commands from './commands'
import * as log from './log'
import {TCommand} from './TCommand'
import {argparse, arg} from '@rondo/argparse'
import {resolve} from './resolve'
const commandNames = Object.keys(commands).filter(cmd => !cmd.startsWith('_'))
const {parse} = argparse({
help: arg('boolean', {alias: 'h'}),
debug: arg('boolean'),
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) {
async function run(
args: any, commands: object, exit: (code: number) => void,
) {
const p = './scripts'
const module = await import(p)
const commandName = args.command
if (!(commandName in commands)) {
const c = commandNames
log.info(
'Invalid command! Use the --help argument to see a list of commands')
exit(1)
@ -39,10 +24,25 @@ async function start(
argv: string[] = process.argv.slice(1),
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 {
args = parse(argv)
await run(args, exit)
await run(args, commands, exit)
} catch (err) {
log.error((args && args.debug ? err.stack : err.message))
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])
}
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)
}
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 args = command === 'ts-node' ?
const nodeArgs = command === 'ts-node' ?
[
'--files',
'--project',
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)
.map(nm => p.join(nm, 'typeorm'))
.find(t => fs.existsSync(t))
@ -70,7 +100,7 @@ function findTsConfig(file: string): string {
return ''
}
export async function browserify(path: string = '.') {
async function browserify(path: string = '.') {
// mkdirSync(join(path, 'build'), {recursive: true})
await run('browserify', [
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', [
'--compress',
'--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 browserify(path)
await uglify(path)
}
export async function watchJs(path: string = '.') {
async function watchJs(path: string) {
await run('watchify', [
join(path, 'esm', 'index.js'),
// '-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', [
'--output-style', 'compressed',
'--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', [
join(path, 'scss'),
'--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'])
const promises = [
build(...['-p', path, '--watch', '--esm']),

View File

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