cli.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ' --device Use "phone" or "tablet" to simulate a mobile device (by user-agent and viewport size).',
  13. ' --screenshot Will take a screenshot and use this value as the output path. It needs to end with ".png".',
  14. ' --js-deep-analysis When activated, the javascriptExecutionTree will contain sub-requests.',
  15. ''
  16. ].join('\n'),
  17. pkg: '../package.json'
  18. });
  19. // Check parameters
  20. if (cli.input.length < 1) {
  21. console.error('Incorrect parameters: url not provided');
  22. process.exit(1);
  23. }
  24. var url = cli.input[0];
  25. var options = {};
  26. // Screenshot option
  27. var screenshot = cli.flags.screenshot;
  28. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  29. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  30. process.exit(1);
  31. }
  32. if (screenshot) {
  33. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  34. // It is not an absolute path, so it is relative to the current command-line directory
  35. screenshot = path.join(process.cwd(), screenshot);
  36. }
  37. options.screenshot = cli.flags.screenshot;
  38. }
  39. // Deep JS analysis option
  40. if (cli.flags.jsDeepAnalysis === true || cli.flags.jsDeepAnalysis === 'true') {
  41. options.jsDeepAnalysis = true;
  42. }
  43. // Device simulation
  44. options.device = cli.flags.device || 'desktop';
  45. (function execute(url, options) {
  46. 'use strict';
  47. ylt(url, options).
  48. then(function(data) {
  49. debug('Success');
  50. console.log(JSON.stringify(data, null, 2));
  51. }).fail(function(err) {
  52. debug('Test failed for %s', url);
  53. console.error(err);
  54. });
  55. debug('Test launched...');
  56. })(url, options);