mirror of https://github.com/Seich/Beau.git
				
				
				
			
		
			
				
	
	
		
			150 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			3.5 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 eyes = require('eyes');
 | |
| 
 | |
| const package = require('../package.json');
 | |
| 
 | |
| program
 | |
| 	.version(package.version)
 | |
| 	.usage(`[options] -r <Request Alias>`)
 | |
| 	.option(
 | |
| 		'-r, --request [request]',
 | |
| 		`The alias for the request you'd like to trigger.`
 | |
| 	)
 | |
| 	.option(
 | |
| 		'-R, --clean-request [request]',
 | |
| 		`The alias for the request you'd like to trigger. This one doesn't format the output.`
 | |
| 	)
 | |
| 	.option(
 | |
| 		'-v, --verbose',
 | |
| 		`Show all the information related to the current request and it's response.`
 | |
| 	)
 | |
| 	.option(
 | |
| 		'-c, --config [file]',
 | |
| 		'Specify your request config file. Defaults to beau.yml in the current directory.',
 | |
| 		'beau.yml'
 | |
| 	)
 | |
| 	.option('-l, --list', `List all requests in the config file.`)
 | |
| 	.option('-L, --clean-list', `List all requests with no formatting.`)
 | |
| 	.option(
 | |
| 		'-t, --truncate [length]',
 | |
| 		`Truncate the content to the given length`
 | |
| 	)
 | |
| 	.parse(process.argv);
 | |
| 
 | |
| if (!fs.existsSync(program.config)) {
 | |
| 	console.error(`The config file, ${program.config} was not found.`);
 | |
| 	process.exit(1);
 | |
| }
 | |
| 
 | |
| const config = yaml.safeLoad(fs.readFileSync(program.config, 'utf-8'));
 | |
| const beau = new Beau(config);
 | |
| 
 | |
| if (
 | |
| 	typeof program.list === 'undefined' &&
 | |
| 	typeof program.request === 'undefined' &&
 | |
| 	typeof program.cleanList === 'undefined' &&
 | |
| 	typeof program.cleanRequest === 'undefined'
 | |
| ) {
 | |
| 	program.help();
 | |
| }
 | |
| 
 | |
| if (program.cleanList) {
 | |
| 	beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT, DOCUMENTATION }) => {
 | |
| 		if (typeof DOCUMENTATION !== 'undefined') {
 | |
| 			let {title, description} = DOCUMENTATION;
 | |
| 			console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}\t${title}\t${description}`);
 | |
| 
 | |
| 		} else {
 | |
| 			console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}`);
 | |
| 		}
 | |
| 	});
 | |
| }
 | |
| 
 | |
| if (program.list) {
 | |
| 	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();
 | |
| }
 | |
| 
 | |
| if (program.cleanRequest) {
 | |
| 	beau.requests.execByAlias(program.cleanRequest).then(res => {
 | |
| 		let {status, headers, body} = res.response;
 | |
| 		let {endpoint} = res.request;
 | |
| 
 | |
| 		process.stdout.write(status + '\n');
 | |
| 		process.stdout.write(endpoint + '\n');
 | |
| 		process.stdout.write(JSON.stringify(headers) + '\n');
 | |
| 		process.stdout.write(JSON.stringify(body) + '\n');
 | |
| 	});
 | |
| }
 | |
| 
 | |
| if (program.request) {
 | |
| 	const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`));
 | |
| 
 | |
| 	spinner.start();
 | |
| 
 | |
| 	beau.requests
 | |
| 		.execByAlias(program.request)
 | |
| 		.then(res => {
 | |
| 			spinner.stop();
 | |
| 
 | |
| 			let { status, body } = res.response;
 | |
| 			let { endpoint } = res.request;
 | |
| 
 | |
| 			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();
 | |
| 
 | |
| 			let maxLength = +(program.truncate || res.length);
 | |
| 			let inspect = eyes.inspector({ maxLength });
 | |
| 
 | |
| 			if (program.verbose) {
 | |
| 				inspect(res);
 | |
| 			} else {
 | |
| 				inspect(body);
 | |
| 			}
 | |
| 
 | |
| 			process.exit(0);
 | |
| 		})
 | |
| 		.catch(function(err) {
 | |
| 			new Line().output();
 | |
| 			console.error(err);
 | |
| 			process.exit(1);
 | |
| 		});
 | |
| }
 |