You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
4.5 KiB
170 lines
4.5 KiB
9 years ago
|
#!/usr/bin/env node
|
||
|
|
||
8 years ago
|
// Packages
|
||
9 years ago
|
import minimist from 'minimist';
|
||
|
import chalk from 'chalk';
|
||
|
import ms from 'ms';
|
||
9 years ago
|
import table from 'text-table';
|
||
8 years ago
|
|
||
|
// Ours
|
||
9 years ago
|
import Now from '../lib';
|
||
|
import login from '../lib/login';
|
||
|
import * as cfg from '../lib/cfg';
|
||
8 years ago
|
import {handleError, error} from '../lib/error';
|
||
9 years ago
|
|
||
9 years ago
|
const argv = minimist(process.argv.slice(2), {
|
||
8 years ago
|
string: ['config', 'token'],
|
||
9 years ago
|
boolean: ['help', 'debug', 'hard'],
|
||
|
alias: {
|
||
|
help: 'h',
|
||
8 years ago
|
config: 'c',
|
||
|
debug: 'd',
|
||
|
token: 't'
|
||
9 years ago
|
}
|
||
9 years ago
|
});
|
||
|
|
||
8 years ago
|
const ids = argv._;
|
||
9 years ago
|
|
||
9 years ago
|
// options
|
||
|
const help = () => {
|
||
|
console.log(`
|
||
8 years ago
|
${chalk.bold('𝚫 now remove')} deploymentId|deploymentName [...deploymentId|deploymentName]
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.dim('Options:')}
|
||
9 years ago
|
|
||
8 years ago
|
-h, --help Output usage information
|
||
|
-c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file
|
||
|
-d, --debug Debug mode [off]
|
||
|
-t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.dim('Examples:')}
|
||
|
|
||
9 years ago
|
${chalk.gray('–')} Remove a deployment identified by ${chalk.dim('`deploymentId`')}:
|
||
9 years ago
|
|
||
|
${chalk.cyan('$ now rm deploymentId')}
|
||
|
|
||
8 years ago
|
${chalk.gray('–')} Remove all deployments with name ${chalk.dim('`my-app`')}:
|
||
|
|
||
|
${chalk.cyan('$ now rm my-app')}
|
||
|
|
||
|
${chalk.gray('–')} Remove two deployments with IDs ${chalk.dim('`eyWt6zuSdeus`')} and ${chalk.dim('`uWHoA9RQ1d1o`')}:
|
||
|
|
||
|
${chalk.cyan('$ now rm eyWt6zuSdeus uWHoA9RQ1d1o')}
|
||
|
|
||
9 years ago
|
${chalk.dim('Alias:')} rm
|
||
9 years ago
|
`);
|
||
|
};
|
||
|
|
||
8 years ago
|
if (argv.help || 0 === ids.length) {
|
||
9 years ago
|
help();
|
||
|
process.exit(0);
|
||
|
}
|
||
|
|
||
9 years ago
|
// options
|
||
9 years ago
|
const debug = argv.debug;
|
||
9 years ago
|
const apiUrl = argv.url || 'https://api.zeit.co';
|
||
9 years ago
|
const hard = argv.hard || false;
|
||
8 years ago
|
if (argv.config) cfg.setConfigFile(argv.config);
|
||
9 years ago
|
const config = cfg.read();
|
||
|
|
||
8 years ago
|
function readConfirmation (matches) {
|
||
9 years ago
|
return new Promise((resolve, reject) => {
|
||
8 years ago
|
|
||
|
process.stdout.write(`> The following deployment${matches.length === 1 ? '' : 's'} will be removed permanently:\n`);
|
||
|
|
||
9 years ago
|
const tbl = table(
|
||
8 years ago
|
matches.map((depl) => {
|
||
|
const time = chalk.gray(ms(new Date() - depl.created) + ' ago');
|
||
|
const url = chalk.underline(`https://${depl.url}`);
|
||
|
return [depl.uid, url, time];
|
||
|
}),
|
||
9 years ago
|
{ align: ['l', 'r', 'l'], hsep: ' '.repeat(6) }
|
||
|
);
|
||
8 years ago
|
process.stdout.write(tbl + '\n');
|
||
|
|
||
|
for (let depl of matches) {
|
||
|
for (let alias of depl.aliases) {
|
||
|
process.stdout.write(
|
||
|
`> ${chalk.yellow('Warning!')} Deployment ${chalk.bold(depl.uid)} ` +
|
||
|
`is an alias for ${chalk.underline(`https://${alias.alias}`)} and will be removed.\n`
|
||
|
);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
process.stdout.write(`${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`);
|
||
9 years ago
|
|
||
9 years ago
|
process.stdin.on('data', (d) => {
|
||
|
process.stdin.pause();
|
||
|
resolve(d.toString().trim());
|
||
|
}).resume();
|
||
|
});
|
||
|
}
|
||
|
|
||
8 years ago
|
Promise.resolve(argv.token || config.token || login(apiUrl))
|
||
9 years ago
|
.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 });
|
||
|
|
||
9 years ago
|
const deployments = await now.list();
|
||
8 years ago
|
|
||
|
const matches = deployments.filter((d) => {
|
||
8 years ago
|
return ids.find((id) => {
|
||
|
// `url` should match the hostname of the deployment
|
||
|
let u = id.replace(/^https\:\/\//i, '');
|
||
|
if (-1 === u.indexOf('.')) {
|
||
|
// `.now.sh` domain is implied if just the subdomain is given
|
||
|
u += '.now.sh';
|
||
|
}
|
||
|
|
||
|
return d.uid === id
|
||
|
|| d.name === id
|
||
|
|| d.url === u;
|
||
|
});
|
||
8 years ago
|
});
|
||
|
|
||
|
if (0 === matches.length) {
|
||
|
error(`Could not find any deployments matching ${ids.map((id) => chalk.bold(`"${id}"`)).join(', ')}. Run ${chalk.dim(`\`now ls\``)} to list.`);
|
||
9 years ago
|
return process.exit(1);
|
||
|
}
|
||
8 years ago
|
|
||
|
const aliases = await Promise.all(matches.map((depl) => now.listAliases(depl.uid)));
|
||
|
for (let i = 0; i < matches.length; i++) {
|
||
|
matches[i].aliases = aliases[i];
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
try {
|
||
8 years ago
|
const confirmation = (await readConfirmation(matches)).toLowerCase();
|
||
9 years ago
|
if ('y' !== confirmation && 'yes' !== confirmation) {
|
||
9 years ago
|
console.log('\n> Aborted');
|
||
|
process.exit(0);
|
||
9 years ago
|
}
|
||
|
|
||
|
const start = new Date();
|
||
8 years ago
|
|
||
|
await Promise.all(matches.map((depl) => now.remove(depl.uid, { hard })));
|
||
|
|
||
9 years ago
|
const elapsed = ms(new Date() - start);
|
||
8 years ago
|
console.log(`${chalk.cyan('> Success!')} [${elapsed}]`);
|
||
|
console.log(table(matches.map((depl) => {
|
||
|
return [ `Deployment ${chalk.bold(depl.uid)} removed` ];
|
||
|
})));
|
||
9 years ago
|
} catch (err) {
|
||
|
handleError(err);
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
|
now.close();
|
||
|
}
|