"use strict";exports.__esModule=true;exports.runLintCheck=runLintCheck;var _fs=require("fs");var _path=require("path");var _findUp=_interopRequireDefault(require("next/dist/compiled/find-up"));var _semver=_interopRequireDefault(require("next/dist/compiled/semver"));var _customFormatter=require("./customFormatter");var _getLintIntent=require("./getLintIntent");var _writeDefaultConfig=require("./writeDefaultConfig");var _getPackageVersion=require("../get-package-version");var _compileError=require("../compile-error");var _hasNecessaryDependencies=require("../has-necessary-dependencies");var Log=_interopRequireWildcard(require("../../build/output/log"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _getRequireWildcardCache(){if(typeof WeakMap!=="function")return null;var cache=new WeakMap();_getRequireWildcardCache=function(){return cache;};return cache;}function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache();if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}const linteableFileTypes=['jsx','js','ts','tsx'];async function lint(deps,baseDir,pagesDir,eslintrcFile,pkgJsonPath){var _ESLint$getErrorResul;// Load ESLint after we're sure it exists:
const{ESLint}=await Promise.resolve(`${deps.resolved}`).then(s=>_interopRequireWildcard(require(s)));if(!ESLint){const eslintVersion=await(0,_getPackageVersion.getPackageVersion)({cwd:baseDir,name:'eslint'});if(eslintVersion&&_semver.default.lt(eslintVersion,'7.0.0')){Log.warn(`Your project has an older version of ESLint installed (${eslintVersion}). Please upgrade to v7 or later to run ESLint during the build process.`);}return null;}let options={useEslintrc:true,baseConfig:{}};let eslint=new ESLint(options);let nextEslintPluginIsEnabled=false;const pagesDirRules=['@next/next/no-html-link-for-pages'];for(const configFile of[eslintrcFile,pkgJsonPath]){var _completeConfig$plugi;if(!configFile)continue;const completeConfig=await eslint.calculateConfigForFile(configFile);if((_completeConfig$plugi=completeConfig.plugins)!=null&&_completeConfig$plugi.includes('@next/next')){nextEslintPluginIsEnabled=true;break;}}if(nextEslintPluginIsEnabled){let updatedPagesDir=false;for(const rule of pagesDirRules){var _rules,_rules2;if(!((_rules=options.baseConfig.rules)!=null&&_rules[rule])&&!((_rules2=options.baseConfig.rules)!=null&&_rules2[rule.replace('@next/next','@next/babel-plugin-next')])){if(!options.baseConfig.rules){options.baseConfig.rules={};}options.baseConfig.rules[rule]=[1,pagesDir];updatedPagesDir=true;}}if(updatedPagesDir){eslint=new ESLint(options);}}const results=await eslint.lintFiles([`${pagesDir}/**/*.{${linteableFileTypes.join(',')}}`]);if(((_ESLint$getErrorResul=ESLint.getErrorResults(results))==null?void 0:_ESLint$getErrorResul.length)>0){throw new _compileError.CompileError(await(0,_customFormatter.formatResults)(baseDir,results));}return(results==null?void 0:results.length)>0?(0,_customFormatter.formatResults)(baseDir,results):null;}async function runLintCheck(baseDir,pagesDir){try{var _await$findUp,_await$findUp2;// Check if any pages exist that can be linted
const pages=await _fs.promises.readdir(pagesDir);if(!pages.some(page=>linteableFileTypes.includes((0,_path.extname)(page).replace('.','')))){return null;}// Find user's .eslintrc file
const eslintrcFile=(_await$findUp=await(0,_findUp.default)(['.eslintrc.js','.eslintrc.yaml','.eslintrc.yml','.eslintrc.json','.eslintrc'],{cwd:baseDir}))!=null?_await$findUp:null;const pkgJsonPath=(_await$findUp2=await(0,_findUp.default)('package.json',{cwd:baseDir}))!=null?_await$findUp2:null;const{eslintConfig:pkgJsonEslintConfig=null}=!!pkgJsonPath?await Promise.resolve(`${pkgJsonPath}`).then(s=>_interopRequireWildcard(require(s))):{};// Check if the project uses ESLint
const eslintIntent=await(0,_getLintIntent.getLintIntent)(eslintrcFile,pkgJsonEslintConfig);if(!eslintIntent){return null;}const firstTimeSetup=eslintIntent.firstTimeSetup;// Ensure ESLint and necessary plugins and configs are installed:
const deps=await(0,_hasNecessaryDependencies.hasNecessaryDependencies)(baseDir,false,!!eslintIntent,eslintrcFile);// Create the user's eslintrc config for them
if(firstTimeSetup)await(0,_writeDefaultConfig.writeDefaultConfig)(eslintrcFile,pkgJsonPath);// Run ESLint
return await lint(deps,baseDir,pagesDir,eslintrcFile,pkgJsonPath);}catch(err){throw err;}}
//# sourceMappingURL=runLintCheck.js.map