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.
279 lines
7.9 KiB
279 lines
7.9 KiB
9 years ago
|
#!/usr/bin/env node
|
||
8 years ago
|
|
||
8 years ago
|
// Packages
|
||
9 years ago
|
import chalk from 'chalk';
|
||
9 years ago
|
import minimist from 'minimist';
|
||
9 years ago
|
import table from 'text-table';
|
||
|
import ms from 'ms';
|
||
8 years ago
|
|
||
|
// Ours
|
||
|
import strlen from '../lib/strlen';
|
||
9 years ago
|
import NowAlias from '../lib/alias';
|
||
|
import login from '../lib/login';
|
||
|
import * as cfg from '../lib/cfg';
|
||
|
import { error } from '../lib/error';
|
||
9 years ago
|
import toHost from '../lib/to-host';
|
||
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'],
|
||
|
alias: {
|
||
|
help: 'h',
|
||
8 years ago
|
config: 'c',
|
||
|
debug: 'd',
|
||
|
token: 't'
|
||
9 years ago
|
}
|
||
9 years ago
|
});
|
||
9 years ago
|
const subcommand = argv._[0];
|
||
|
|
||
|
// options
|
||
|
const help = () => {
|
||
|
console.log(`
|
||
9 years ago
|
${chalk.bold('𝚫 now alias')} <ls | set | rm> <deployment> <alias>
|
||
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
|
|
||
9 years ago
|
${chalk.gray('–')} Lists all your aliases:
|
||
9 years ago
|
|
||
|
${chalk.cyan('$ now alias ls')}
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.gray('–')} Adds a new alias to ${chalk.underline('my-api.now.sh')}:
|
||
9 years ago
|
|
||
|
${chalk.cyan(`$ now alias set ${chalk.underline('api-ownv3nc9f8.now.sh')} ${chalk.underline('my-api.now.sh')}`)}
|
||
9 years ago
|
|
||
9 years ago
|
The ${chalk.dim('`.now.sh`')} suffix can be ommited:
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.cyan('$ now alias set api-ownv3nc9f8 my-api')}
|
||
9 years ago
|
|
||
|
The deployment id can be used as the source:
|
||
|
|
||
9 years ago
|
${chalk.cyan('$ now alias set deploymentId my-alias')}
|
||
9 years ago
|
|
||
9 years ago
|
Custom domains work as alias targets:
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.cyan(`$ now alias set ${chalk.underline('api-ownv3nc9f8.now.sh')} ${chalk.underline('my-api.com')}`)}
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.dim('–')} The subcommand ${chalk.dim('`set`')} is the default and can be skipped.
|
||
9 years ago
|
${chalk.dim('–')} ${chalk.dim('`http(s)://`')} in the URLs is unneeded / ignored.
|
||
9 years ago
|
|
||
9 years ago
|
${chalk.gray('–')} Removing an alias:
|
||
9 years ago
|
|
||
|
${chalk.cyan('$ now alias rm aliasId')}
|
||
9 years ago
|
|
||
9 years ago
|
To get the list of alias ids, use ${chalk.dim('`now alias ls`')}.
|
||
9 years ago
|
|
||
|
${chalk.dim('Alias:')} ln
|
||
9 years ago
|
`);
|
||
|
};
|
||
|
|
||
9 years ago
|
// options
|
||
9 years ago
|
const debug = argv.debug;
|
||
9 years ago
|
const apiUrl = argv.url || 'https://api.zeit.co';
|
||
8 years ago
|
if (argv.config) cfg.setConfigFile(argv.config);
|
||
9 years ago
|
|
||
9 years ago
|
const exit = (code) => {
|
||
|
// we give stdout some time to flush out
|
||
9 years ago
|
// because there's a node bug where
|
||
9 years ago
|
// stdout writes are asynchronous
|
||
|
// https://github.com/nodejs/node/issues/6456
|
||
|
setTimeout(() => process.exit(code || 0), 100);
|
||
|
};
|
||
9 years ago
|
|
||
9 years ago
|
if (argv.help || !subcommand) {
|
||
9 years ago
|
help();
|
||
9 years ago
|
exit(0);
|
||
|
} else {
|
||
|
const config = cfg.read();
|
||
|
|
||
8 years ago
|
Promise.resolve(argv.token || config.token || login(apiUrl))
|
||
9 years ago
|
.then(async (token) => {
|
||
|
try {
|
||
|
await run(token);
|
||
|
} catch (err) {
|
||
9 years ago
|
if (err.userError) {
|
||
|
error(err.message);
|
||
|
} else {
|
||
|
error(`Unknown error: ${err.stack}`);
|
||
|
}
|
||
9 years ago
|
exit(1);
|
||
|
}
|
||
|
})
|
||
|
.catch((e) => {
|
||
|
error(`Authentication error – ${e.message}`);
|
||
|
exit(1);
|
||
|
});
|
||
9 years ago
|
}
|
||
|
|
||
|
async function run (token) {
|
||
|
const alias = new NowAlias(apiUrl, token, { debug });
|
||
9 years ago
|
const args = argv._.slice(1);
|
||
9 years ago
|
|
||
|
switch (subcommand) {
|
||
9 years ago
|
case 'list':
|
||
9 years ago
|
case 'ls':
|
||
9 years ago
|
if (0 !== args.length) {
|
||
8 years ago
|
error(`Invalid number of arguments. Usage: ${chalk.cyan('`now alias ls`')}`);
|
||
9 years ago
|
return exit(1);
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
const start_ = new Date();
|
||
9 years ago
|
const list = await alias.list();
|
||
|
const urls = new Map(list.map(l => [l.uid, l.url]));
|
||
|
const aliases = await alias.ls();
|
||
|
aliases.sort((a, b) => new Date(b.created) - new Date(a.created));
|
||
9 years ago
|
const current = new Date();
|
||
9 years ago
|
|
||
8 years ago
|
const header = [['', 'id', 'source', 'url', 'created'].map(s => chalk.dim(s))];
|
||
8 years ago
|
const text = 0 === list.length ? null : table(header.concat(aliases.map((_alias) => {
|
||
9 years ago
|
const _url = chalk.underline(`https://${_alias.alias}`);
|
||
|
const target = _alias.deploymentId;
|
||
|
const _sourceUrl = urls.get(target)
|
||
|
? chalk.underline(`https://${urls.get(target)}`)
|
||
|
: chalk.gray('<null>');
|
||
|
|
||
|
const time = chalk.gray(ms(current - new Date(_alias.created)) + ' ago');
|
||
|
return [
|
||
|
'',
|
||
|
// we default to `''` because some early aliases didn't
|
||
|
// have an uid associated
|
||
|
null == _alias.uid ? '' : _alias.uid,
|
||
|
_sourceUrl,
|
||
|
_url,
|
||
|
time
|
||
|
];
|
||
8 years ago
|
})), { align: ['l', 'r', 'l', 'l'], hsep: ' '.repeat(2), stringLength: strlen });
|
||
9 years ago
|
|
||
8 years ago
|
const elapsed_ = ms(new Date() - start_);
|
||
8 years ago
|
console.log(`> ${aliases.length} alias${aliases.length !== 1 ? 'es' : ''} found ${chalk.gray(`[${elapsed_}]`)}`);
|
||
9 years ago
|
if (text) console.log('\n' + text + '\n');
|
||
9 years ago
|
break;
|
||
9 years ago
|
|
||
|
case 'remove':
|
||
9 years ago
|
case 'rm':
|
||
9 years ago
|
const _target = String(args[0]);
|
||
|
if (!_target) {
|
||
|
const err = new Error('No alias id specified');
|
||
|
err.userError = true;
|
||
|
throw err;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (1 !== args.length) {
|
||
8 years ago
|
error(`Invalid number of arguments. Usage: ${chalk.cyan('`now alias rm <id>`')}`);
|
||
9 years ago
|
return exit(1);
|
||
|
}
|
||
|
|
||
9 years ago
|
const _aliases = await alias.ls();
|
||
|
const _alias = findAlias(_target, _aliases);
|
||
|
|
||
|
if (!_alias) {
|
||
|
const err = new Error(`Alias not found by "${_target}". Run ${chalk.dim('`now alias ls`')} to see your aliases.`);
|
||
|
err.userError = true;
|
||
|
throw err;
|
||
|
}
|
||
|
|
||
|
try {
|
||
9 years ago
|
const confirmation = (await readConfirmation(alias, _alias, _aliases)).toLowerCase();
|
||
9 years ago
|
if ('y' !== confirmation && 'yes' !== confirmation) {
|
||
|
console.log('\n> Aborted');
|
||
|
process.exit(0);
|
||
|
}
|
||
|
|
||
|
const start = new Date();
|
||
|
await alias.rm(_alias);
|
||
|
const elapsed = ms(new Date() - start);
|
||
|
console.log(`${chalk.cyan('> Success!')} Alias ${chalk.bold(_alias.uid)} removed [${elapsed}]`);
|
||
|
} catch (err) {
|
||
|
error(err);
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
9 years ago
|
break;
|
||
9 years ago
|
|
||
9 years ago
|
case 'add':
|
||
9 years ago
|
case 'set':
|
||
9 years ago
|
if (2 !== args.length) {
|
||
8 years ago
|
error(`Invalid number of arguments. Usage: ${chalk.cyan('`now alias set <id> <domain>`')}`);
|
||
9 years ago
|
return exit(1);
|
||
9 years ago
|
}
|
||
9 years ago
|
await alias.set(String(args[0]), String(args[1]));
|
||
9 years ago
|
break;
|
||
9 years ago
|
|
||
9 years ago
|
default:
|
||
9 years ago
|
if (2 === argv._.length) {
|
||
|
await alias.set(String(argv._[0]), String(argv._[1]));
|
||
|
} else if (argv._.length >= 3) {
|
||
9 years ago
|
error('Invalid number of arguments');
|
||
|
help();
|
||
|
exit(1);
|
||
|
} else {
|
||
|
error('Please specify a valid subcommand: ls | set | rm');
|
||
|
help();
|
||
|
exit(1);
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
alias.close();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
function indent (text, n) {
|
||
|
return text.split('\n').map((l) => ' '.repeat(n) + l).join('\n');
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
async function readConfirmation (alias, _alias, list) {
|
||
|
const deploymentsList = await alias.list();
|
||
|
const urls = new Map(deploymentsList.map(l => [l.uid, l.url]));
|
||
9 years ago
|
|
||
9 years ago
|
return new Promise((resolve, reject) => {
|
||
|
const time = chalk.gray(ms(new Date() - new Date(_alias.created)) + ' ago');
|
||
9 years ago
|
const _sourceUrl = chalk.underline(`https://${urls.get(_alias.deploymentId)}`);
|
||
9 years ago
|
const tbl = table(
|
||
9 years ago
|
[[_alias.uid, _sourceUrl, chalk.underline(`https://${_alias.alias}`), time]],
|
||
9 years ago
|
{ align: ['l', 'r', 'l'], hsep: ' '.repeat(6) }
|
||
|
);
|
||
|
|
||
9 years ago
|
process.stdout.write('> The following alias will be removed permanently\n');
|
||
9 years ago
|
process.stdout.write(' ' + tbl + '\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();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function findAlias (alias, list) {
|
||
|
let key, val;
|
||
|
if (/\./.test(alias)) {
|
||
|
val = toHost(alias);
|
||
|
key = 'alias';
|
||
|
} else {
|
||
|
val = alias;
|
||
|
key = 'uid';
|
||
|
}
|
||
|
|
||
|
const _alias = list.find((d) => {
|
||
|
if (d[key] === val) {
|
||
|
if (debug) console.log(`> [debug] matched alias ${d.uid} by ${key} ${val}`);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// match prefix
|
||
|
if (`${val}.now.sh` === d.alias) {
|
||
|
if (debug) console.log(`> [debug] matched alias ${d.uid} by url ${d.host}`);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
});
|
||
|
|
||
|
return _alias;
|
||
|
}
|