cli.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Simulates a device. Choose between phone (default), tablet, desktop and desktop-hd.',
  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. ' --block-domain Disallow requests to given (comma-separated) domains.',
  21. ' --allow-domain Only allow requests to given (comma-separated) domains.',
  22. ' --no-externals Block all domains except the main one.',
  23. ' --reporter The output format: "json" or "xml". Default is "json".',
  24. ''
  25. ].join('\n'),
  26. pkg: require('../package.json')
  27. });
  28. // Check parameters
  29. if (cli.input.length < 1) {
  30. cli.showHelp();
  31. }
  32. var url = cli.input[0];
  33. var options = {};
  34. // Screenshot option
  35. var screenshot = cli.flags.screenshot;
  36. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  37. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  38. process.exit(1);
  39. }
  40. if (screenshot) {
  41. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  42. // It is not an absolute path, so it is relative to the current command-line directory
  43. screenshot = path.join(process.cwd(), screenshot);
  44. }
  45. options.screenshot = cli.flags.screenshot;
  46. }
  47. // Device simulation
  48. options.device = cli.flags.device || 'mobile';
  49. // Wait for CSS selector
  50. options.waitForSelector = cli.flags.waitForSelector || null;
  51. // Proxy
  52. options.proxy = cli.flags.proxy || null;
  53. // Cookie
  54. options.cookie = cli.flags.cookie || null;
  55. // HTTP basic auth
  56. options.authUser = cli.flags.authUser || null;
  57. options.authPass = cli.flags.authPass || null;
  58. // Domain blocking
  59. options.blockDomain = cli.flags.blockDomain || null;
  60. options.allowDomain = cli.flags.allowDomain || null;
  61. options.noExternals = cli.flags.noExternals || null;
  62. // Output format
  63. if (cli.flags.reporter && cli.flags.reporter !== 'json' && cli.flags.reporter !== 'xml') {
  64. console.error('Incorrect parameters: reporter has to be "json" or "xml"');
  65. process.exit(1);
  66. }
  67. (function execute(url, options) {
  68. 'use strict';
  69. ylt(url, options).
  70. then(function(data) {
  71. debug('Success');
  72. switch(cli.flags.reporter) {
  73. case 'xml':
  74. var serializer = new EasyXml({
  75. manifest: true
  76. });
  77. // Remove some heavy parts of the results object
  78. delete data.toolsResults;
  79. delete data.javascriptExecutionTree;
  80. var xmlOutput = serializer.render(data);
  81. // Remove special chars from XML tags: # [ ]
  82. xmlOutput = xmlOutput.replace(/<([^>]*)#([^>]*)>/g, '<$1>');
  83. xmlOutput = xmlOutput.replace(/<([^>]*)\[([^>]*)>/g, '<$1>');
  84. xmlOutput = xmlOutput.replace(/<([^>]*)\]([^>]*)>/g, '<$1>');
  85. // Remove special chars from text content: \n \0
  86. xmlOutput = xmlOutput.replace(/(<[a-zA-Z]*>[^<]*)\n([^<]*<\/[a-zA-Z]*>)/g, '$1$2');
  87. xmlOutput = xmlOutput.replace(/\0/g, '');
  88. xmlOutput = xmlOutput.replace(/\uFFFF/g, '');
  89. xmlOutput = xmlOutput.replace(/\u0002/g, '');
  90. console.log(xmlOutput);
  91. break;
  92. default:
  93. console.log(JSON.stringify(data, null, 2));
  94. }
  95. }).fail(function(err) {
  96. debug('Test failed for %s', url);
  97. console.error(err);
  98. });
  99. debug('Test launched...');
  100. })(url, options);