cli.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ' --proxy Sets an HTTP proxy to pass through. Syntax is "host:port".',
  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. // Device simulation
  45. options.device = cli.flags.device || 'desktop';
  46. // Wait for CSS selector
  47. options.waitForSelector = cli.flags.waitForSelector || null;
  48. // Proxy
  49. options.proxy = cli.flags.proxy || null;
  50. // Cookie
  51. options.cookie = cli.flags.cookie || null;
  52. // HTTP basic auth
  53. options.authUser = cli.flags.authUser || null;
  54. options.authPass = cli.flags.authPass || null;
  55. // Output format
  56. if (cli.flags.reporter && cli.flags.reporter !== 'json' && cli.flags.reporter !== 'xml') {
  57. console.error('Incorrect parameters: reporter has to be "json" or "xml"');
  58. process.exit(1);
  59. }
  60. (function execute(url, options) {
  61. 'use strict';
  62. ylt(url, options).
  63. then(function(data) {
  64. debug('Success');
  65. switch(cli.flags.reporter) {
  66. case 'xml':
  67. var serializer = new EasyXml({
  68. manifest: true
  69. });
  70. // Remove some heavy parts of the results object
  71. delete data.toolsResults;
  72. delete data.javascriptExecutionTree;
  73. var xmlOutput = serializer.render(data);
  74. // Remove special chars from XML tags: # [ ]
  75. xmlOutput = xmlOutput.replace(/<([^>]*)#([^>]*)>/g, '<$1>');
  76. xmlOutput = xmlOutput.replace(/<([^>]*)\[([^>]*)>/g, '<$1>');
  77. xmlOutput = xmlOutput.replace(/<([^>]*)\]([^>]*)>/g, '<$1>');
  78. // Remove special chars from text content: \n \0
  79. xmlOutput = xmlOutput.replace(/(<[a-zA-Z]*>[^<]*)\n([^<]*<\/[a-zA-Z]*>)/g, '$1$2');
  80. xmlOutput = xmlOutput.replace(/\0/g, '');
  81. xmlOutput = xmlOutput.replace(/\uFFFF/g, '');
  82. console.log(xmlOutput);
  83. break;
  84. default:
  85. console.log(JSON.stringify(data, null, 2));
  86. }
  87. }).fail(function(err) {
  88. debug('Test failed for %s', url);
  89. console.error(err);
  90. });
  91. debug('Test launched...');
  92. })(url, options);