init.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (_) try {
  29. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. Object.defineProperty(exports, "__esModule", { value: true });
  50. var fs_1 = require("fs");
  51. var json5_1 = require("json5");
  52. var path_1 = require("path");
  53. var presets_1 = require("../helpers/presets");
  54. exports.run = function (args) { return __awaiter(void 0, void 0, void 0, function () {
  55. var file, filePath, name, isPackage, exists, pkgFile, hasPackage, _a, jestPreset, askedTsconfig, force, jsdom, tsconfig, pkgJson, jsFilesProcessor, shouldPostProcessWithBabel, preset, body, base, tsJestConf, content;
  56. return __generator(this, function (_b) {
  57. file = args._[0] || 'jest.config.js';
  58. filePath = path_1.join(process.cwd(), file);
  59. name = path_1.basename(file);
  60. isPackage = name === 'package.json';
  61. exists = fs_1.existsSync(filePath);
  62. pkgFile = isPackage ? filePath : path_1.join(process.cwd(), 'package.json');
  63. hasPackage = isPackage || fs_1.existsSync(pkgFile);
  64. _a = args.jestPreset, jestPreset = _a === void 0 ? true : _a, askedTsconfig = args.tsconfig, force = args.force, jsdom = args.jsdom;
  65. tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig;
  66. pkgJson = hasPackage ? JSON.parse(fs_1.readFileSync(pkgFile, 'utf8')) : {};
  67. jsFilesProcessor = args.js, shouldPostProcessWithBabel = args.babel;
  68. if (jsFilesProcessor == null) {
  69. jsFilesProcessor = shouldPostProcessWithBabel ? 'babel' : undefined;
  70. }
  71. else if (shouldPostProcessWithBabel == null) {
  72. shouldPostProcessWithBabel = jsFilesProcessor === 'babel';
  73. }
  74. if (jsFilesProcessor === 'babel') {
  75. preset = presets_1.jsWIthBabel;
  76. }
  77. else if (jsFilesProcessor === 'ts') {
  78. preset = presets_1.jsWithTs;
  79. }
  80. else {
  81. preset = presets_1.defaults;
  82. }
  83. if (isPackage && !exists) {
  84. throw new Error("File " + file + " does not exists.");
  85. }
  86. else if (!isPackage && exists && !force) {
  87. throw new Error("Configuration file " + file + " already exists.");
  88. }
  89. if (!isPackage && !name.endsWith('.js')) {
  90. throw new TypeError("Configuration file " + file + " must be a .js file or the package.json.");
  91. }
  92. if (hasPackage && pkgJson.jest) {
  93. if (force && !isPackage) {
  94. delete pkgJson.jest;
  95. fs_1.writeFileSync(pkgFile, JSON.stringify(pkgJson, undefined, ' '));
  96. }
  97. else if (!force) {
  98. throw new Error("A Jest configuration is already set in " + pkgFile + ".");
  99. }
  100. }
  101. if (isPackage) {
  102. base = jestPreset ? { preset: preset.name } : __assign({}, preset.value);
  103. if (!jsdom)
  104. base.testEnvironment = 'node';
  105. if (tsconfig || shouldPostProcessWithBabel) {
  106. tsJestConf = {};
  107. base.globals = { 'ts-jest': tsJestConf };
  108. if (tsconfig)
  109. tsJestConf.tsconfig = tsconfig;
  110. if (shouldPostProcessWithBabel)
  111. tsJestConf.babelConfig = true;
  112. }
  113. body = JSON.stringify(__assign(__assign({}, pkgJson), { jest: base }), undefined, ' ');
  114. }
  115. else {
  116. content = [];
  117. if (!jestPreset) {
  118. content.push(preset.jsImport('tsjPreset') + ";", '');
  119. }
  120. content.push('module.exports = {');
  121. if (jestPreset) {
  122. content.push(" preset: '" + preset.name + "',");
  123. }
  124. else {
  125. content.push(" ...tsjPreset,");
  126. }
  127. if (!jsdom)
  128. content.push(" testEnvironment: 'node',");
  129. if (tsconfig || shouldPostProcessWithBabel) {
  130. content.push(" globals: {");
  131. content.push(" 'ts-jest': {");
  132. if (tsconfig)
  133. content.push(" tsconfig: " + json5_1.stringify(tsconfig) + ",");
  134. if (shouldPostProcessWithBabel)
  135. content.push(" babelConfig: true,");
  136. content.push(" },");
  137. content.push(" },");
  138. }
  139. content.push('};');
  140. body = content.join('\n');
  141. }
  142. fs_1.writeFileSync(filePath, body);
  143. process.stderr.write("\nJest configuration written to \"" + filePath + "\".\n");
  144. return [2];
  145. });
  146. }); };
  147. exports.help = function () { return __awaiter(void 0, void 0, void 0, function () {
  148. return __generator(this, function (_a) {
  149. process.stdout.write("\nUsage:\n ts-jest config:init [options] [<config-file>]\n\nArguments:\n <config-file> Can be a js or json Jest config file. If it is a\n package.json file, the configuration will be read from\n the \"jest\" property.\n Default: jest.config.js\n\nOptions:\n --force Discard any existing Jest config\n --js ts|babel Process .js files with ts-jest if 'ts' or with\n babel-jest if 'babel'\n --no-jest-preset Disable the use of Jest presets\n --tsconfig <file> Path to the tsconfig.json file\n --babel Pipe babel-jest after ts-jest\n --jsdom Use jsdom as test environment instead of node\n");
  150. return [2];
  151. });
  152. }); };