Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

cli.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Shazron Abdullah
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. const path = require('path')
  21. const command_lib = require('./commands')
  22. const help = require('./help')
  23. let nopt
  24. /*
  25. * init
  26. *
  27. * initializes nopt and simctl
  28. * nopt, and simctl are require()d in try-catch below to print a nice error
  29. * message if one of them is not installed.
  30. */
  31. function init () {
  32. try {
  33. nopt = require('nopt')
  34. let code = command_lib.init()
  35. if (code !== 0) {
  36. process.exit(code)
  37. }
  38. } catch (e) {
  39. console.error(
  40. 'Please run npm install from this directory:\n\t' +
  41. path.dirname(__dirname)
  42. )
  43. process.exit(2)
  44. }
  45. }
  46. function cli (inputArgs) {
  47. let knownOpts =
  48. {
  49. 'version': Boolean,
  50. 'help': Boolean,
  51. 'verbose': Boolean,
  52. 'exit': Boolean,
  53. 'use-gdb': Boolean,
  54. 'uuid': String,
  55. 'env': String,
  56. 'setenv': Array,
  57. 'stdout': path,
  58. 'stderr': path,
  59. 'timeout': Number,
  60. 'args': Array,
  61. 'devicetypeid': String
  62. }
  63. let shortHands = null
  64. // If no inputArgs given, use process.argv.
  65. inputArgs = inputArgs || process.argv
  66. init()
  67. let args = nopt(knownOpts, shortHands, inputArgs)
  68. process.on('uncaughtException', function (err) {
  69. if (!args.verbose) {
  70. console.error(err.message)
  71. } else {
  72. console.error(err.stack)
  73. }
  74. process.exit(1)
  75. })
  76. let cmd = args.argv.remain[0]
  77. // some options do *not* need commands and can be run
  78. if (args.help) {
  79. console.log(help())
  80. } else if (args.version) {
  81. console.log(require('../package').version)
  82. } else if (cmd && command_lib[cmd]) { // command found
  83. command_lib[cmd](args)
  84. } else {
  85. console.log(help())
  86. process.exit(1)
  87. }
  88. }
  89. module.exports = cli