Home Manual Reference Source

src/services/cli/cliSHValidateAnalyze.js

  1. const { provider } = require('jimple');
  2. const CLICommand = require('../../abstracts/cliCommand');
  3. /**
  4. * This is a private command the shell script executes before running the analyze command in order
  5. * to validate the arguments and throw any necessary error. The reason we do this in two separated
  6. * commands is that the shell script takes all the output of the run command and tries to execute
  7. * it, so we can't include execptions in there.
  8. * @extends {CLICommand}
  9. */
  10. class CLISHValidateAnalyzeCommand extends CLICommand {
  11. /**
  12. * Class constructor.
  13. * @param {Targets} targets To validate a target existence.
  14. */
  15. constructor(targets) {
  16. super();
  17. /**
  18. * A local reference for the `targets` service.
  19. * @type {Targets}
  20. */
  21. this.targets = targets;
  22. /**
  23. * The instruction needed to trigger the command.
  24. * @type {string}
  25. */
  26. this.command = 'sh-validate-analyze [target]';
  27. /**
  28. * A description of the command, just to follow the interface as the command won't show up on
  29. * the help interface.
  30. * @type {string}
  31. */
  32. this.description = 'Validate the arguments before the shell executes the task';
  33. /**
  34. * Hide the command from the help interface.
  35. * @type {boolean}
  36. */
  37. this.hidden = true;
  38. /**
  39. * Enable unknown options so other services can customize the run command.
  40. * @type {boolean}
  41. */
  42. this.allowUnknownOptions = true;
  43. }
  44. /**
  45. * Handle the execution of the command and validate the target existence.
  46. * @param {?string} name The name of the target.
  47. * @throws {Error} If the target type is `browser`.
  48. */
  49. handle(name) {
  50. const target = name ?
  51. // If the target doesn't exist, this will throw an error.
  52. this.targets.getTarget(name) :
  53. // Get the default target or throw an error if the project doesn't have targets.
  54. this.targets.getDefaultTarget();
  55.  
  56. if (!target.is.browser && !target.bundle) {
  57. throw new Error(`'${target.name}' doesn't do bundling, so it can't be analyzed`);
  58. }
  59.  
  60. return target;
  61. }
  62. }
  63. /**
  64. * The service provider that once registered on the app container will set an instance of
  65. * `CLISHValidateAnalyzeCommand` as the `cliSHValidateAnalyzeCommand` service.
  66. * @example
  67. * // Register it on the container
  68. * container.register(cliSHValidateAnalyzeCommand);
  69. * // Getting access to the service instance
  70. * const cliSHValidateAnalyzeCommand = container.get('cliSHValidateAnalyzeCommand');
  71. * @type {Provider}
  72. */
  73. const cliSHValidateAnalyzeCommand = provider((app) => {
  74. app.set('cliSHValidateAnalyzeCommand', () => new CLISHValidateAnalyzeCommand(
  75. app.get('targets')
  76. ));
  77. });
  78.  
  79. module.exports = {
  80. CLISHValidateAnalyzeCommand,
  81. cliSHValidateAnalyzeCommand,
  82. };