Browse Source

Merge pull request #70 from zeit/add/alias-removal-warning

Add alias removal warning
master
Guillermo Rauch 9 years ago
committed by GitHub
parent
commit
e7b0362c7f
  1. 13
      bin/now-remove
  2. 15
      lib/alias.js
  3. 26
      lib/index.js

13
bin/now-remove

@ -51,7 +51,7 @@ const hard = argv.hard || false;
const config = cfg.read(); const config = cfg.read();
function readConfirmation (app) { function readConfirmation (app, aliases) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const time = chalk.gray(ms(new Date() - app.created) + ' ago'); const time = chalk.gray(ms(new Date() - app.created) + ' ago');
const tbl = table( const tbl = table(
@ -61,6 +61,13 @@ function readConfirmation (app) {
process.stdout.write('> The following deployment will be removed permanently\n'); process.stdout.write('> The following deployment will be removed permanently\n');
process.stdout.write(' ' + tbl + '\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.stdout.write(`${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`);
process.stdin.on('data', (d) => { process.stdin.on('data', (d) => {
@ -90,8 +97,10 @@ async function remove (token) {
const deployments = await now.list(); const deployments = await now.list();
const app = deployments.find((d) => d.uid === deploymentId); const app = deployments.find((d) => d.uid === deploymentId);
const aliases = await now.listAliases(app.uid);
try { try {
const confirmation = (await readConfirmation(app)).toLowerCase(); const confirmation = (await readConfirmation(app, aliases)).toLowerCase();
if ('y' !== confirmation && 'yes' !== confirmation) { if ('y' !== confirmation && 'yes' !== confirmation) {
console.log('\n> Aborted'); console.log('\n> Aborted');
process.exit(0); process.exit(0);

15
lib/alias.js

@ -1,4 +1,3 @@
import retry from 'async-retry';
import dns from 'dns'; import dns from 'dns';
import Now from '../lib'; import Now from '../lib';
import toHost from './to-host'; import toHost from './to-host';
@ -15,18 +14,10 @@ export default class Alias extends Now {
throw err; throw err;
} }
return this.retry(async (bail, attempt) => { return this.listAliases(target.uid);
const res = await this._fetch(`/now/deployments/${target.uid}/aliases`); } else {
const body = await res.json(); return this.listAliases();
return body.aliases;
});
} }
return this.retry(async (bail, attempt) => {
const res = await this._fetch('/now/aliases');
const body = await res.json();
return body.aliases;
});
} }
async rm (_alias) { async rm (_alias) {

26
lib/index.js

@ -74,7 +74,7 @@ export default class Now extends EventEmitter {
const nowProperties = pkg.now || {}; const nowProperties = pkg.now || {};
const engines = nowProperties.engines || pkg.engines; const engines = nowProperties.engines || pkg.engines;
const deployment = await retry(async (bail) => { const deployment = await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /now/create'); if (this._debug) console.time('> [debug] /now/create');
const res = await this._fetch('/now/create', { const res = await this._fetch('/now/create', {
method: 'POST', method: 'POST',
@ -210,7 +210,7 @@ export default class Now extends EventEmitter {
async list (app) { async list (app) {
const query = app ? `?app=${encodeURIComponent(app)}` : ''; const query = app ? `?app=${encodeURIComponent(app)}` : '';
const { deployments } = await retry(async (bail) => { const { deployments } = await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /list'); if (this._debug) console.time('> [debug] /list');
const res = await this._fetch('/now/list' + query); const res = await this._fetch('/now/list' + query);
if (this._debug) console.timeEnd('> [debug] /list'); if (this._debug) console.timeEnd('> [debug] /list');
@ -233,10 +233,20 @@ export default class Now extends EventEmitter {
return deployments; return deployments;
} }
async listAliases (deploymentId) {
return this.retry(async (bail, attempt) => {
const res = await this._fetch(deploymentId
? `/now/deployments/${deploymentId}/aliases`
: '/now/aliases');
const body = await res.json();
return body.aliases;
});
}
async remove (deploymentId, { hard }) { async remove (deploymentId, { hard }) {
const data = { deploymentId, hard }; const data = { deploymentId, hard };
await retry(async (bail) => { await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /remove'); if (this._debug) console.time('> [debug] /remove');
const res = await this._fetch('/now/remove', { const res = await this._fetch('/now/remove', {
method: 'DELETE', method: 'DELETE',
@ -255,11 +265,19 @@ export default class Now extends EventEmitter {
if (200 !== res.status) { if (200 !== res.status) {
throw new Error('Removing deployment failed'); throw new Error('Removing deployment failed');
} }
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); });
return true; return true;
} }
retry (fn) {
return retry(fn, {
retries: 5,
randomize: true,
onRetry: this._onRetry
});
}
_onRetry (err) { _onRetry (err) {
if (this._debug) { if (this._debug) {
console.log(`> [debug] Retrying: ${err.stack}`); console.log(`> [debug] Retrying: ${err.stack}`);

Loading…
Cancel
Save