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.
142 lines
3.4 KiB
142 lines
3.4 KiB
9 years ago
|
#!/usr/bin/env node
|
||
|
|
||
8 years ago
|
// Packages
|
||
9 years ago
|
import fs from 'fs-promise';
|
||
9 years ago
|
import minimist from 'minimist';
|
||
9 years ago
|
import chalk from 'chalk';
|
||
|
import table from 'text-table';
|
||
|
import ms from 'ms';
|
||
8 years ago
|
|
||
|
// Ours
|
||
|
import strlen from '../lib/strlen';
|
||
9 years ago
|
import Now from '../lib';
|
||
|
import login from '../lib/login';
|
||
|
import * as cfg from '../lib/cfg';
|
||
8 years ago
|
import {handleError, error} from '../lib/error';
|
||
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 help = () => {
|
||
|
console.log(`
|
||
9 years ago
|
${chalk.bold('𝚫 now list')} [app]
|
||
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:')}
|
||
|
|
||
|
${chalk.gray('–')} List all deployments
|
||
|
|
||
|
${chalk.cyan('$ now ls')}
|
||
|
|
||
|
${chalk.gray('–')} List all deployments for the app ${chalk.dim('`my-app`')}
|
||
|
|
||
|
${chalk.cyan('$ now ls my-app')}
|
||
|
|
||
|
${chalk.dim('Alias:')} ls
|
||
9 years ago
|
`);
|
||
|
};
|
||
|
|
||
9 years ago
|
if (argv.help) {
|
||
9 years ago
|
help();
|
||
|
process.exit(0);
|
||
|
}
|
||
|
|
||
9 years ago
|
const app = argv._[0];
|
||
|
|
||
|
// 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
|
const config = cfg.read();
|
||
|
|
||
8 years ago
|
Promise.resolve(argv.token || config.token || login(apiUrl))
|
||
9 years ago
|
.then(async (token) => {
|
||
|
try {
|
||
|
await list(token);
|
||
|
} catch (err) {
|
||
|
error(`Unknown error: ${err.stack}`);
|
||
|
process.exit(1);
|
||
|
}
|
||
|
})
|
||
|
.catch((e) => {
|
||
|
error(`Authentication error – ${e.message}`);
|
||
|
process.exit(1);
|
||
|
});
|
||
|
|
||
|
async function list (token) {
|
||
|
const now = new Now(apiUrl, token, { debug });
|
||
8 years ago
|
const start = new Date();
|
||
9 years ago
|
|
||
|
let deployments;
|
||
|
try {
|
||
|
deployments = await now.list(app);
|
||
|
} catch (err) {
|
||
|
handleError(err);
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
9 years ago
|
now.close();
|
||
|
|
||
|
const apps = new Map();
|
||
|
for (const dep of deployments) {
|
||
|
const deps = apps.get(dep.name) || [];
|
||
|
apps.set(dep.name, deps.concat(dep));
|
||
|
}
|
||
|
|
||
9 years ago
|
const sorted = await sort([...apps]);
|
||
|
|
||
9 years ago
|
const current = Date.now();
|
||
9 years ago
|
const text = sorted.map(([name, deps]) => {
|
||
9 years ago
|
const t = table(deps.map(({ uid, url, created }) => {
|
||
9 years ago
|
const _url = chalk.underline(`https://${url}`);
|
||
9 years ago
|
const time = chalk.gray(ms(current - created) + ' ago');
|
||
9 years ago
|
return [uid, _url, time];
|
||
8 years ago
|
}), { align: ['l', 'r', 'l'], hsep: ' '.repeat(6), stringLength: strlen });
|
||
9 years ago
|
return chalk.bold(name) + '\n\n' + indent(t, 2);
|
||
9 years ago
|
}).join('\n\n');
|
||
8 years ago
|
const elapsed = ms(new Date() - start);
|
||
8 years ago
|
console.log(`> ${deployments.length} deployment${deployments.length !== 1 ? 's' : ''} found ${chalk.gray(`[${elapsed}]`)}`);
|
||
9 years ago
|
if (text) console.log('\n' + text + '\n');
|
||
|
}
|
||
|
|
||
9 years ago
|
async function sort (apps) {
|
||
|
let pkg;
|
||
|
try {
|
||
|
const json = await fs.readFile('package.json');
|
||
|
pkg = JSON.parse(json);
|
||
|
} catch (err) {
|
||
|
pkg = {};
|
||
|
}
|
||
|
|
||
|
return apps
|
||
|
.map(([name, deps]) => {
|
||
|
deps = deps.slice().sort((a, b) => {
|
||
|
return b.created - a.created;
|
||
|
});
|
||
|
return [name, deps];
|
||
|
})
|
||
|
.sort(([nameA, depsA], [nameB, depsB]) => {
|
||
|
if (pkg.name === nameA) return -1;
|
||
|
if (pkg.name === nameB) return 1;
|
||
|
return depsB[0].created - depsA[0].created;
|
||
|
});
|
||
|
}
|
||
|
|
||
9 years ago
|
function indent (text, n) {
|
||
|
return text.split('\n').map((l) => ' '.repeat(n) + l).join('\n');
|
||
9 years ago
|
}
|