text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const tslib_1 = require("tslib");
  const fs_1 = require("fs");
  const path = require("path");
  const ts_utils_1 = require("./utility/ts-utils");
  const eslint_utils_1 = require("./utility/eslint-utils");
  const create_directory_1 = require("../eslint/utility/create-directory");
  /**
   * Adapted from @angular-eslint/builder source
   */
  function run(options, context) {
      return tslib_1.__awaiter(this, void 0, void 0, function* () {
          if (options.linter === 'tslint') {
              throw new Error(`'tslint' option is no longer supported. Update your angular.json to use "@nrwl/linter:eslint" builder.`);
          }
          const systemRoot = context.root;
          process.chdir(context.cwd);
          const projectName = context.projectName || '<???>';
          const printInfo = options.format && !options.silent;
          if (printInfo) {
              console.info(`\nLinting ${JSON.stringify(projectName)}...`);
          }
          const projectESLint = yield eslint_utils_1.loadESLint();
          const version = projectESLint.Linter.version &&
              projectESLint.Linter.version.split('.');
          if (!version ||
              version.length < 2 ||
              Number(version[0]) < 6 ||
              (Number(version[0]) === 6 && Number(version[1]) < 1)) {
              throw new Error('ESLint must be version 6.1 or higher.');
          }
          // We want users to have the option of not specifying the config path, and let
          // eslint automatically resolve the `.eslintrc.json` files in each folder.
          const eslintConfigPath = options.config
              ? path.resolve(systemRoot, options.config)
              : undefined;
          let lintReports = [];
          const lintedFiles = new Set();
          if (options.tsConfig) {
              const tsConfigs = Array.isArray(options.tsConfig)
                  ? options.tsConfig
                  : [options.tsConfig];
              const allPrograms = tsConfigs.map((tsConfig) => ts_utils_1.createProgram(path.resolve(systemRoot, tsConfig)));
              for (const program of allPrograms) {
                  lintReports = [
                      ...lintReports,
                      ...(yield eslint_utils_1.lint(systemRoot, eslintConfigPath, options, lintedFiles, program, allPrograms)),
                  ];
              }
          }
          else {
              lintReports = [
                  ...lintReports,
                  ...(yield eslint_utils_1.lint(systemRoot, eslintConfigPath, options, lintedFiles)),
              ];
          }
          if (lintReports.length === 0) {
              throw new Error('Invalid lint configuration. Nothing to lint.');
          }
          const formatter = projectESLint.CLIEngine.getFormatter(options.format);
          const bundledReport = {
              errorCount: 0,
              fixableErrorCount: 0,
              fixableWarningCount: 0,
              warningCount: 0,
              results: [],
              usedDeprecatedRules: [],
          };
          for (const report of lintReports) {
              // output fixes to disk
              projectESLint.CLIEngine.outputFixes(report);
              if (report.errorCount || report.warningCount) {
                  bundledReport.errorCount += report.errorCount;
                  bundledReport.warningCount += report.warningCount;
                  bundledReport.fixableErrorCount += report.fixableErrorCount;
                  bundledReport.fixableWarningCount += report.fixableWarningCount;
                  bundledReport.results.push(...report.results);
                  bundledReport.usedDeprecatedRules.push(...report.usedDeprecatedRules);
              }
          }
          const formattedResults = formatter(bundledReport.results);
          console.info(formattedResults);
          if (options.outputFile) {
              const pathToFile = path.join(context.root, options.outputFile);
              create_directory_1.createDirectory(path.dirname(pathToFile));
              fs_1.writeFileSync(pathToFile, formattedResults);
          }
          if (bundledReport.warningCount > 0 && printInfo) {
              console.warn('Lint warnings found in the listed files.\n');
          }
          if (bundledReport.errorCount > 0 && printInfo) {
              console.error('Lint errors found in the listed files.\n');
          }
          if (bundledReport.warningCount === 0 &&
              bundledReport.errorCount === 0 &&
              printInfo) {
              console.info('All files pass linting.\n');
          }
          return {
              success: options.force ||
                  (bundledReport.errorCount === 0 &&
                      (options.maxWarnings === -1 ||
                          bundledReport.warningCount <= options.maxWarnings)),
          };
      });
  }
  exports.default = run;
  //# sourceMappingURL=lint.impl.js.map