@zohodesk/testinglibrary 0.1.8-eslint-12 → 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");
@@ -14,47 +16,45 @@ var _path = _interopRequireDefault(require("path"));
14
16
  var _logger = require("../utils/logger");
15
17
  var _cliArgsToObject = require("../utils/cliArgsToObject");
16
18
  var _fileUtils = require("../utils/fileUtils");
19
+ var _requestJarAPI = require("./requestJarAPI");
17
20
  async function runEslintForDiff(cliParams, options = {}) {
18
21
  // $(git diff --name-only HEAD HEAD~1 | grep -E '\.(js|jsx)$' | xargs)
22
+ // await requestJAR(cliParams);
19
23
  const {
20
- diffPath = null
24
+ diffPath = null,
25
+ lintEnv = null
21
26
  } = (0, _cliArgsToObject.cliArgsToObject)(cliParams);
22
27
  const format = 'json';
23
28
  const reportPath = getReportPath();
24
29
  const diffJSON = diffPath ? require(getDiffJsonPath(diffPath)) : null;
25
30
  const filteredDiffFiles = getDiffFiles(diffJSON);
26
- 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
+ }
27
36
  return await new Promise(resolve => {
28
- const command = ['eslint', `${lintFiles.join(' ')}`, `--format=${format}`, '-o', `${reportPath}`];
29
- const lint_process = (0, _child_process.spawn)('npx', command);
30
- lint_process.stdout.on('data', data => {
31
- _logger.Logger.log(_logger.Logger.INFO_TYPE, data);
32
- });
33
- lint_process.stderr.on('data', errorOutput => {
34
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, errorOutput);
35
- });
36
- lint_process.on('error', error => {
37
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, error);
38
- });
39
- 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 {
40
50
  if (diffPath && !(filteredDiffFiles.length === 0)) {
41
51
  (0, _fileUtils.writeFileContents)(reportPath, filterReport_JSON(require(reportPath), diffJSON));
42
52
  }
43
- _logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Lint Executed ...`);
44
53
  resolve({
45
54
  status: 'success'
46
55
  });
47
- });
56
+ }
48
57
  });
49
-
50
- // .finally(() => {
51
- // if (diffPath && !(filteredDiffFiles.length === 0)) {
52
- // writeFileContents(
53
- // reportPath,
54
- // filterReport_JSON(require(reportPath), diffJSON)
55
- // );
56
- // }
57
- // });
58
58
  }
59
59
  function filterReport_JSON(resultJson, diffJson) {
60
60
  const impactLineArray = filterDiffJson(diffJson);
@@ -97,6 +97,9 @@ function filterDiffJson(diffJson) {
97
97
  });
98
98
  return diffFilesMap;
99
99
  }
100
+ function getRootDir() {
101
+ return _path.default.resolve(process.cwd());
102
+ }
100
103
  function getPathFromTestDir(diffPath) {
101
104
  if (!diffPath.includes('uat')) {
102
105
  return undefined;
@@ -129,44 +132,31 @@ function getDiffFiles(diffJson) {
129
132
  return diff === null || diff === void 0 ? void 0 : diff.new_path;
130
133
  });
131
134
  }
132
- function validateReport() {
133
- if (1) {} else {
134
- throw new Error('Report Failed');
135
- process.exit(1);
136
- }
137
- }
138
135
  function getReportPath(reportPath = null, reportOptions = {}) {
139
136
  let jsonReportPath = reportPath || _path.default.resolve(process.cwd(), 'uat', 'lint-report', 'lintReport.json');
140
- if (!(0, _fileUtils.checkIfFileExists)(jsonReportPath)) {
141
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, 'Cannot Find JSON Report ...');
142
- process.exit(0);
143
- }
144
137
  return reportPath ? reportPath : jsonReportPath;
145
138
  }
146
139
  function getDiffJsonPath(diffJsonPath) {
147
140
  return _path.default.resolve(diffJsonPath);
148
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
+ }
149
155
 
150
- // function gitDiffFiles() {
151
- // const childProcess = spawnSync('git', [
152
- // 'diff',
153
- // 'HEAD',
154
- // 'HEAD~1',
155
- // '--name-only'
156
- // ]);
157
- // const diff_Files = childProcess.stdout.toString().split('\n').filter(Boolean);
158
- // var actualDiff_Files = diff_Files.map(filePath => {
159
- // var fileName = path.basename(filePath);
160
- // if (
161
- // filePath.includes('/page-object-model/') ||
162
- // filePath.includes('/assertions/')
163
- // ) {
164
- // return path.resolve(process.cwd(), '../', '../', filePath);
165
- // }
166
- // if (fileName.includes('.spec.js')) {
167
- // return path.resolve(process.cwd(), '../', '../', filePath);
168
- // }
169
- // return false;
170
- // });
171
- // return actualDiff_Files;
156
+ // function validateReport() {
157
+ // if (1) {
158
+ // } else {
159
+ // throw new Error('Report Failed');
160
+ // process.exit(1);
161
+ // }
172
162
  // }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.requestJAR = requestJAR;
8
+ var _https = _interopRequireDefault(require("https"));
9
+ var _cliArgsToObject = require("../utils/cliArgsToObject");
10
+ var _logger = require("../utils/logger");
11
+ var _fs = _interopRequireDefault(require("fs"));
12
+ var _path = _interopRequireDefault(require("path"));
13
+ var _child_process = require("child_process");
14
+ var _rootPath = require("../utils/rootPath");
15
+ async function requestJAR(cliParams, options = {}) {
16
+ const {
17
+ pat,
18
+ requestUrl
19
+ } = (0, _cliArgsToObject.cliArgsToObject)(cliParams);
20
+ const endpoint = requestUrl || 'https://zgit.csez.zohocorpin.com/zohodesk/zohodesk/-/raw/master/sonardependencies/lib/sonar-scanner-cli-4.8.0.2856.jar?inline=false';
21
+ const body = {
22
+ method: 'GET',
23
+ headers: {
24
+ 'Content-Type': 'application/java-archive',
25
+ 'PRIVATE-TOKEN': `${pat}`
26
+ }
27
+ };
28
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cloning jar ${(0, _rootPath.getSonarJarPath)()}`);
29
+ if (!pat) {
30
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `PAT token is Needed ..`);
31
+ process.exit(0);
32
+ }
33
+ return await new Promise(resolve => {
34
+ const jarAPI = _https.default.request(endpoint, body, response => {
35
+ var responseChunk = '';
36
+ const fileStream = _fs.default.createWriteStream((0, _rootPath.getSonarJarPath)());
37
+ response.on('data', data => {
38
+ responseChunk += data;
39
+ });
40
+ response.pipe(fileStream);
41
+ fileStream.on('finish', finish => {
42
+ fileStream.close();
43
+ });
44
+ response.on('error', error => {
45
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Error - ${error}`);
46
+ });
47
+ response.on('end', end => {
48
+ // at end
49
+ resolve({
50
+ path: (0, _rootPath.getSonarJarPath)()
51
+ });
52
+ });
53
+ });
54
+ jarAPI.end();
55
+ });
56
+ }
@@ -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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/testinglibrary",
3
- "version": "0.1.8-eslint-12",
3
+ "version": "0.1.8-eslint-14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "scripts": {