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.

203 lines
5.1 KiB

#!/usr/bin/env node
// Packages
const minimist = require("minimist");
const chalk = require("chalk");
const ms = require("ms");
const table = require("text-table");
const isURL = require("is-url");
// Ours
const Now = require("../lib");
const login = require("../lib/login");
const cfg = require("../lib/cfg");
const { handleError, error } = require("../lib/error");
const logo = require("../lib/utils/output/logo");
const argv = minimist(process.argv.slice(2), {
string: ["config", "token"],
boolean: ["help", "debug", "hard", "yes"],
alias: {
help: "h",
config: "c",
debug: "d",
token: "t",
yes: "y"
}
});
const ids = argv._;
9 years ago
// options
const help = () => {
console.log(
`
${chalk.bold(`${logo} now remove`)} deploymentId|deploymentName [...deploymentId|deploymentName]
9 years ago
${chalk.dim("Options:")}
9 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
-y, --yes Skip confirmation
${chalk.dim("Examples:")}
${chalk.gray("–")} Remove a deployment identified by ${chalk.dim("`deploymentId`")}:
${chalk.cyan("$ now rm deploymentId")}
${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")}
${chalk.dim("Alias:")} rm
`
);
};
9 years ago
if (argv.help || ids.length === 0) {
help();
process.exit(0);
9 years ago
}
// options
const debug = argv.debug;
const apiUrl = argv.url || "https://api.zeit.co";
const hard = argv.hard || false;
const skipConfirmation = argv.yes || false;
if (argv.config) {
cfg.setConfigFile(argv.config);
}
const config = cfg.read();
function readConfirmation(matches) {
return new Promise(resolve => {
process.stdout.write(
`> The following deployment${matches.length === 1 ? "" : "s"} will be removed permanently:\n`
);
const tbl = table(
matches.map(depl => {
const time = chalk.gray(ms(new Date() - depl.created) + " ago");
const url = depl.url ? chalk.underline(`https://${depl.url}`) : "";
return [depl.uid, url, time];
}),
{ align: ["l", "r", "l"], hsep: " ".repeat(6) }
);
process.stdout.write(tbl + "\n");
for (const depl of matches) {
for (const 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`
);
}
}
process.stdout.write(
`${chalk.bold.red("> Are you sure?")} ${chalk.gray("[y/N] ")}`
);
process.stdin
.on("data", d => {
process.stdin.pause();
resolve(d.toString().trim());
})
.resume();
});
}
Promise.resolve(argv.token || config.token || login(apiUrl))
.then(async token => {
try {
await remove(token);
} catch (err) {
error(`Unknown error: ${err}\n${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 matches = deployments.filter(d => {
return ids.find(id => {
// Normalize URL by removing slash from the end
if (isURL(id) && id.slice(-1) === "/") {
id = id.slice(0, -1);
}
// `url` should match the hostname of the deployment
let u = id.replace(/^https:\/\//i, "");
if (u.indexOf(".") === -1) {
// `.now.sh` domain is implied if just the subdomain is given
u += ".now.sh";
}
return d.uid === id || d.name === id || d.url === u;
});
});
if (matches.length === 0) {
error(
`Could not find any deployments matching ${ids
.map(id => chalk.bold(`"${id}"`))
.join(", ")}. Run ${chalk.dim(`\`now ls\``)} to list.`
);
return process.exit(1);
Add zeit world (wip) (#67) * alias: clean up the alias (trailing and leading dots) * alias: improve domain validation and implement zeit.world * is-zeit-world: detect valid zeit.world nameservers * package: add domain-regex dep * now-alias: fix edge case with older aliases or removed deployments * alias: move listing aliases and retrying to `index` * index: generalize retrying and alias listing * alias: remove `retry` dep * now-remove: print out alias warning * typo * now-alias: prevent double lookup * now: add domain / domains command * now-deploy: document `domain` * agent: allow for tls-less, agent-less requests while testing * is-zeit-world: fix nameserver typo * dns: as-promised * now-alias: fix `rm` table * now-alias: no longer admit argument after `alias ls` @rase- please verify this, but I think it was overkill? * admit "aliases" as an alternative to alias * make domain name resolution, creation and verification reusable * index: add nameserver discover, domain setup functions (reused between alias and domains) * now-domains: add command * domains: commands * package: bump eslint * now-alias: simplify sort * now-domains: sort list * now-domains: improve deletion and output of empty elements * errors: improve output * domains: add more debug output * domains: extra logging * errors: improve logging * now-remove: improve `now rm` error handling * index: more reasonable retrying * alias: support empty dns configurations * dns: remove ns fn * alias: improve post-dns-editing verification * index: remove unneeded dns lookup * index: implement new `getNameservers` * index: customizable retries * alias: improve error * improve error handling and cert retrying * customizable retries * alias: better error handling * alias: display both error messages * better error handling * improve error handling for certificate verification errors * alias: set up a `*` CNAME to simplify further aliases * alias: fewer retries for certs, more spaced out (prevent rate limiting issues) * alias: ultimate error handling * add whois fallback * adjust timer labels for whois fallback * index: whois fallback also upon 500 errors * alias: fix error message * fix duplicate aliases declaration
9 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];
}
try {
if (!skipConfirmation) {
const confirmation = (await readConfirmation(matches)).toLowerCase();
if (confirmation !== "y" && confirmation !== "yes") {
console.log("\n> Aborted");
process.exit(0);
}
}
const start = new Date();
await Promise.all(matches.map(depl => now.remove(depl.uid, { hard })));
const elapsed = ms(new Date() - start);
console.log(`${chalk.cyan("> Success!")} [${elapsed}]`);
console.log(
table(
matches.map(depl => {
return [`Deployment ${chalk.bold(depl.uid)} removed`];
})
)
);
} catch (err) {
handleError(err);
process.exit(1);
}
now.close();
}