#!/usr/bin/env node import minimist from 'minimist'; import chalk from 'chalk'; import ms from 'ms'; import table from 'text-table'; import Now from '../lib'; import login from '../lib/login'; import * as cfg from '../lib/cfg'; import { handleError, error } from '../lib/error'; const argv = minimist(process.argv.slice(2), { boolean: ['help', 'debug', 'hard'], alias: { help: 'h', debug: 'd' } }); const deploymentId = argv._[0]; // options const help = () => { console.log(` ${chalk.bold('𝚫 now remove')} [deploymentId] ${chalk.dim('Options:')} -h, --help output usage information -d, --debug debug mode [off] ${chalk.dim('Examples:')} ${chalk.gray('–')} Remove a deployment identified by ${chalk.dim('`deploymentId`')}: ${chalk.cyan('$ now rm deploymentId')} ${chalk.dim('Alias:')} rm `); }; if (argv.help || !deploymentId) { help(); process.exit(0); } // options const debug = argv.debug; const apiUrl = argv.url || 'https://api.zeit.co'; const hard = argv.hard || false; const config = cfg.read(); function readConfirmation (depl, aliases) { return new Promise((resolve, reject) => { const time = chalk.gray(ms(new Date() - depl.created) + ' ago'); const tbl = table( [[depl.uid, chalk.underline(`https://${depl.url}`), time]], { align: ['l', 'r', 'l'], hsep: ' '.repeat(6) } ); process.stdout.write('> The following deployment will be removed permanently\n'); process.stdout.write(' ' + tbl + '\n'); if (aliases.length) { process.stdout.write(`> ${chalk.yellow('Warning!')} This deployment's ` + `${chalk.bold(aliases.length + ' alias' + (aliases.length > 1 ? 'es': ''))} ` + `will be removed. Run ${chalk.dim('`now alias ls`')} to list.\n`); } process.stdout.write(`${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`); process.stdin.on('data', (d) => { process.stdin.pause(); resolve(d.toString().trim()); }).resume(); }); } Promise.resolve(config.token || login(apiUrl)) .then(async (token) => { try { await remove(token); } catch (err) { error(`Unknown error: ${err.stack}`); process.exit(1); } }) .catch((e) => { error(`Authentication error – ${e.message}`); process.exit(1); }); async function remove (token) { const now = new Now(apiUrl, token, { debug }); const deployments = await now.list(); const depl = deployments.find((d) => d.uid === deploymentId); if (null != deploymentId && !depl) { error(`Could not find a deployment by ${chalk.bold(`"${deploymentId}"`)}. Run ${chalk.dim(`\`now ls\``)} to list.`); return process.exit(1); } const aliases = await now.listAliases(depl.uid); try { const confirmation = (await readConfirmation(depl, aliases)).toLowerCase(); if ('y' !== confirmation && 'yes' !== confirmation) { console.log('\n> Aborted'); process.exit(0); } const start = new Date(); await now.remove(deploymentId, { hard }); const elapsed = ms(new Date() - start); console.log(`${chalk.cyan('> Success!')} Deployment ${chalk.bold(deploymentId)} removed [${elapsed}]`); } catch (err) { handleError(err); process.exit(1); } now.close(); }