@zohodesk/testinglibrary 0.1.8-eslint-13 → 0.1.8-eslint-14

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.
@@ -7,6 +7,8 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.getDiffFiles = getDiffFiles;
8
8
  exports.getDiffJsonPath = getDiffJsonPath;
9
9
  exports.getReportPath = getReportPath;
10
+ exports.getRootDir = getRootDir;
11
+ exports.gitDiffFiles = gitDiffFiles;
10
12
  exports.runEslintForDiff = runEslintForDiff;
11
13
  var _getFilePath = require("../utils/getFilePath");
12
14
  var _child_process = require("child_process");
@@ -17,46 +19,42 @@ var _fileUtils = require("../utils/fileUtils");
17
19
  var _requestJarAPI = require("./requestJarAPI");
18
20
  async function runEslintForDiff(cliParams, options = {}) {
19
21
  // $(git diff --name-only HEAD HEAD~1 | grep -E '\.(js|jsx)$' | xargs)
20
- await (0, _requestJarAPI.requestJAR)(cliParams);
22
+ // await requestJAR(cliParams);
21
23
  const {
22
- diffPath = null
24
+ diffPath = null,
25
+ lintEnv = null
23
26
  } = (0, _cliArgsToObject.cliArgsToObject)(cliParams);
24
27
  const format = 'json';
25
28
  const reportPath = getReportPath();
26
29
  const diffJSON = diffPath ? require(getDiffJsonPath(diffPath)) : null;
27
30
  const filteredDiffFiles = getDiffFiles(diffJSON);
28
- const lintFiles = !filteredDiffFiles ? [_path.default.resolve(process.cwd(), 'uat', 'modules')] : filteredDiffFiles;
31
+ console.log(filterDiffJson);
32
+ var lintFiles = !filteredDiffFiles ? [_path.default.resolve(process.cwd(), 'uat', 'modules')] : filteredDiffFiles;
33
+ if (lintEnv === 'pre-commit') {
34
+ lintFiles = gitDiffFiles();
35
+ }
29
36
  return await new Promise(resolve => {
30
- const command = ['eslint', `${lintFiles.join(' ')}`, `--format=${format}`, '-o', `${reportPath}`];
31
- const lint_process = (0, _child_process.spawn)('npx', command);
32
- lint_process.stdout.on('data', data => {
33
- _logger.Logger.log(_logger.Logger.INFO_TYPE, data);
34
- });
35
- lint_process.stderr.on('data', errorOutput => {
36
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, errorOutput);
37
- });
38
- lint_process.on('error', error => {
39
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, error);
40
- });
41
- lint_process.on('close', onClose => {
37
+ const command = ['eslint', lintFiles.join(' '), `--format=${format}`, '-o', `${reportPath}`];
38
+ try {
39
+ const lint_process = (0, _child_process.spawnSync)('npx', command, {
40
+ shell: true,
41
+ cwd: process.cwd()
42
+ });
43
+ if (lint_process.error) {
44
+ throw lintFiles.error;
45
+ }
46
+ } catch (error) {
47
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, error);
48
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, 'Execution Failed');
49
+ } finally {
42
50
  if (diffPath && !(filteredDiffFiles.length === 0)) {
43
51
  (0, _fileUtils.writeFileContents)(reportPath, filterReport_JSON(require(reportPath), diffJSON));
44
52
  }
45
- _logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Lint Executed ...`);
46
53
  resolve({
47
54
  status: 'success'
48
55
  });
49
- });
56
+ }
50
57
  });
51
-
52
- // .finally(() => {
53
- // if (diffPath && !(filteredDiffFiles.length === 0)) {
54
- // writeFileContents(
55
- // reportPath,
56
- // filterReport_JSON(require(reportPath), diffJSON)
57
- // );
58
- // }
59
- // });
60
58
  }
61
59
  function filterReport_JSON(resultJson, diffJson) {
62
60
  const impactLineArray = filterDiffJson(diffJson);
@@ -99,6 +97,9 @@ function filterDiffJson(diffJson) {
99
97
  });
100
98
  return diffFilesMap;
101
99
  }
100
+ function getRootDir() {
101
+ return _path.default.resolve(process.cwd());
102
+ }
102
103
  function getPathFromTestDir(diffPath) {
103
104
  if (!diffPath.includes('uat')) {
104
105
  return undefined;
@@ -131,44 +132,31 @@ function getDiffFiles(diffJson) {
131
132
  return diff === null || diff === void 0 ? void 0 : diff.new_path;
132
133
  });
133
134
  }
134
- function validateReport() {
135
- if (1) {} else {
136
- throw new Error('Report Failed');
137
- process.exit(1);
138
- }
139
- }
140
135
  function getReportPath(reportPath = null, reportOptions = {}) {
141
136
  let jsonReportPath = reportPath || _path.default.resolve(process.cwd(), 'uat', 'lint-report', 'lintReport.json');
142
- if (!(0, _fileUtils.checkIfFileExists)(jsonReportPath)) {
143
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, 'Cannot Find JSON Report ...');
144
- process.exit(0);
145
- }
146
137
  return reportPath ? reportPath : jsonReportPath;
147
138
  }
148
139
  function getDiffJsonPath(diffJsonPath) {
149
140
  return _path.default.resolve(diffJsonPath);
150
141
  }
142
+ function gitDiffFiles() {
143
+ const command = 'git diff HEAD HEAD~1 --name-only';
144
+ const child = (0, _child_process.spawnSync)(command, {
145
+ shell: true
146
+ });
147
+ return child.stdout.toString().split('\n').map(filePath => _path.default.resolve(getRootLintPath(), filePath));
148
+ }
149
+ function getRootLintPath() {
150
+ const childProcess = (0, _child_process.spawnSync)('git', ['rev-parse', '--show-toplevel'], {
151
+ shell: true
152
+ });
153
+ return childProcess.stdout.toString().split('\n').filter(Boolean).pop();
154
+ }
151
155
 
152
- // function gitDiffFiles() {
153
- // const childProcess = spawnSync('git', [
154
- // 'diff',
155
- // 'HEAD',
156
- // 'HEAD~1',
157
- // '--name-only'
158
- // ]);
159
- // const diff_Files = childProcess.stdout.toString().split('\n').filter(Boolean);
160
- // var actualDiff_Files = diff_Files.map(filePath => {
161
- // var fileName = path.basename(filePath);
162
- // if (
163
- // filePath.includes('/page-object-model/') ||
164
- // filePath.includes('/assertions/')
165
- // ) {
166
- // return path.resolve(process.cwd(), '../', '../', filePath);
167
- // }
168
- // if (fileName.includes('.spec.js')) {
169
- // return path.resolve(process.cwd(), '../', '../', filePath);
170
- // }
171
- // return false;
172
- // });
173
- // return actualDiff_Files;
156
+ // function validateReport() {
157
+ // if (1) {
158
+ // } else {
159
+ // throw new Error('Report Failed');
160
+ // process.exit(1);
161
+ // }
174
162
  // }
@@ -9,14 +9,16 @@ var _path = _interopRequireDefault(require("path"));
9
9
  var _fileUtils = require("../utils/fileUtils");
10
10
  var _cliArgsToObject = require("../utils/cliArgsToObject");
11
11
  var _eslintLintStage = require("./eslintLintStage");
12
+ var _startSeverSonarQube = require("./startSever-sonarQube");
12
13
  function getSummaryOfLint(cliParams) {
13
14
  const {
14
15
  repo,
15
16
  branch,
16
- username
17
+ username,
18
+ lintEnv
17
19
  } = (0, _cliArgsToObject.cliArgsToObject)(cliParams);
18
20
  return {
19
- projectNameL: `${repo}_${branch},${username}`,
21
+ projectNameL: lintEnv === 'pre-commit' ? (0, _startSeverSonarQube.getProjectNameFrmGit)() : `${repo}_${branch},${username}`,
20
22
  reportUrl: `https://serverlinter.zohodesk.csez.zohocorpin.com/project/issues?id=${repo}_${branch}_${username}&resolve=false`,
21
23
  jsonReportPath: (0, _eslintLintStage.getReportPath)().toString()
22
24
  };
@@ -4,6 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
+ exports.getProjectNameFrmGit = getProjectNameFrmGit;
7
8
  exports.runSonarAnalysis = runSonarAnalysis;
8
9
  var _path = _interopRequireDefault(require("path"));
9
10
  var _fs = _interopRequireDefault(require("fs"));
@@ -22,13 +23,15 @@ var _rootPath = require("../utils/rootPath");
22
23
 
23
24
  function runSonarAnalysis(cliParams) {
24
25
  var _getFilePathFrmReport, _getFilePathFrmReport2;
26
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, 'sending report to server ...');
25
27
  const {
26
28
  diffPath,
27
29
  repo,
28
30
  branch,
29
31
  username,
30
32
  login = null,
31
- password = null
33
+ password = null,
34
+ lintEnv = null
32
35
  } = (0, _cliArgsToObject.cliArgsToObject)(cliParams);
33
36
  const sonarqube_host_url = 'https://serverlinter-np.zohodesk.csez.zohocorpin.com/';
34
37
  const sonar_scanner_path = (0, _rootPath.getSonarJarPath)();
@@ -36,10 +39,16 @@ function runSonarAnalysis(cliParams) {
36
39
  Dlogin,
37
40
  Dpassword
38
41
  } = getCredentials(login, password);
39
- const project_name = repo && branch && branch ? `${repo}_${branch}_${username}` : 'UAT_Testing';
40
- const resultFilesPath = diffPath ? (_getFilePathFrmReport = getFilePathFrmReport(require((0, _eslintLintStage.getReportPath)()))) === null || _getFilePathFrmReport === void 0 ? void 0 : _getFilePathFrmReport.stepFilesPath.join(',') : [' '].join('');
42
+ var project_name = repo && branch && branch ? `${repo}_${branch}_${username}` : 'UAT_Testing';
43
+ if (lintEnv === 'pre-commit') {
44
+ project_name = getProjectNameFrmGit();
45
+ }
46
+ var resultFilesPath = diffPath && !(lintEnv == 'pre-commit') ? (_getFilePathFrmReport = getFilePathFrmReport(require((0, _eslintLintStage.getReportPath)()))) === null || _getFilePathFrmReport === void 0 ? void 0 : _getFilePathFrmReport.stepFilesPath.join(',') : [`${_path.default.resolve(process.cwd(), 'uat', 'modules')}`].join(',');
41
47
  const source_directories = ((_getFilePathFrmReport2 = getFilePathFrmReport(require((0, _eslintLintStage.getReportPath)()))) === null || _getFilePathFrmReport2 === void 0 ? void 0 : _getFilePathFrmReport2.featureFilesPath) || [' '];
42
- const source_directories_str = source_directories.join(',');
48
+ const source_directories_str = lintEnv == 'pre-commit' ? ' ' : source_directories.join(',');
49
+ if (lintEnv == 'pre-commit') {
50
+ resultFilesPath = (0, _eslintLintStage.gitDiffFiles)().join(',');
51
+ }
43
52
  const command = ['-jar', sonar_scanner_path, `-Dsonar.host.url=${sonarqube_host_url}`, `-Dsonar.projectKey=${project_name}`, `-Dsonar.login=${Dlogin}`, `-Dsonar.password=${Dpassword}`, `-Dsonar.sources=${source_directories_str}`, `-Dsonar.tests=${resultFilesPath}`, '-Dsonar.language=js', `-Dsonar.eslint.reportPaths=${(0, _eslintLintStage.getReportPath)()}`, '-Dsonar.javascript.lcov.reportPaths=coverage/lcov-report/lcov.info'];
44
53
  const child_process = (0, _child_process.spawn)('java', command);
45
54
  child_process.stdout.on('data', data => {
@@ -68,6 +77,30 @@ function getFilePathFrmReport(resultJson) {
68
77
  featureFilesPath: []
69
78
  });
70
79
  }
80
+ function getProjectNameFrmGit() {
81
+ return `${getRepoName()}_${getBranchName()}_${getGitUsername()}`;
82
+ }
83
+ function getGitUsername() {
84
+ const userName = getSpwanOutput('git', ['config', 'user.email']);
85
+ return userName.pop().split('@')[0];
86
+ }
87
+ function getRepoName() {
88
+ const repoName = getSpwanOutput('git', ['remote', 'get-url', 'origin']);
89
+ return repoName.pop().split('/').pop().split('.')[0];
90
+ }
91
+ function getBranchName() {
92
+ const branchName = getSpwanOutput('git', ['branch'])[1];
93
+ if (branchName.startsWith('*')) {
94
+ return branchName.replace('*', ' ').trim();
95
+ }
96
+ return branchName;
97
+ }
98
+ function getSpwanOutput(command, params = []) {
99
+ const demon_process = (0, _child_process.spawnSync)(command, {
100
+ shell: true
101
+ });
102
+ return demon_process.stdout.toString().split('\n').filter(Boolean);
103
+ }
71
104
  function getCredentials(login, password) {
72
105
  if (login && password) {
73
106
  return {
@@ -48,5 +48,9 @@ function getExecutableBinaryPath(command) {
48
48
  }
49
49
  function getSonarJarPath() {
50
50
  const jarPath = _path.default.resolve('node_modules', '@zohodesk', 'testinglibrary', 'build', 'lint-ci', 'java-jar', 'sonar-scanner-cli-4.8.0.2856.jar');
51
- return jarPath;
51
+ if ((0, _fileUtils.checkIfFileExists)(jarPath)) {
52
+ return jarPath;
53
+ }
54
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, 'cannot find - Java-Jar');
55
+ process.exit(0);
52
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/testinglibrary",
3
- "version": "0.1.8-eslint-13",
3
+ "version": "0.1.8-eslint-14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "scripts": {