"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLintCheckCommand = getLintCheckCommand; exports.performLintCheck = performLintCheck; exports.getLintFixCommand = getLintFixCommand; exports.performLintFix = performLintFix; exports.getPrettierCommand = getPrettierCommand; exports.makeFilesPrettier = makeFilesPrettier; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const defaults_1 = require("./defaults"); /* global process require __dirname */ const eslintPath = require.resolve("eslint"); const prettierPath = require.resolve("prettier"); const eslintDir = path_1.default.parse(eslintPath).dir; const eslintFilePath = path_1.default.resolve(eslintDir, "../bin/eslint.js"); const prettierFilePath = path_1.default.resolve(prettierPath, "../bin/prettier.cjs"); const eslintConfigPath = path_1.default.resolve(__dirname, "../config/eslint.config.mjs"); const eslintTestConfigPath = path_1.default.resolve(__dirname, "../config/eslint.config.test.mjs"); function execCommand(command) { const execSync = require("child_process").execSync; execSync(command, { stdio: "inherit" }); } function normalizeFilePath(filePath) { return filePath.replace(/ /g, "\\ "); // Converting space to '\\' } function getEsLintBaseCommand(useTestConfig = false) { const projLintConfig = path_1.default.resolve(process.cwd(), "eslint.config.mjs"); const prodConfig = fs_1.default.existsSync(projLintConfig) ? projLintConfig : eslintConfigPath; const configFilePath = useTestConfig ? eslintTestConfigPath : prodConfig; const eslintBaseCommand = `node "${eslintFilePath}" -c "${configFilePath}"`; return eslintBaseCommand; } function getLintCheckCommand(files, useTestConfig = false) { const eslintCommand = `${getEsLintBaseCommand(useTestConfig)} "${normalizeFilePath(files)}"`; return eslintCommand; } function performLintCheck(files, useTestConfig = false) { try { const command = getLintCheckCommand(files, useTestConfig); execCommand(command); defaults_1.usageDataObject.reportSuccess("performLintCheck()", { exitCode: defaults_1.ESLintExitCode.NoLintErrors }); } catch (err) { if (err.status && err.status == defaults_1.ESLintExitCode.HasLintError) { defaults_1.usageDataObject.reportExpectedException("performLintCheck()", err, { exitCode: defaults_1.ESLintExitCode.HasLintError, }); } else { defaults_1.usageDataObject.reportException("performLintCheck()", err); } throw err; } } function getLintFixCommand(files, useTestConfig = false) { const eslintCommand = `${getEsLintBaseCommand(useTestConfig)} --fix ${normalizeFilePath(files)}`; return eslintCommand; } function performLintFix(files, useTestConfig = false) { try { const command = getLintFixCommand(files, useTestConfig); execCommand(command); defaults_1.usageDataObject.reportSuccess("performLintFix()", { exitCode: defaults_1.ESLintExitCode.NoLintErrors }); } catch (err) { if (err.status && err.status == defaults_1.ESLintExitCode.HasLintError) { defaults_1.usageDataObject.reportExpectedException("performLintFix()", err, { exitCode: defaults_1.ESLintExitCode.HasLintError, }); } else { defaults_1.usageDataObject.reportException("performLintFix()", err); } throw err; } } function getPrettierCommand(files) { const prettierFixCommand = `node ${prettierFilePath} --parser typescript --write ${normalizeFilePath(files)}`; return prettierFixCommand; } function makeFilesPrettier(files) { try { const command = getPrettierCommand(files); execCommand(command); defaults_1.usageDataObject.reportSuccess("makeFilesPrettier()", { exitCode: defaults_1.PrettierExitCode.NoFormattingProblems, }); } catch (err) { if (err.status && err.status == defaults_1.PrettierExitCode.HasFormattingProblem) { defaults_1.usageDataObject.reportExpectedException("makeFilesPrettier()", err, { exitCode: defaults_1.PrettierExitCode.HasFormattingProblem, }); } else { defaults_1.usageDataObject.reportException("makeFilesPrettier()", err); } throw err; } } //# sourceMappingURL=lint.js.map