Home Manual Reference Source

src/services/cli/cliSHNodeRun.js

  1. const { provider } = require('jimple');
  2. const CLICommand = require('../../abstracts/cliCommand');
  3. /**
  4. * This is a private command the shell script executes in order to run a Node target with `nodemon`.
  5. * @extends {CLICommand}
  6. */
  7. class CLISHNodeRunCommand extends CLICommand {
  8. /**
  9. * Class constructor.
  10. * @param {BuildNodeRunner} buildNodeRunner To actually run a target.
  11. * @param {Targets} targets To get a target information.
  12. */
  13. constructor(buildNodeRunner, targets) {
  14. super();
  15. /**
  16. * A local reference for the `buildNodeRunner` service.
  17. * @type {BuildNodeRunner}
  18. */
  19. this.buildNodeRunner = buildNodeRunner;
  20. /**
  21. * A local reference for the `targets` service.
  22. * @type {Targets}
  23. */
  24. this.targets = targets;
  25. /**
  26. * The instruction needed to trigger the command.
  27. * @type {string}
  28. */
  29. this.command = 'sh-node-run [target]';
  30. /**
  31. * A description of the command, just to follow the interface as the command won't show up on
  32. * the help interface.
  33. * @type {string}
  34. */
  35. this.description = 'Run a Node target that wasn\'t bundled';
  36. /**
  37. * Hide the command from the help interface.
  38. * @type {boolean}
  39. */
  40. this.hidden = true;
  41. /**
  42. * Enable unknown options so other services can customize the run command.
  43. * @type {boolean}
  44. */
  45. this.allowUnknownOptions = true;
  46. this.addOption(
  47. 'inspect',
  48. '-i, --inspect',
  49. 'Enables the Node inspector',
  50. false
  51. );
  52. }
  53. /**
  54. * Handle the execution of the command and runs a Node target.
  55. * @param {string} name The name of the target.
  56. * @param {Command} command The executed command (sent by `commander`).
  57. * @param {Object} options The command options.
  58. * @param {boolean} options.inspect Whether or not to enable the Node inspector.
  59. * @return {Nodemon}
  60. */
  61. handle(name, command, options) {
  62. const target = this.targets.getTarget(name);
  63. return this.buildNodeRunner.runTarget(target, options.inspect);
  64. }
  65. }
  66. /**
  67. * The service provider that once registered on the app container will set an instance of
  68. * `CLISHNodeRunCommand` as the `cliSHNodeRunCommand` service.
  69. * @example
  70. * // Register it on the container
  71. * container.register(cliSHNodeRunCommand);
  72. * // Getting access to the service instance
  73. * const cliSHNodeRunCommand = container.get('cliSHNodeRunCommand');
  74. * @type {Provider}
  75. */
  76. const cliSHNodeRunCommand = provider((app) => {
  77. app.set('cliSHNodeRunCommand', () => new CLISHNodeRunCommand(
  78. app.get('buildNodeRunner'),
  79. app.get('targets')
  80. ));
  81. });
  82.  
  83. module.exports = {
  84. CLISHNodeRunCommand,
  85. cliSHNodeRunCommand,
  86. };