cli.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. var debug = require('debug')('ylt:cli');
  3. var meow = require('meow');
  4. var path = require('path');
  5. var ylt = require('../lib/index');
  6. var cli = meow({
  7. help: [
  8. 'Usage',
  9. ' yellowlabtools <url> <options>',
  10. '',
  11. 'Options:',
  12. ' --screenshot Will take a screenshot and use this value as the output path. It needs to end with ".png".',
  13. ''
  14. ].join('\n'),
  15. pkg: '../package.json'
  16. });
  17. // Check parameters
  18. if (cli.input.length < 1) {
  19. console.error('Incorrect parameters: url not provided');
  20. process.exit(1);
  21. }
  22. var url = cli.input[0];
  23. var options = {};
  24. // Screenshot option
  25. var screenshot = cli.flags.screenshot;
  26. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  27. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  28. process.exit(1);
  29. }
  30. if (screenshot) {
  31. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  32. // It is not an absolute path, so it is relative to the current command-line directory
  33. screenshot = path.join(process.cwd(), screenshot);
  34. }
  35. options.screenshot = cli.flags.screenshot
  36. }
  37. (function execute(url, options) {
  38. 'use strict';
  39. ylt(url, options).
  40. then(function(data) {
  41. debug('Success');
  42. console.log(JSON.stringify(data, null, 2));
  43. }).fail(function(err) {
  44. debug('Test failed for %s', url);
  45. console.error(err);
  46. });
  47. debug('Test launched...');
  48. })(url, options);