UNPKG

divshot-cli

Version:
117 lines (97 loc) 3.43 kB
var fs = require('fs'); var path = require('path'); var Nash = require('nash'); var homeDir = require('home-dir'); var Divshot = require('divshot-api'); var format = require('chalk'); var dumper = require('divshot-dumper'); var User = require('./lib/user'); var Cwd = require('./lib/cwd'); var environments = require('./lib/environments'); var commands = require('./lib/commands'); var errors = require('./lib/errors'); var CLIENT_ID = '526753cf2f55bd0002000006'; var API_HOST = process.env.API_HOST || 'https://api.divshot.com'; process.env.DIVSHOT_HASHED_BUCKET || (process.env.DIVSHOT_HASHED_BUCKET = "divshot-io-hashed-production"); var cliConfigDirectory = path.join(homeDir(), '.divshot'); var user = new User(cliConfigDirectory); var cwd = new Cwd(); var title = format.bold.yellow('++++++ Divshot has joined Firebase! ++++++'); var description = [ 'Divshot has joined Firebase, and will be shutting down services ' + format.bold('December 14, 2015.'), ' You can use ' + format.bold('divshot migrate') + ' to migrate your existing apps to Firebase Hosting.\n', ' Please visit ' + format.bold.underline('https://divshot.com/') + ' for more information.' ].join('\n'); // Set up CLI var cli = Nash.createCli({ title: title, description: description, color: 'white', api: Divshot.createClient({ token: user.get('token'), host: API_HOST, client_id: CLIENT_ID }), user: user, cwd: cwd, errors: errors, environments: environments, timeout: 30000 }); // Set up process watching dumping machine dumper(cli.api.events); // Flags cli.flag('-a', '--app') .description('override the directory\'s app name') .handler(function (appName) { cwd.overrideAppName(appName); }); cli.flag('-t', '--token') .description('override your current user authentication token') .handler(function (token) { cli.user.set('token', token, false); cli.api.setToken(token); }); cli.flag('-v', '--version') .description('show the CLI version number') .exit(true) .handler(function () { var package = require(path.resolve(__dirname, './package.json')); cli.log(package.version); }); cli.flag('-c', '--config') .description('use a different config file') .handler(function (filename) { cli.cwd.setConfigFilename(filename) }); cli.flag('--timeout') .description('set command timeout, in milliseconds') .handler(function (timeout) { cli.timeout = timeout; }); cli.flag('-o', '--org') .description('set command to operate on the given organization') .handler(function (org) { cli.org = org; }); // Helpers cli.method('authenticate', function (cli, command, done) { if (!cli.user.authenticated()) return done(cli.errors.NOT_AUTHENTICATED); done(); }); cli.method('isApp', function (cli, command, next) { if(!cli.cwd.getConfig().name) return next(cli.errors.DIRECTORY_NOT_APP); next(); }); cli.catchAll(function (type, attemptedCommand) { // Undefined command if (!attemptedCommand) return cli.commands.help({debug: true}); cli.log(); cli.log(format.bold('"' + attemptedCommand + '"') + ' is not a Divshot ' + type + '.'); cli.log('Please use ' + format.bold('"divshot help"') + ' for a list of Divshot commands.'); cli.log(); cli.log('Note: you may need to update Divshot in order to use that command: ' + format.bold('npm install -g divshot-cli')); }); // Add commands commands.connect(cli); module.exports = cli;