cli.js 4.6 KB

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