setFromArgv.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = setFromArgv;
  6. var _utils = require('./utils');
  7. function _objectSpread(target) {
  8. for (var i = 1; i < arguments.length; i++) {
  9. var source = arguments[i] != null ? arguments[i] : {};
  10. var ownKeys = Object.keys(source);
  11. if (typeof Object.getOwnPropertySymbols === 'function') {
  12. ownKeys = ownKeys.concat(
  13. Object.getOwnPropertySymbols(source).filter(function(sym) {
  14. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  15. })
  16. );
  17. }
  18. ownKeys.forEach(function(key) {
  19. _defineProperty(target, key, source[key]);
  20. });
  21. }
  22. return target;
  23. }
  24. function _defineProperty(obj, key, value) {
  25. if (key in obj) {
  26. Object.defineProperty(obj, key, {
  27. value: value,
  28. enumerable: true,
  29. configurable: true,
  30. writable: true
  31. });
  32. } else {
  33. obj[key] = value;
  34. }
  35. return obj;
  36. }
  37. /**
  38. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  39. *
  40. * This source code is licensed under the MIT license found in the
  41. * LICENSE file in the root directory of this source tree.
  42. */
  43. const specialArgs = ['_', '$0', 'h', 'help', 'config'];
  44. function setFromArgv(options, argv) {
  45. const argvToOptions = Object.keys(argv)
  46. .filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1)
  47. .reduce((options, key) => {
  48. switch (key) {
  49. case 'coverage':
  50. options.collectCoverage = argv[key];
  51. break;
  52. case 'json':
  53. options.useStderr = argv[key];
  54. break;
  55. case 'watchAll':
  56. options.watch = false;
  57. options.watchAll = argv[key];
  58. break;
  59. case 'env':
  60. options.testEnvironment = argv[key];
  61. break;
  62. case 'config':
  63. break;
  64. case 'coverageThreshold':
  65. case 'globals':
  66. case 'moduleNameMapper':
  67. case 'transform':
  68. case 'haste':
  69. const str = argv[key];
  70. if ((0, _utils.isJSONString)(str)) {
  71. options[key] = JSON.parse(str);
  72. }
  73. break;
  74. default:
  75. options[key] = argv[key];
  76. }
  77. return options;
  78. }, {});
  79. return _objectSpread(
  80. {},
  81. options,
  82. (0, _utils.isJSONString)(argv.config) ? JSON.parse(argv.config) : null,
  83. argvToOptions
  84. );
  85. }