@zohodesk/codestandard-validator 0.0.6-exp-20 → 0.0.6-exp-21
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/pre-commit.js +293 -392
- package/build/lib/cli.js +8 -6
- package/build/lib/postinstall.js +4 -4
- package/build/utils/CloneCommonLinterRepo/cloneViaCdt.js +1 -1
- package/build/utils/FileAndFolderOperations/filterFiles.js +2 -27
- package/build/utils/FileAndFolderOperations/versionControl.js +8 -7
- package/build/utils/General/getGeneralInfo.js +3 -62
- package/build/utils/General/writeProjectDetailsToJson.js +2 -7
- package/build/utils/PluginsInstallation/arePluginsInstalled.js +9 -5
- package/build/utils/PluginsInstallation/checkIfPluginsAreInstalled.js +59 -5
- package/build/utils/PluginsInstallation/installPlugins.js +76 -23
- package/index.js +1 -1
- package/package.json +2 -3
|
@@ -1,409 +1,310 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// }
|
|
40
|
-
|
|
41
|
-
// /**
|
|
42
|
-
// * @function {getStagedFiles} - methods return staged files
|
|
43
|
-
// * @returns {Array<string>} - array of files
|
|
44
|
-
// */
|
|
45
|
-
|
|
46
|
-
// async function getStagedFiles(){
|
|
47
|
-
// return new Promise((resolve, reject) => {
|
|
48
|
-
// exec("git diff --staged --name-only",(error,stderr,stdout) =>{
|
|
49
|
-
// if (error){
|
|
50
|
-
// if(error != null)
|
|
51
|
-
// reject("Couldn't fetch staged files")
|
|
52
|
-
// }
|
|
53
|
-
// else if(stderr){
|
|
54
|
-
// resolve(filterDeltedFileFromStagedFiles(stderr.trim().split('\n')))
|
|
55
|
-
// }
|
|
56
|
-
// else if(stdout.trim() === ''){
|
|
57
|
-
// resolve(stdout.trim())
|
|
58
|
-
// }
|
|
59
|
-
// })
|
|
60
|
-
// })
|
|
61
|
-
|
|
62
|
-
// }
|
|
63
|
-
|
|
64
|
-
// /**
|
|
65
|
-
// * @function {filterDeltedFileFromStagedFiles} - filter deleted staged files
|
|
66
|
-
// * @param {Array<string>} files - staged files
|
|
67
|
-
// * @returns
|
|
68
|
-
// */
|
|
69
|
-
// function filterDeltedFileFromStagedFiles(files){
|
|
70
|
-
// return files.filter((file) =>{
|
|
71
|
-
// const absolutePath= path.resolve(getRootDirectory(),file)
|
|
72
|
-
// if(fs.existsSync(absolutePath)){
|
|
73
|
-
// return true
|
|
74
|
-
// }
|
|
75
|
-
// return false
|
|
76
|
-
// })
|
|
77
|
-
// }
|
|
78
|
-
|
|
79
|
-
// async function lintFiles(filePath){
|
|
80
|
-
// switch(path.extname(filePath)){
|
|
81
|
-
// case '.js' ||'.ts' || '.tsx' || '.jsx' : {
|
|
82
|
-
// return await findEslintErrors(filePath )
|
|
83
|
-
// }
|
|
84
|
-
|
|
85
|
-
// case '.css' || '.scss' :{
|
|
86
|
-
// return await findStyleLintErrors(filePath)
|
|
87
|
-
// }
|
|
88
|
-
// default : {
|
|
89
|
-
// return []
|
|
90
|
-
// }
|
|
91
|
-
// }
|
|
92
|
-
// }
|
|
93
|
-
|
|
94
|
-
// /**
|
|
95
|
-
// * @function findEslintErrors - method Lint given file based on given configuration
|
|
96
|
-
// * @param {*} file - path of file to lint
|
|
97
|
-
// * @returns {Array<string>} - array of command line report as a string
|
|
98
|
-
// */
|
|
99
|
-
|
|
100
|
-
// function findEslintErrors(file){
|
|
101
|
-
// let nodeModulesPathOfProject = `${getNodeModulesPath()}`
|
|
102
|
-
// let eslintExecutablePath = getEslintExecutablePath()
|
|
103
|
-
// let eslintConfigurationFilePath = `${nodeModulesPathOfProject}/.eslintrc.js`
|
|
104
|
-
// let isEslintExecutablePresent = fs.existsSync(eslintExecutablePath) ? true : false
|
|
105
|
-
// let isNodeModulesPresent = fs.existsSync(nodeModulesPathOfProject)
|
|
106
|
-
|
|
107
|
-
// if(isNodeModulesPresent){
|
|
108
|
-
// if(isEslintExecutablePresent){
|
|
109
|
-
// return new Promise ((resolve, reject) => {
|
|
110
|
-
// exec(`npx --ignore-existing "${eslintExecutablePath}" --config "${eslintConfigurationFilePath}" --no-inline-config --resolve-plugins-relative-to="${nodeModulesPathOfProject}/node_modules" ${file}`,(error,stderr,stdout) => {
|
|
111
|
-
// if(stderr){
|
|
112
|
-
// resolve(stderr.trim().split('\n'))
|
|
113
|
-
// }
|
|
114
|
-
// else if(error){
|
|
115
|
-
// Logger.log(Logger.FAILURE_TYPE,error)
|
|
116
|
-
// reject("Error executing eslint command")
|
|
117
|
-
// }
|
|
118
|
-
// else{
|
|
119
|
-
// resolve([])
|
|
120
|
-
// }
|
|
121
|
-
// })
|
|
122
|
-
// })
|
|
123
|
-
// }
|
|
124
|
-
// else{
|
|
125
|
-
// Logger.log(Logger.INFO_TYPE,'Eslint executable not found. make sure eslint plugin is installed')
|
|
126
|
-
// }
|
|
127
|
-
// }
|
|
128
|
-
// else{
|
|
129
|
-
// Logger.log(Logger.INFO_TYPE,'node_modules not found')
|
|
130
|
-
// }
|
|
131
|
-
// }
|
|
132
|
-
|
|
133
|
-
// /**
|
|
134
|
-
// *
|
|
135
|
-
// * @param {*} params
|
|
136
|
-
// */
|
|
137
|
-
// function findStyleLintErrors(filePath) {
|
|
138
|
-
// const configFilePath = path.resolve(getNodeModulesPath(),'.stylelintrc.json')
|
|
139
|
-
// const absolutePath = path.join(getRootDirectory(),filePath)
|
|
140
|
-
// try{
|
|
141
|
-
// return new Promise ((resolve, reject) => {
|
|
142
|
-
// exec(`npx stylelint ${absolutePath} --config ${configFilePath}`,{cwd:getNodeModulesPath()},(error,stderr,stdout) => {
|
|
143
|
-
// if(stderr){
|
|
144
|
-
// resolve(stderr.trim().split('\n'))
|
|
145
|
-
// }
|
|
146
|
-
// else if(error){
|
|
147
|
-
// Logger.log(Logger.FAILURE_TYPE,error)
|
|
148
|
-
// reject("Error executing eslint command")
|
|
149
|
-
// }
|
|
150
|
-
// else{
|
|
151
|
-
// resolve([])
|
|
152
|
-
// }
|
|
153
|
-
// })
|
|
154
|
-
// })
|
|
155
|
-
// }catch(error){
|
|
156
|
-
// Logger.log(Logger.FAILURE_TYPE,`Issue is lint css files`)
|
|
157
|
-
// return []
|
|
158
|
-
// }
|
|
159
|
-
// }
|
|
160
|
-
|
|
161
|
-
// /**
|
|
162
|
-
// * @function {calculateGitDiffForFile} - method calculate diff of file
|
|
163
|
-
// * @param {*} branch_name - branch name
|
|
164
|
-
// * @param {*} file - path of file
|
|
165
|
-
// * @returns {Promise<Array<string>>} - array of command line report as a string
|
|
166
|
-
// */
|
|
167
|
-
// async function calculateGitDiffForFile(branch_name,file){
|
|
168
|
-
// let gitDiffCommand = `git diff -U0 ${branch_name.trim()} ${file}`
|
|
169
|
-
// return new Promise ((resolve,reject) => {
|
|
170
|
-
// exec(gitDiffCommand,(error, stderr) => {
|
|
171
|
-
// if(stderr){
|
|
172
|
-
// resolve(stderr.trim().split('\n'))
|
|
173
|
-
// }
|
|
174
|
-
// else if(error) {
|
|
175
|
-
// reject(error)
|
|
176
|
-
// }
|
|
177
|
-
// })
|
|
178
|
-
// })
|
|
179
|
-
// }
|
|
180
|
-
// /**
|
|
181
|
-
// * @function {areAllPluginsInstalled} - method whether plugin is installed or not
|
|
182
|
-
// * @returns {Boolean} - return boolean based on plugin installed or not
|
|
183
|
-
// */
|
|
184
|
-
|
|
185
|
-
// function areAllPluginsInstalled(){
|
|
186
|
-
// let unInstalledPlugins = checkIfPluginsAreInstalled().uninstalledPlugins
|
|
187
|
-
// return unInstalledPlugins.length === 0 ? true : false
|
|
188
|
-
// }
|
|
189
|
-
|
|
190
|
-
// /**
|
|
191
|
-
// * @function {isOnlyWarningsPresentInFile} - method that checks if only eslint warnings are present in a file
|
|
192
|
-
// * @returns {Boolean} - returns boolean based on only warnings present or not in file
|
|
193
|
-
// */
|
|
194
|
-
// function isOnlyWarningsPresentInFile(eslintErrorsPresent){
|
|
195
|
-
// let severityOfEachErrorInFile = []
|
|
196
|
-
// eslintErrorsPresent.map(error => {
|
|
197
|
-
// let partsInString = error.split(" ")
|
|
198
|
-
// let severityOfError = partsInString.find(word => word === 'error' || word === 'warning'|| word === '✖')
|
|
199
|
-
// severityOfEachErrorInFile.push(severityOfError)
|
|
200
|
-
// })
|
|
201
|
-
// return !(severityOfEachErrorInFile.includes('✖') || severityOfEachErrorInFile.includes('error'))
|
|
202
|
-
// }
|
|
203
|
-
|
|
204
|
-
// /**
|
|
205
|
-
// * @function {preCommitHook} - method execute pre commit hook
|
|
206
|
-
// * @returns {void}
|
|
207
|
-
// */
|
|
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/EslintConfigFileUtils/getEslintExecutablePath');
|
|
11
|
+
const {
|
|
12
|
+
getNodeModulesPath
|
|
13
|
+
} = require('../../utils/General/getNodeModulesPath');
|
|
14
|
+
const {
|
|
15
|
+
filterFiles,
|
|
16
|
+
filterWarningInFile
|
|
17
|
+
} = require('../../utils/FileAndFolderOperations/filterFiles');
|
|
18
|
+
const {
|
|
19
|
+
Logger
|
|
20
|
+
} = require('../../utils/Logger/Logger');
|
|
21
|
+
const {
|
|
22
|
+
checkIfPluginsAreInstalled
|
|
23
|
+
} = require('../../utils/PluginsInstallation/checkIfPluginsAreInstalled');
|
|
24
|
+
const {
|
|
25
|
+
getBranchName
|
|
26
|
+
} = require('../../utils/GitActions/gitActions');
|
|
27
|
+
const {
|
|
28
|
+
getConfigurationPrecommit,
|
|
29
|
+
getSupportedLanguage
|
|
30
|
+
} = require('../../utils/General/getGeneralInfo');
|
|
31
|
+
const {
|
|
32
|
+
getRootDirectory
|
|
33
|
+
} = require('../../utils/General/RootDirectoryUtils/getRootDirectory');
|
|
34
|
+
const {
|
|
35
|
+
impactBasedPrecommit,
|
|
36
|
+
shouldWarningsAbortCommit
|
|
37
|
+
} = getConfigurationPrecommit();
|
|
208
38
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// let shouldAbortCommit = false
|
|
227
|
-
// try{
|
|
228
|
-
// current_branch = await getBranchName()
|
|
229
|
-
// }
|
|
230
|
-
// catch{
|
|
231
|
-
// Logger.log(Logger.INFO_TYPE,"Error fetching current branch")
|
|
232
|
-
// }
|
|
233
|
-
// try{
|
|
234
|
-
// staged_files = await getStagedFiles()
|
|
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
|
+
}
|
|
235
56
|
|
|
236
|
-
|
|
237
|
-
|
|
57
|
+
/**
|
|
58
|
+
* @function {getStagedFiles} - methods return staged files
|
|
59
|
+
* @returns {Array<string>} - array of files
|
|
60
|
+
*/
|
|
238
61
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
+
}
|
|
251
75
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
+
}
|
|
260
90
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
91
|
+
/**
|
|
92
|
+
* @function findEslintErrors - method Lint given file based on given configuration
|
|
93
|
+
* @param {*} file - path of file to lint
|
|
94
|
+
* @returns {Array<string>} - array of command line report as a string
|
|
95
|
+
*/
|
|
264
96
|
|
|
265
|
-
|
|
266
|
-
|
|
97
|
+
async function findEslintErrors(file) {
|
|
98
|
+
let nodeModulesPathOfProject = `${getNodeModulesPath()}`;
|
|
99
|
+
let eslintExecutablePath = getEslintExecutablePath();
|
|
100
|
+
let eslintConfigurationFilePath = `${nodeModulesPathOfProject}/.eslintrc.js`;
|
|
101
|
+
let isEslintExecutablePresent = fs.existsSync(eslintExecutablePath) ? true : false;
|
|
102
|
+
let isNodeModulesPresent = fs.existsSync(nodeModulesPathOfProject);
|
|
103
|
+
if (isNodeModulesPresent) {
|
|
104
|
+
if (isEslintExecutablePresent) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
exec(`npx --ignore-existing "${eslintExecutablePath}" --config "${eslintConfigurationFilePath}" --no-inline-config --resolve-plugins-relative-to="${nodeModulesPathOfProject}/node_modules" ${file}`, (error, stderr, stdout) => {
|
|
107
|
+
if (stderr) {
|
|
108
|
+
resolve(stderr.trim().split('\n'));
|
|
109
|
+
} else if (error) {
|
|
110
|
+
Logger.log(Logger.FAILURE_TYPE, error);
|
|
111
|
+
reject("Error executing eslint command");
|
|
112
|
+
} else {
|
|
113
|
+
resolve([]);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
} else {
|
|
118
|
+
Logger.log(Logger.INFO_TYPE, 'Eslint executable not found. make sure eslint plugin is installed');
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
Logger.log(Logger.INFO_TYPE, 'node_modules not found');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @function {calculateGitDiffForFile} - method calculate diff of file
|
|
126
|
+
* @param {*} branch_name - branch name
|
|
127
|
+
* @param {*} file - path of file
|
|
128
|
+
* @returns {Promise<Array<string>>} - array of command line report as a string
|
|
129
|
+
*/
|
|
130
|
+
async function calculateGitDiffForFile(branch_name, file) {
|
|
131
|
+
let gitDiffCommand = `git diff -U0 ${branch_name.trim()} ${file}`;
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
exec(gitDiffCommand, (error, stderr) => {
|
|
134
|
+
if (stderr) {
|
|
135
|
+
resolve(stderr.trim().split('\n'));
|
|
136
|
+
} else if (error) {
|
|
137
|
+
reject(error);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* @function {areAllPluginsInstalled} - method whether plugin is installed or not
|
|
144
|
+
* @returns {Boolean} - return boolean based on plugin installed or not
|
|
145
|
+
*/
|
|
267
146
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
// if(changesEndLine === undefined){
|
|
273
|
-
// changedLinesEndArray.push(changesStartLine)
|
|
274
|
-
// }
|
|
275
|
-
// else{
|
|
276
|
-
// changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1)
|
|
277
|
-
// }
|
|
278
|
-
// }
|
|
279
|
-
// for (let error=1; error < errorsInFile.length - 2; error++){
|
|
280
|
-
// //errorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
|
|
281
|
-
// //errorsInFile[error].trim().split(' ')[0] => 69:26
|
|
282
|
-
// //errorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
|
|
147
|
+
function areAllPluginsInstalled() {
|
|
148
|
+
let unInstalledPlugins = checkIfPluginsAreInstalled().uninstalledPlugins;
|
|
149
|
+
return unInstalledPlugins.length === 0 ? true : false;
|
|
150
|
+
}
|
|
283
151
|
|
|
284
|
-
|
|
152
|
+
/**
|
|
153
|
+
* @function {isOnlyWarningsPresentInFile} - method that checks if only eslint warnings are present in a file
|
|
154
|
+
* @returns {Boolean} - returns boolean based on only warnings present or not in file
|
|
155
|
+
*/
|
|
156
|
+
function isOnlyWarningsPresentInFile(eslintErrorsPresent) {
|
|
157
|
+
let severityOfEachErrorInFile = [];
|
|
158
|
+
eslintErrorsPresent.map(error => {
|
|
159
|
+
let partsInString = error.split(" ");
|
|
160
|
+
let severityOfError = partsInString.find(word => word === 'error' || word === 'warning');
|
|
161
|
+
severityOfEachErrorInFile.push(severityOfError);
|
|
162
|
+
});
|
|
163
|
+
return !severityOfEachErrorInFile.includes('error');
|
|
164
|
+
}
|
|
285
165
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
// }
|
|
291
|
-
// }
|
|
292
|
-
// }
|
|
293
|
-
// if(eslintErrorsInChangedLines.length > 0){
|
|
294
|
-
// isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(eslintErrorsInChangedLines)
|
|
295
|
-
// Logger.log(Logger.FAILURE_TYPE,`\x1b[1m${currentFileName}\x1b[0m`)
|
|
296
|
-
// for(let eslintError of eslintErrorsInChangedLines){
|
|
297
|
-
// Logger.log(Logger.FAILURE_TYPE,`\x1b[37m${eslintError.trimEnd()}\x1b[0m`)
|
|
298
|
-
// }
|
|
299
|
-
// if(shouldWarningsAbortCommit){
|
|
300
|
-
// hasEslintErrorsInChangedLines = true
|
|
301
|
-
// shouldAbortCommit = true
|
|
302
|
-
// }
|
|
303
|
-
// else if(!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile){
|
|
304
|
-
// hasEslintErrorsInChangedLines = false
|
|
305
|
-
// }
|
|
306
|
-
// else if(!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile){
|
|
307
|
-
// hasEslintErrorsInChangedLines = true
|
|
308
|
-
// shouldAbortCommit = true
|
|
309
|
-
// }
|
|
310
|
-
// }
|
|
311
|
-
// }else{
|
|
312
|
-
// if(errorsInFile.length > 0 ){
|
|
313
|
-
// let startIndex = 1
|
|
314
|
-
// let endIndex = errorsInFile.length - 2
|
|
315
|
-
// let listOsEslintErrors = errorsInFile.slice(startIndex,endIndex)
|
|
316
|
-
// isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(listOsEslintErrors)
|
|
317
|
-
// Logger.log(Logger.FAILURE_TYPE,`\x1b[1m${currentFileName}\x1b[0m`)
|
|
318
|
-
// for(let eslintError of listOsEslintErrors){
|
|
319
|
-
// Logger.log(Logger.FAILURE_TYPE,`\x1b[37m${eslintError.trimEnd()}\x1b[0m`)
|
|
320
|
-
// }
|
|
321
|
-
// if(shouldWarningsAbortCommit){
|
|
322
|
-
// hasEslintErrorsInFiles = true
|
|
323
|
-
// shouldAbortCommit = true
|
|
324
|
-
// }
|
|
325
|
-
// else if(!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile){
|
|
326
|
-
// hasEslintErrorsInFiles = false
|
|
327
|
-
// }
|
|
328
|
-
// else if(!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile){
|
|
329
|
-
// hasEslintErrorsInFiles = true
|
|
330
|
-
// shouldAbortCommit = true
|
|
331
|
-
// }
|
|
332
|
-
// }
|
|
333
|
-
// }
|
|
334
|
-
// }
|
|
335
|
-
// }
|
|
336
|
-
// }
|
|
337
|
-
// catch(err){
|
|
338
|
-
// Logger.log(Logger.FAILURE_TYPE,err)
|
|
339
|
-
// Logger.log(Logger.FAILURE_TYPE,"Error in executing eslint command")
|
|
340
|
-
// }
|
|
166
|
+
/**
|
|
167
|
+
* @function {preCommitHook} - method execute pre commit hook
|
|
168
|
+
* @returns {void}
|
|
169
|
+
*/
|
|
341
170
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
// }
|
|
171
|
+
async function preCommitHook() {
|
|
172
|
+
Logger.log(Logger.INFO_TYPE, 'Executing pre commit hook...');
|
|
173
|
+
Logger.log(Logger.INFO_TYPE, `working dir : ${process.cwd()}`);
|
|
174
|
+
try {
|
|
175
|
+
let isMerge = await isMergeCommit();
|
|
176
|
+
Logger.log(Logger.INFO_TYPE, 'Looks like you have merged. So skipping pre commit check');
|
|
177
|
+
process.exit(0);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (areAllPluginsInstalled()) {
|
|
180
|
+
let staged_files = [];
|
|
181
|
+
let eslintConfigFiles = ['.eslintrc.js'];
|
|
182
|
+
let exemptionFiles = [];
|
|
183
|
+
let current_branch = '';
|
|
184
|
+
let hasEslintErrorsInChangedLines = false;
|
|
185
|
+
let hasEslintErrorsInFiles = false;
|
|
186
|
+
let areFilesStaged = false;
|
|
187
|
+
let shouldAbortCommit = false;
|
|
188
|
+
try {
|
|
189
|
+
current_branch = await getBranchName();
|
|
190
|
+
} catch {
|
|
191
|
+
Logger.log(Logger.INFO_TYPE, "Error fetching current branch");
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
staged_files = await getStagedFiles();
|
|
195
|
+
if (!staged_files.length == 0) {
|
|
196
|
+
staged_files = filterFiles(staged_files, eslintConfigFiles, true);
|
|
369
197
|
|
|
370
|
-
//
|
|
198
|
+
// staged_files = filterFiles(staged_files,exemptionFiles) //this is the code for giving exemption to a file during pre commit
|
|
199
|
+
// if(staged_files.length === 0){
|
|
200
|
+
// Logger.log(Logger.SUCCESS_TYPE,`Commit Successful`)
|
|
201
|
+
// process.exit(0)
|
|
202
|
+
// }
|
|
203
|
+
areFilesStaged = true;
|
|
204
|
+
for (let file in staged_files) {
|
|
205
|
+
let currentFileName = staged_files[file];
|
|
206
|
+
let changedLinesArray = [];
|
|
207
|
+
let eslintErrorsInChangedLines = [];
|
|
208
|
+
let isOnlyEslintWarningsPresentInFile = false;
|
|
209
|
+
if (getSupportedLanguage().includes(path.extname(staged_files[file]))) {
|
|
210
|
+
try {
|
|
211
|
+
var eslintErrorsInFile = await findEslintErrors(staged_files[file]);
|
|
212
|
+
// eslintErrorsInFile = impactBasedPrecommit == false ? filterWarningInFile(eslintErrorsInFile) : eslintErrorsInFile
|
|
213
|
+
if (staged_files[file] && typeof staged_files[file] == 'string') {
|
|
214
|
+
if (!eslintErrorsInFile.length == 0) {
|
|
215
|
+
//Calculating changed lines in a file and storing them in respective arrays
|
|
216
|
+
if (impactBasedPrecommit) {
|
|
217
|
+
//git diff is computed and stored in an array
|
|
218
|
+
let git_diff = await calculateGitDiffForFile(current_branch, staged_files[file]);
|
|
219
|
+
changedLinesArray = git_diff.filter(line => line.startsWith('@@'));
|
|
220
|
+
let changedLinesStartArray = [];
|
|
221
|
+
let changedLinesEndArray = [];
|
|
222
|
+
for (let number of changedLinesArray) {
|
|
223
|
+
let changesStartLine = parseInt(number.split(' ')[2].split(',')[0]);
|
|
224
|
+
changedLinesStartArray.push(changesStartLine);
|
|
225
|
+
let changesEndLine = number.split(' ')[2].split(',')[1];
|
|
226
|
+
if (changesEndLine === undefined) {
|
|
227
|
+
changedLinesEndArray.push(changesStartLine);
|
|
228
|
+
} else {
|
|
229
|
+
changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (let error = 1; error < eslintErrorsInFile.length - 2; error++) {
|
|
233
|
+
//eslintErrorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
|
|
234
|
+
//eslintErrorsInFile[error].trim().split(' ')[0] => 69:26
|
|
235
|
+
//eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
|
|
371
236
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
237
|
+
let eslintErrorLineNumber = eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0];
|
|
238
|
+
for (let lineNumber in changedLinesStartArray) {
|
|
239
|
+
if (eslintErrorLineNumber >= changedLinesStartArray[lineNumber] && eslintErrorLineNumber <= changedLinesEndArray[lineNumber]) {
|
|
240
|
+
eslintErrorsInChangedLines.push(eslintErrorsInFile[error]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (eslintErrorsInChangedLines.length > 0) {
|
|
245
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(eslintErrorsInChangedLines);
|
|
246
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
247
|
+
for (let eslintError of eslintErrorsInChangedLines) {
|
|
248
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
249
|
+
}
|
|
250
|
+
if (shouldWarningsAbortCommit) {
|
|
251
|
+
hasEslintErrorsInChangedLines = true;
|
|
252
|
+
shouldAbortCommit = true;
|
|
253
|
+
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
254
|
+
hasEslintErrorsInChangedLines = false;
|
|
255
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
256
|
+
hasEslintErrorsInChangedLines = true;
|
|
257
|
+
shouldAbortCommit = true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
} else {
|
|
261
|
+
if (eslintErrorsInFile.length > 0) {
|
|
262
|
+
let startIndex = 1;
|
|
263
|
+
let endIndex = eslintErrorsInFile.length - 2;
|
|
264
|
+
let listOsEslintErrors = eslintErrorsInFile.slice(startIndex, endIndex);
|
|
265
|
+
isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(listOsEslintErrors);
|
|
266
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
|
|
267
|
+
for (let eslintError of listOsEslintErrors) {
|
|
268
|
+
Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
|
|
269
|
+
}
|
|
270
|
+
if (shouldWarningsAbortCommit) {
|
|
271
|
+
hasEslintErrorsInFiles = true;
|
|
272
|
+
shouldAbortCommit = true;
|
|
273
|
+
} else if (!shouldWarningsAbortCommit && isOnlyEslintWarningsPresentInFile) {
|
|
274
|
+
hasEslintErrorsInFiles = false;
|
|
275
|
+
} else if (!shouldWarningsAbortCommit && !isOnlyEslintWarningsPresentInFile) {
|
|
276
|
+
hasEslintErrorsInFiles = true;
|
|
277
|
+
shouldAbortCommit = true;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
} catch (err) {
|
|
284
|
+
Logger.log(Logger.FAILURE_TYPE, err);
|
|
285
|
+
Logger.log(Logger.FAILURE_TYPE, "Error in executing eslint command");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} else if (staged_files.length === 0) {
|
|
290
|
+
Logger.log(Logger.INFO_TYPE, 'No files have been staged. Stage your files before committing');
|
|
291
|
+
}
|
|
292
|
+
} catch {
|
|
293
|
+
Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
|
|
294
|
+
}
|
|
295
|
+
if (shouldAbortCommit) {
|
|
296
|
+
Logger.log(Logger.FAILURE_TYPE, `There are eslint errors/warnings present. So commit is aborted.`);
|
|
297
|
+
process.exit(1);
|
|
298
|
+
} else if (shouldAbortCommit === false && staged_files.length !== 0) {
|
|
299
|
+
Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
|
|
300
|
+
process.exit(0);
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
Logger.log(Logger.FAILURE_TYPE, 'Commit failed since some eslint plugins are not installed');
|
|
304
|
+
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`);
|
|
305
|
+
Logger.log(Logger.INFO_TYPE, 'Execute the command and kindly try committing again.');
|
|
306
|
+
process.exit(1);
|
|
397
307
|
}
|
|
398
|
-
execSync("npx ZDPrecommit setupPlugins", {
|
|
399
|
-
cwd: getNodeModulesPath()
|
|
400
|
-
});
|
|
401
|
-
} catch (error) {
|
|
402
|
-
Logger.log(Logger.FAILURE_TYPE, `Kindly retry npm install. & ${error.message}`);
|
|
403
308
|
}
|
|
404
309
|
}
|
|
405
|
-
|
|
406
|
-
await validateRemotePackages();
|
|
407
|
-
require("./lint");
|
|
408
|
-
}
|
|
409
|
-
lint();
|
|
310
|
+
preCommitHook();
|
package/build/lib/cli.js
CHANGED
|
@@ -13,11 +13,11 @@ const {
|
|
|
13
13
|
installEslintExtension
|
|
14
14
|
} = require("../utils/ExtensionInstallation/installEslintExtension");
|
|
15
15
|
const {
|
|
16
|
-
|
|
17
|
-
} = require("../utils/
|
|
16
|
+
installPlugins
|
|
17
|
+
} = require("../utils/PluginsInstallation/installPlugins");
|
|
18
18
|
const {
|
|
19
|
-
|
|
20
|
-
} = require("../utils/PluginsInstallation/
|
|
19
|
+
checkIfPluginsAreInstalled
|
|
20
|
+
} = require("../utils/PluginsInstallation/checkIfPluginsAreInstalled");
|
|
21
21
|
const [,, action, ...options] = process.argv;
|
|
22
22
|
async function run() {
|
|
23
23
|
switch (action) {
|
|
@@ -33,7 +33,10 @@ async function run() {
|
|
|
33
33
|
}
|
|
34
34
|
case "setupPlugins":
|
|
35
35
|
{
|
|
36
|
-
|
|
36
|
+
const {
|
|
37
|
+
uninstalledPlugins
|
|
38
|
+
} = checkIfPluginsAreInstalled();
|
|
39
|
+
await executeMethodsThatReturnBooleanValue("Some issue occurred in installing plugins", installPlugins, uninstalledPlugins);
|
|
37
40
|
break;
|
|
38
41
|
}
|
|
39
42
|
case "setupExtension":
|
|
@@ -43,7 +46,6 @@ async function run() {
|
|
|
43
46
|
}
|
|
44
47
|
default:
|
|
45
48
|
{
|
|
46
|
-
Logger.log(Logger.FAILURE_TYPE, `oops! command not supported :( ...`);
|
|
47
49
|
break;
|
|
48
50
|
}
|
|
49
51
|
}
|
package/build/lib/postinstall.js
CHANGED
|
@@ -7,8 +7,8 @@ const {
|
|
|
7
7
|
setupHusky
|
|
8
8
|
} = require("../utils/HuskySetup/setupHusky");
|
|
9
9
|
const {
|
|
10
|
-
|
|
11
|
-
} = require("../utils/
|
|
10
|
+
createEslintConfigFile
|
|
11
|
+
} = require("../utils/EslintConfigFileUtils/createEslintConfigFile");
|
|
12
12
|
const {
|
|
13
13
|
Logger
|
|
14
14
|
} = require("../utils/Logger/Logger");
|
|
@@ -34,9 +34,9 @@ async function postInstall() {
|
|
|
34
34
|
await executeMethodsThatReturnBooleanValue("Some issue in writing node_modules path to json", writeFsPaths, null);
|
|
35
35
|
isGitInitialized() ? await executeMethodsThatReturnBooleanValue("Some issue occurred in setting up husky.", setupHusky, null) : null;
|
|
36
36
|
await executeMethodsThatReturnBooleanValue("Make sure zgit.csez.zohocorpin.com is accessible", cloneViaCdt, null);
|
|
37
|
-
await executeMethodsThatReturnBooleanValue("Some issue occurred in creating eslint config file.",
|
|
37
|
+
await executeMethodsThatReturnBooleanValue("Some issue occurred in creating eslint config file.", createEslintConfigFile, null);
|
|
38
38
|
Logger.log(Logger.SUCCESS_TYPE, "Pre commit setup successfull");
|
|
39
|
-
|
|
39
|
+
arePluginsInstalled();
|
|
40
40
|
} catch (error) {
|
|
41
41
|
Logger.log(Logger.FAILURE_TYPE, `Kindly retry npm install. & ${error.message}`);
|
|
42
42
|
}
|
|
@@ -51,7 +51,7 @@ function cloneViaCdt() {
|
|
|
51
51
|
Logger.log(Logger.INFO_TYPE, `Running in ${runningEnv}`);
|
|
52
52
|
absoluteEndPoint = `https://${userName}:${decrypt(token, process.env.DECRYPT_TOKEN)}@${endPoint}`;
|
|
53
53
|
}
|
|
54
|
-
var commandToCloneCommonConfigRepo = `npx cdt clone --clone:type=${type} --clone:url=${absoluteEndPoint} --clone:branch=${branch} --clone:cacheDir=${cacheDirectory} --clone:proj:name=${commonLinterRepoName}`;
|
|
54
|
+
var commandToCloneCommonConfigRepo = `npx cdt clone --clone:type=${type} --clone:url=${absoluteEndPoint} --clone:branch=${process.env.CONFIGURATION_BRANCH || branch} --clone:cacheDir=${cacheDirectory} --clone:proj:name=${commonLinterRepoName}`;
|
|
55
55
|
let isCommonConfigurationClonedSuccessfully = executeSynchronizedCommands(execSync, [commandToCloneCommonConfigRepo, {
|
|
56
56
|
cwd: getClonedDirPath()
|
|
57
57
|
}], `Lint Configuration Cloned Successfully - ${getRepoName() || 'common'}`, 'Could not clone the linters common repo', false, true);
|
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
const {
|
|
4
4
|
Logger
|
|
5
5
|
} = require('../Logger/Logger');
|
|
6
|
-
|
|
7
|
-
existsSync
|
|
8
|
-
} = require("fs");
|
|
6
|
+
|
|
9
7
|
/**
|
|
10
8
|
* @function filterFiles - removes certain files from set of source files
|
|
11
9
|
* @param {Array} arrayOfFilesToBeFiltered - array of files which must be filtered
|
|
@@ -14,29 +12,6 @@ const {
|
|
|
14
12
|
* @returns {Array} - containing the resultant set of files after filtering
|
|
15
13
|
*/
|
|
16
14
|
function filterFiles(arrayOfFilesToBeFiltered, filesToBeRemoved, isConfigFileNeedToBeRemoved = false) {
|
|
17
|
-
/**
|
|
18
|
-
* @function filterFilesByExtension - filter javascript files. omit feature files
|
|
19
|
-
* @param {Array<String>} lintFiles - linter files as Array
|
|
20
|
-
* @returns {Array<String>}
|
|
21
|
-
* */
|
|
22
|
-
function filterFilesByExtension(lintFiles) {
|
|
23
|
-
return lintFiles.reduce((files, currentFile) => {
|
|
24
|
-
if (currentFile.includes('.feature') && existsSync(currentFile)) {
|
|
25
|
-
files.featureFiles.push(currentFile);
|
|
26
|
-
}
|
|
27
|
-
if (currentFile.includes('.js') || currentFile.includes('.ts') || currentFile.includes('.tsx') || currentFile.includes('.jsx') && existsSync(currentFile)) {
|
|
28
|
-
files.JsFiles.push(currentFile);
|
|
29
|
-
}
|
|
30
|
-
if (currentFile.includes('.css') || currentFile.includes('.scss') && existsSync(currentFile)) {
|
|
31
|
-
files.CssFiles.push(currentFile);
|
|
32
|
-
}
|
|
33
|
-
return files;
|
|
34
|
-
}, {
|
|
35
|
-
featureFiles: [],
|
|
36
|
-
JsFiles: [],
|
|
37
|
-
CssFiles: []
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
15
|
if (filesToBeRemoved.length !== 0) {
|
|
41
16
|
if (isConfigFileNeedToBeRemoved) {
|
|
42
17
|
arrayOfFilesToBeFiltered.filter(file => {
|
|
@@ -56,7 +31,7 @@ function filterFiles(arrayOfFilesToBeFiltered, filesToBeRemoved, isConfigFileNee
|
|
|
56
31
|
return false;
|
|
57
32
|
}
|
|
58
33
|
});
|
|
59
|
-
return
|
|
34
|
+
return filteredArrayofFilesWithoutConfigFile;
|
|
60
35
|
} else if (filesToBeRemoved.length === 0) {
|
|
61
36
|
return arrayOfFilesToBeFiltered;
|
|
62
37
|
}
|
|
@@ -16,13 +16,14 @@ const {
|
|
|
16
16
|
function createVersionControlFile(uninstalledPlugins) {
|
|
17
17
|
Logger.log(Logger.INFO_TYPE, `Rule Plugin Versions are Noted in pluginVersionControl.js`);
|
|
18
18
|
const versionfilePath = path.join(getNodeModulesPath(), 'node_modules', '@zohodesk', 'codestandard-validator', 'pluginVersionControl.js');
|
|
19
|
-
existsSync(versionfilePath)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
if (existsSync(versionfilePath)) {
|
|
20
|
+
writeFileSync(versionfilePath, `module.exports.plugins = [${uninstalledPlugins.map(plugin => {
|
|
21
|
+
return JSON.stringify({
|
|
22
|
+
plugin: `${plugin}`,
|
|
23
|
+
time: `${new Date()}`
|
|
24
|
+
});
|
|
25
|
+
}).join(',')}]`);
|
|
26
|
+
}
|
|
26
27
|
}
|
|
27
28
|
module.exports = {
|
|
28
29
|
createVersionControlFile
|
|
@@ -6,19 +6,11 @@ const fs = require("fs");
|
|
|
6
6
|
const {
|
|
7
7
|
execSync
|
|
8
8
|
} = require('child_process');
|
|
9
|
-
const {
|
|
10
|
-
Logger
|
|
11
|
-
} = require('../Logger/Logger');
|
|
12
|
-
const {
|
|
13
|
-
readOnlyToken,
|
|
14
|
-
commitHashEndPoint
|
|
15
|
-
} = require('../../../jsonUtils/commonLinterRepoDetails');
|
|
16
9
|
|
|
17
10
|
/**
|
|
18
11
|
* @function getTimeStampInfo - to fetch various timestamp details
|
|
19
12
|
* @returns {object} - each property of the object denotes various timestamp details
|
|
20
13
|
*/
|
|
21
|
-
|
|
22
14
|
function getTimeStampInfo() {
|
|
23
15
|
const date = new Date();
|
|
24
16
|
return {
|
|
@@ -35,7 +27,6 @@ function getTimeStampInfo() {
|
|
|
35
27
|
* @function getEnv - to fetch the os type
|
|
36
28
|
* @returns {string} - indicates the os
|
|
37
29
|
*/
|
|
38
|
-
|
|
39
30
|
function getEnv() {
|
|
40
31
|
return os.type();
|
|
41
32
|
}
|
|
@@ -44,7 +35,6 @@ function getEnv() {
|
|
|
44
35
|
* @function getConfigPath - get a path of lint configuration path
|
|
45
36
|
* @returns {string}
|
|
46
37
|
*/
|
|
47
|
-
|
|
48
38
|
function getConfigPath() {
|
|
49
39
|
const configPath = path.resolve(process.cwd(), 'lint.config.js');
|
|
50
40
|
const defaultConfigPath = path.resolve(__dirname, '..', '..', 'setup', 'sample.config.js');
|
|
@@ -55,27 +45,19 @@ function getConfigPath() {
|
|
|
55
45
|
* @function getConfiguration - get configuration object of lint configuration
|
|
56
46
|
* @returns
|
|
57
47
|
*/
|
|
58
|
-
|
|
59
48
|
function getConfigurationPrecommit() {
|
|
60
49
|
return require(getConfigPath());
|
|
61
50
|
}
|
|
62
51
|
|
|
63
52
|
/**
|
|
64
|
-
* @function
|
|
65
|
-
* @returns {string}
|
|
53
|
+
* @function getSupportedLanguage - get support language
|
|
54
|
+
* @returns {Array<string>}
|
|
66
55
|
*/
|
|
67
|
-
|
|
68
56
|
function getSupportedLanguage() {
|
|
69
57
|
const {
|
|
70
58
|
supportedExtensions
|
|
71
59
|
} = getConfigurationPrecommit();
|
|
72
60
|
const _language = supportedExtensions;
|
|
73
|
-
_language.push('.js');
|
|
74
|
-
_language.push('.jsx');
|
|
75
|
-
_language.push('.ts');
|
|
76
|
-
_language.push('.tsx');
|
|
77
|
-
_language.push('.css');
|
|
78
|
-
_language.push('.scss');
|
|
79
61
|
return _language;
|
|
80
62
|
}
|
|
81
63
|
function getRunningEnv() {
|
|
@@ -84,51 +66,10 @@ function getRunningEnv() {
|
|
|
84
66
|
shell: true
|
|
85
67
|
}).toString().trim();
|
|
86
68
|
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* @function getLastCommitHash - Fetches the last commit hash from a GitLab project using its API.
|
|
90
|
-
*
|
|
91
|
-
* @note This function assumes access to a specific GitLab instance and a hardcoded token and project ID.
|
|
92
|
-
* @returns {string} The latest commit hash (SHA) from the specified GitLab repository.
|
|
93
|
-
* @throws {Error} Will throw an error if the API request fails or returns unexpected data.
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
function getLastCommitHash() {
|
|
97
|
-
try {
|
|
98
|
-
const cmd = `curl --header "PRIVATE-TOKEN: ${readOnlyToken}" --url ${commitHashEndPoint}`;
|
|
99
|
-
return JSON.parse(execSync(cmd))[0].id;
|
|
100
|
-
} catch (err) {
|
|
101
|
-
Logger.log(Logger.FAILURE_TYPE, err);
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* @function getRequiredInfoPath - Returns the absolute path to the `fsUtils.json` file located in the `jsonUtils` directory at the project root.
|
|
108
|
-
* @returns {string} The full path to the `fsUtils.json` file.
|
|
109
|
-
*/
|
|
110
|
-
|
|
111
|
-
function getRequiredInfoPath() {
|
|
112
|
-
return path.join(process.cwd(), 'jsonUtils', 'fsUtils.json');
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* @function getStoredCommitHash - Loads and returns the stored commit hash from `fsUtils.json`.
|
|
117
|
-
* @returns {string} The commit hash stored in the `fsUtils.json` file.
|
|
118
|
-
* @throws {Error} Will throw if the file does not exist or does not contain a `commitHash` key.
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
function getStoredCommitHash() {
|
|
122
|
-
const commitInfoPath = path.join(__dirname, '..', '..', '..', 'jsonUtils', 'fsUtils.json');
|
|
123
|
-
return fs.existsSync(commitInfoPath) && require(commitInfoPath).commitHash;
|
|
124
|
-
}
|
|
125
69
|
module.exports = {
|
|
126
70
|
getSupportedLanguage,
|
|
127
71
|
getTimeStampInfo,
|
|
128
72
|
getEnv,
|
|
129
73
|
getConfigurationPrecommit,
|
|
130
|
-
getRunningEnv
|
|
131
|
-
getLastCommitHash,
|
|
132
|
-
getRequiredInfoPath,
|
|
133
|
-
getStoredCommitHash
|
|
74
|
+
getRunningEnv
|
|
134
75
|
};
|
|
@@ -7,16 +7,11 @@ const {
|
|
|
7
7
|
const {
|
|
8
8
|
executeSynchronizedCommands
|
|
9
9
|
} = require('./executeSyncCommands');
|
|
10
|
-
const {
|
|
11
|
-
getLastCommitHash,
|
|
12
|
-
getRequiredInfoPath
|
|
13
|
-
} = require('./getGeneralInfo');
|
|
14
10
|
function writeFsPaths() {
|
|
15
11
|
var fileContent = {
|
|
16
|
-
nodeModulesPath: path.resolve(process.cwd(), '..', '..', '..')
|
|
17
|
-
commitHash: getLastCommitHash()
|
|
12
|
+
nodeModulesPath: path.resolve(process.cwd(), '..', '..', '..')
|
|
18
13
|
};
|
|
19
|
-
return executeSynchronizedCommands(writeFileSync, [
|
|
14
|
+
return executeSynchronizedCommands(writeFileSync, [path.join(process.cwd(), 'jsonUtils', 'fsUtils.json'), JSON.stringify(fileContent), 'utf-8'], 'node_modules path updated in json file', 'Unable to write node_modules path to json file', false, true);
|
|
20
15
|
}
|
|
21
16
|
module.exports = {
|
|
22
17
|
writeFsPaths
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
executeMethodsThatReturnBooleanValue
|
|
5
|
-
} = require('../General/wrapperFunctionToExecuteAFunction');
|
|
6
3
|
const {
|
|
7
4
|
checkIfPluginsAreInstalled
|
|
8
5
|
} = require('./checkIfPluginsAreInstalled');
|
|
6
|
+
const {
|
|
7
|
+
printUninstalledPlugins
|
|
8
|
+
} = require('./printUninstalledPlugins');
|
|
9
9
|
const {
|
|
10
10
|
installPlugins
|
|
11
11
|
} = require('./installPlugins');
|
|
12
|
-
|
|
12
|
+
function arePluginsInstalled() {
|
|
13
13
|
let {
|
|
14
14
|
uninstalledPlugins,
|
|
15
15
|
noPluginMessage
|
|
16
16
|
} = checkIfPluginsAreInstalled();
|
|
17
|
-
|
|
17
|
+
let areAllPluginsInstalled = uninstalledPlugins.length === 0 ? true : false;
|
|
18
|
+
if (!areAllPluginsInstalled) {
|
|
19
|
+
installPlugins(uninstalledPlugins);
|
|
20
|
+
}
|
|
21
|
+
// printUninstalledPlugins(uninstalledPlugins,noPluginMessage)
|
|
18
22
|
}
|
|
19
23
|
module.exports = {
|
|
20
24
|
arePluginsInstalled
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
|
+
readdirSync,
|
|
4
5
|
existsSync
|
|
5
6
|
} = require('fs');
|
|
6
7
|
const path = require('path');
|
|
8
|
+
const {
|
|
9
|
+
getNodeModulesPath
|
|
10
|
+
} = require('../General/getNodeModulesPath');
|
|
7
11
|
const {
|
|
8
12
|
Logger
|
|
9
13
|
} = require('../Logger/Logger');
|
|
10
14
|
const {
|
|
11
15
|
getLibraryInstalledLocation
|
|
12
16
|
} = require('../General/getLibraryInstalledLocation');
|
|
17
|
+
const {
|
|
18
|
+
executeSynchronizedCommands
|
|
19
|
+
} = require('../General/executeSyncCommands');
|
|
13
20
|
const {
|
|
14
21
|
endPoint,
|
|
15
22
|
commonLinterRepoName
|
|
@@ -19,7 +26,33 @@ const {
|
|
|
19
26
|
} = require('../General/wrapperFunctionToExecuteAFunction');
|
|
20
27
|
const {
|
|
21
28
|
getServicePathElseCommon
|
|
22
|
-
} = require('../
|
|
29
|
+
} = require('../EslintConfigFileUtils/getLintConfiguration');
|
|
30
|
+
function getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, devDependencies) {
|
|
31
|
+
let pluginsInNodeModules = executeSynchronizedCommands(readdirSync, [path.join(getNodeModulesPath(), 'node_modules')], '', 'Unable to get the plugins inside node_modules', true, false);
|
|
32
|
+
let pluginsToBeInstalled = [];
|
|
33
|
+
pluginsForCurrentRepo.filter(plugin => {
|
|
34
|
+
if (plugin.packageName.startsWith('@')) {
|
|
35
|
+
let scope = plugin.packageName.split('/')[0];
|
|
36
|
+
let pluginName = plugin.packageName.split('/')[1];
|
|
37
|
+
let scopedPlugins = executeSynchronizedCommands(readdirSync, [path.join(getNodeModulesPath(), 'node_modules', scope)], '', 'Unable to get the plugins inside the scope inside node_modules', true, false);
|
|
38
|
+
let isPluginInstalled = scopedPlugins.includes(pluginName) ? true : false;
|
|
39
|
+
if (!isPluginInstalled) {
|
|
40
|
+
pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
let isPluginInstalled = pluginsInNodeModules.includes(plugin.packageName) ? true : false;
|
|
44
|
+
if (!isPluginInstalled) {
|
|
45
|
+
pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (pluginNamesOfDevDependencyPlugins.includes(plugin.packageName)) {
|
|
49
|
+
if (plugin.version !== devDependencies[plugin.packageName]) {
|
|
50
|
+
pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return pluginsToBeInstalled;
|
|
55
|
+
}
|
|
23
56
|
function getAllPlugins() {
|
|
24
57
|
let serviceSpecificPlugins = [];
|
|
25
58
|
const pathToCommonPluginsFile = path.join(getLibraryInstalledLocation(), commonLinterRepoName, 'common', 'pluginVersion.js');
|
|
@@ -46,6 +79,29 @@ function checkIfPluginsAreInstalled() {
|
|
|
46
79
|
noPluginMessage: '',
|
|
47
80
|
uninstalledPlugins: []
|
|
48
81
|
};
|
|
82
|
+
// let pluginsIndevDependencies = {}
|
|
83
|
+
// let pluginNamesOfDevDependencyPlugins = []
|
|
84
|
+
// let arePluginsPresentIndevDependencies = false
|
|
85
|
+
// var arePluginsPresentForCurrentRepo = false
|
|
86
|
+
|
|
87
|
+
// if(existsSync(path.join(getNodeModulesPath(),'package.json'))){
|
|
88
|
+
// let packageJsonContent = require(path.join(getNodeModulesPath(),'package.json'))
|
|
89
|
+
// pluginsIndevDependencies = packageJsonContent.devDependencies
|
|
90
|
+
|
|
91
|
+
// if(pluginsIndevDependencies !== undefined){
|
|
92
|
+
// arePluginsPresentIndevDependencies = true
|
|
93
|
+
// pluginNamesOfDevDependencyPlugins = Object.keys(pluginsIndevDependencies)
|
|
94
|
+
// }
|
|
95
|
+
// else if(pluginsIndevDependencies === undefined){
|
|
96
|
+
// Logger.log(Logger.INFO_TYPE,'No devDependencies present!')
|
|
97
|
+
// }
|
|
98
|
+
// }
|
|
99
|
+
// else{
|
|
100
|
+
// arePluginsPresentIndevDependencies = false
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
// arePluginsPresentForCurrentRepo = pluginsForCurrentRepo.length !== 0 ? true : false
|
|
104
|
+
|
|
49
105
|
uninstalledPlugins = pluginsForCurrentRepo.map(plugin => {
|
|
50
106
|
const {
|
|
51
107
|
packageName,
|
|
@@ -58,12 +114,10 @@ function checkIfPluginsAreInstalled() {
|
|
|
58
114
|
if (isPluginPresent && checkPluginHasProperVersion(packageName, pluginPath, version)) {
|
|
59
115
|
return false;
|
|
60
116
|
}
|
|
61
|
-
return {
|
|
62
|
-
packageName,
|
|
63
|
-
version
|
|
64
|
-
};
|
|
117
|
+
return `${packageName}@${version}`;
|
|
65
118
|
}).filter(Boolean);
|
|
66
119
|
if (uninstalledPlugins.length > 0) {
|
|
120
|
+
// uninstalledPlugins = getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, pluginsIndevDependencies);
|
|
67
121
|
plugin.noPluginMessage = '';
|
|
68
122
|
plugin.uninstalledPlugins = uninstalledPlugins;
|
|
69
123
|
return plugin;
|
|
@@ -6,6 +6,9 @@ const {
|
|
|
6
6
|
const {
|
|
7
7
|
writeFileSync
|
|
8
8
|
} = require('fs');
|
|
9
|
+
const {
|
|
10
|
+
checkIfPluginsAreInstalled
|
|
11
|
+
} = require('./checkIfPluginsAreInstalled');
|
|
9
12
|
const {
|
|
10
13
|
executeSynchronizedCommands
|
|
11
14
|
} = require('../General/executeSyncCommands');
|
|
@@ -23,32 +26,35 @@ const {
|
|
|
23
26
|
* @function installPlugins - installs uninstalled plugins for the project
|
|
24
27
|
* @returns {boolean} - indicating if plugins to be installed are installed successfully or not
|
|
25
28
|
*/
|
|
26
|
-
|
|
27
29
|
function installPlugins(pluginsToBeInstalled) {
|
|
28
|
-
|
|
30
|
+
// let {uninstalledPlugins : pluginsToBeInstalled} = checkIfPluginsAreInstalled()
|
|
31
|
+
if (pluginsToBeInstalled.length > 0) {
|
|
32
|
+
let packageJsonBeforePluginsInstallation = getPackageJsonContentBeforeInstallingPlugins();
|
|
33
|
+
let installCommand = `npm install --save-dev ${pluginsToBeInstalled.join(' ')}`;
|
|
34
|
+
Logger.log(Logger.INFO_TYPE, 'Installing plugins ....');
|
|
35
|
+
Logger.log(Logger.INFO_TYPE, `Install command being executed: ${installCommand}`);
|
|
36
|
+
let arePluginsInstalledSuccessfully = executeSynchronizedCommands(execSync, [installCommand, {
|
|
37
|
+
stdio: 'inherit',
|
|
38
|
+
cwd: getNodeModulesPath()
|
|
39
|
+
}], '', 'Some issue occured while installing plugins', false, true);
|
|
40
|
+
if (arePluginsInstalledSuccessfully) {
|
|
41
|
+
Logger.log(Logger.SUCCESS_TYPE, 'Plugins installation successful');
|
|
42
|
+
let isPackageJsonRestored = restorePackageJsonContent(packageJsonBeforePluginsInstallation);
|
|
43
|
+
if (isPackageJsonRestored) {
|
|
44
|
+
createVersionControlFile(pluginsToBeInstalled);
|
|
45
|
+
// return true
|
|
46
|
+
} else {
|
|
47
|
+
Logger.log(Logger.FAILURE_TYPE, 'Unable to restore package.json content');
|
|
48
|
+
// return false
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
Logger.log(Logger.FAILURE_TYPE, "Unable to install plugins.Kindly retry the command");
|
|
52
|
+
// return false
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
29
55
|
Logger.log(Logger.INFO_TYPE, 'Plugins are already installed');
|
|
30
|
-
return true
|
|
31
|
-
}
|
|
32
|
-
const packageJsonBackup = getPackageJsonContentBeforeInstallingPlugins();
|
|
33
|
-
const installCommand = `npm install --save-dev ${pluginsToBeInstalled.join(' ')}`;
|
|
34
|
-
Logger.log(Logger.INFO_TYPE, 'Installing plugins...');
|
|
35
|
-
Logger.log(Logger.INFO_TYPE, `Install command being executed: ${installCommand}`);
|
|
36
|
-
const installed = executeSynchronizedCommands(execSync, [installCommand, {
|
|
37
|
-
stdio: 'inherit',
|
|
38
|
-
cwd: getNodeModulesPath()
|
|
39
|
-
}], '', 'Some issue occurred while installing plugins', false, true);
|
|
40
|
-
if (!installed) {
|
|
41
|
-
Logger.log(Logger.FAILURE_TYPE, 'Unable to install plugins. Kindly retry the command');
|
|
42
|
-
return false;
|
|
56
|
+
// return true
|
|
43
57
|
}
|
|
44
|
-
Logger.log(Logger.SUCCESS_TYPE, 'Plugins installation successful');
|
|
45
|
-
const restored = restorePackageJsonContent(packageJsonBackup);
|
|
46
|
-
if (!restored) {
|
|
47
|
-
Logger.log(Logger.FAILURE_TYPE, 'Unable to restore package.json content');
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
createVersionControlFile(pluginsToBeInstalled);
|
|
51
|
-
return true;
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
/**
|
|
@@ -70,6 +76,53 @@ function restorePackageJsonContent(packageJsonContentBeforePluginsInstallation)
|
|
|
70
76
|
let isPackageJsonRestored = executeSynchronizedCommands(writeFileSync, [packageJsonFilePath, JSON.stringify(packageJsonContentBeforePluginsInstallation, null, 2)], 'Package.json content restored successfully', 'Unable to restore package.json content', false, true);
|
|
71
77
|
return isPackageJsonRestored;
|
|
72
78
|
}
|
|
79
|
+
|
|
80
|
+
// function appendInstalledPluginsToPackageJson(pluginsToBeAppendedInPackageJson){
|
|
81
|
+
// let packageJsonFilePath = `${nodeModulesPathOfProject}/package.json`
|
|
82
|
+
// let packageJsonContent = require(packageJsonFilePath)
|
|
83
|
+
// let devDependencies = packageJsonContent.devDependencies
|
|
84
|
+
// let pluginsToBeAddedInDevDependencies = {}
|
|
85
|
+
|
|
86
|
+
// executeSynchronizedCommands(process.chdir,[nodeModulesPathOfProject],'','Unable to navigate to node_modules path when trying to revert changes in package.json',false,false)
|
|
87
|
+
|
|
88
|
+
// let gitDiffOutput = executeSynchronizedCommands(execSync,['git diff --name-only'],'','Unable to execute git diff command while checking if package.json is changed',true,false).toString().trim()
|
|
89
|
+
// let isPackageJsonChanged = gitDiffOutput.includes('package.json') ? true : false
|
|
90
|
+
|
|
91
|
+
// if(isPackageJsonChanged){
|
|
92
|
+
// let packageJsonChangesRevertCommand = `git checkout "${packageJsonFilePath}"`
|
|
93
|
+
// isPackageJsonChangesRevertedSuccessfully = executeSynchronizedCommands(execSync,[packageJsonChangesRevertCommand,{stdio:'inherit'}],'Changes in package.json reverted successfully','Unable to revert the changes in package.json file',false,true)
|
|
94
|
+
|
|
95
|
+
// if(isPackageJsonChangesRevertedSuccessfully){
|
|
96
|
+
// pluginsToBeAppendedInPackageJson.map(uninstalledPlugin => {
|
|
97
|
+
// if(uninstalledPlugin.startsWith('@')){
|
|
98
|
+
// let indexOfAtCharacter = uninstalledPlugin.indexOf('@')
|
|
99
|
+
// let indexOfSecondOccurenceOfAtCharacter = uninstalledPlugin.indexOf('@',indexOfAtCharacter + 1)
|
|
100
|
+
// let unInstalledPluginName = uninstalledPlugin.slice(0,indexOfSecondOccurenceOfAtCharacter)
|
|
101
|
+
// let unInstalledPluginVersion = uninstalledPlugin.slice(indexOfSecondOccurenceOfAtCharacter + 1)
|
|
102
|
+
// pluginsToBeAddedInDevDependencies[unInstalledPluginName] = unInstalledPluginVersion
|
|
103
|
+
// }
|
|
104
|
+
// else{
|
|
105
|
+
// pluginsToBeAddedInDevDependencies[uninstalledPlugin.split('@')[0]] = uninstalledPlugin.split('@')[1]
|
|
106
|
+
// }
|
|
107
|
+
// })
|
|
108
|
+
// let updatedPluginsIndevDependencies = {
|
|
109
|
+
// ...devDependencies,
|
|
110
|
+
// ...pluginsToBeAddedInDevDependencies
|
|
111
|
+
// }
|
|
112
|
+
// packageJsonContent.devDependencies = updatedPluginsIndevDependencies
|
|
113
|
+
|
|
114
|
+
// let modifiedPackageJson = {
|
|
115
|
+
// ...packageJsonContent,
|
|
116
|
+
// }
|
|
117
|
+
// let isPluginsAddedInPackageJson = executeSynchronizedCommands(writeFileSync,[packageJsonFilePath,JSON.stringify(modifiedPackageJson,null,2)],'Newly installed plugins successfully added to package.json','Unable to append installed plugins to package.json',false,true)
|
|
118
|
+
// return isPluginsAddedInPackageJson
|
|
119
|
+
// }
|
|
120
|
+
// else{
|
|
121
|
+
// return true
|
|
122
|
+
// }
|
|
123
|
+
// }
|
|
124
|
+
// }
|
|
125
|
+
|
|
73
126
|
module.exports = {
|
|
74
127
|
installPlugins
|
|
75
128
|
};
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { getLintConfigurationUtil } = require("./build/utils/
|
|
1
|
+
const { getLintConfigurationUtil } = require("./build/utils/EslintConfigFileUtils/getLintConfiguration");
|
|
2
2
|
const { rulesConfig, plugins, extendPlugins } = getLintConfigurationUtil();
|
|
3
3
|
|
|
4
4
|
module.exports = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zohodesk/codestandard-validator",
|
|
3
|
-
"version": "0.0.6-exp-
|
|
3
|
+
"version": "0.0.6-exp-21",
|
|
4
4
|
"description": "library to enforce code standard using eslint",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"@babel/core": "7.24.7",
|
|
23
23
|
"@babel/plugin-transform-runtime": "7.24.7",
|
|
24
24
|
"@babel/preset-env": "7.24.7",
|
|
25
|
-
"husky": "7.0.4"
|
|
26
|
-
"stylelint": "^16.0.0"
|
|
25
|
+
"husky": "7.0.4"
|
|
27
26
|
}
|
|
28
27
|
}
|