cli.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. ' --wait-for-selector Once the page is loaded, Phantomas will wait until the given CSS selector matches some elements.',
  16. ' --cookie Adds a cookie on the main domain.',
  17. ' --auth-user Basic HTTP authentication username.',
  18. ' --auth-pass Basic HTTP authentication password.',
  19. ''
  20. ].join('\n'),
  21. pkg: '../package.json'
  22. });
  23. // Check parameters
  24. if (cli.input.length < 1) {
  25. cli.showHelp();
  26. }
  27. var url = cli.input[0];
  28. var options = {};
  29. // Screenshot option
  30. var screenshot = cli.flags.screenshot;
  31. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  32. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  33. process.exit(1);
  34. }
  35. if (screenshot) {
  36. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  37. // It is not an absolute path, so it is relative to the current command-line directory
  38. screenshot = path.join(process.cwd(), screenshot);
  39. }
  40. options.screenshot = cli.flags.screenshot;
  41. }
  42. // Deep JS analysis option
  43. if (cli.flags.jsDeepAnalysis === true || cli.flags.jsDeepAnalysis === 'true') {
  44. options.jsDeepAnalysis = true;
  45. }
  46. // Device simulation
  47. options.device = cli.flags.device || 'desktop';
  48. // Wait for CSS selector
  49. options.waitForSelector = cli.flags.waitForSelector || null;
  50. // Cookie
  51. options.cookie = cli.flags.cookie || null;
  52. // HTTP basic auth
  53. options.authUser = cli.flags.authUser || null;
  54. options.authPass = cli.flags.authPass || null;
  55. (function execute(url, options) {
  56. 'use strict';
  57. ylt(url, options).
  58. then(function(data) {
  59. debug('Success');
  60. console.log(JSON.stringify(data, null, 2));
  61. }).fail(function(err) {
  62. debug('Test failed for %s', url);
  63. console.error(err);
  64. });
  65. debug('Test launched...');
  66. })(url, options);