cli.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env node
  2. var debug = require('debug')('ylt:cli');
  3. var meow = require('meow');
  4. var path = require('path');
  5. var jstoxml = require('jstoxml');
  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. ' --js-deep-analysis When activated, the javascriptExecutionTree will contain sub-requests.',
  16. ' --wait-for-selector Once the page is loaded, Phantomas will wait until the given CSS selector matches some elements.',
  17. ' --cookie Adds a cookie on the main domain.',
  18. ' --auth-user Basic HTTP authentication username.',
  19. ' --auth-pass Basic HTTP authentication password.',
  20. ' --reporter The output format: "json" or "xml". Default is "json".',
  21. ''
  22. ].join('\n'),
  23. pkg: '../package.json'
  24. });
  25. // Check parameters
  26. if (cli.input.length < 1) {
  27. cli.showHelp();
  28. }
  29. var url = cli.input[0];
  30. var options = {};
  31. // Screenshot option
  32. var screenshot = cli.flags.screenshot;
  33. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  34. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  35. process.exit(1);
  36. }
  37. if (screenshot) {
  38. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  39. // It is not an absolute path, so it is relative to the current command-line directory
  40. screenshot = path.join(process.cwd(), screenshot);
  41. }
  42. options.screenshot = cli.flags.screenshot;
  43. }
  44. // Deep JS analysis option
  45. if (cli.flags.jsDeepAnalysis === true || cli.flags.jsDeepAnalysis === 'true') {
  46. options.jsDeepAnalysis = true;
  47. }
  48. // Device simulation
  49. options.device = cli.flags.device || 'desktop';
  50. // Wait for CSS selector
  51. options.waitForSelector = cli.flags.waitForSelector || null;
  52. // Cookie
  53. options.cookie = cli.flags.cookie || null;
  54. // HTTP basic auth
  55. options.authUser = cli.flags.authUser || null;
  56. options.authPass = cli.flags.authPass || null;
  57. // Output format
  58. if (cli.flags.reporter && cli.flags.reporter !== 'json' && cli.flags.reporter !== 'xml') {
  59. console.error('Incorrect parameters: reporter has to be "json" or "xml"');
  60. process.exit(1);
  61. }
  62. (function execute(url, options) {
  63. 'use strict';
  64. ylt(url, options).
  65. then(function(data) {
  66. debug('Success');
  67. switch(cli.flags.reporter) {
  68. case 'xml':
  69. console.log(jstoxml.toXML(data, {indent: ' '}));
  70. break;
  71. default:
  72. console.log(JSON.stringify(data, null, 2));
  73. }
  74. }).fail(function(err) {
  75. debug('Test failed for %s', url);
  76. console.error(err);
  77. });
  78. debug('Test launched...');
  79. })(url, options);