99 lines
4.5 KiB
JavaScript
99 lines
4.5 KiB
JavaScript
|
"use strict";
|
||
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
// Licensed under the MIT license.
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.makeFilesPrettier = exports.getPrettierCommand = exports.performLintFix = exports.getLintFixCommand = exports.performLintCheck = exports.getLintCheckCommand = void 0;
|
||
|
const fs = require("fs");
|
||
|
const path = require("path");
|
||
|
const defaults_1 = require("./defaults");
|
||
|
/* global require, __dirname, process */
|
||
|
const eslintPath = require.resolve("eslint");
|
||
|
const prettierPath = require.resolve("prettier");
|
||
|
const eslintDir = path.parse(eslintPath).dir;
|
||
|
const eslintFilePath = path.resolve(eslintDir, "../bin/eslint.js");
|
||
|
const prettierFilePath = path.resolve(prettierPath, "../bin/prettier.cjs");
|
||
|
const eslintConfigPath = path.resolve(__dirname, "../config/.eslintrc.json");
|
||
|
const eslintTestConfigPath = path.resolve(__dirname, "../config/.eslintrc.test.json");
|
||
|
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.resolve(process.cwd(), ".eslintrc.json");
|
||
|
const prodConfig = fs.existsSync(projLintConfig) ? projLintConfig : eslintConfigPath;
|
||
|
const configFilePath = useTestConfig ? eslintTestConfigPath : prodConfig;
|
||
|
const eslintBaseCommand = `node "${eslintFilePath}" -c "${configFilePath}" --resolve-plugins-relative-to "${__dirname}"`;
|
||
|
return eslintBaseCommand;
|
||
|
}
|
||
|
function getLintCheckCommand(files, useTestConfig = false) {
|
||
|
const eslintCommand = `${getEsLintBaseCommand(useTestConfig)} "${normalizeFilePath(files)}"`;
|
||
|
return eslintCommand;
|
||
|
}
|
||
|
exports.getLintCheckCommand = getLintCheckCommand;
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
exports.performLintCheck = performLintCheck;
|
||
|
function getLintFixCommand(files, useTestConfig = false) {
|
||
|
const eslintCommand = `${getEsLintBaseCommand(useTestConfig)} --fix ${normalizeFilePath(files)}`;
|
||
|
return eslintCommand;
|
||
|
}
|
||
|
exports.getLintFixCommand = getLintFixCommand;
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
exports.performLintFix = performLintFix;
|
||
|
function getPrettierCommand(files) {
|
||
|
const prettierFixCommand = `node ${prettierFilePath} --parser typescript --write ${normalizeFilePath(files)}`;
|
||
|
return prettierFixCommand;
|
||
|
}
|
||
|
exports.getPrettierCommand = getPrettierCommand;
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
exports.makeFilesPrettier = makeFilesPrettier;
|
||
|
//# sourceMappingURL=lint.js.map
|