@zohodesk/codestandard-validator 0.0.6-exp-18 → 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/lint.js +138 -237
- package/build/hooks/Precommit/pre-commit.js +297 -29
- package/build/lib/cli.js +8 -6
- package/build/lib/postinstall.js +1 -1
- package/build/utils/CloneCommonLinterRepo/cloneViaCdt.js +2 -2
- package/build/utils/ConfigFileUtils/createConfigFiles.js +68 -0
- package/build/utils/ConfigFileUtils/getEslintExecutablePath.js +24 -0
- package/build/utils/ConfigFileUtils/getLintConfiguration.js +54 -0
- package/build/utils/FileAndFolderOperations/versionControl.js +8 -7
- package/build/utils/General/getGeneralInfo.js +1 -55
- package/build/utils/General/writeProjectDetailsToJson.js +2 -7
- package/build/utils/PluginsInstallation/Worker/installPluginsByWoker.js +5 -2
- package/build/utils/PluginsInstallation/arePluginsInstalled.js +9 -5
- package/build/utils/PluginsInstallation/checkIfPluginsAreInstalled.js +57 -0
- package/build/utils/PluginsInstallation/installPlugins.js +76 -22
- package/package.json +1 -1
|
@@ -1,42 +1,310 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = validateRemotePackages;
|
|
7
|
-
var _executeSyncCommands = require("../../utils/General/executeSyncCommands");
|
|
8
|
-
var _getNodeModulesPath = require("../../utils/General/getNodeModulesPath");
|
|
9
3
|
const {
|
|
10
|
-
|
|
11
|
-
} = require(
|
|
4
|
+
exec
|
|
5
|
+
} = require('child_process');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
12
8
|
const {
|
|
13
|
-
|
|
14
|
-
} = require(
|
|
9
|
+
getEslintExecutablePath
|
|
10
|
+
} = require('../../utils/EslintConfigFileUtils/getEslintExecutablePath');
|
|
15
11
|
const {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} = require("../../utils/General/getGeneralInfo");
|
|
12
|
+
getNodeModulesPath
|
|
13
|
+
} = require('../../utils/General/getNodeModulesPath');
|
|
19
14
|
const {
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
filterFiles,
|
|
16
|
+
filterWarningInFile
|
|
17
|
+
} = require('../../utils/FileAndFolderOperations/filterFiles');
|
|
22
18
|
const {
|
|
23
19
|
Logger
|
|
24
|
-
} = require(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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();
|
|
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;
|
|
31
86
|
}
|
|
32
|
-
|
|
33
|
-
|
|
87
|
+
return false;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
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
|
+
*/
|
|
96
|
+
|
|
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
|
+
}
|
|
34
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
|
+
*/
|
|
146
|
+
|
|
147
|
+
function areAllPluginsInstalled() {
|
|
148
|
+
let unInstalledPlugins = checkIfPluginsAreInstalled().uninstalledPlugins;
|
|
149
|
+
return unInstalledPlugins.length === 0 ? true : false;
|
|
150
|
+
}
|
|
151
|
+
|
|
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
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @function {preCommitHook} - method execute pre commit hook
|
|
168
|
+
* @returns {void}
|
|
169
|
+
*/
|
|
170
|
+
|
|
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);
|
|
35
178
|
} catch (error) {
|
|
36
|
-
|
|
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);
|
|
197
|
+
|
|
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
|
|
236
|
+
|
|
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);
|
|
307
|
+
}
|
|
37
308
|
}
|
|
38
309
|
}
|
|
39
|
-
(
|
|
40
|
-
await validateRemotePackages();
|
|
41
|
-
require('./lint');
|
|
42
|
-
})();
|
|
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
|
@@ -36,7 +36,7 @@ async function postInstall() {
|
|
|
36
36
|
await executeMethodsThatReturnBooleanValue("Make sure zgit.csez.zohocorpin.com is accessible", cloneViaCdt, null);
|
|
37
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
|
}
|
|
@@ -49,9 +49,9 @@ function cloneViaCdt() {
|
|
|
49
49
|
const runningEnv = getRunningEnv();
|
|
50
50
|
if (runningEnv === "CI" || runningEnv === "DEVAUTOMATION") {
|
|
51
51
|
Logger.log(Logger.INFO_TYPE, `Running in ${runningEnv}`);
|
|
52
|
-
absoluteEndPoint = `https://${userName}:${decrypt(token,
|
|
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);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
writeFileSync,
|
|
6
|
+
existsSync,
|
|
7
|
+
unlinkSync,
|
|
8
|
+
readFileSync
|
|
9
|
+
} = require('fs');
|
|
10
|
+
const {
|
|
11
|
+
getNodeModulesPath
|
|
12
|
+
} = require('../General/getNodeModulesPath.js');
|
|
13
|
+
const {
|
|
14
|
+
Logger
|
|
15
|
+
} = require('../Logger/Logger.js');
|
|
16
|
+
const {
|
|
17
|
+
executeSynchronizedCommands
|
|
18
|
+
} = require('../General/executeSyncCommands.js');
|
|
19
|
+
const {
|
|
20
|
+
getServicePathElseCommon
|
|
21
|
+
} = require('./getLintConfiguration.js');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates the ESLint configuration file from the service-specific or common template.
|
|
25
|
+
* Also creates the Stylelint configuration file if available.
|
|
26
|
+
*
|
|
27
|
+
* @returns {boolean} Success status of ESLint config creation
|
|
28
|
+
*/
|
|
29
|
+
function createConfigFiles() {
|
|
30
|
+
const eslintConfigFilePath = path.join(getNodeModulesPath(), '.eslintrc.js');
|
|
31
|
+
const stylelintConfigFilePath = path.join(getNodeModulesPath(), '.stylelintrc.js');
|
|
32
|
+
const {
|
|
33
|
+
pathOfServiceSpecificEslintConfigFile,
|
|
34
|
+
cssLintConfig
|
|
35
|
+
} = getServicePathElseCommon();
|
|
36
|
+
try {
|
|
37
|
+
const eslintContent = readConfig(pathOfServiceSpecificEslintConfigFile, 'eslint');
|
|
38
|
+
const stylelintContent = readConfig(cssLintConfig, 'stylelint');
|
|
39
|
+
if (stylelintContent) {
|
|
40
|
+
writeConfig(stylelintConfigFilePath, stylelintContent, 'stylelint');
|
|
41
|
+
}
|
|
42
|
+
if (existsSync(eslintConfigFilePath)) {
|
|
43
|
+
executeSynchronizedCommands(unlinkSync, [eslintConfigFilePath], 'ESLint configuration file removed successfully!', 'Unable to remove the ESLint config file.', false, false);
|
|
44
|
+
}
|
|
45
|
+
return writeConfig(eslintConfigFilePath, eslintContent, 'eslint');
|
|
46
|
+
} catch (error) {
|
|
47
|
+
Logger.log(Logger.FAILURE_TYPE, error);
|
|
48
|
+
Logger.log(Logger.FAILURE_TYPE, 'Error occurred while creating ESLint config file');
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Reads a configuration file with logging and error handling.
|
|
55
|
+
*/
|
|
56
|
+
function readConfig(filePath, type) {
|
|
57
|
+
return executeSynchronizedCommands(readFileSync, [filePath, 'utf-8'], '', `Unable to read content of ${type} config file.`, true, false);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Writes a configuration file with logging and error handling.
|
|
62
|
+
*/
|
|
63
|
+
function writeConfig(filePath, content, type) {
|
|
64
|
+
return executeSynchronizedCommands(writeFileSync, [filePath, content, 'utf-8'], `${type[0].toUpperCase() + type.slice(1)} configuration file created successfully!`, `Unable to create and write a ${type} configuration file.`, false, true);
|
|
65
|
+
}
|
|
66
|
+
module.exports = {
|
|
67
|
+
createConfigFiles
|
|
68
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
readdirSync
|
|
6
|
+
} = require('fs');
|
|
7
|
+
const {
|
|
8
|
+
getNodeModulesPath
|
|
9
|
+
} = require('../General/getNodeModulesPath');
|
|
10
|
+
const {
|
|
11
|
+
executeSynchronizedCommands
|
|
12
|
+
} = require('../General/executeSyncCommands');
|
|
13
|
+
const {
|
|
14
|
+
getLibraryInstalledLocation
|
|
15
|
+
} = require('../General/getLibraryInstalledLocation');
|
|
16
|
+
function getEslintExecutablePath() {
|
|
17
|
+
let eslintLibraryName = 'eslint';
|
|
18
|
+
let librariesInNodeModulesOfProject = executeSynchronizedCommands(readdirSync, [path.join(getNodeModulesPath(), 'node_modules')], '', 'Unable to get the plugins inside node_modules', true, false);
|
|
19
|
+
let isEslintInstalledinProject = librariesInNodeModulesOfProject.some(library => library === eslintLibraryName);
|
|
20
|
+
return isEslintInstalledinProject ? path.join(getNodeModulesPath(), 'node_modules', eslintLibraryName, 'bin', 'eslint.js') : path.join(getLibraryInstalledLocation(), 'node_modules', eslintLibraryName, 'bin', 'eslint.js');
|
|
21
|
+
}
|
|
22
|
+
module.exports = {
|
|
23
|
+
getEslintExecutablePath
|
|
24
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
existsSync
|
|
6
|
+
} = require('fs');
|
|
7
|
+
const {
|
|
8
|
+
endPoint,
|
|
9
|
+
commonLinterRepoName
|
|
10
|
+
} = require("../../../jsonUtils/commonLinterRepoDetails.js");
|
|
11
|
+
const {
|
|
12
|
+
getRepoName
|
|
13
|
+
} = require('../GitActions/gitActions.js');
|
|
14
|
+
const {
|
|
15
|
+
getLibraryInstalledLocation
|
|
16
|
+
} = require('../General/getLibraryInstalledLocation.js');
|
|
17
|
+
const {
|
|
18
|
+
executeMethodsThatMayThrowException
|
|
19
|
+
} = require("../General/wrapperFunctionToExecuteAFunction.js");
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @function getLintConfiguration - returns eslint configuration file from common linters repo
|
|
23
|
+
* @returns {object} - containing properties of the eslint configuration file
|
|
24
|
+
*/
|
|
25
|
+
function getServicePathElseCommon() {
|
|
26
|
+
const repoName = getRepoName();
|
|
27
|
+
const libraryInstalledLocation = getLibraryInstalledLocation();
|
|
28
|
+
let commonConfigPath = path.join(libraryInstalledLocation, commonLinterRepoName, 'common', 'index.js');
|
|
29
|
+
const _configurationPath = repoName && existsSync(path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, 'index.js')) ? path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, 'index.js') : commonConfigPath;
|
|
30
|
+
let _pathOfServiceSpecificEslintConfigFile = repoName && existsSync(path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, '.eslintrc.js')) ? path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, '.eslintrc.js') : path.join(libraryInstalledLocation, commonLinterRepoName, 'common', '.eslintrc.js');
|
|
31
|
+
let _pluginVersionPath = repoName && existsSync(path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, 'pluginVersion.js')) ? path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, 'pluginVersion.js') : undefined;
|
|
32
|
+
let _stylelintPath = repoName && existsSync(path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, '.stylelintrc.js')) ? path.join(libraryInstalledLocation, commonLinterRepoName, 'services', repoName, '.stylelintrc.js') : path.join(libraryInstalledLocation, commonLinterRepoName, 'common', '.stylelintrc.js');
|
|
33
|
+
return {
|
|
34
|
+
configurationPath: _configurationPath,
|
|
35
|
+
pathOfServiceSpecificEslintConfigFile: _pathOfServiceSpecificEslintConfigFile,
|
|
36
|
+
pluginVersionPath: _pluginVersionPath,
|
|
37
|
+
cssLintConfig: _stylelintPath
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function getLintConfigurationUtil() {
|
|
41
|
+
const {
|
|
42
|
+
configurationPath
|
|
43
|
+
} = getServicePathElseCommon();
|
|
44
|
+
const exceptionHandleObject = {
|
|
45
|
+
rulesConfig: [{}, {}],
|
|
46
|
+
plugins: [],
|
|
47
|
+
extendPlugins: []
|
|
48
|
+
};
|
|
49
|
+
return executeMethodsThatMayThrowException(exceptionHandleObject, `Kindly Ensure that Configuration is setup in Configuration repo \n ${endPoint}`, require, configurationPath);
|
|
50
|
+
}
|
|
51
|
+
module.exports = {
|
|
52
|
+
getLintConfigurationUtil,
|
|
53
|
+
getServicePathElseCommon
|
|
54
|
+
};
|
|
@@ -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,13 +6,6 @@ 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
|
|
@@ -67,63 +60,16 @@ function getSupportedLanguage() {
|
|
|
67
60
|
const _language = supportedExtensions;
|
|
68
61
|
return _language;
|
|
69
62
|
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @function getRunningEnv - Retrieves the current running environment by executing the `npm config get lint_env` command.
|
|
73
|
-
* @returns {string} The current lint environment value set in the npm config.
|
|
74
|
-
*/
|
|
75
|
-
|
|
76
63
|
function getRunningEnv() {
|
|
77
64
|
const command = "npm config get lint_env";
|
|
78
65
|
return execSync(command, {
|
|
79
66
|
shell: true
|
|
80
67
|
}).toString().trim();
|
|
81
68
|
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @function getLastCommitHash - Fetches the last commit hash from a GitLab project using its API.
|
|
85
|
-
*
|
|
86
|
-
* @note This function assumes access to a specific GitLab instance and a hardcoded token and project ID.
|
|
87
|
-
* @returns {string} The latest commit hash (SHA) from the specified GitLab repository.
|
|
88
|
-
* @throws {Error} Will throw an error if the API request fails or returns unexpected data.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
function getLastCommitHash() {
|
|
92
|
-
try {
|
|
93
|
-
const cmd = `curl --header "PRIVATE-TOKEN: ${readOnlyToken}" --url ${commitHashEndPoint}`;
|
|
94
|
-
return JSON.parse(execSync(cmd))[0]["id"];
|
|
95
|
-
} catch (err) {
|
|
96
|
-
Logger.log(Logger.FAILURE_TYPE, err);
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* @function getRequiredInfoPath - Returns the absolute path to the `fsUtils.json` file located in the `jsonUtils` directory at the project root.
|
|
103
|
-
* @returns {string} The full path to the `fsUtils.json` file.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
function getRequiredInfoPath() {
|
|
107
|
-
return path.join(process.cwd(), 'jsonUtils', 'fsUtils.json');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @function getStoredCommitHash - Loads and returns the stored commit hash from `fsUtils.json`.
|
|
112
|
-
* @returns {string} The commit hash stored in the `fsUtils.json` file.
|
|
113
|
-
* @throws {Error} Will throw if the file does not exist or does not contain a `commitHash` key.
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
function getStoredCommitHash() {
|
|
117
|
-
const commitInfoPath = path.join(__dirname, '..', '..', '..', 'jsonUtils', 'fsUtils.json');
|
|
118
|
-
return fs.existsSync(commitInfoPath) && require(commitInfoPath).commitHash;
|
|
119
|
-
}
|
|
120
69
|
module.exports = {
|
|
121
70
|
getSupportedLanguage,
|
|
122
71
|
getTimeStampInfo,
|
|
123
72
|
getEnv,
|
|
124
73
|
getConfigurationPrecommit,
|
|
125
|
-
getRunningEnv
|
|
126
|
-
getLastCommitHash,
|
|
127
|
-
getRequiredInfoPath,
|
|
128
|
-
getStoredCommitHash
|
|
74
|
+
getRunningEnv
|
|
129
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
|