{"size": 2051, "ext": "js", "max_stars_repo_path": "bin/sqlite-json.js", "max_stars_repo_name": "sbonds/sqlite-json", "max_stars_repo_stars_event_min_datetime": "2015-07-26T15:16:32.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-21T02:40:28.000Z", "max_issues_repo_path": "bin/sqlite-json.js", "max_issues_repo_name": "medstudioinc/sqlite-json", "max_issues_repo_issues_event_min_datetime": "2020-09-04T17:03:48.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T03:53:50.000Z", "max_forks_repo_path": "bin/sqlite-json.js", "max_forks_repo_name": "medstudioinc/sqlite-json", "max_forks_repo_forks_event_min_datetime": "2015-07-06T10:23:47.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-25T16:23:44.000Z"}
{"max_stars_count": 73, "max_issues_count": 19, "max_forks_count": 19, "avg_line_length": 32.5555555556, "max_line_length": 101, "alphanum_fraction": 0.4856167723}
#!/usr/bin/env node

const program = require('commander'),
    sj = require('..');

function list(val) { return val.split(','); }

program
    .version('1.0.0')
    .usage('[options] <database> [sql]')
    .description('Export a SQLite table to JSON')
    .option('-k, --key <key>', 'Key output to column', String)
    .option('-t, --table <table>', 'table to query', String)
    .option('-c, --columns <list>', 'Comma-delimited list of columns to output (Default: all)', list)
    .option('-w, --where <clause>', 'WHERE clause to add to table query', String)
    .option('-o, --output <file>', 'Save result to file', String)
    .action(function(database, sql, options) {
        if (typeof(sql) == 'object' && typeof(options) === 'undefined') {
            options = sql;
            sql = null;
        }

        const output = options.output;

        options = {
            table: options.table || null,
            query: options.query || null,
            key: options.key || null,
            where: options.where || null,
            columns: options.columns || null
        };

        sj(database).json(sql, options, function(err, json) {

            if (err) {
                if (String(err).indexOf('no such table') > -1)
                    console.error('error: table not found');
                else
                    console.error(err.message);

                process.exit(1);

            } else if (output) {
                require('fs').writeFile(output, json, function(err) {
                    if (err) {
                        process.stderr.write(err.message);
                        process.exit(1);
                    }
                    else process.stdout.write(program.output + '\n');
                });

            } else {
                process.stdout.on('error', function(err) {
                    console.error(err.message);
                    process.exit(1);
                });
                process.stdout.write(json + '\n');
            }
        });
    });

program.parse(process.argv);
