@zohodesk/codestandard-validator 0.0.2-exp-6 → 0.0.2-exp-7

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.
@@ -23,6 +23,13 @@ const {
23
23
  const {
24
24
  getBranchName
25
25
  } = require('../../utils/GitActions/gitActions');
26
+ const {
27
+ getConfigurationPrecommit,
28
+ getSupportedLanguage
29
+ } = require('../../utils/General/getGeneralInfo');
30
+ const {
31
+ impactBasedPrecommit
32
+ } = getConfigurationPrecommit();
26
33
 
27
34
  /**
28
35
  * @function isMergeCommit - This method check whether it is merge or not
@@ -141,6 +148,7 @@ async function preCommitHook() {
141
148
  let exemptionFiles = [];
142
149
  let current_branch = '';
143
150
  let hasEslintErrorsInChangedLines = false;
151
+ let hasEslintErrorsInFiles = false;
144
152
  let areFilesStaged = false;
145
153
  try {
146
154
  current_branch = await getBranchName();
@@ -162,7 +170,7 @@ async function preCommitHook() {
162
170
  let currentFileName = staged_files[file];
163
171
  let changedLinesArray = [];
164
172
  let eslintErrorsInChangedLines = [];
165
- if (path.extname(staged_files[file]) === '.js') {
173
+ if (getSupportedLanguage().includes(path.extname(staged_files[file]))) {
166
174
  try {
167
175
  let eslintErrorsInFile = await findEslintErrors(staged_files[file]);
168
176
  if (staged_files[file] && typeof staged_files[file] == 'string') {
@@ -174,34 +182,44 @@ async function preCommitHook() {
174
182
  let changedLinesEndArray = [];
175
183
 
176
184
  //Calculating changed lines in a file and storing them in respective arrays
177
- for (let number of changedLinesArray) {
178
- let changesStartLine = parseInt(number.split(' ')[2].split(',')[0]);
179
- changedLinesStartArray.push(changesStartLine);
180
- let changesEndLine = number.split(' ')[2].split(',')[1];
181
- if (changesEndLine === undefined) {
182
- changedLinesEndArray.push(changesStartLine);
183
- } else {
184
- changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1);
185
+ if (impactBasedPrecommit) {
186
+ for (let number of changedLinesArray) {
187
+ let changesStartLine = parseInt(number.split(' ')[2].split(',')[0]);
188
+ changedLinesStartArray.push(changesStartLine);
189
+ let changesEndLine = number.split(' ')[2].split(',')[1];
190
+ if (changesEndLine === undefined) {
191
+ changedLinesEndArray.push(changesStartLine);
192
+ } else {
193
+ changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1);
194
+ }
185
195
  }
186
- }
187
- for (let error = 1; error < eslintErrorsInFile.length - 2; error++) {
188
- //eslintErrorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
189
- //eslintErrorsInFile[error].trim().split(' ')[0] => 69:26
190
- //eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
196
+ for (let error = 1; error < eslintErrorsInFile.length - 2; error++) {
197
+ //eslintErrorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
198
+ //eslintErrorsInFile[error].trim().split(' ')[0] => 69:26
199
+ //eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
191
200
 
192
- let eslintErrorLineNumber = eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0];
193
- for (let lineNumber in changedLinesStartArray) {
194
- if (eslintErrorLineNumber >= changedLinesStartArray[lineNumber] && eslintErrorLineNumber <= changedLinesEndArray[lineNumber]) {
195
- eslintErrorsInChangedLines.push(eslintErrorsInFile[error]);
201
+ let eslintErrorLineNumber = eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0];
202
+ for (let lineNumber in changedLinesStartArray) {
203
+ if (eslintErrorLineNumber >= changedLinesStartArray[lineNumber] && eslintErrorLineNumber <= changedLinesEndArray[lineNumber]) {
204
+ eslintErrorsInChangedLines.push(eslintErrorsInFile[error]);
205
+ }
196
206
  }
197
207
  }
198
- }
199
- if (eslintErrorsInChangedLines.length > 0) {
200
- Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
201
- for (let eslintError of eslintErrorsInChangedLines) {
202
- Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
208
+ if (eslintErrorsInChangedLines.length > 0) {
209
+ Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
210
+ for (let eslintError of eslintErrorsInChangedLines) {
211
+ Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
212
+ }
213
+ hasEslintErrorsInChangedLines = true;
214
+ }
215
+ } else {
216
+ if (eslintErrorsInFile.length > 0) {
217
+ Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
218
+ for (let eslintError of eslintErrorsInChangedLines) {
219
+ Logger.log(Logger.FAILURE_TYPE, `\x1b[37m${eslintError.trimEnd()}\x1b[0m`);
220
+ }
221
+ hasEslintErrorsInFiles = true;
203
222
  }
204
- hasEslintErrorsInChangedLines = true;
205
223
  }
206
224
  }
207
225
  }
@@ -220,7 +238,10 @@ async function preCommitHook() {
220
238
  if (hasEslintErrorsInChangedLines) {
221
239
  Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
222
240
  process.exit(1);
223
- } else if (!hasEslintErrorsInChangedLines && areFilesStaged) {
241
+ } else if (hasEslintErrorsInFiles) {
242
+ Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted`);
243
+ process.exit(1);
244
+ } else if (!hasEslintErrorsInFiles && !hasEslintErrorsInChangedLines && areFilesStaged) {
224
245
  Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
225
246
  process.exit(0);
226
247
  }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+
5
+ /**
6
+ * Configuration object for linting and reporting.
7
+ * @property {string} ruleConfigurationPath - The path to the ESLint configuration file.
8
+ * @property {boolean} impactBased - Indicates if the linting is impact-based.
9
+ * @property {string} lintReportPath - The path to the lint report JSON file.
10
+ * @property {string} metricServerHost - The URL of the SonarQube server.
11
+ * @property {string} exemptionInstanceHost - This is Exemption running host URL
12
+ * @property {string} metric_token - The token for authentication with the SonarQube server.
13
+ * @property {string} gitEndPoint - API EndPoint for Git Actions
14
+ * @property {string} tsConfigurationPath - The path of the ts configuration Path
15
+ * @property {number} projectId - project id of repository
16
+ * @property {boolean} impactBasedPrecommit - Indicates if the linting is impact-based in pre commit
17
+ * @property {string} token - Encrypted Authentication Token
18
+ * @property {string} compareBranch - Branch to compare diff
19
+ */
20
+
21
+ module.exports = {
22
+ ruleConfigurationPath: path.resolve(process.cwd(), ".eslintrc.js"),
23
+ impactBased: true,
24
+ lintReportPath: path.resolve(process.cwd(), "lint-report", "lintReport.json"),
25
+ metricServerHost: "https://client-linters.zohodesk.csez.zohocorpin.com",
26
+ exemptionInstanceHost: "",
27
+ metric_token: "zxh_9737850jh2l53ml17223929ihii73072j54j2260",
28
+ branchDiffPath: path.resolve(process.cwd(), "diffBranch.json"),
29
+ gitEndPoint: "https://zgit.csez.zohocorpin.com",
30
+ tsConfigurationPath: path.resolve(process.cwd(), 'tsconfig.json'),
31
+ projectId: `project-id`,
32
+ impactBasedPrecommit: true,
33
+ token: "w-OkG3f5OOM1Rkly8phZ",
34
+ compareBranch: 'release'
35
+ };
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  const os = require('os');
4
+ const path = require('path');
5
+ const fs = require("fs");
4
6
 
5
7
  /**
6
8
  * @function getTimeStampInfo - to fetch various timestamp details
@@ -25,7 +27,40 @@ function getTimeStampInfo() {
25
27
  function getEnv() {
26
28
  return os.type();
27
29
  }
30
+
31
+ /**
32
+ * @function getConfigPath - get a path of lint configuration path
33
+ * @returns {string}
34
+ */
35
+ function getConfigPath() {
36
+ const configPath = path.resolve(process.cwd(), 'lint.config.js');
37
+ const defaultConfigPath = path.resolve(__dirname, '..', '..', 'setup', 'sample.config.js');
38
+ return fs.exitSync(configPath) ? configPath : defaultConfigPath;
39
+ }
40
+
41
+ /**
42
+ * @function getConfiguration - get configuration object of lint configuration
43
+ * @returns
44
+ */
45
+ function getConfigurationPrecommit() {
46
+ return require(getConfigPath());
47
+ }
48
+
49
+ /**
50
+ * @function getSupportedLanguage - get support language
51
+ * @returns {Array<string>}
52
+ */
53
+ function getSupportedLanguage() {
54
+ const _language = [];
55
+ _language.push('js');
56
+ _language.push('jsx');
57
+ _language.push('ts');
58
+ _language.push('tsx');
59
+ return _language;
60
+ }
28
61
  module.exports = {
62
+ getSupportedLanguage,
29
63
  getTimeStampInfo,
30
- getEnv
64
+ getEnv,
65
+ getConfigurationPrecommit
31
66
  };
@@ -4,17 +4,21 @@
4
4
  const {
5
5
  execSync
6
6
  } = require('child_process');
7
+ const {
8
+ readdirSync
9
+ } = require('fs');
10
+ const path = require('path');
7
11
  const initializeHusky = require('./initializeHusky');
8
12
  const configurePrecommitHook = require('./configurePrecommitHook');
13
+ const {
14
+ getNodeModulesPath
15
+ } = require('../General/getNodeModulesPath');
9
16
  const {
10
17
  executeSynchronizedCommands
11
18
  } = require('../General/executeSyncCommands');
12
19
  const {
13
20
  Logger
14
21
  } = require('../Logger/Logger');
15
- const {
16
- getRootDirectory
17
- } = require('../General/RootDirectoryUtils/getRootDirectory');
18
22
  let isCustomPrecommitConfigurationSuccessful = false;
19
23
 
20
24
  /**
@@ -49,25 +53,16 @@ function setupHusky() {
49
53
  * @returns {boolean} - indicating if husky package installed successfully or not
50
54
  */
51
55
  function installHuskyPackage() {
52
- // checking globally so removed
53
- // const nodeModulesPathOfProject = path.join(getNodeModulesPath(),'node_modules')
54
- // let pluginsInNodeModules = executeSynchronizedCommands(readdirSync,[nodeModulesPathOfProject],'','Unable to get directories in node_modules when checking if husky is installed',true,false)
56
+ const nodeModulesPathOfProject = path.join(getNodeModulesPath(), 'node_modules');
57
+ let pluginsInNodeModules = executeSynchronizedCommands(readdirSync, [nodeModulesPathOfProject], '', 'Unable to get directories in node_modules when checking if husky is installed', true, false);
55
58
  let husky = {
56
59
  packageName: 'husky',
57
- version: '7.0.4'
60
+ version: '^7.0.4'
58
61
  };
59
-
60
- // let isHuskyInstalled = pluginsInNodeModules.includes(husky.packageName) ? true : false
61
- try {
62
- const huskyVersion = execSync("husky -v", {
63
- cwd: getRootDirectory()
64
- }).toString();
65
- if (!(huskyVersion.trim() == husky.version)) {
66
- throw new Error("error");
67
- }
68
- } catch {
62
+ let isHuskyInstalled = pluginsInNodeModules.includes(husky.packageName) ? true : false;
63
+ if (!isHuskyInstalled) {
69
64
  Logger.log(Logger.INFO_TYPE, 'Installing husky package ....');
70
- let installCommand = `npm install ${husky.packageName}@${husky.version} --save-dev --save-exact -g`;
65
+ let installCommand = `npm install ${husky.packageName}@${husky.version} --save-dev`;
71
66
  let isHuskyPackageInstalled = executeSynchronizedCommands(execSync, [installCommand, {
72
67
  stdio: 'inherit'
73
68
  }], 'Husky package Installed Successfully', 'Unable to install husky package', false, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/codestandard-validator",
3
- "version": "0.0.2-exp-6",
3
+ "version": "0.0.2-exp-7",
4
4
  "description": "library to enforce code standard using eslint",
5
5
  "main": "index.js",
6
6
  "scripts": {