@zohodesk/codestandard-validator 0.0.4-exp-17 → 0.0.4-exp-18
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/pre-commit.js +21 -3
- package/build/lib/cli.js +7 -1
- package/build/lib/postinstall.js +1 -1
- package/build/setup/sample.config.js +2 -2
- package/build/utils/CloneCommonLinterRepo/cloneViaCdt.js +24 -3
- package/build/utils/FileAndFolderOperations/versionControl.js +25 -0
- package/build/utils/General/Hash.js +21 -0
- package/build/utils/General/getGeneralInfo.js +11 -1
- package/build/utils/HuskySetup/initializeHusky.js +1 -1
- package/build/utils/HuskySetup/setupHusky.js +8 -51
- package/build/utils/PluginsInstallation/arePluginsInstalled.js +12 -2
- package/build/utils/PluginsInstallation/checkIfPluginsAreInstalled.js +85 -20
- package/build/utils/PluginsInstallation/installPlugins.js +44 -7
- package/build/utils/PluginsInstallation/printUninstalledPlugins.js +5 -5
- package/package.json +1 -1
|
@@ -28,6 +28,9 @@ const {
|
|
|
28
28
|
getConfigurationPrecommit,
|
|
29
29
|
getSupportedLanguage
|
|
30
30
|
} = require('../../utils/General/getGeneralInfo');
|
|
31
|
+
const {
|
|
32
|
+
getRootDirectory
|
|
33
|
+
} = require('../../utils/General/RootDirectoryUtils/getRootDirectory');
|
|
31
34
|
const {
|
|
32
35
|
impactBasedPrecommit,
|
|
33
36
|
shouldWarningsAbortCommit
|
|
@@ -62,7 +65,7 @@ async function getStagedFiles() {
|
|
|
62
65
|
if (error) {
|
|
63
66
|
if (error != null) reject("Couldn't fetch staged files");
|
|
64
67
|
} else if (stderr) {
|
|
65
|
-
resolve(stderr.trim().split('\n'));
|
|
68
|
+
resolve(filterDeltedFileFromStagedFiles(stderr.trim().split('\n')));
|
|
66
69
|
} else if (stdout.trim() === '') {
|
|
67
70
|
resolve(stdout.trim());
|
|
68
71
|
}
|
|
@@ -70,6 +73,21 @@ async function getStagedFiles() {
|
|
|
70
73
|
});
|
|
71
74
|
}
|
|
72
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;
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
/**
|
|
74
92
|
* @function findEslintErrors - method Lint given file based on given configuration
|
|
75
93
|
* @param {*} file - path of file to lint
|
|
@@ -275,9 +293,9 @@ async function preCommitHook() {
|
|
|
275
293
|
Logger.log(Logger.INFO_TYPE, 'Error executing pre commit hook');
|
|
276
294
|
}
|
|
277
295
|
if (shouldAbortCommit) {
|
|
278
|
-
Logger.log(Logger.FAILURE_TYPE, `There are eslint errors present. So commit is aborted
|
|
296
|
+
Logger.log(Logger.FAILURE_TYPE, `There are eslint errors/warnings present. So commit is aborted.`);
|
|
279
297
|
process.exit(1);
|
|
280
|
-
} else {
|
|
298
|
+
} else if (shouldAbortCommit === false && staged_files.length !== 0) {
|
|
281
299
|
Logger.log(Logger.SUCCESS_TYPE, `Commit Successful`);
|
|
282
300
|
process.exit(0);
|
|
283
301
|
}
|
package/build/lib/cli.js
CHANGED
|
@@ -15,6 +15,9 @@ const {
|
|
|
15
15
|
const {
|
|
16
16
|
installPlugins
|
|
17
17
|
} = require("../utils/PluginsInstallation/installPlugins");
|
|
18
|
+
const {
|
|
19
|
+
checkIfPluginsAreInstalled
|
|
20
|
+
} = require("../utils/PluginsInstallation/checkIfPluginsAreInstalled");
|
|
18
21
|
const [,, action, ...options] = process.argv;
|
|
19
22
|
async function run() {
|
|
20
23
|
switch (action) {
|
|
@@ -30,7 +33,10 @@ async function run() {
|
|
|
30
33
|
}
|
|
31
34
|
case "setupPlugins":
|
|
32
35
|
{
|
|
33
|
-
|
|
36
|
+
const {
|
|
37
|
+
uninstalledPlugins
|
|
38
|
+
} = checkIfPluginsAreInstalled();
|
|
39
|
+
await executeMethodsThatReturnBooleanValue("Some issue occurred in installing plugins", installPlugins, uninstalledPlugins);
|
|
34
40
|
break;
|
|
35
41
|
}
|
|
36
42
|
case "setupExtension":
|
package/build/lib/postinstall.js
CHANGED
|
@@ -35,7 +35,7 @@ async function postInstall() {
|
|
|
35
35
|
isGitInitialized() ? await executeMethodsThatReturnBooleanValue("Some issue occurred in setting up husky.", setupHusky, null) : null;
|
|
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
|
-
Logger.log(Logger.SUCCESS_TYPE, "Pre commit setup
|
|
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}`);
|
|
@@ -8,7 +8,7 @@ const path = require("path");
|
|
|
8
8
|
* @property {boolean} impactBased - Indicates if the linting is impact-based.
|
|
9
9
|
* @property {string} lintReportPath - The path to the lint report JSON file.
|
|
10
10
|
* @property {string} metricServerHost - The URL of the SonarQube server.
|
|
11
|
-
* @property {string}
|
|
11
|
+
* @property {string} meticHandler_api_token - The token for authentication with the SonarQube server.
|
|
12
12
|
* @property {string} metric_token - The token for authentication with the SonarQube server.
|
|
13
13
|
* @property {string} gitEndPoint - API EndPoint for Git Actions
|
|
14
14
|
* @property {string} tsConfigurationPath - The path of the ts configuration Path
|
|
@@ -24,7 +24,7 @@ module.exports = {
|
|
|
24
24
|
impactBased: true,
|
|
25
25
|
lintReportPath: path.resolve(process.cwd(), "lint-report", "lintReport.json"),
|
|
26
26
|
metricServerHost: "https://client-linters.zohodesk.csez.zohocorpin.com",
|
|
27
|
-
|
|
27
|
+
meticHandler_api_token: "oyRcKJZxm2RkXytfAzVtmyRlLIftou5anC==",
|
|
28
28
|
metric_token: "zxh_9737850jh2l53ml17223929ihii73072j54j2260",
|
|
29
29
|
branchDiffPath: path.resolve(process.cwd(), "diffBranch.json"),
|
|
30
30
|
gitEndPoint: "https://zgit.csez.zohocorpin.com",
|
|
@@ -16,21 +16,42 @@ const {
|
|
|
16
16
|
const {
|
|
17
17
|
executeSynchronizedCommands
|
|
18
18
|
} = require('../General/executeSyncCommands');
|
|
19
|
-
|
|
19
|
+
var {
|
|
20
20
|
type,
|
|
21
21
|
endPoint,
|
|
22
22
|
branch,
|
|
23
23
|
cacheDirectory,
|
|
24
|
-
commonLinterRepoName
|
|
24
|
+
commonLinterRepoName,
|
|
25
|
+
user
|
|
25
26
|
} = require('../../../jsonUtils/commonLinterRepoDetails');
|
|
27
|
+
const {
|
|
28
|
+
getConfigurationPrecommit,
|
|
29
|
+
getRunningEnv
|
|
30
|
+
} = require("../General/getGeneralInfo");
|
|
31
|
+
const {
|
|
32
|
+
decrypt
|
|
33
|
+
} = require("../General/Hash");
|
|
34
|
+
const {
|
|
35
|
+
Logger
|
|
36
|
+
} = require("../Logger/Logger");
|
|
26
37
|
/**
|
|
27
38
|
* @function cloneViaCdt - Using the "clint development tool" clones the common linter_configuration repo into precommit library
|
|
28
39
|
* @returns {boolean} - indicating the success or failure of the cloning process
|
|
29
40
|
*/
|
|
30
41
|
|
|
31
42
|
function cloneViaCdt() {
|
|
43
|
+
const {
|
|
44
|
+
token
|
|
45
|
+
} = getConfigurationPrecommit();
|
|
32
46
|
removeFolder(getDeleteDirPath());
|
|
33
|
-
const
|
|
47
|
+
const userName = user;
|
|
48
|
+
var absoluteEndPoint = `https://${endPoint}`;
|
|
49
|
+
const runningEnv = getRunningEnv();
|
|
50
|
+
if (runningEnv === "CI" || runningEnv === "DEVAUTOMATION") {
|
|
51
|
+
Logger.log(Logger.INFO_TYPE, `Running in ${runningEnv}`);
|
|
52
|
+
absoluteEndPoint = `https://${userName}:${decrypt(token, 12)}@${endPoint}`;
|
|
53
|
+
}
|
|
54
|
+
var commandToCloneCommonConfigRepo = `npx cdt clone --clone:type=${type} --clone:url=${absoluteEndPoint} --clone:branch=${branch} --clone:cacheDir=${cacheDirectory} --clone:proj:name=${commonLinterRepoName}`;
|
|
34
55
|
let isCommonConfigurationClonedSuccessfully = executeSynchronizedCommands(execSync, [commandToCloneCommonConfigRepo, {
|
|
35
56
|
cwd: getClonedDirPath()
|
|
36
57
|
}], `Lint Configuration Cloned Successfully - ${getRepoName() || 'common'}`, 'Could not clone the linters common repo', false, true);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
getNodeModulesPath
|
|
6
|
+
} = require('../General/getNodeModulesPath');
|
|
7
|
+
const {
|
|
8
|
+
writeFileSync
|
|
9
|
+
} = require('fs');
|
|
10
|
+
const {
|
|
11
|
+
Logger
|
|
12
|
+
} = require('../Logger/Logger');
|
|
13
|
+
function createVersionControlFile(uninstalledPlugins) {
|
|
14
|
+
Logger.log(Logger.INFO_TYPE, `Rule Plugin Versions are Noted in pluginVersionControl.js`);
|
|
15
|
+
const versionfilePath = path.join(getNodeModulesPath(), 'node_modules', '@zohodesk', 'codestandard-validator', 'pluginVersionControl.js');
|
|
16
|
+
writeFileSync(versionfilePath, `module.exports.plugins = [${uninstalledPlugins.map(plugin => {
|
|
17
|
+
return JSON.stringify({
|
|
18
|
+
plugin: `${plugin}`,
|
|
19
|
+
time: `${new Date()}`
|
|
20
|
+
});
|
|
21
|
+
}).join(',')}]`);
|
|
22
|
+
}
|
|
23
|
+
module.exports = {
|
|
24
|
+
createVersionControlFile
|
|
25
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.encrypt = exports.decrypt = void 0;
|
|
7
|
+
const caesarCipher = (str, shift) => {
|
|
8
|
+
return str.split("").map(char => {
|
|
9
|
+
let code = char.charCodeAt();
|
|
10
|
+
if (code >= 65 && code <= 90) {
|
|
11
|
+
return String.fromCharCode((code - 65 + shift) % 26 + 65);
|
|
12
|
+
} else if (code >= 97 && code <= 122) {
|
|
13
|
+
return String.fromCharCode((code - 97 + shift) % 26 + 97);
|
|
14
|
+
}
|
|
15
|
+
return char;
|
|
16
|
+
}).join("");
|
|
17
|
+
};
|
|
18
|
+
const encrypt = (plaintext, shift) => caesarCipher(plaintext, shift);
|
|
19
|
+
exports.encrypt = encrypt;
|
|
20
|
+
const decrypt = (ciphertext, shift) => caesarCipher(ciphertext, 26 - shift);
|
|
21
|
+
exports.decrypt = decrypt;
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
const os = require('os');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const fs = require("fs");
|
|
6
|
+
const {
|
|
7
|
+
execSync
|
|
8
|
+
} = require('child_process');
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
11
|
* @function getTimeStampInfo - to fetch various timestamp details
|
|
@@ -58,9 +61,16 @@ function getSupportedLanguage() {
|
|
|
58
61
|
_language.push('.tsx');
|
|
59
62
|
return _language;
|
|
60
63
|
}
|
|
64
|
+
function getRunningEnv() {
|
|
65
|
+
const command = "npm config get lint_env";
|
|
66
|
+
return execSync(command, {
|
|
67
|
+
shell: true
|
|
68
|
+
}).toString().trim();
|
|
69
|
+
}
|
|
61
70
|
module.exports = {
|
|
62
71
|
getSupportedLanguage,
|
|
63
72
|
getTimeStampInfo,
|
|
64
73
|
getEnv,
|
|
65
|
-
getConfigurationPrecommit
|
|
74
|
+
getConfigurationPrecommit,
|
|
75
|
+
getRunningEnv
|
|
66
76
|
};
|
|
@@ -23,7 +23,7 @@ function initializeHusky() {
|
|
|
23
23
|
let isNavigationToRootDirectorySuccessful = navigateToRootDirectory(getRootDirectory());
|
|
24
24
|
if (isNavigationToRootDirectorySuccessful) {
|
|
25
25
|
try {
|
|
26
|
-
execSync('npx husky install');
|
|
26
|
+
execSync('npx husky@7 install');
|
|
27
27
|
return true;
|
|
28
28
|
} catch (error) {
|
|
29
29
|
Logger.log(Logger.FAILURE_TYPE, error.toString());
|
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
/* eslint-disable no-console */
|
|
4
|
-
const {
|
|
5
|
-
execSync
|
|
6
|
-
} = require('child_process');
|
|
7
|
-
const {
|
|
8
|
-
readdirSync
|
|
9
|
-
} = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
4
|
const initializeHusky = require('./initializeHusky');
|
|
12
5
|
const configurePrecommitHook = require('./configurePrecommitHook');
|
|
13
|
-
const {
|
|
14
|
-
getNodeModulesPath
|
|
15
|
-
} = require('../General/getNodeModulesPath');
|
|
16
|
-
const {
|
|
17
|
-
executeSynchronizedCommands
|
|
18
|
-
} = require('../General/executeSyncCommands');
|
|
19
6
|
const {
|
|
20
7
|
Logger
|
|
21
8
|
} = require('../Logger/Logger');
|
|
@@ -26,51 +13,21 @@ let isCustomPrecommitConfigurationSuccessful = false;
|
|
|
26
13
|
* @returns {boolean} - indicating whether entire custom precommit hook setup using husky process is success or failure
|
|
27
14
|
*/
|
|
28
15
|
function setupHusky() {
|
|
29
|
-
let
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
Logger.log(Logger.SUCCESS_TYPE, 'Custom pre commit hook setup was successful');
|
|
37
|
-
return true;
|
|
38
|
-
} else {
|
|
39
|
-
Logger.log(Logger.FAILURE_TYPE, 'Couldn\'t configure custom pre commit hook');
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
16
|
+
let isHuskyInitializedSuccessfully = initializeHusky();
|
|
17
|
+
if (isHuskyInitializedSuccessfully) {
|
|
18
|
+
Logger.log(Logger.SUCCESS_TYPE, 'Husky installation successful');
|
|
19
|
+
isCustomPrecommitConfigurationSuccessful = configurePrecommitHook();
|
|
20
|
+
if (isCustomPrecommitConfigurationSuccessful) {
|
|
21
|
+
Logger.log(Logger.SUCCESS_TYPE, 'Custom pre commit hook setup was successful');
|
|
22
|
+
return true;
|
|
42
23
|
} else {
|
|
24
|
+
Logger.log(Logger.FAILURE_TYPE, 'Couldn\'t configure custom pre commit hook');
|
|
43
25
|
return false;
|
|
44
26
|
}
|
|
45
27
|
} else {
|
|
46
|
-
Logger.log(Logger.FAILURE_TYPE, 'Some issue in installing husky package.');
|
|
47
28
|
return false;
|
|
48
29
|
}
|
|
49
30
|
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @function installHuskyPackage - installs husky package if not installed for the project
|
|
53
|
-
* @returns {boolean} - indicating if husky package installed successfully or not
|
|
54
|
-
*/
|
|
55
|
-
function installHuskyPackage() {
|
|
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);
|
|
58
|
-
let husky = {
|
|
59
|
-
packageName: 'husky',
|
|
60
|
-
version: '^7.0.4'
|
|
61
|
-
};
|
|
62
|
-
let isHuskyInstalled = pluginsInNodeModules.includes(husky.packageName) ? true : false;
|
|
63
|
-
if (!isHuskyInstalled) {
|
|
64
|
-
Logger.log(Logger.INFO_TYPE, 'Installing husky package ....');
|
|
65
|
-
let installCommand = `npm install ${husky.packageName}@${husky.version} --save-dev`;
|
|
66
|
-
let isHuskyPackageInstalled = executeSynchronizedCommands(execSync, [installCommand, {
|
|
67
|
-
stdio: 'inherit'
|
|
68
|
-
}], 'Husky package Installed Successfully', 'Unable to install husky package', false, true);
|
|
69
|
-
return isHuskyPackageInstalled;
|
|
70
|
-
}
|
|
71
|
-
Logger.log(Logger.SUCCESS_TYPE, 'Husky package already installed!');
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
31
|
module.exports = {
|
|
75
32
|
setupHusky
|
|
76
33
|
};
|
|
@@ -6,9 +6,19 @@ const {
|
|
|
6
6
|
const {
|
|
7
7
|
printUninstalledPlugins
|
|
8
8
|
} = require('./printUninstalledPlugins');
|
|
9
|
+
const {
|
|
10
|
+
installPlugins
|
|
11
|
+
} = require('./installPlugins');
|
|
9
12
|
function arePluginsInstalled() {
|
|
10
|
-
let
|
|
11
|
-
|
|
13
|
+
let {
|
|
14
|
+
uninstalledPlugins,
|
|
15
|
+
noPluginMessage
|
|
16
|
+
} = checkIfPluginsAreInstalled();
|
|
17
|
+
let areAllPluginsInstalled = uninstalledPlugins.length === 0 ? true : false;
|
|
18
|
+
if (!areAllPluginsInstalled) {
|
|
19
|
+
installPlugins(uninstalledPlugins);
|
|
20
|
+
}
|
|
21
|
+
// printUninstalledPlugins(uninstalledPlugins,noPluginMessage)
|
|
12
22
|
}
|
|
13
23
|
module.exports = {
|
|
14
24
|
arePluginsInstalled
|
|
@@ -74,30 +74,50 @@ function getAllPlugins() {
|
|
|
74
74
|
*/
|
|
75
75
|
function checkIfPluginsAreInstalled() {
|
|
76
76
|
const pluginsForCurrentRepo = getAllPlugins();
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
var uninstalledPlugins = [];
|
|
78
|
+
var plugin = {
|
|
79
79
|
noPluginMessage: '',
|
|
80
80
|
uninstalledPlugins: []
|
|
81
81
|
};
|
|
82
|
-
let pluginsIndevDependencies = {}
|
|
83
|
-
let pluginNamesOfDevDependencyPlugins = []
|
|
84
|
-
let arePluginsPresentIndevDependencies = false
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
82
|
+
// let pluginsIndevDependencies = {}
|
|
83
|
+
// let pluginNamesOfDevDependencyPlugins = []
|
|
84
|
+
// let arePluginsPresentIndevDependencies = false
|
|
85
|
+
// var arePluginsPresentForCurrentRepo = false
|
|
86
|
+
|
|
87
|
+
// if(existsSync(path.join(getNodeModulesPath(),'package.json'))){
|
|
88
|
+
// let packageJsonContent = require(path.join(getNodeModulesPath(),'package.json'))
|
|
89
|
+
// pluginsIndevDependencies = packageJsonContent.devDependencies
|
|
90
|
+
|
|
91
|
+
// if(pluginsIndevDependencies !== undefined){
|
|
92
|
+
// arePluginsPresentIndevDependencies = true
|
|
93
|
+
// pluginNamesOfDevDependencyPlugins = Object.keys(pluginsIndevDependencies)
|
|
94
|
+
// }
|
|
95
|
+
// else if(pluginsIndevDependencies === undefined){
|
|
96
|
+
// Logger.log(Logger.INFO_TYPE,'No devDependencies present!')
|
|
97
|
+
// }
|
|
98
|
+
// }
|
|
99
|
+
// else{
|
|
100
|
+
// arePluginsPresentIndevDependencies = false
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
// arePluginsPresentForCurrentRepo = pluginsForCurrentRepo.length !== 0 ? true : false
|
|
104
|
+
|
|
105
|
+
uninstalledPlugins = pluginsForCurrentRepo.map(plugin => {
|
|
106
|
+
const {
|
|
107
|
+
packageName,
|
|
108
|
+
version
|
|
109
|
+
} = plugin;
|
|
110
|
+
const {
|
|
111
|
+
isPluginPresent,
|
|
112
|
+
pluginPath
|
|
113
|
+
} = checkPluginsPresentInNodemodules(packageName);
|
|
114
|
+
if (isPluginPresent && checkPluginHasProperVersion(packageName, pluginPath, version)) {
|
|
115
|
+
return false;
|
|
94
116
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (arePluginsPresentForCurrentRepo || arePluginsPresentIndevDependencies) {
|
|
100
|
-
uninstalledPlugins = getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, pluginsIndevDependencies);
|
|
117
|
+
return `${packageName}@${version}`;
|
|
118
|
+
}).filter(Boolean);
|
|
119
|
+
if (uninstalledPlugins.length > 0) {
|
|
120
|
+
// uninstalledPlugins = getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, pluginsIndevDependencies);
|
|
101
121
|
plugin.noPluginMessage = '';
|
|
102
122
|
plugin.uninstalledPlugins = uninstalledPlugins;
|
|
103
123
|
return plugin;
|
|
@@ -107,6 +127,51 @@ function checkIfPluginsAreInstalled() {
|
|
|
107
127
|
return plugin;
|
|
108
128
|
}
|
|
109
129
|
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @function returnExactPluginVersion - to return the exact plugin version after removing caret or tilde symbol if present
|
|
133
|
+
* @param {string} pluginVersion
|
|
134
|
+
* @returns {string} - which represents exactPluginVersion
|
|
135
|
+
*/
|
|
136
|
+
function returnExactPluginVersion(pluginVersion) {
|
|
137
|
+
return pluginVersion.replace(/[\^~]/, '');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @function checkPluginsPresentInNodemodules - to check if the plugin is present in the node_modules of the project
|
|
142
|
+
* @param {string} rulePluginName
|
|
143
|
+
* @returns
|
|
144
|
+
*/
|
|
145
|
+
function checkPluginsPresentInNodemodules(rulePluginName) {
|
|
146
|
+
try {
|
|
147
|
+
const pluginPath = require.resolve(rulePluginName.toString());
|
|
148
|
+
return {
|
|
149
|
+
pluginPath: pluginPath,
|
|
150
|
+
isPluginPresent: existsSync(pluginPath)
|
|
151
|
+
};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return {
|
|
154
|
+
pluginPath: "",
|
|
155
|
+
isPluginPresent: false
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @function checkPluginHasProperVersion - to check if the plugin has the proper version`
|
|
162
|
+
* @param {string} rulePluginName
|
|
163
|
+
* @param {string} installationPath
|
|
164
|
+
* @param {string} remotePluginVersion
|
|
165
|
+
* @returns
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
function checkPluginHasProperVersion(rulePluginName, installationPath, remotePluginVersion) {
|
|
169
|
+
const pluginPathArray = installationPath.toString().split(rulePluginName);
|
|
170
|
+
const rulePluginPackageJSONPath = path.join(pluginPathArray[0], rulePluginName, 'package.json');
|
|
171
|
+
const remotePluginExactVersion = returnExactPluginVersion(remotePluginVersion);
|
|
172
|
+
const packageJsonPluginExactVersion = returnExactPluginVersion(require(rulePluginPackageJSONPath).version);
|
|
173
|
+
return remotePluginExactVersion == packageJsonPluginExactVersion ? true : false;
|
|
174
|
+
}
|
|
110
175
|
module.exports = {
|
|
111
176
|
checkIfPluginsAreInstalled
|
|
112
177
|
};
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
const {
|
|
4
4
|
execSync
|
|
5
5
|
} = require('child_process');
|
|
6
|
+
const {
|
|
7
|
+
writeFileSync
|
|
8
|
+
} = require('fs');
|
|
6
9
|
const {
|
|
7
10
|
checkIfPluginsAreInstalled
|
|
8
11
|
} = require('./checkIfPluginsAreInstalled');
|
|
@@ -12,34 +15,68 @@ const {
|
|
|
12
15
|
const {
|
|
13
16
|
Logger
|
|
14
17
|
} = require('../Logger/Logger');
|
|
18
|
+
const {
|
|
19
|
+
createVersionControlFile
|
|
20
|
+
} = require('../FileAndFolderOperations/versionControl');
|
|
21
|
+
const {
|
|
22
|
+
getNodeModulesPath
|
|
23
|
+
} = require('../General/getNodeModulesPath');
|
|
15
24
|
|
|
16
25
|
/**
|
|
17
26
|
* @function installPlugins - installs uninstalled plugins for the project
|
|
18
27
|
* @returns {boolean} - indicating if plugins to be installed are installed successfully or not
|
|
19
28
|
*/
|
|
20
|
-
function installPlugins() {
|
|
21
|
-
let uninstalledPlugins = checkIfPluginsAreInstalled()
|
|
22
|
-
let pluginsToBeInstalled = uninstalledPlugins.uninstalledPlugins;
|
|
29
|
+
function installPlugins(pluginsToBeInstalled) {
|
|
30
|
+
// let {uninstalledPlugins : pluginsToBeInstalled} = checkIfPluginsAreInstalled()
|
|
23
31
|
if (pluginsToBeInstalled.length > 0) {
|
|
32
|
+
let packageJsonBeforePluginsInstallation = getPackageJsonContentBeforeInstallingPlugins();
|
|
24
33
|
let installCommand = `npm install --save-dev ${pluginsToBeInstalled.join(' ')}`;
|
|
25
34
|
Logger.log(Logger.INFO_TYPE, 'Installing plugins ....');
|
|
26
35
|
Logger.log(Logger.INFO_TYPE, `Install command being executed: ${installCommand}`);
|
|
27
36
|
let arePluginsInstalledSuccessfully = executeSynchronizedCommands(execSync, [installCommand, {
|
|
28
|
-
stdio: 'inherit'
|
|
37
|
+
stdio: 'inherit',
|
|
38
|
+
cwd: getNodeModulesPath()
|
|
29
39
|
}], '', 'Some issue occured while installing plugins', false, true);
|
|
30
40
|
if (arePluginsInstalledSuccessfully) {
|
|
31
41
|
Logger.log(Logger.SUCCESS_TYPE, 'Plugins installation successful');
|
|
32
|
-
|
|
42
|
+
let isPackageJsonRestored = restorePackageJsonContent(packageJsonBeforePluginsInstallation);
|
|
43
|
+
if (isPackageJsonRestored) {
|
|
44
|
+
createVersionControlFile(pluginsToBeInstalled);
|
|
45
|
+
// return true
|
|
46
|
+
} else {
|
|
47
|
+
Logger.log(Logger.FAILURE_TYPE, 'Unable to restore package.json content');
|
|
48
|
+
// return false
|
|
49
|
+
}
|
|
33
50
|
} else {
|
|
34
51
|
Logger.log(Logger.FAILURE_TYPE, "Unable to install plugins.Kindly retry the command");
|
|
35
|
-
return false
|
|
52
|
+
// return false
|
|
36
53
|
}
|
|
37
54
|
} else {
|
|
38
55
|
Logger.log(Logger.INFO_TYPE, 'Plugins are already installed');
|
|
39
|
-
return true
|
|
56
|
+
// return true
|
|
40
57
|
}
|
|
41
58
|
}
|
|
42
59
|
|
|
60
|
+
/**
|
|
61
|
+
* @function getPackageJsonContentBeforeInstallingPlugins - to get the content of package.json before installing plugins
|
|
62
|
+
* @returns {Object} - package.json content before installing plugins
|
|
63
|
+
*/
|
|
64
|
+
function getPackageJsonContentBeforeInstallingPlugins() {
|
|
65
|
+
let packageJsonFilePath = `${getNodeModulesPath()}/package.json`;
|
|
66
|
+
let packageJsonContentBeforePluginsInstallation = require(packageJsonFilePath);
|
|
67
|
+
return packageJsonContentBeforePluginsInstallation;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @function restorePackageJsonContent - restore the same content as it was before installing plugins
|
|
72
|
+
* @returns {Boolean} - indicating the success or failure of restoring package.json content
|
|
73
|
+
*/
|
|
74
|
+
function restorePackageJsonContent(packageJsonContentBeforePluginsInstallation) {
|
|
75
|
+
let packageJsonFilePath = `${getNodeModulesPath()}/package.json`;
|
|
76
|
+
let isPackageJsonRestored = executeSynchronizedCommands(writeFileSync, [packageJsonFilePath, JSON.stringify(packageJsonContentBeforePluginsInstallation, null, 2)], 'Package.json content restored successfully', 'Unable to restore package.json content', false, true);
|
|
77
|
+
return isPackageJsonRestored;
|
|
78
|
+
}
|
|
79
|
+
|
|
43
80
|
// function appendInstalledPluginsToPackageJson(pluginsToBeAppendedInPackageJson){
|
|
44
81
|
// let packageJsonFilePath = `${nodeModulesPathOfProject}/package.json`
|
|
45
82
|
// let packageJsonContent = require(packageJsonFilePath)
|
|
@@ -8,17 +8,17 @@ const {
|
|
|
8
8
|
* @function printUninstalledPlugins - to notify the user regarding plugins for the repository has been installed or not
|
|
9
9
|
* @param {Array} uninstalledPlugins - contains list of uninstalled plugins for the project
|
|
10
10
|
*/
|
|
11
|
-
function printUninstalledPlugins(uninstalledPlugins) {
|
|
11
|
+
function printUninstalledPlugins(uninstalledPlugins, noPluginMessage) {
|
|
12
12
|
let commandToInstallPlugins = 'npx ZDPrecommit setupPlugins';
|
|
13
|
-
if (uninstalledPlugins.
|
|
13
|
+
if (uninstalledPlugins.length !== 0) {
|
|
14
14
|
Logger.log(Logger.INFO_TYPE, 'The following plugins are not installed:');
|
|
15
|
-
uninstalledPlugins.
|
|
15
|
+
uninstalledPlugins.map(plugin => {
|
|
16
16
|
Logger.log(Logger.INFO_TYPE, `"${plugin}"`);
|
|
17
17
|
});
|
|
18
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
|
-
} else if (uninstalledPlugins.
|
|
19
|
+
} else if (uninstalledPlugins.length === 0 && noPluginMessage.trim() === '') {
|
|
20
20
|
Logger.log(Logger.INFO_TYPE, 'Plugins are installed already');
|
|
21
|
-
} else if (uninstalledPlugins.
|
|
21
|
+
} else if (uninstalledPlugins.length === 0 && noPluginMessage.trim() !== '') {
|
|
22
22
|
Logger.log(Logger.INFO_TYPE, 'No plugins present for the repository');
|
|
23
23
|
}
|
|
24
24
|
}
|