cli.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env node
  2. var debug = require('debug')('ylt:cli');
  3. var meow = require('meow');
  4. var path = require('path');
  5. var ylt = require('../lib/index');
  6. var cli = meow({
  7. help: [
  8. 'Usage',
  9. ' yellowlabtools <url> <options>',
  10. '',
  11. 'Options:',
  12. ' --screenshot Will take a screenshot and use this value as the output path. It needs to end with ".png".',
  13. ' --js-deep-analysis When activated, the javascriptExecutionTree will contain sub-requests.',
  14. ''
  15. ].join('\n'),
  16. pkg: '../package.json'
  17. });
  18. // Check parameters
  19. if (cli.input.length < 1) {
  20. console.error('Incorrect parameters: url not provided');
  21. process.exit(1);
  22. }
  23. var url = cli.input[0];
  24. var options = {};
  25. // Screenshot option
  26. var screenshot = cli.flags.screenshot;
  27. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  28. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  29. process.exit(1);
  30. }
  31. if (screenshot) {
  32. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  33. // It is not an absolute path, so it is relative to the current command-line directory
  34. screenshot = path.join(process.cwd(), screenshot);
  35. }
  36. options.screenshot = cli.flags.screenshot;
  37. }
  38. // Deep JS analysis option
  39. if (cli.flags.jsDeepAnalysis === true || cli.flags.jsDeepAnalysis === 'true') {
  40. options.jsDeepAnalysis = true;
  41. }
  42. (function execute(url, options) {
  43. 'use strict';
  44. ylt(url, options).
  45. then(function(data) {
  46. debug('Success');
  47. console.log(JSON.stringify(data, null, 2));
  48. }).fail(function(err) {
  49. debug('Test failed for %s', url);
  50. console.error(err);
  51. });
  52. debug('Test launched...');
  53. })(url, options);