cli.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. console.error('Incorrect parameters: url not provided');
  26. process.exit(1);
  27. }
  28. var url = cli.input[0];
  29. var options = {};
  30. // Screenshot option
  31. var screenshot = cli.flags.screenshot;
  32. if (screenshot && (typeof screenshot !== 'string' || screenshot.toLowerCase().indexOf('.png', screenshot.length - 4) === -1)) {
  33. console.error('Incorrect parameters: screenshot must be a path that ends with ".png"');
  34. process.exit(1);
  35. }
  36. if (screenshot) {
  37. if (path.resolve(screenshot) !== path.normalize(screenshot)) {
  38. // It is not an absolute path, so it is relative to the current command-line directory
  39. screenshot = path.join(process.cwd(), screenshot);
  40. }
  41. options.screenshot = cli.flags.screenshot;
  42. }
  43. // Deep JS analysis option
  44. if (cli.flags.jsDeepAnalysis === true || cli.flags.jsDeepAnalysis === 'true') {
  45. options.jsDeepAnalysis = true;
  46. }
  47. // Device simulation
  48. options.device = cli.flags.device || 'desktop';
  49. // Wait for CSS selector
  50. options.waitForSelector = cli.flags.waitForSelector || null;
  51. // Cookie
  52. options.cookie = cli.flags.cookie || null;
  53. // HTTP basic auth
  54. options.authUser = cli.flags.authUser || null;
  55. options.authPass = cli.flags.authPass || null;
  56. (function execute(url, options) {
  57. 'use strict';
  58. ylt(url, options).
  59. then(function(data) {
  60. debug('Success');
  61. console.log(JSON.stringify(data, null, 2));
  62. }).fail(function(err) {
  63. debug('Test failed for %s', url);
  64. console.error(err);
  65. });
  66. debug('Test launched...');
  67. })(url, options);