@zohodesk/codestandard-validator 0.0.4-exp-14 → 0.0.4-exp-16

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/bin/cli.js CHANGED
File without changes
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  const {
4
- exec
4
+ exec,
5
+ execSync
5
6
  } = require('child_process');
6
7
  const fs = require('fs');
7
8
  const path = require('path');
@@ -87,6 +88,22 @@ function filterDeltedFileFromStagedFiles(files) {
87
88
  return false;
88
89
  });
89
90
  }
91
+ async function lintFiles(filePath) {
92
+ switch (path.extname(filePath)) {
93
+ case '.js' || '.ts' || '.tsx' || '.jsx':
94
+ {
95
+ return await findEslintErrors(filePath);
96
+ }
97
+ case '.css' || '.scss':
98
+ {
99
+ return await findStyleLintErrors(filePath);
100
+ }
101
+ default:
102
+ {
103
+ return [];
104
+ }
105
+ }
106
+ }
90
107
 
91
108
  /**
92
109
  * @function findEslintErrors - method Lint given file based on given configuration
@@ -94,7 +111,7 @@ function filterDeltedFileFromStagedFiles(files) {
94
111
  * @returns {Array<string>} - array of command line report as a string
95
112
  */
96
113
 
97
- async function findEslintErrors(file) {
114
+ function findEslintErrors(file) {
98
115
  let nodeModulesPathOfProject = `${getNodeModulesPath()}`;
99
116
  let eslintExecutablePath = getEslintExecutablePath();
100
117
  let eslintConfigurationFilePath = `${nodeModulesPathOfProject}/.eslintrc.js`;
@@ -121,6 +138,35 @@ async function findEslintErrors(file) {
121
138
  Logger.log(Logger.INFO_TYPE, 'node_modules not found');
122
139
  }
123
140
  }
141
+
142
+ /**
143
+ *
144
+ * @param {*} params
145
+ */
146
+ function findStyleLintErrors(filePath) {
147
+ const configFilePath = path.resolve(getNodeModulesPath(), '.stylelintrc.json');
148
+ const absolutePath = path.join(getRootDirectory(), filePath);
149
+ try {
150
+ return new Promise((resolve, reject) => {
151
+ exec(`npx stylelint ${absolutePath} --config ${configFilePath}`, {
152
+ cwd: getNodeModulesPath()
153
+ }, (error, stderr, stdout) => {
154
+ if (stderr) {
155
+ resolve(stderr.trim().split('\n'));
156
+ } else if (error) {
157
+ Logger.log(Logger.FAILURE_TYPE, error);
158
+ reject("Error executing eslint command");
159
+ } else {
160
+ resolve([]);
161
+ }
162
+ });
163
+ });
164
+ } catch (error) {
165
+ Logger.log(Logger.FAILURE_TYPE, `Issue is lint css files`);
166
+ return [];
167
+ }
168
+ }
169
+
124
170
  /**
125
171
  * @function {calculateGitDiffForFile} - method calculate diff of file
126
172
  * @param {*} branch_name - branch name
@@ -157,10 +203,10 @@ function isOnlyWarningsPresentInFile(eslintErrorsPresent) {
157
203
  let severityOfEachErrorInFile = [];
158
204
  eslintErrorsPresent.map(error => {
159
205
  let partsInString = error.split(" ");
160
- let severityOfError = partsInString.find(word => word === 'error' || word === 'warning');
206
+ let severityOfError = partsInString.find(word => word === 'error' || word === 'warning' || word === '✖');
161
207
  severityOfEachErrorInFile.push(severityOfError);
162
208
  });
163
- return !severityOfEachErrorInFile.includes('error');
209
+ return !(severityOfEachErrorInFile.includes('✖') || severityOfEachErrorInFile.includes('error'));
164
210
  }
165
211
 
166
212
  /**
@@ -193,29 +239,33 @@ async function preCommitHook() {
193
239
  try {
194
240
  staged_files = await getStagedFiles();
195
241
  if (!staged_files.length == 0) {
196
- staged_files = filterFiles(staged_files, eslintConfigFiles, true);
242
+ const {
243
+ JsFiles: staged_filesJS,
244
+ CssFiles
245
+ } = filterFiles(staged_files, eslintConfigFiles, true);
197
246
 
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){
247
+ // staged_filesJS = filterFiles(staged_filesJS,exemptionFiles) //this is the code for giving exemption to a file during pre commit
248
+ // if(staged_filesJS.length === 0){
200
249
  // Logger.log(Logger.SUCCESS_TYPE,`Commit Successful`)
201
250
  // process.exit(0)
202
251
  // }
203
252
  areFilesStaged = true;
204
- for (let file in staged_files) {
205
- let currentFileName = staged_files[file];
253
+ var stagedFiles = [...staged_filesJS, ...CssFiles];
254
+ for (let file in stagedFiles) {
255
+ let currentFileName = stagedFiles[file];
206
256
  let changedLinesArray = [];
207
257
  let eslintErrorsInChangedLines = [];
208
258
  let isOnlyEslintWarningsPresentInFile = false;
209
- if (getSupportedLanguage().includes(path.extname(staged_files[file]))) {
259
+ if (getSupportedLanguage().includes(path.extname(stagedFiles[file]))) {
210
260
  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) {
261
+ var errorsInFile = await lintFiles(stagedFiles[file]);
262
+ // eslintErrorsInFile = impactBasedPrecommit == false ? filterWarningInFile(errorsInFile) : errorsInFile
263
+ if (stagedFiles[file] && typeof stagedFiles[file] == 'string') {
264
+ if (!errorsInFile.length == 0) {
215
265
  //Calculating changed lines in a file and storing them in respective arrays
216
266
  if (impactBasedPrecommit) {
217
267
  //git diff is computed and stored in an array
218
- let git_diff = await calculateGitDiffForFile(current_branch, staged_files[file]);
268
+ let git_diff = await calculateGitDiffForFile(current_branch, stagedFiles[file]);
219
269
  changedLinesArray = git_diff.filter(line => line.startsWith('@@'));
220
270
  let changedLinesStartArray = [];
221
271
  let changedLinesEndArray = [];
@@ -229,15 +279,15 @@ async function preCommitHook() {
229
279
  changedLinesEndArray.push(changesStartLine + parseInt(changesEndLine) - 1);
230
280
  }
231
281
  }
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
282
+ for (let error = 1; error < errorsInFile.length - 2; error++) {
283
+ //errorsInFile[error].trim() - 69:26 error => Do not hardcode content. Use I18N key instead no-hardcoding/no-hardcoding,
284
+ //errorsInFile[error].trim().split(' ')[0] => 69:26
285
+ //errorsInFile[error].trim().split(' ')[0].split(':')[0] => 69
236
286
 
237
- let eslintErrorLineNumber = eslintErrorsInFile[error].trim().split(' ')[0].split(':')[0];
287
+ let eslintErrorLineNumber = errorsInFile[error].trim().split(' ')[0].split(':')[0];
238
288
  for (let lineNumber in changedLinesStartArray) {
239
289
  if (eslintErrorLineNumber >= changedLinesStartArray[lineNumber] && eslintErrorLineNumber <= changedLinesEndArray[lineNumber]) {
240
- eslintErrorsInChangedLines.push(eslintErrorsInFile[error]);
290
+ eslintErrorsInChangedLines.push(errorsInFile[error]);
241
291
  }
242
292
  }
243
293
  }
@@ -258,10 +308,10 @@ async function preCommitHook() {
258
308
  }
259
309
  }
260
310
  } else {
261
- if (eslintErrorsInFile.length > 0) {
311
+ if (errorsInFile.length > 0) {
262
312
  let startIndex = 1;
263
- let endIndex = eslintErrorsInFile.length - 2;
264
- let listOsEslintErrors = eslintErrorsInFile.slice(startIndex, endIndex);
313
+ let endIndex = errorsInFile.length - 2;
314
+ let listOsEslintErrors = errorsInFile.slice(startIndex, endIndex);
265
315
  isOnlyEslintWarningsPresentInFile = isOnlyWarningsPresentInFile(listOsEslintErrors);
266
316
  Logger.log(Logger.FAILURE_TYPE, `\x1b[1m${currentFileName}\x1b[0m`);
267
317
  for (let eslintError of listOsEslintErrors) {
@@ -3,7 +3,9 @@
3
3
  const {
4
4
  Logger
5
5
  } = require('../Logger/Logger');
6
-
6
+ const {
7
+ existsSync
8
+ } = require("fs");
7
9
  /**
8
10
  * @function filterFiles - removes certain files from set of source files
9
11
  * @param {Array} arrayOfFilesToBeFiltered - array of files which must be filtered
@@ -12,6 +14,29 @@ const {
12
14
  * @returns {Array} - containing the resultant set of files after filtering
13
15
  */
14
16
  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') && existsSync(currentFile)) {
31
+ files.CssFiles.push(currentFile);
32
+ }
33
+ return files;
34
+ }, {
35
+ featureFiles: [],
36
+ JsFiles: [],
37
+ CssFiles: []
38
+ });
39
+ }
15
40
  if (filesToBeRemoved.length !== 0) {
16
41
  if (isConfigFileNeedToBeRemoved) {
17
42
  arrayOfFilesToBeFiltered.filter(file => {
@@ -31,7 +56,7 @@ function filterFiles(arrayOfFilesToBeFiltered, filesToBeRemoved, isConfigFileNee
31
56
  return false;
32
57
  }
33
58
  });
34
- return filteredArrayofFilesWithoutConfigFile;
59
+ return filterFilesByExtension(filteredArrayofFilesWithoutConfigFile);
35
60
  } else if (filesToBeRemoved.length === 0) {
36
61
  return arrayOfFilesToBeFiltered;
37
62
  }
@@ -59,6 +59,8 @@ function getSupportedLanguage() {
59
59
  _language.push('.jsx');
60
60
  _language.push('.ts');
61
61
  _language.push('.tsx');
62
+ _language.push('.css');
63
+ _language.push('.scss');
62
64
  return _language;
63
65
  }
64
66
  function getRunningEnv() {
@@ -26,7 +26,7 @@ const {
26
26
  function installPlugins(pluginsToBeInstalled) {
27
27
  // let {uninstalledPlugins : pluginsToBeInstalled} = checkIfPluginsAreInstalled()
28
28
  if (pluginsToBeInstalled.length > 0) {
29
- let installCommand = `npm install --no-save ${pluginsToBeInstalled.join(' ')}`;
29
+ let installCommand = `npm install --save ${pluginsToBeInstalled.join(' ')}`;
30
30
  Logger.log(Logger.INFO_TYPE, 'Installing plugins ....');
31
31
  Logger.log(Logger.INFO_TYPE, `Install command being executed: ${installCommand}`);
32
32
  let arePluginsInstalledSuccessfully = executeSynchronizedCommands(execSync, [installCommand, {
@@ -15,7 +15,7 @@ function printUninstalledPlugins(uninstalledPlugins, noPluginMessage) {
15
15
  uninstalledPlugins.map(plugin => {
16
16
  Logger.log(Logger.INFO_TYPE, `"${plugin}"`);
17
17
  });
18
- // Logger.log(Logger.FAILURE_TYPE, `Kindly execute the command \x1b[37m"${commandToInstallPlugins}\x1b[0m" \x1b[31mfrom the location where package.json of the repo is present to install the plugins`)
18
+ Logger.log(Logger.FAILURE_TYPE, `Kindly execute the command \x1b[37m"${commandToInstallPlugins}\x1b[0m" \x1b[31mfrom the location where package.json of the repo is present to install the plugins`);
19
19
  } else if (uninstalledPlugins.length === 0 && noPluginMessage.trim() === '') {
20
20
  Logger.log(Logger.INFO_TYPE, 'Plugins are installed already');
21
21
  } else if (uninstalledPlugins.length === 0 && noPluginMessage.trim() !== '') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/codestandard-validator",
3
- "version": "0.0.4-exp-14",
3
+ "version": "0.0.4-exp-16",
4
4
  "description": "library to enforce code standard using eslint",
5
5
  "main": "index.js",
6
6
  "scripts": {