@zohodesk/codestandard-validator 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/hooks/Precommit/commit.js +54 -0
- package/build/hooks/Precommit/errorhelpers.js +57 -0
- package/build/hooks/Precommit/guard.js +50 -0
- package/build/hooks/Precommit/lint.js +118 -0
- package/build/hooks/Precommit/pre-commit-default.js +369 -0
- package/build/hooks/Precommit/pre-commit.js +359 -355
- package/build/hooks/Precommit/utils.js +48 -0
- package/build/hooks/hook.js +19 -10
- package/build/lib/postinstall.js +24 -15
- package/build/setup/sample.config.js +4 -3
- package/build/utils/CloneCommonLinterRepo/cloneViaCdt.js +2 -2
- package/build/utils/General/SonarQubeUtil.js +108 -0
- package/build/utils/General/getGeneralInfo.js +1 -2
- package/build/utils/HuskySetup/configurePrecommitHook.js +4 -1
- package/build/utils/HuskySetup/initializeHusky.js +4 -4
- package/build/utils/Logger/Logger.js +3 -0
- package/build/utils/PluginsInstallation/installPlugins.js +30 -25
- package/changeLog.md +6 -1
- package/index.js +1 -1
- package/jsonUtils/MandatoryListRules.js +3 -0
- package/jsonUtils/commonLinterRepoDetails.js +0 -1
- package/package.json +3 -2
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
filterFiles
|
|
6
|
+
} = require("../../utils/FileAndFolderOperations/filterFiles");
|
|
7
|
+
const {
|
|
8
|
+
Logger
|
|
9
|
+
} = require("../../utils/Logger/Logger");
|
|
10
|
+
const {
|
|
11
|
+
handleMergeCommit,
|
|
12
|
+
ensurePluginsInstalled,
|
|
13
|
+
safeGetStagedFiles,
|
|
14
|
+
safeGetBranch
|
|
15
|
+
} = require("./guard");
|
|
16
|
+
const {
|
|
17
|
+
runLintWorkflow
|
|
18
|
+
} = require("./lint");
|
|
19
|
+
global.analytics = {
|
|
20
|
+
status: "SUCCESS",
|
|
21
|
+
sonarQubeStatus: false,
|
|
22
|
+
message: "No issues are found, your code adheres to the required standards.",
|
|
23
|
+
isExemptionApproved: false,
|
|
24
|
+
totalIssues: 0
|
|
25
|
+
};
|
|
26
|
+
async function preCommitHook() {
|
|
27
|
+
Logger.log(Logger.INFO_TYPE, "\n Executing pre commit hook...");
|
|
28
|
+
Logger.log(Logger.INFO_TYPE, `working dir: ${process.cwd()}`);
|
|
29
|
+
if (await handleMergeCommit()) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
;
|
|
33
|
+
if (!(await ensurePluginsInstalled())) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
;
|
|
37
|
+
const stagedFiles = await safeGetStagedFiles();
|
|
38
|
+
if (stagedFiles.length === 0) {
|
|
39
|
+
Logger.log(Logger.INFO_TYPE, "No staged files. Commit skipped.");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const {
|
|
43
|
+
JsFiles
|
|
44
|
+
} = filterFiles(stagedFiles, [".eslintrc.js"], true);
|
|
45
|
+
const branch = await safeGetBranch();
|
|
46
|
+
const shouldAbort = await runLintWorkflow(JsFiles, branch);
|
|
47
|
+
if (shouldAbort) {
|
|
48
|
+
Logger.log(Logger.FAILURE_TYPE, "There are linter errors/warnings. Commit aborted 🙁.");
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
Logger.log(Logger.SUCCESS_TYPE, `Code is good to go 🙂`);
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
preCommitHook();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
Logger
|
|
5
|
+
} = require("../../utils/Logger/Logger");
|
|
6
|
+
const {
|
|
7
|
+
execSync
|
|
8
|
+
} = require('child_process');
|
|
9
|
+
const path = require("path");
|
|
10
|
+
const {
|
|
11
|
+
getNodeModulesPath
|
|
12
|
+
} = require("../../utils/General/getNodeModulesPath");
|
|
13
|
+
const {
|
|
14
|
+
getRootDirectory
|
|
15
|
+
} = require("../../utils/General/RootDirectoryUtils/getRootDirectory");
|
|
16
|
+
function getAbsolutePath(p1, p2) {
|
|
17
|
+
if (path.isAbsolute(p2)) {
|
|
18
|
+
return path.normalize(p2);
|
|
19
|
+
}
|
|
20
|
+
return path.resolve(p1, p2);
|
|
21
|
+
}
|
|
22
|
+
async function calculateGitDiffForFile(branch, file) {
|
|
23
|
+
try {
|
|
24
|
+
const stdout = execSync(`git diff -U0 ${branch.trim()} -- ${getAbsolutePath(getNodeModulesPath(), path.resolve(getRootDirectory(), file))}`).toString();
|
|
25
|
+
return stdout;
|
|
26
|
+
} catch (err) {
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function parseDiffHunks(diff) {
|
|
31
|
+
return diff.filter(line => line.startsWith("@@")).map(hunk => {
|
|
32
|
+
const [start, count = 1] = hunk.split(" ")[2].slice(1).split(",").map(Number);
|
|
33
|
+
return [start, start + (count - 1)];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async function filterErrorsByChangedLines(errors, branch, file) {
|
|
37
|
+
const diff = await calculateGitDiffForFile(branch, file);
|
|
38
|
+
const changedRanges = parseDiffHunks(diff);
|
|
39
|
+
return errors.filter(err => {
|
|
40
|
+
const line = parseInt(err.split(" ")[0].split(":")[0], 10);
|
|
41
|
+
return changedRanges.some(([s, e]) => line >= s && line <= e);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function filterFileLevelErrors(errors) {
|
|
45
|
+
return errors.slice(1, -2); // strip ESLint summary lines
|
|
46
|
+
}
|
|
47
|
+
function logErrors(file, errors) {
|
|
48
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${file}\x1b[0m`);
|
|
49
|
+
errors.forEach(err => Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${err.trimEnd()}\x1b[0m`));
|
|
50
|
+
}
|
|
51
|
+
module.exports = {
|
|
52
|
+
filterErrorsByChangedLines,
|
|
53
|
+
filterFileLevelErrors,
|
|
54
|
+
logErrors,
|
|
55
|
+
parseDiffHunks,
|
|
56
|
+
calculateGitDiffForFile
|
|
57
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
getBranchName
|
|
5
|
+
} = require("../../utils/GitActions/gitActions");
|
|
6
|
+
const {
|
|
7
|
+
Logger
|
|
8
|
+
} = require("../../utils/Logger/Logger");
|
|
9
|
+
const {
|
|
10
|
+
isMergeCommit,
|
|
11
|
+
areAllPluginsInstalled,
|
|
12
|
+
getStagedFiles
|
|
13
|
+
} = require("./utils");
|
|
14
|
+
async function handleMergeCommit() {
|
|
15
|
+
if (await isMergeCommit()) {
|
|
16
|
+
Logger.log(Logger.INFO_TYPE, "Merge detected. Skipping pre-commit check.");
|
|
17
|
+
process.exit(0);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
async function ensurePluginsInstalled() {
|
|
23
|
+
if (areAllPluginsInstalled()) return true;
|
|
24
|
+
Logger.log(Logger.FAILURE_TYPE, "Commit failed since some lint plugins are not installed");
|
|
25
|
+
Logger.log(Logger.INFO_TYPE, `Run \x1b[37mnpx ZDPrecommit setupPlugins \x1b[33m in the project root to install them.`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
async function safeGetStagedFiles() {
|
|
30
|
+
try {
|
|
31
|
+
return await getStagedFiles();
|
|
32
|
+
} catch {
|
|
33
|
+
Logger.log(Logger.INFO_TYPE, "Error fetching staged files");
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function safeGetBranch() {
|
|
38
|
+
try {
|
|
39
|
+
return await getBranchName();
|
|
40
|
+
} catch {
|
|
41
|
+
Logger.log(Logger.INFO_TYPE, "Error fetching current branch");
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
module.exports = {
|
|
46
|
+
safeGetBranch,
|
|
47
|
+
safeGetStagedFiles,
|
|
48
|
+
handleMergeCommit,
|
|
49
|
+
ensurePluginsInstalled
|
|
50
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
execSync
|
|
5
|
+
} = require('child_process');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const {
|
|
9
|
+
getNodeModulesPath
|
|
10
|
+
} = require('../../utils/General/getNodeModulesPath');
|
|
11
|
+
const {
|
|
12
|
+
getEslintExecutablePath
|
|
13
|
+
} = require('../../utils/ConfigFileUtils/getEslintExecutablePath');
|
|
14
|
+
const {
|
|
15
|
+
getRootDirectory
|
|
16
|
+
} = require('../../utils/General/RootDirectoryUtils/getRootDirectory');
|
|
17
|
+
const {
|
|
18
|
+
getSupportedLanguage
|
|
19
|
+
} = require('../../utils/General/getGeneralInfo');
|
|
20
|
+
const {
|
|
21
|
+
calculateGitDiffForFile
|
|
22
|
+
} = require('./errorhelpers');
|
|
23
|
+
const {
|
|
24
|
+
Execution,
|
|
25
|
+
SonarQube,
|
|
26
|
+
EsLint
|
|
27
|
+
} = require('@zohodesk/codestandard-analytics');
|
|
28
|
+
const {
|
|
29
|
+
getBranchName
|
|
30
|
+
} = require('../../utils/GitActions/gitActions');
|
|
31
|
+
const {
|
|
32
|
+
extractDiffHunks
|
|
33
|
+
} = require('./utils');
|
|
34
|
+
const {
|
|
35
|
+
Logger
|
|
36
|
+
} = require('../../utils/Logger/Logger');
|
|
37
|
+
async function lintFiles(filePath) {
|
|
38
|
+
const ext = path.extname(filePath);
|
|
39
|
+
if (['.js', '.ts', '.tsx', '.jsx', '.properties'].includes(ext)) {
|
|
40
|
+
return findEslintErrors(filePath);
|
|
41
|
+
}
|
|
42
|
+
if (['.css', '.scss'].includes(ext)) {
|
|
43
|
+
return findStyleLintErrors(filePath);
|
|
44
|
+
}
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
async function findEslintErrors(file) {
|
|
48
|
+
const nodeModulesPath = getNodeModulesPath();
|
|
49
|
+
const eslintPath = getEslintExecutablePath();
|
|
50
|
+
const eslintConfig = `${nodeModulesPath}/.eslintrc.js`;
|
|
51
|
+
if (!fs.existsSync(nodeModulesPath)) {
|
|
52
|
+
Logger.log(Logger.INFO_TYPE, 'node_modules not found');
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
if (!fs.existsSync(eslintPath)) {
|
|
56
|
+
Logger.log(Logger.INFO_TYPE, 'Eslint executable not found. Make sure eslint plugin is installed');
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
return execSync(`npx --ignore-existing "${eslintPath}" --config "${eslintConfig}" --no-inline-config --resolve-plugins-relative-to="${nodeModulesPath}/node_modules" ${path.resolve(getRootDirectory(), file)}`).then(({
|
|
60
|
+
stderr
|
|
61
|
+
}) => stderr ? stderr.toString().trim().split('\n') : []).catch(err => {
|
|
62
|
+
Logger.log(Logger.FAILURE_TYPE, err);
|
|
63
|
+
throw new Error('Error executing eslint command');
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function findStyleLintErrors(filePath) {
|
|
67
|
+
const configFile = path.resolve(getNodeModulesPath(), '.stylelintrc.js');
|
|
68
|
+
const absolutePath = path.join(getRootDirectory(), filePath);
|
|
69
|
+
return execSync(`npx stylelint ${absolutePath} --config ${configFile}`, {
|
|
70
|
+
cwd: getNodeModulesPath()
|
|
71
|
+
}).then(({
|
|
72
|
+
stdout
|
|
73
|
+
}) => stdout ? stdout.toString().trim().split('\n') : []).catch(err => {
|
|
74
|
+
Logger.log(Logger.FAILURE_TYPE, err);
|
|
75
|
+
return [];
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async function runLintWorkflow(files, branch) {
|
|
79
|
+
const branchDiff = {
|
|
80
|
+
diffs: []
|
|
81
|
+
};
|
|
82
|
+
var branchName = getBranchName();
|
|
83
|
+
for (const file of files) {
|
|
84
|
+
if (!getSupportedLanguage().includes(path.extname(file))) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
;
|
|
88
|
+
const changeset = extractDiffHunks(await calculateGitDiffForFile(branchName, file));
|
|
89
|
+
const diff = {
|
|
90
|
+
old_path: file,
|
|
91
|
+
new_path: file,
|
|
92
|
+
diff: changeset
|
|
93
|
+
};
|
|
94
|
+
branchDiff.diffs.push(diff);
|
|
95
|
+
}
|
|
96
|
+
const diffPath = path.resolve(getNodeModulesPath(), 'diffBranch.json');
|
|
97
|
+
fs.writeFileSync(diffPath, JSON.stringify(branchDiff));
|
|
98
|
+
const cliobj = {
|
|
99
|
+
env: 'ci',
|
|
100
|
+
cmdExecuted: 'precommit'
|
|
101
|
+
};
|
|
102
|
+
const execute = new Execution(EsLint, SonarQube, cliobj);
|
|
103
|
+
await execute.executeLintHandler().finally(async () => {
|
|
104
|
+
await execute.executeMetricHandler();
|
|
105
|
+
/**
|
|
106
|
+
* global.analytics.totalIssues = global.analytics.totalIssues + 1;
|
|
107
|
+
global.analytics.status = "FAILURE";
|
|
108
|
+
global.analytics.message = 'Issues are detected, please review and resolve the errors'
|
|
109
|
+
*/
|
|
110
|
+
// const { issues, hasIssue, totalIssues } = fetchProjectIssuesViaCurl("projectName");
|
|
111
|
+
});
|
|
112
|
+
// Logger.log(Logger.INFO_TYPE,global.analytics)
|
|
113
|
+
return global.analytics.status == 'FAILURE' ? true : false;
|
|
114
|
+
}
|
|
115
|
+
module.exports = {
|
|
116
|
+
runLintWorkflow,
|
|
117
|
+
lintFiles
|
|
118
|
+
};
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
exec
|
|
5
|
+
} = require('child_process');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const {
|
|
9
|
+
getEslintExecutablePath
|
|
10
|
+
} = require('../../utils/ConfigFileUtils/getEslintExecutablePath');
|
|
11
|
+
const {
|
|
12
|
+
getNodeModulesPath
|
|
13
|
+
} = require('../../utils/General/getNodeModulesPath');
|
|
14
|
+
const {
|
|
15
|
+
filterFiles
|
|
16
|
+
} = require('../../utils/FileAndFolderOperations/filterFiles');
|
|
17
|
+
const {
|
|
18
|
+
Logger
|
|
19
|
+
} = require('../../utils/Logger/Logger');
|
|
20
|
+
const {
|
|
21
|
+
checkIfPluginsAreInstalled
|
|
22
|
+
} = require('../../utils/PluginsInstallation/checkIfPluginsAreInstalled');
|
|
23
|
+
const {
|
|
24
|
+
getBranchName
|
|
25
|
+
} = require('../../utils/GitActions/gitActions');
|
|
26
|
+
const {
|
|
27
|
+
getConfigurationPrecommit,
|
|
28
|
+
getSupportedLanguage
|
|
29
|
+
} = require('../../utils/General/getGeneralInfo');
|
|
30
|
+
const {
|
|
31
|
+
getRootDirectory
|
|
32
|
+
} = require('../../utils/General/RootDirectoryUtils/getRootDirectory');
|
|
33
|
+
const MandatoryListRules = require('../../../jsonUtils/MandatoryListRules');
|
|
34
|
+
const {
|
|
35
|
+
impactBasedPrecommit,
|
|
36
|
+
shouldWarningsAbortCommit
|
|
37
|
+
} = getConfigurationPrecommit();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @function isMergeCommit - This method check whether it is merge or not
|
|
41
|
+
* @returns {Boolean} - return boolean based on latest merge changes
|
|
42
|
+
*/
|
|
43
|
+
async function isMergeCommit() {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
exec('git rev-parse -q --verify MERGE_HEAD', (error, stderr, stdout) => {
|
|
46
|
+
if (error) {
|
|
47
|
+
reject(error.toString().trim());
|
|
48
|
+
} else if (stderr) {
|
|
49
|
+
resolve(stderr.trim());
|
|
50
|
+
} else if (stdout) {
|
|
51
|
+
resolve(stdout.trim());
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @function {getStagedFiles} - methods return staged files
|
|
59
|
+
* @returns {Array<string>} - array of files
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
async function getStagedFiles() {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
exec("git diff --staged --name-only", (error, stderr, stdout) => {
|
|
65
|
+
if (error) {
|
|
66
|
+
if (error != null) reject("Couldn't fetch staged files");
|
|
67
|
+
} else if (stderr) {
|
|
68
|
+
resolve(filterDeltedFileFromStagedFiles(stderr.trim().split('\n')));
|
|
69
|
+
} else if (stdout.trim() === '') {
|
|
70
|
+
resolve(stdout.trim());
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @function {filterDeltedFileFromStagedFiles} - filter deleted staged files
|
|
78
|
+
* @param {Array<string>} files - staged files
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
function filterDeltedFileFromStagedFiles(files) {
|
|
82
|
+
return files.filter(file => {
|
|
83
|
+
const absolutePath = path.resolve(getRootDirectory(), file);
|
|
84
|
+
if (fs.existsSync(absolutePath)) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async function lintFiles(filePath) {
|
|
91
|
+
switch (String(path.extname(filePath))) {
|
|
92
|
+
case '.js':
|
|
93
|
+
case '.ts':
|
|
94
|
+
case '.tsx':
|
|
95
|
+
case '.jsx':
|
|
96
|
+
case '.properties':
|
|
97
|
+
{
|
|
98
|
+
return await findEslintErrors(filePath);
|
|
99
|
+
}
|
|
100
|
+
case '.css':
|
|
101
|
+
case '.scss':
|
|
102
|
+
{
|
|
103
|
+
return await findStyleLintErrors(filePath);
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
{
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @function findEslintErrors - method Lint given file based on given configuration
|
|
114
|
+
* @param {*} file - path of file to lint
|
|
115
|
+
* @returns {Array<string>} - array of command line report as a string
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
function findEslintErrors(file) {
|
|
119
|
+
let nodeModulesPathOfProject = `${getNodeModulesPath()}`;
|
|
120
|
+
let eslintExecutablePath = getEslintExecutablePath();
|
|
121
|
+
let eslintConfigurationFilePath = `${nodeModulesPathOfProject}/.eslintrc.js`;
|
|
122
|
+
let isEslintExecutablePresent = fs.existsSync(eslintExecutablePath) ? true : false;
|
|
123
|
+
let isNodeModulesPresent = fs.existsSync(nodeModulesPathOfProject);
|
|
124
|
+
if (isNodeModulesPresent) {
|
|
125
|
+
if (isEslintExecutablePresent) {
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
exec(`npx --ignore-existing "${eslintExecutablePath}" --config "${eslintConfigurationFilePath}" --no-inline-config --resolve-plugins-relative-to="${nodeModulesPathOfProject}/node_modules" ${path.resolve(getRootDirectory(), file)}`, (error, stderr, stdout) => {
|
|
128
|
+
if (stderr) {
|
|
129
|
+
resolve(stderr.trim().split('\n'));
|
|
130
|
+
} else if (error) {
|
|
131
|
+
Logger.log(Logger.FAILURE_TYPE, error);
|
|
132
|
+
reject("Error executing eslint command");
|
|
133
|
+
} else {
|
|
134
|
+
resolve([]);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
Logger.log(Logger.INFO_TYPE, 'Eslint executable not found. make sure eslint plugin is installed');
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
Logger.log(Logger.INFO_TYPE, 'node_modules not found');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
*
|
|
148
|
+
* @param {*} params
|
|
149
|
+
*/
|
|
150
|
+
function findStyleLintErrors(filePath) {
|
|
151
|
+
const configFilePath = path.resolve(getNodeModulesPath(), '.stylelintrc.js');
|
|
152
|
+
const absolutePath = path.join(getRootDirectory(), filePath);
|
|
153
|
+
try {
|
|
154
|
+
return new Promise((resolve, reject) => {
|
|
155
|
+
exec(`npx stylelint ${absolutePath} --config ${configFilePath}`, {
|
|
156
|
+
cwd: getNodeModulesPath()
|
|
157
|
+
}, (error, stderr, stdout) => {
|
|
158
|
+
if (stdout) {
|
|
159
|
+
resolve(stdout.trim().split('\n'));
|
|
160
|
+
} else if (error) {
|
|
161
|
+
Logger.log(Logger.FAILURE_TYPE, error);
|
|
162
|
+
reject("Error executing stylelint command");
|
|
163
|
+
} else {
|
|
164
|
+
resolve([]);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
Logger.log(Logger.FAILURE_TYPE, `Issue is lint css files`);
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @function {calculateGitDiffForFile} - method calculate diff of file
|
|
176
|
+
* @param {*} branch_name - branch name
|
|
177
|
+
* @param {*} file - path of file
|
|
178
|
+
* @returns {Promise<Array<string>>} - array of command line report as a string
|
|
179
|
+
*/
|
|
180
|
+
async function calculateGitDiffForFile(branch_name, file) {
|
|
181
|
+
let gitDiffCommand = `git diff -U0 ${branch_name.trim()} ${path.resolve(getRootDirectory(), file)}`;
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
exec(gitDiffCommand, (error, stderr) => {
|
|
184
|
+
if (stderr) {
|
|
185
|
+
resolve(stderr.trim().split('\n'));
|
|
186
|
+
} else if (error) {
|
|
187
|
+
reject(error);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @function {areAllPluginsInstalled} - method whether plugin is installed or not
|
|
194
|
+
* @returns {Boolean} - return boolean based on plugin installed or not
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
function areAllPluginsInstalled() {
|
|
198
|
+
let unInstalledPlugins = checkIfPluginsAreInstalled().uninstalledPlugins;
|
|
199
|
+
return unInstalledPlugins.length === 0 ? true : false;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @function {isOnlyWarningsPresentInFile} - method that checks if only eslint warnings are present in a file
|
|
204
|
+
* @returns {Boolean} - returns boolean based on only warnings present or not in file
|
|
205
|
+
*/
|
|
206
|
+
function isOnlyWarningsPresentInFile(eslintErrorsPresent) {
|
|
207
|
+
let severityOfEachErrorInFile = [];
|
|
208
|
+
eslintErrorsPresent.map(error => {
|
|
209
|
+
let partsInString = error.split(" ");
|
|
210
|
+
let severityOfError = partsInString.find(word => word === 'error' || word === 'warning' || word === '✖');
|
|
211
|
+
severityOfEachErrorInFile.push(severityOfError);
|
|
212
|
+
});
|
|
213
|
+
return !(severityOfEachErrorInFile.includes('✖') || severityOfEachErrorInFile.includes('error'));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @function {preCommitHook} - method execute pre commit hook
|
|
218
|
+
* @returns {void}
|
|
219
|
+
*/
|
|
220
|
+
|
|
221
|
+
async function preCommitHook_default() {
|
|
222
|
+
Logger.log(Logger.INFO_TYPE, '\n Executing pre commit hook...');
|
|
223
|
+
Logger.log(Logger.INFO_TYPE, `working dir : ${process.cwd()}`);
|
|
224
|
+
try {
|
|
225
|
+
let isMerge = await isMergeCommit();
|
|
226
|
+
Logger.log(Logger.INFO_TYPE, 'Looks like you have merged. So skipping pre commit check');
|
|
227
|
+
process.exit(0);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
if (areAllPluginsInstalled()) {
|
|
230
|
+
let staged_files = [];
|
|
231
|
+
let eslintConfigFiles = ['.eslintrc.js'];
|
|
232
|
+
let exemptionFiles = [];
|
|
233
|
+
let current_branch = '';
|
|
234
|
+
let hasEslintErrorsInChangedLines = false;
|
|
235
|
+
let hasEslintErrorsInFiles = false;
|
|
236
|
+
let areFilesStaged = false;
|
|
237
|
+
let shouldAbortCommit = false;
|
|
238
|
+
try {
|
|
239
|
+
current_branch = await getBranchName();
|
|
240
|
+
} catch {
|
|
241
|
+
Logger.log(Logger.INFO_TYPE, "Error fetching current branch");
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
staged_files = await getStagedFiles();
|
|
245
|
+
if (!staged_files.length == 0) {
|
|
246
|
+
const {
|
|
247
|
+
JsFiles: staged_filesJS,
|
|
248
|
+
CssFiles
|
|
249
|
+
} = filterFiles(staged_files, eslintConfigFiles, true);
|
|
250
|
+
|
|
251
|
+
// staged_filesJS = filterFiles(staged_filesJS,exemptionFiles) //this is the code for giving exemption to a file during pre commit
|
|
252
|
+
// if(staged_filesJS.length === 0){
|
|
253
|
+
// Logger.log(Logger.SUCCESS_TYPE,`Commit Successful`)
|
|
254
|
+
// process.exit(0)
|
|
255
|
+
// }
|
|
256
|
+
|
|
257
|
+
// CssFiles not Enabled For while
|
|
258
|
+
areFilesStaged = true;
|
|
259
|
+
var stagedFiles = [...staged_filesJS];
|
|
260
|
+
for (let file in stagedFiles) {
|
|
261
|
+
let currentFileName = stagedFiles[file];
|
|
262
|
+
let changedLinesArray = [];
|
|
263
|
+
let eslintErrorsInChangedLines = new Set();
|
|
264
|
+
let isOnlyEslintWarningsPresentInFile = false;
|
|
265
|
+
if (getSupportedLanguage().includes(path.extname(stagedFiles[file]))) {
|
|
266
|
+
try {
|
|
267
|
+
var errorsInFile = await lintFiles(stagedFiles[file]);
|
|
268
|
+
// eslintErrorsInFile = impactBasedPrecommit == false ? filterWarningInFile(errorsInFile) : errorsInFile
|
|
269
|
+
if (stagedFiles[file] && typeof stagedFiles[file] == 'string') {
|
|
270
|
+
if (!errorsInFile.length == 0) {
|
|
271
|
+
//Calculating changed lines in a file and storing them in respective arrays
|
|
272
|
+
if (impactBasedPrecommit) {
|
|
273
|
+
//git diff is computed and stored in an array
|
|
274
|
+
let git_diff = await calculateGitDiffForFile(current_branch, stagedFiles[file]);
|
|
275
|
+
changedLinesArray = git_diff.filter(line => line.startsWith('@@'));
|
|
276
|
+
let changedLinesStartArray = [];
|
|
277
|
+
let changedLinesEndArray = [];
|
|
278
|
+
for (let number of changedLinesArray) {
|
|
279
|
+
let changesStartLine = parseInt(number.split(' ')[2].split(',')[0]);
|
|
280
|
+
changedLinesStartArray.push(changesStartLine);
|
|
281
|
+
let changesEndLine = number.split(' ')[2].split(',')[1];
|
|
282
|
+
if (changesEndLine === undefined) {
|
|
283
|
+
changedLinesEndArray.push(changesStartLine);
|
|
284
|
+
} else {
|
|
285
|
+
changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (let error = 1; error < errorsInFile.length - 2; error++) {
|
|
289
|
+
//errorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
|
|
290
|
+
//errorsInFile[error].trim().split(' ')[0] => 69:26
|
|
291
|
+
//errorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
|
|
292
|
+
|
|
293
|
+
let eslintErrorLineNumber = errorsInFile[error].trim().split(' ')[0].split(':')[0];
|
|
294
|
+
if (MandatoryListRules.some(ruleId => errorsInFile[error].trim().includes(ruleId))) {
|
|
295
|
+
eslintErrorsInChangedLines.add(errorsInFile[error]);
|
|
296
|
+
}
|
|
297
|
+
for (let lineNumber in changedLinesStartArray) {
|
|
298
|
+
if (eslintErrorLineNumber >= changedLinesStartArray[lineNumber] && eslintErrorLineNumber <= changedLinesEndArray[lineNumber]) {
|
|
299
|
+
eslintErrorsInChangedLines.add(errorsInFile[error]);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (eslintErrorsInChangedLines.size > 0) {
|
|
304
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(Array.from(eslintErrorsInChangedLines));
|
|
305
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
306
|
+
for (let eslintError of Array.from(eslintErrorsInChangedLines)) {
|
|
307
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
308
|
+
}
|
|
309
|
+
if (shouldWarningsAbortCommit) {
|
|
310
|
+
hasEslintErrorsInChangedLines = true;
|
|
311
|
+
shouldAbortCommit = true;
|
|
312
|
+
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
313
|
+
hasEslintErrorsInChangedLines = false;
|
|
314
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
315
|
+
hasEslintErrorsInChangedLines = true;
|
|
316
|
+
shouldAbortCommit = true;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
if (errorsInFile.length > 0) {
|
|
321
|
+
let startIndex = 1;
|
|
322
|
+
let endIndex = errorsInFile.length - 2;
|
|
323
|
+
let listOsEslintErrors = errorsInFile.slice(startIndex, endIndex);
|
|
324
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(listOsEslintErrors);
|
|
325
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
326
|
+
for (let eslintError of listOsEslintErrors) {
|
|
327
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
328
|
+
}
|
|
329
|
+
if (shouldWarningsAbortCommit) {
|
|
330
|
+
hasEslintErrorsInFiles = true;
|
|
331
|
+
shouldAbortCommit = true;
|
|
332
|
+
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
333
|
+
hasEslintErrorsInFiles = false;
|
|
334
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
335
|
+
hasEslintErrorsInFiles = true;
|
|
336
|
+
shouldAbortCommit = true;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} catch (err) {
|
|
343
|
+
Logger.log(Logger.FAILURE_TYPE, err);
|
|
344
|
+
Logger.log(Logger.FAILURE_TYPE, "Error in executing lint command");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
} else if (staged_files.length === 0) {
|
|
349
|
+
Logger.log(Logger.INFO_TYPE, 'No files have been staged. Stage your files before committing');
|
|
350
|
+
}
|
|
351
|
+
} catch {
|
|
352
|
+
Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
|
|
353
|
+
}
|
|
354
|
+
if (shouldAbortCommit) {
|
|
355
|
+
Logger.log(Logger.FAILURE_TYPE, `There are linter errors/warnings present. So commit is aborted.`);
|
|
356
|
+
process.exit(1);
|
|
357
|
+
} else if (shouldAbortCommit === false && staged_files.length !== 0) {
|
|
358
|
+
Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
|
|
359
|
+
process.exit(0);
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
Logger.log(Logger.FAILURE_TYPE, 'Commit failed since some lint plugins are not installed');
|
|
363
|
+
Logger.log(Logger.INFO_TYPE, `Kindly execute the command \x1b[37mnpx ZDPrecommit setupPlugins \x1b[33mfrom the location where package.json is present to install the plugins`);
|
|
364
|
+
Logger.log(Logger.INFO_TYPE, 'Execute the command and kindly try committing again.');
|
|
365
|
+
process.exit(1);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
preCommitHook_default();
|