writeDefaultConfig.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.writeDefaultConfig = writeDefaultConfig;
  6. var _fs = require("fs");
  7. var _chalk = _interopRequireDefault(require("next/dist/compiled/chalk"));
  8. var _os = _interopRequireDefault(require("os"));
  9. var _path = _interopRequireDefault(require("path"));
  10. var CommentJson = _interopRequireWildcard(require("next/dist/compiled/comment-json"));
  11. var Log = _interopRequireWildcard(require("../../build/output/log"));
  12. function _interopRequireDefault(obj) {
  13. return obj && obj.__esModule ? obj : {
  14. default: obj
  15. };
  16. }
  17. function _getRequireWildcardCache() {
  18. if (typeof WeakMap !== "function") return null;
  19. var cache = new WeakMap();
  20. _getRequireWildcardCache = function() {
  21. return cache;
  22. };
  23. return cache;
  24. }
  25. function _interopRequireWildcard(obj) {
  26. if (obj && obj.__esModule) {
  27. return obj;
  28. }
  29. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  30. return {
  31. default: obj
  32. };
  33. }
  34. var cache = _getRequireWildcardCache();
  35. if (cache && cache.has(obj)) {
  36. return cache.get(obj);
  37. }
  38. var newObj = {};
  39. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  40. for(var key in obj){
  41. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  42. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  43. if (desc && (desc.get || desc.set)) {
  44. Object.defineProperty(newObj, key, desc);
  45. } else {
  46. newObj[key] = obj[key];
  47. }
  48. }
  49. }
  50. newObj.default = obj;
  51. if (cache) {
  52. cache.set(obj, newObj);
  53. }
  54. return newObj;
  55. }
  56. async function writeDefaultConfig(baseDir, { exists , emptyEslintrc , emptyPkgJsonConfig }, selectedConfig, eslintrcFile, pkgJsonPath, packageJsonConfig) {
  57. if (!exists && emptyEslintrc && eslintrcFile) {
  58. const ext = _path.default.extname(eslintrcFile);
  59. let newFileContent;
  60. if (ext === ".yaml" || ext === ".yml") {
  61. newFileContent = "extends: 'next'";
  62. } else {
  63. newFileContent = CommentJson.stringify(selectedConfig, null, 2);
  64. if (ext === ".js") {
  65. newFileContent = "module.exports = " + newFileContent;
  66. }
  67. }
  68. await _fs.promises.writeFile(eslintrcFile, newFileContent + _os.default.EOL);
  69. Log.info(`We detected an empty ESLint configuration file (${_chalk.default.bold(_path.default.basename(eslintrcFile))}) and updated it for you!`);
  70. } else if (!exists && emptyPkgJsonConfig && packageJsonConfig) {
  71. packageJsonConfig.eslintConfig = selectedConfig;
  72. if (pkgJsonPath) await _fs.promises.writeFile(pkgJsonPath, CommentJson.stringify(packageJsonConfig, null, 2) + _os.default.EOL);
  73. Log.info(`We detected an empty ${_chalk.default.bold("eslintConfig")} field in package.json and updated it for you!`);
  74. } else if (!exists) {
  75. await _fs.promises.writeFile(_path.default.join(baseDir, ".eslintrc.json"), CommentJson.stringify(selectedConfig, null, 2) + _os.default.EOL);
  76. console.log(_chalk.default.green(`We created the ${_chalk.default.bold(".eslintrc.json")} file for you and included your selected configuration.`));
  77. }
  78. }
  79. //# sourceMappingURL=writeDefaultConfig.js.map