cli.js 4.8 KB

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