cli.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env node
  2. var debug = require('debug')('ylt:cli');
  3. var meow = require('meow');
  4. var path = require('path');
  5. var EasyXml = require('easyxml');
  6. var ylt = require('../lib/index');
  7. var cli = meow({
  8. help: [
  9. 'Usage',
  10. ' yellowlabtools <url> <options>',
  11. '',
  12. 'Options:',
  13. ' --device Use "phone" or "tablet" to simulate a mobile device (by user-agent and viewport size).',
  14. ' --screenshot Will take a screenshot and use this value as the output path. It needs to end with ".png".',
  15. ' --wait-for-selector Once the page is loaded, Phantomas will wait until the given CSS selector matches some elements.',
  16. ' --cookie Adds a cookie on the main domain.',
  17. ' --auth-user Basic HTTP authentication username.',
  18. ' --auth-pass Basic HTTP authentication password.',
  19. ' --reporter The output format: "json" or "xml". Default is "json".',
  20. ''
  21. ].join('\n'),
  22. pkg: '../package.json'
  23. });
  24. // Check parameters
  25. if (cli.input.length < 1) {
  26. cli.showHelp();
  27. }
  28. var url = cli.input[0];
  29. var options = {};
  30. // Screenshot option
  31. var screenshot = cli.flags.screenshot;
  32. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  33. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  34. process.exit(1);
  35. }
  36. if (screenshot) {
  37. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  38. // It is not an absolute path, so it is relative to the current command-line directory
  39. screenshot = path.join(process.cwd(), screenshot);
  40. }
  41. options.screenshot = cli.flags.screenshot;
  42. }
  43. // Device simulation
  44. options.device = cli.flags.device || 'desktop';
  45. // Wait for CSS selector
  46. options.waitForSelector = cli.flags.waitForSelector || null;
  47. // Cookie
  48. options.cookie = cli.flags.cookie || null;
  49. // HTTP basic auth
  50. options.authUser = cli.flags.authUser || null;
  51. options.authPass = cli.flags.authPass || null;
  52. // Output format
  53. if (cli.flags.reporter && cli.flags.reporter !== 'json' && cli.flags.reporter !== 'xml') {
  54. console.error('Incorrect parameters: reporter has to be "json" or "xml"');
  55. process.exit(1);
  56. }
  57. (function execute(url, options) {
  58. 'use strict';
  59. ylt(url, options).
  60. then(function(data) {
  61. debug('Success');
  62. switch(cli.flags.reporter) {
  63. case 'xml':
  64. var serializer = new EasyXml();
  65. console.log(serializer.render(data));
  66. break;
  67. default:
  68. console.log(JSON.stringify(data, null, 2));
  69. }
  70. }).fail(function(err) {
  71. debug('Test failed for %s', url);
  72. console.error(err);
  73. });
  74. debug('Test launched...');
  75. })(url, options);