#!/usr/bin/env node import program from 'commander'; import { resolve } from 'path'; import { copy } from 'copy-paste'; import login from '../lib/login'; import now from '../lib'; import fs from 'fs'; import ms from 'ms'; import os from 'os'; program .option('-d, --debug', 'Debug mode [off]', false) .option('-L, --login', 'Configure login') .option('-C, --no-clipboard', 'Do not attempt to copy URL to clipboard') .parse(process.argv); let path = program.args[program.args.length - 1]; if (path) { if ('/' !== path[0]) { path = resolve(process.cwd(), path); } } else { path = process.cwd(); } let config; try { config = fs.readFileSync(resolve(os.homedir(), '.now.json'), 'utf8'); config = JSON.parse(config); } catch (err) {} if (!config || !config.token) { login() .then((token) => { if (program.L) { console.log('> Logged in successfully. Token saved in ~/.now.json'); process.exit(0); } else { sync(token); } }) .catch((e) => { error(`Authentication error – ${e.message}`); process.exit(1); }); } else { sync(config.token); } function sync (token) { const debug = !!program.debug; const clipboard = !program.noClipboard; const start = Date.now(); console.log(`> Deploying ${path}`); now(path, token, { debug }) .then((url) => { const elapsed = ms(new Date() - start); if (clipboard) { copy(url, (err) => { console.log(`> ${url} ${!err ? '(copied to clipboard)' : ''} [${elapsed}]`); }); } else { console.log(`> ${url} [${elapsed}]`); } }, (err) => { if (403 === err.status) { error('Authentication error. Run `now -L` to log-in again.'); } else if (500 === err.status) { error('Unexpected server error. Please retry'); } else { error(`Unexpected error. Please retry. (${err.message})`); } process.exit(1); }); } function error (err) { console.error(`> \u001b[31mError!\u001b[39m ${err}.`); }