mirror of https://github.com/Seich/Beau.git
				
				
				
			
		
			
				
	
	
		
			132 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			132 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
| #!/usr/bin/env node
 | |
| const program = require('commander');
 | |
| const process = require('process');
 | |
| const Beau = require('../beau');
 | |
| const yaml = require('js-yaml');
 | |
| const fs = require('fs');
 | |
| const { Line, Spinner } = require('clui');
 | |
| const clc = require('cli-color');
 | |
| const jsome = require('jsome');
 | |
| 
 | |
| const package = require('../package.json');
 | |
| 
 | |
| program.version(package.version);
 | |
| 
 | |
| program
 | |
| 	.command('request <alias>')
 | |
| 	.option(
 | |
| 		'-c --config <config>',
 | |
| 		'Specify your request config file. Defaults to beau.yml in the current directory.',
 | |
| 		'beau.yml'
 | |
| 	)
 | |
| 	.option(
 | |
| 		'-v --verbose',
 | |
| 		'Show all the information available on the current request.',
 | |
| 		false
 | |
| 	)
 | |
| 	.option('--no-format', 'Return the text without any special formatting.')
 | |
| 	.action(async (alias, { config, format, verbose }) => {
 | |
| 		const beau = loadConfig(config);
 | |
| 		let spinner;
 | |
| 
 | |
| 		if (format) {
 | |
| 			spinner = new Spinner(clc.yellow(`Requesting: ${alias}`));
 | |
| 			spinner.start();
 | |
| 		}
 | |
| 
 | |
| 		try {
 | |
| 			let res = await beau.requests.execByAlias(alias);
 | |
| 			let { status, headers, body } = res.response;
 | |
| 			let { endpoint } = res.request;
 | |
| 
 | |
| 			if (format) {
 | |
| 				spinner.stop();
 | |
| 
 | |
| 				status = status.toString().startsWith(2)
 | |
| 					? clc.green(status)
 | |
| 					: clc.red(status);
 | |
| 
 | |
| 				new Line()
 | |
| 					.padding(2)
 | |
| 					.column('Status', 20, [clc.cyan])
 | |
| 					.column('Endpoint', 20, [clc.cyan])
 | |
| 					.output();
 | |
| 
 | |
| 				new Line()
 | |
| 					.padding(2)
 | |
| 					.column(status, 20)
 | |
| 					.column(endpoint)
 | |
| 					.output();
 | |
| 
 | |
| 				new Line().output();
 | |
| 
 | |
| 				if (verbose) {
 | |
| 					jsome(res);
 | |
| 				} else {
 | |
| 					jsome(body);
 | |
| 				}
 | |
| 			} else {
 | |
| 				console.log(status);
 | |
| 				console.log(endpoint);
 | |
| 				console.log(JSON.stringify(headers));
 | |
| 				console.log(JSON.stringify(body));
 | |
| 			}
 | |
| 
 | |
| 			process.exit(0);
 | |
| 		} catch (err) {
 | |
| 			new Line().output();
 | |
| 			console.error(err.message);
 | |
| 			process.exit(1);
 | |
| 		}
 | |
| 	});
 | |
| 
 | |
| program
 | |
| 	.command('list')
 | |
| 	.option(
 | |
| 		'-c --config <config>',
 | |
| 		'Specify your request config file. Defaults to beau.yml in the current directory.',
 | |
| 		'beau.yml'
 | |
| 	)
 | |
| 	.option('--no-format', 'Return the text without any special formatting.')
 | |
| 	.action(({ config, format }) => {
 | |
| 		const beau = loadConfig(config);
 | |
| 
 | |
| 		if (format) {
 | |
| 			new Line()
 | |
| 				.padding(2)
 | |
| 				.column('HTTP Verb', 20, [clc.cyan])
 | |
| 				.column('Alias', 30, [clc.cyan])
 | |
| 				.column('Endpoint', 20, [clc.cyan])
 | |
| 				.output();
 | |
| 
 | |
| 			beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) =>
 | |
| 				new Line()
 | |
| 					.padding(2)
 | |
| 					.column(VERB, 20, [clc.yellow])
 | |
| 					.column(ALIAS, 30, [clc.yellow])
 | |
| 					.column(ENDPOINT)
 | |
| 					.output()
 | |
| 			);
 | |
| 
 | |
| 			new Line().output();
 | |
| 		} else {
 | |
| 			beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) => {
 | |
| 				console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}`);
 | |
| 			});
 | |
| 		}
 | |
| 	});
 | |
| 
 | |
| program.parse(process.argv);
 | |
| 
 | |
| if (!program.args.length) program.help();
 | |
| 
 | |
| function loadConfig(configFile) {
 | |
| 	if (!fs.existsSync(configFile)) {
 | |
| 		console.error(`The config file, ${configFile} was not found.`);
 | |
| 		process.exit(1);
 | |
| 	}
 | |
| 
 | |
| 	const config = yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
 | |
| 	return new Beau(config);
 | |
| }
 |