<commit_before>#! /usr/bin/env node
/**
 * Commandline interface
 * Created by xiamx on 2016-08-10.
 */

import * as yargs from 'yargs'
import * as fs from 'fs'
import { typescriptOfSchema, getDatabase, extractCommand } from '../src/index'

let argv: any = yargs
    .usage('Usage: $0 <command> [options]')
    .command('generate', 'generate type definition')
    .demand(1)
    // tslint:disable-next-line 
    .example('$0 generate -c postgres://username:password@localhost/db -t table1 -t table2 -s schema -n namespace -o interface_output.ts', 'generate typescript interfaces from schema')
    .demand('c')
    .alias('c', 'conn')
    .nargs('c', 1)
    .describe('c', 'database connection string')
    .alias('t', 'table')
    .nargs('t', 1)
    .describe('t', 'table name')
    .alias('s', 'schema')
    .nargs('s', 1)
    .describe('s', 'schema name')
    .demand('n')
    .alias('n', 'namespace')
    .nargs('n', 1)
    .describe('n', 'namespace for interfaces')
    .demand('o')
    .nargs('o', 1)
    .alias('o', 'output')
    .describe('o', 'output file name')
    .help('h')
    .alias('h', 'help')
    .argv

function getTime() {
    let padTime = (value: number) => `0${value}`.slice(-2)
    let time = new Date()
    const yyyy = time.getFullYear()
    const MM = padTime(time.getMonth() + 1)
    const dd = padTime(time.getDate())
    const hh = padTime(time.getHours())
    const mm = padTime(time.getMinutes())
    const ss = padTime(time.getSeconds())
    return `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`
}

(async () => {

    try {
        let db = getDatabase(argv.c)

        if (!Array.isArray(argv.t)) {
            if (!argv.t) {
                argv.t = []
            } else {
                argv.t = [argv.t]
            }
        }

        let formattedOutput = await typescriptOfSchema(
            db, argv.n, argv.t, argv.s,
            extractCommand(process.argv, argv.c), getTime()
        )
        fs.writeFileSync(argv.o, formattedOutput)

    } catch (e) {
        console.error(e)
        process.exit(1)
    }

})().then(() => {
    process.exit()
})<commit_msg>Support loading config from schemats.json [#49]<commit_after>#! /usr/bin/env node
/**
 * Commandline interface
 * Created by xiamx on 2016-08-10.
 */

import * as yargs from 'yargs'
import * as fs from 'fs'
import { typescriptOfSchema, getDatabase, extractCommand } from '../src/index'

interface SchematsConfig {
    conn: string,
    table: string[] | string,
    schema: string,
    namespace: string,
    output: string
}

let argv: SchematsConfig = yargs
    .usage('Usage: $0 <command> [options]')
    .global('config')
    .config()
    .default('config', 'schemats.json')
    .command('generate', 'generate type definition')
    .demand(1)
    // tslint:disable-next-line 
    .example('$0 generate -c postgres://username:password@localhost/db -t table1 -t table2 -s schema -n namespace -o interface_output.ts', 'generate typescript interfaces from schema')
    .demand('c')
    .alias('c', 'conn')
    .nargs('c', 1)
    .describe('c', 'database connection string')
    .alias('t', 'table')
    .nargs('t', 1)
    .describe('t', 'table name')
    .alias('s', 'schema')
    .nargs('s', 1)
    .describe('s', 'schema name')
    .demand('n')
    .alias('n', 'namespace')
    .nargs('n', 1)
    .describe('n', 'namespace for interfaces')
    .demand('o')
    .nargs('o', 1)
    .alias('o', 'output')
    .describe('o', 'output file name')
    .help('h')
    .alias('h', 'help')
    .argv

function getTime() {
    let padTime = (value: number) => `0${value}`.slice(-2)
    let time = new Date()
    const yyyy = time.getFullYear()
    const MM = padTime(time.getMonth() + 1)
    const dd = padTime(time.getDate())
    const hh = padTime(time.getHours())
    const mm = padTime(time.getMinutes())
    const ss = padTime(time.getSeconds())
    return `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`
}

(async () => {

    try {
        let db = getDatabase(argv.conn)

        if (!Array.isArray(argv.table)) {
            if (!argv.table) {
                argv.table = []
            } else {
                argv.table = [argv.table]
            }
        }

        let formattedOutput = await typescriptOfSchema(
            db, argv.namespace, argv.table, argv.schema,
            extractCommand(process.argv, argv.conn), getTime()
        )
        fs.writeFileSync(argv.output, formattedOutput)

    } catch (e) {
        console.error(e)
        process.exit(1)
    }

})().then(() => {
    process.exit()
})
{'fraction_non_alphanumeric': 0.1813953488372093, 'fraction_numerical': 0.010188261351052049, 'mean_word_length': 2.484567901234568, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3658220', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1505, 'n_words': 360}