Browse Source

Add very basic `now-alias` structure

master
Tony Kovanen 9 years ago
parent
commit
cdcded8b95
  1. 4
      bin/now
  2. 122
      bin/now-alias
  3. 37
      lib/alias.js

4
bin/now

@ -19,8 +19,8 @@ const exit = (code) => {
};
const defaultCommand = 'deploy';
const commands = new Set([defaultCommand, 'list', 'ls', 'rm', 'remove']);
const aliases = new Map([['ls', 'list'], ['rm', 'remove']]);
const commands = new Set([defaultCommand, 'list', 'ls', 'rm', 'remove', 'alias']);
const aliases = new Map([['ls', 'list'], ['rm', 'remove'], ['ln', 'alias']]);
let cmd = argv._[0];
let args = [];

122
bin/now-alias

@ -0,0 +1,122 @@
#!/usr/bin/env node
import minimist from 'minimist';
// import chalk from 'chalk';
// import ms from 'ms';
// import table from 'text-table';
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));
const subcommand = argv._[0];
// options
const help = () => {
console.log(`
𝚫 now alias <ls | set | rm> <now_url> <alias> [≤alias2>[, <alias3>]]
Alias: ln
Options:
-h, --help output usage information
-d, --debug Debug mode [off]
Examples:
Lists all the aliases you have configured:
$ now alias ls
Adds a particular alias to my-api.now.sh:
$ now alias set api-ownv3nc9f8.now.sh my-api.now.sh
The \`.now.sh\` suffix can be ommited:
$ now alias set api-ownv3nc9f8 my-api
The deployment id can be used as the source:
$ now alias set deploymentId my-alias
Custom domains work as well. They will ask you for your email
address as we dynamically provision a certificate:
$ now alias set api-ownv3nc9f8.now.sh my-api.com
You can optionally put \`http://\` or \`https://\` in front of the
parameters and they will be ignored.
Removing an alias:
$ now alias rm aliasId
To get the list of alias ids, use \`now alias ls\`.
`);
};
if (argv.h || argv.help) {
help();
process.exit(0);
}
if (!subcommand) {
error('No subcommand specified');
help();
process.exit(1);
}
// options
const debug = argv.debug || argv.d;
const apiUrl = argv.url || 'https://api.zeit.co';
const config = cfg.read();
Promise.resolve(config.token || login(apiUrl))
.then(async (token) => {
try {
await run(token);
} catch (err) {
error(`Unknown error: ${err.stack}`);
process.exit(1);
}
})
.catch((e) => {
error(`Authentication error – ${e.message}`);
process.exit(1);
});
function getArgs (args) {
return { url: args[1], aliases: args.slice(2) };
}
async function run (token) {
const alias = new NowAlias(apiUrl, token, { debug });
const { url, aliases } = getArgs(argv._);
if ('ls' !== subcommand) {
if (!url) {
error('No deployment url specified. You can see active deployments with `now ls`.');
help();
process.exit(1);
} else if (!aliases.length) {
error('No alias url specified. This is the URL your deployment will be tied to.');
}
}
switch (subcommand) {
case 'ls':
alias.ls(url);
break;
case 'rm':
alias.rm(url, aliases);
break;
case 'set':
alias.set(url, aliases);
break;
default:
error('Invalid subcommand');
help();
process.exit();
}
}

37
lib/alias.js

@ -0,0 +1,37 @@
import EventEmitter from 'events';
import Agent from './agent';
export default class Now extends EventEmitter {
constructor (url, token, { forceNew = false, debug = false }) {
super();
this._token = token;
this._debug = debug;
this._forceNew = forceNew;
this._agent = new Agent(url, { debug });
this._onRetry = this._onRetry.bind(this);
}
ls (url) {
console.log('list', url);
}
rm (url, aliases) {
console.log('rm', url, aliases);
}
set (url, aliases) {
console.log('set', url, aliases);
}
_onRetry (err) {
if (this._debug) {
console.log(`> [debug] Retrying: ${err.stack}`);
}
}
async _fetch (_url, opts = {}) {
opts.headers = opts.headers || {};
opts.headers.authorization = `Bearer ${this._token}`;
return await this._agent.fetch(_url, opts);
}
}
Loading…
Cancel
Save