cli.js 4.2 KB

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