UNPKG

@swell/cli

Version:

Swell's command line interface/utility

44 lines (43 loc) 1.34 kB
import git from '@npmcli/git'; import { Command } from '@oclif/core'; import { FetchError } from 'node-fetch'; import Api from './lib/api.js'; import style from './lib/style.js'; const { GitUnknownError } = git.errors; /** * A base class for Swell CLI commands. * * This class extends the oclif Command class and adds: * * - an Api instance * - logging methods * - a catch method to handle errors */ export class SwellCommand extends Command { static examples = ['<%= config.bin %> <%= command.id %>']; api; requiresAuth = true; constructor(argv, config) { super(argv, config); this.api = new Api(); } debugJson(...args) { const stringified = args.map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg, null, 4)); this.debug(...stringified); } logError(message) { this.log(`${style.error(message)}`); } logSuccess(message) { this.log(`${style.success(message)}`); } async catch(error) { if (error instanceof GitUnknownError) { this.error(style.error(`git: ${error.stderr || error.stdout || error.message}`)); } else if (error instanceof FetchError) { this.error(`Could not connect to Swell API. Please try again later: ${error.message}`); } throw error; } }