|
|
|
#!/usr/bin/env node
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import minimist from 'minimist';
|
|
|
|
import table from 'text-table';
|
|
|
|
import ms from 'ms';
|
|
|
|
import NowAlias from '../lib/alias';
|
|
|
|
import login from '../lib/login';
|
|
|
|
import * as cfg from '../lib/cfg';
|
|
|
|
import { error } from '../lib/error';
|
|
|
|
|
|
|
|
const argv = minimist(process.argv.slice(2), {
|
|
|
|
boolean: ['help', 'debug'],
|
|
|
|
alias: {
|
|
|
|
help: 'h',
|
|
|
|
debug: 'd'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const subcommand = argv._[0];
|
|
|
|
|
|
|
|
// options
|
|
|
|
const help = () => {
|
|
|
|
console.log(`
|
|
|
|
${chalk.bold('𝚫 now alias')} <ls | set | rm> <deployment> <alias>
|
|
|
|
|
|
|
|
${chalk.dim('Options:')}
|
|
|
|
|
|
|
|
-h, --help output usage information
|
|
|
|
-d, --debug debug mode [off]
|
|
|
|
|
|
|
|
${chalk.dim('Examples:')}
|
|
|
|
|
|
|
|
${chalk.gray('–')} Lists all your aliases:
|
|
|
|
|
|
|
|
${chalk.cyan('$ now alias ls')}
|
|
|
|
|
|
|
|
${chalk.gray('–')} Adds a new alias to ${chalk.underline('my-api.now.sh')}:
|
|
|
|
|
|
|
|
${chalk.cyan(`$ now alias set ${chalk.underline('api-ownv3nc9f8.now.sh')} ${chalk.underline('my-api.now.sh')}`)}
|
|
|
|
|
|
|
|
The ${chalk.dim('`.now.sh`')} suffix can be ommited:
|
|
|
|
|
|
|
|
${chalk.cyan('$ now alias set api-ownv3nc9f8 my-api')}
|
|
|
|
|
|
|
|
The deployment id can be used as the source:
|
|
|
|
|
|
|
|
${chalk.cyan('$ now alias set deploymentId my-alias')}
|
|
|
|
|
|
|
|
Custom domains work as alias targets:
|
|
|
|
|
|
|
|
${chalk.cyan(`$ now alias set ${chalk.underline('api-ownv3nc9f8.now.sh')} ${chalk.underline('my-api.com')}`)}
|
|
|
|
|
|
|
|
${chalk.dim('–')} The subcommand ${chalk.dim('`set`')} is the default and can be skipped.
|
|
|
|
${chalk.dim('–')} ${chalk.dim('`http(s)://`')} in the URLs is unneeded / ignored.
|
|
|
|
|
|
|
|
${chalk.gray('–')} Removing an alias:
|
|
|
|
|
|
|
|
${chalk.cyan('$ now alias rm aliasId')}
|
|
|
|
|
|
|
|
To get the list of alias ids, use ${chalk.dim('`now alias ls`')}.
|
|
|
|
|
|
|
|
${chalk.dim('Alias:')} ln
|
|
|
|
`);
|
|
|
|
};
|
|
|
|
|
|
|
|
// options
|
|
|
|
const debug = argv.debug;
|
|
|
|
const apiUrl = argv.url || 'https://api.zeit.co';
|
|
|
|
|
|
|
|
const exit = (code) => {
|
|
|
|
// we give stdout some time to flush out
|
|
|
|
// because there's a node bug where
|
|
|
|
// stdout writes are asynchronous
|
|
|
|
// https://github.com/nodejs/node/issues/6456
|
|
|
|
setTimeout(() => process.exit(code || 0), 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (argv.help || !subcommand) {
|
|
|
|
help();
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
const config = cfg.read();
|
|
|
|
|
|
|
|
Promise.resolve(config.token || login(apiUrl))
|
|
|
|
.then(async (token) => {
|
|
|
|
try {
|
|
|
|
await run(token);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.userError) {
|
|
|
|
error(err.message);
|
|
|
|
} else {
|
|
|
|
error(`Unknown error: ${err.stack}`);
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
error(`Authentication error – ${e.message}`);
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run (token) {
|
|
|
|
const alias = new NowAlias(apiUrl, token, { debug });
|
|
|
|
const args = argv._.slice(1);
|
|
|
|
|
|
|
|
switch (subcommand) {
|
|
|
|
case 'list':
|
|
|
|
case 'ls':
|
|
|
|
const target = null != args[0] ? String(args[0]) : null;
|
|
|
|
const aliases = await alias.ls(target);
|
|
|
|
const byTarget = new Map();
|
|
|
|
if (target) {
|
|
|
|
byTarget.set(target, aliases);
|
|
|
|
} else {
|
|
|
|
aliases.forEach((_alias) => {
|
|
|
|
const _aliases = byTarget.get(_alias.deploymentId) || [];
|
|
|
|
byTarget.set(_alias.deploymentId, _aliases.concat(_alias));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const sorted = await sort([...byTarget]);
|
|
|
|
|
|
|
|
const current = Date.now();
|
|
|
|
const text = sorted.map(([target, _aliases]) => {
|
|
|
|
const t = table(_aliases.map((_alias) => {
|
|
|
|
const _url = chalk.underline(`https://${_alias.alias}`);
|
|
|
|
const time = chalk.gray(ms(current - _alias.created) + ' ago');
|
|
|
|
return [_alias.uid, _url, time];
|
|
|
|
}), { align: ['l', 'r', 'l'], hsep: ' '.repeat(6) });
|
|
|
|
return chalk.bold(target) + '\n\n' + indent(t, 2);
|
|
|
|
}).join('\n\n');
|
|
|
|
|
|
|
|
if (text) console.log('\n' + text + '\n');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'remove':
|
|
|
|
case 'rm':
|
|
|
|
await alias.rm(String(args[0]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'add':
|
|
|
|
case 'set':
|
|
|
|
if (2 !== args.length) {
|
|
|
|
error('Invalid number of arguments');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await alias.set(String(args[0]), String(args[1]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (2 === args.length) {
|
|
|
|
await alias.set(String(args[0]), String(args[1]));
|
|
|
|
} else if (args.length >= 3) {
|
|
|
|
error('Invalid number of arguments');
|
|
|
|
help();
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
error('Please specify a valid subcommand: ls | set | rm');
|
|
|
|
help();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sort (aliases) {
|
|
|
|
let pkg;
|
|
|
|
try {
|
|
|
|
const json = await fs.readFile('package.json');
|
|
|
|
pkg = JSON.parse(json);
|
|
|
|
} catch (err) {
|
|
|
|
pkg = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return aliases
|
|
|
|
.map(([target, _aliases]) => {
|
|
|
|
_aliases = _aliases.slice().sort((a, b) => {
|
|
|
|
return b.created - a.created;
|
|
|
|
});
|
|
|
|
return [target, _aliases];
|
|
|
|
})
|
|
|
|
.sort(([targetA, aliasesA], [targetB, aliasesB]) => {
|
|
|
|
if (pkg.name === targetA) return -1;
|
|
|
|
if (pkg.name === targetB) return 1;
|
|
|
|
return aliasesB[0].created - aliasesA[0].created;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function indent (text, n) {
|
|
|
|
return text.split('\n').map((l) => ' '.repeat(n) + l).join('\n');
|
|
|
|
}
|