grt
Version:
grt command-line interface — test, diff, preview APIs
76 lines (64 loc) • 2.18 kB
JavaScript
import { Command } from 'commander';
import dotenv from 'dotenv';
import { runTest } from './test.js';
import { runDiff } from './diff.js';
import { runUsage } from './usage.js';
import { runPreview } from './preview.js';
import { saveToken, getProjectConfig } from './config.js';
import { runInit } from './init.js';
import { runSchemaUpdate } from './schemaUpdate.js';
dotenv.config();
const program = new Command();
program
.name('greatlabs')
.description('greatlabs CLI — test, diff, preview APIs')
.version('0.2.0');
program
.command('init')
.description('Create project config in .great.json')
.action(runInit);
program
.command('login')
.description('Save your greatlabs token')
.argument('<token>')
.action(saveToken);
program
.command('test')
.description('Test your current ref against your live API')
.option('-r, --ref <ref>', 'Branch/tag', null)
.requiredOption('-u, --url <url>', 'Live API base URL')
.option('--auth <token>', 'Live API auth token')
.action((opts) => {
const { owner, repo, defaultRef } = getProjectConfig();
opts.owner = owner;
opts.repo = repo;
opts.ref = opts.ref || defaultRef;
runTest(opts);
});
program
.command('diff')
.description('Show changelog between refs')
.option('--base <base>', 'Base ref', 'main')
.option('--head <head>', 'Head ref', null)
.action((opts) => {
const { owner, repo, defaultRef } = getProjectConfig();
const head = opts.head || require('child_process')
.execSync('git rev-parse --abbrev-ref HEAD')
.toString().trim();
runDiff({ owner, repo, base: opts.base, head });
});
program
.command('preview')
.description('Print preview URLs for current ref')
.option('-r, --ref <ref>', 'Branch/tag')
.action((opts) => {
const { owner, repo, defaultRef } = getProjectConfig();
const ref = opts.ref || defaultRef;
runPreview({ owner, repo, ref });
});
program
.command('schema update')
.description('Sync schema refs with GitHub branches (remove stale ones)')
.action(runSchemaUpdate);
program.parse();