@zohodesk/codestandard-validator 0.0.1 → 0.0.2

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.
Files changed (38) hide show
  1. package/build/grafana-config/grafana-config.js +16 -0
  2. package/build/grafana-config/grfanaAPI.js +72 -0
  3. package/build/hooks/Precommit/filterUtils.js +42 -0
  4. package/build/hooks/Precommit/pre-commit.js +235 -0
  5. package/build/lib/cli.js +47 -0
  6. package/build/lib/postinstall.js +44 -0
  7. package/build/setup/initialSetup.js +60 -0
  8. package/build/utils/CloneCommonLinterRepo/HelperFunctions/helpers.js +19 -0
  9. package/build/utils/CloneCommonLinterRepo/cloneViaCdt.js +41 -0
  10. package/build/utils/EslintConfigFileUtils/createEslintConfigFile.js +54 -0
  11. package/build/utils/EslintConfigFileUtils/getEslintExecutablePath.js +24 -0
  12. package/build/utils/EslintConfigFileUtils/getLintConfiguration.js +52 -0
  13. package/build/utils/ExtensionInstallation/installEslintExtension.js +25 -0
  14. package/build/utils/FileAndFolderOperations/deleteFile.js +28 -0
  15. package/build/utils/FileAndFolderOperations/filterFiles.js +41 -0
  16. package/build/utils/FileAndFolderOperations/getFileContent.js +23 -0
  17. package/build/utils/FileAndFolderOperations/grantExecutionPermission.js +35 -0
  18. package/build/utils/FileAndFolderOperations/removeFolder.js +37 -0
  19. package/build/utils/General/RootDirectoryUtils/getRootDirectory.js +23 -0
  20. package/build/utils/General/RootDirectoryUtils/navigateToRootDirectory.js +25 -0
  21. package/build/utils/General/executeSyncCommands.js +40 -0
  22. package/build/utils/General/getGeneralInfo.js +31 -0
  23. package/build/utils/General/getLibraryInstalledLocation.js +18 -0
  24. package/build/utils/General/getNodeModulesPath.js +14 -0
  25. package/build/utils/General/wrapperFunctionToExecuteAFunction.js +39 -0
  26. package/build/utils/General/writeProjectDetailsToJson.js +18 -0
  27. package/build/utils/GitActions/gitActions.js +88 -0
  28. package/build/utils/HuskySetup/configurePrecommitHook.js +39 -0
  29. package/build/utils/HuskySetup/initializeHusky.js +35 -0
  30. package/build/utils/HuskySetup/setupHusky.js +76 -0
  31. package/build/utils/Logger/Logger.js +26 -0
  32. package/build/utils/PluginsInstallation/arePluginsInstalled.js +15 -0
  33. package/build/utils/PluginsInstallation/checkIfPluginsAreInstalled.js +112 -0
  34. package/build/utils/PluginsInstallation/installPlugins.js +91 -0
  35. package/build/utils/PluginsInstallation/printUninstalledPlugins.js +27 -0
  36. package/build/utils/PrecommitUsingGitSetup/update-git-precommithook.js +37 -0
  37. package/changeLog.md +3 -0
  38. package/package.json +1 -1
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
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
+ const initializeHusky = require('./initializeHusky');
12
+ const configurePrecommitHook = require('./configurePrecommitHook');
13
+ const {
14
+ getNodeModulesPath
15
+ } = require('../General/getNodeModulesPath');
16
+ const {
17
+ executeSynchronizedCommands
18
+ } = require('../General/executeSyncCommands');
19
+ const {
20
+ Logger
21
+ } = require('../Logger/Logger');
22
+ let isCustomPrecommitConfigurationSuccessful = false;
23
+
24
+ /**
25
+ * @function setupHusky - to setup custom precommit hook using husky plugin
26
+ * @returns {boolean} - indicating whether entire custom precommit hook setup using husky process is success or failure
27
+ */
28
+ function setupHusky() {
29
+ let isHuskyPackageInstalled = installHuskyPackage();
30
+ if (isHuskyPackageInstalled) {
31
+ let isHuskyInitializedSuccessfully = initializeHusky();
32
+ if (isHuskyInitializedSuccessfully) {
33
+ Logger.log(Logger.SUCCESS_TYPE, 'Husky installation successful');
34
+ isCustomPrecommitConfigurationSuccessful = configurePrecommitHook();
35
+ if (isCustomPrecommitConfigurationSuccessful) {
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
+ }
42
+ } else {
43
+ return false;
44
+ }
45
+ } else {
46
+ Logger.log(Logger.FAILURE_TYPE, 'Some issue in installing husky package.');
47
+ return false;
48
+ }
49
+ }
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
+ module.exports = {
75
+ setupHusky
76
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ class LoggerImpl {
4
+ constructor() {
5
+ this.SUCCESS_TYPE = "success";
6
+ this.FAILURE_TYPE = "failure";
7
+ this.INFO_TYPE = "info";
8
+ this.colors = {
9
+ success: ["\x1b[36m", "\x1b[0m"],
10
+ failure: ["\x1b[31m", "\x1b[0m"],
11
+ info: ["\x1b[33m", "\x1b[0m"]
12
+ };
13
+ this.consoleLogger = console;
14
+ }
15
+ error(err) {
16
+ this.consoleLogger.error(err);
17
+ }
18
+ catchInfo(InfoMessage) {
19
+ precommit_log.set(timeStamp, InfoMessage);
20
+ }
21
+ log(type, message) {
22
+ const color = this.colors[type];
23
+ this.consoleLogger.log(`${color[0]}${message}${color[1]}`);
24
+ }
25
+ }
26
+ module.exports.Logger = new LoggerImpl();
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ const {
4
+ checkIfPluginsAreInstalled
5
+ } = require('./checkIfPluginsAreInstalled');
6
+ const {
7
+ printUninstalledPlugins
8
+ } = require('./printUninstalledPlugins');
9
+ function arePluginsInstalled() {
10
+ let uninstalledPlugins = checkIfPluginsAreInstalled();
11
+ printUninstalledPlugins(uninstalledPlugins);
12
+ }
13
+ module.exports = {
14
+ arePluginsInstalled
15
+ };
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+
3
+ const {
4
+ readdirSync,
5
+ existsSync
6
+ } = require('fs');
7
+ const path = require('path');
8
+ const {
9
+ getNodeModulesPath
10
+ } = require('../General/getNodeModulesPath');
11
+ const {
12
+ Logger
13
+ } = require('../Logger/Logger');
14
+ const {
15
+ getLibraryInstalledLocation
16
+ } = require('../General/getLibraryInstalledLocation');
17
+ const {
18
+ executeSynchronizedCommands
19
+ } = require('../General/executeSyncCommands');
20
+ const {
21
+ endPoint,
22
+ commonLinterRepoName
23
+ } = require('../../../jsonUtils/commonLinterRepoDetails');
24
+ const {
25
+ executeMethodsThatMayThrowException
26
+ } = require('../General/wrapperFunctionToExecuteAFunction');
27
+ const {
28
+ getServicePathElseCommon
29
+ } = require('../EslintConfigFileUtils/getLintConfiguration');
30
+ function getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, devDependencies) {
31
+ let pluginsInNodeModules = executeSynchronizedCommands(readdirSync, [path.join(getNodeModulesPath(), 'node_modules')], '', 'Unable to get the plugins inside node_modules', true, false);
32
+ let pluginsToBeInstalled = [];
33
+ pluginsForCurrentRepo.filter(plugin => {
34
+ if (plugin.packageName.startsWith('@')) {
35
+ let scope = plugin.packageName.split('/')[0];
36
+ let pluginName = plugin.packageName.split('/')[1];
37
+ let scopedPlugins = executeSynchronizedCommands(readdirSync, [path.join(getNodeModulesPath(), 'node_modules', scope)], '', 'Unable to get the plugins inside the scope inside node_modules', true, false);
38
+ let isPluginInstalled = scopedPlugins.includes(pluginName) ? true : false;
39
+ if (!isPluginInstalled) {
40
+ pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
41
+ }
42
+ } else {
43
+ let isPluginInstalled = pluginsInNodeModules.includes(plugin.packageName) ? true : false;
44
+ if (!isPluginInstalled) {
45
+ pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
46
+ }
47
+ }
48
+ if (pluginNamesOfDevDependencyPlugins.includes(plugin.packageName)) {
49
+ if (plugin.version !== devDependencies[plugin.packageName]) {
50
+ pluginsToBeInstalled.push(`${plugin.packageName}@${plugin.version}`);
51
+ }
52
+ }
53
+ });
54
+ return pluginsToBeInstalled;
55
+ }
56
+ function getAllPlugins() {
57
+ let serviceSpecificPlugins = [];
58
+ const pathToCommonPluginsFile = path.join(getLibraryInstalledLocation(), commonLinterRepoName, 'common', 'pluginVersion.js');
59
+ let commonPlugins = executeMethodsThatMayThrowException([], 'Make sure there is a pluginVersion.js file in common folder under configuration folder', require, pathToCommonPluginsFile);
60
+ const {
61
+ pluginVersionPath: serviceSpecificPluginsFile
62
+ } = getServicePathElseCommon();
63
+ if (serviceSpecificPluginsFile !== undefined) {
64
+ serviceSpecificPlugins = executeMethodsThatMayThrowException([], `Kindly Ensure that Configuration is setup in Configuration repo \n ${endPoint}`, require, serviceSpecificPluginsFile);
65
+ } else {
66
+ serviceSpecificPlugins = [];
67
+ }
68
+ return [...commonPlugins, ...serviceSpecificPlugins];
69
+ }
70
+
71
+ /**
72
+ * @function checkIfPluginsAreInstalled - to check if all plugins present in the common linter_configuration repository has been installed or not
73
+ * @returns {Object} - contains two properties namely a string which says no plugins present for the repository or an empty string and an array containing plugins to be installed
74
+ */
75
+ function checkIfPluginsAreInstalled() {
76
+ const pluginsForCurrentRepo = getAllPlugins();
77
+ let uninstalledPlugins = [];
78
+ let plugin = {
79
+ noPluginMessage: '',
80
+ uninstalledPlugins: []
81
+ };
82
+ let pluginsIndevDependencies = {};
83
+ let pluginNamesOfDevDependencyPlugins = [];
84
+ let arePluginsPresentIndevDependencies = false;
85
+ let arePluginsPresentForCurrentRepo = false;
86
+ if (existsSync(path.join(getNodeModulesPath(), 'package.json'))) {
87
+ let packageJsonContent = require(path.join(getNodeModulesPath(), 'package.json'));
88
+ pluginsIndevDependencies = packageJsonContent.devDependencies;
89
+ if (pluginsIndevDependencies !== undefined) {
90
+ arePluginsPresentIndevDependencies = true;
91
+ pluginNamesOfDevDependencyPlugins = Object.keys(pluginsIndevDependencies);
92
+ } else if (pluginsIndevDependencies === undefined) {
93
+ Logger.log(Logger.INFO_TYPE, 'No devDependencies present!');
94
+ }
95
+ } else {
96
+ arePluginsPresentIndevDependencies = false;
97
+ }
98
+ arePluginsPresentForCurrentRepo = pluginsForCurrentRepo.length !== 0 ? true : false;
99
+ if (arePluginsPresentForCurrentRepo || arePluginsPresentIndevDependencies) {
100
+ uninstalledPlugins = getUnInstalledPlugins(pluginsForCurrentRepo, pluginNamesOfDevDependencyPlugins, pluginsIndevDependencies);
101
+ plugin.noPluginMessage = '';
102
+ plugin.uninstalledPlugins = uninstalledPlugins;
103
+ return plugin;
104
+ } else {
105
+ plugin.noPluginMessage = 'No plugins present for the repository';
106
+ plugin.uninstalledPlugins = [];
107
+ return plugin;
108
+ }
109
+ }
110
+ module.exports = {
111
+ checkIfPluginsAreInstalled
112
+ };
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ const {
4
+ execSync
5
+ } = require('child_process');
6
+ const {
7
+ checkIfPluginsAreInstalled
8
+ } = require('./checkIfPluginsAreInstalled');
9
+ const {
10
+ executeSynchronizedCommands
11
+ } = require('../General/executeSyncCommands');
12
+ const {
13
+ Logger
14
+ } = require('../Logger/Logger');
15
+
16
+ /**
17
+ * @function installPlugins - installs uninstalled plugins for the project
18
+ * @returns {boolean} - indicating if plugins to be installed are installed successfully or not
19
+ */
20
+ function installPlugins() {
21
+ let uninstalledPlugins = checkIfPluginsAreInstalled();
22
+ let pluginsToBeInstalled = uninstalledPlugins.uninstalledPlugins;
23
+ if (pluginsToBeInstalled.length > 0) {
24
+ let installCommand = `npm install --save-dev ${pluginsToBeInstalled.join(' ')}`;
25
+ Logger.log(Logger.INFO_TYPE, 'Installing plugins ....');
26
+ Logger.log(Logger.INFO_TYPE, `Install command being executed: ${installCommand}`);
27
+ let arePluginsInstalledSuccessfully = executeSynchronizedCommands(execSync, [installCommand, {
28
+ stdio: 'inherit'
29
+ }], '', 'Some issue occured while installing plugins', false, true);
30
+ if (arePluginsInstalledSuccessfully) {
31
+ Logger.log(Logger.SUCCESS_TYPE, 'Plugins installation successful');
32
+ return true;
33
+ } else {
34
+ Logger.log(Logger.FAILURE_TYPE, "Unable to install plugins.Kindly retry the command");
35
+ return false;
36
+ }
37
+ } else {
38
+ Logger.log(Logger.INFO_TYPE, 'Plugins are already installed');
39
+ return true;
40
+ }
41
+ }
42
+
43
+ // function appendInstalledPluginsToPackageJson(pluginsToBeAppendedInPackageJson){
44
+ // let packageJsonFilePath = `${nodeModulesPathOfProject}/package.json`
45
+ // let packageJsonContent = require(packageJsonFilePath)
46
+ // let devDependencies = packageJsonContent.devDependencies
47
+ // let pluginsToBeAddedInDevDependencies = {}
48
+
49
+ // executeSynchronizedCommands(process.chdir,[nodeModulesPathOfProject],'','Unable to navigate to node_modules path when trying to revert changes in package.json',false,false)
50
+
51
+ // let gitDiffOutput = executeSynchronizedCommands(execSync,['git diff --name-only'],'','Unable to execute git diff command while checking if package.json is changed',true,false).toString().trim()
52
+ // let isPackageJsonChanged = gitDiffOutput.includes('package.json') ? true : false
53
+
54
+ // if(isPackageJsonChanged){
55
+ // let packageJsonChangesRevertCommand = `git checkout "${packageJsonFilePath}"`
56
+ // isPackageJsonChangesRevertedSuccessfully = executeSynchronizedCommands(execSync,[packageJsonChangesRevertCommand,{stdio:'inherit'}],'Changes in package.json reverted successfully','Unable to revert the changes in package.json file',false,true)
57
+
58
+ // if(isPackageJsonChangesRevertedSuccessfully){
59
+ // pluginsToBeAppendedInPackageJson.map(uninstalledPlugin => {
60
+ // if(uninstalledPlugin.startsWith('@')){
61
+ // let indexOfAtCharacter = uninstalledPlugin.indexOf('@')
62
+ // let indexOfSecondOccurenceOfAtCharacter = uninstalledPlugin.indexOf('@',indexOfAtCharacter + 1)
63
+ // let unInstalledPluginName = uninstalledPlugin.slice(0,indexOfSecondOccurenceOfAtCharacter)
64
+ // let unInstalledPluginVersion = uninstalledPlugin.slice(indexOfSecondOccurenceOfAtCharacter + 1)
65
+ // pluginsToBeAddedInDevDependencies[unInstalledPluginName] = unInstalledPluginVersion
66
+ // }
67
+ // else{
68
+ // pluginsToBeAddedInDevDependencies[uninstalledPlugin.split('@')[0]] = uninstalledPlugin.split('@')[1]
69
+ // }
70
+ // })
71
+ // let updatedPluginsIndevDependencies = {
72
+ // ...devDependencies,
73
+ // ...pluginsToBeAddedInDevDependencies
74
+ // }
75
+ // packageJsonContent.devDependencies = updatedPluginsIndevDependencies
76
+
77
+ // let modifiedPackageJson = {
78
+ // ...packageJsonContent,
79
+ // }
80
+ // let isPluginsAddedInPackageJson = executeSynchronizedCommands(writeFileSync,[packageJsonFilePath,JSON.stringify(modifiedPackageJson,null,2)],'Newly installed plugins successfully added to package.json','Unable to append installed plugins to package.json',false,true)
81
+ // return isPluginsAddedInPackageJson
82
+ // }
83
+ // else{
84
+ // return true
85
+ // }
86
+ // }
87
+ // }
88
+
89
+ module.exports = {
90
+ installPlugins
91
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ const {
4
+ Logger
5
+ } = require("../Logger/Logger");
6
+
7
+ /**
8
+ * @function printUninstalledPlugins - to notify the user regarding plugins for the repository has been installed or not
9
+ * @param {Array} uninstalledPlugins - contains list of uninstalled plugins for the project
10
+ */
11
+ function printUninstalledPlugins(uninstalledPlugins) {
12
+ let commandToInstallPlugins = 'npx ZDPrecommit setupPlugins';
13
+ if (uninstalledPlugins.uninstalledPlugins.length !== 0) {
14
+ Logger.log(Logger.INFO_TYPE, 'The following plugins are not installed:');
15
+ uninstalledPlugins.uninstalledPlugins.map(plugin => {
16
+ Logger.log(Logger.INFO_TYPE, `"${plugin}"`);
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`);
19
+ } else if (uninstalledPlugins.uninstalledPlugins.length === 0 && uninstalledPlugins.noPluginMessage.trim() === '') {
20
+ Logger.log(Logger.INFO_TYPE, 'Plugins are installed already');
21
+ } else if (uninstalledPlugins.uninstalledPlugins.length === 0 && uninstalledPlugins.noPluginMessage.trim() !== '') {
22
+ Logger.log(Logger.INFO_TYPE, 'No plugins present for the repository');
23
+ }
24
+ }
25
+ module.exports = {
26
+ printUninstalledPlugins
27
+ };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ const {
4
+ existsSync
5
+ } = require("fs");
6
+ const {
7
+ getRootDirectory
8
+ } = require('../General/RootDirectoryUtils/getRootDirectory');
9
+ const {
10
+ getNodeModulesPath
11
+ } = require('../General/getNodeModulesPath');
12
+ const path = require('path');
13
+ const fs = require('fs');
14
+ function getCustomPrecommitHookPath() {
15
+ return path.resolve(getNodeModulesPath(), "node_modules", "@zohodesk", "codestandard-validator", "bin", "execute.js");
16
+ }
17
+
18
+ /**
19
+ * @function updateGitFile - updates default precommit hook provided by git with the custom precommit hook file
20
+ * @returns {null}
21
+ */
22
+ function updateGitFile() {
23
+ const preCommit_File_Path_Default = path.resolve(getRootDirectory(), '.git', 'hooks', 'pre-commit.sample');
24
+ let preCommit_File_Path = path.join(path.dirname(preCommit_File_Path_Default), 'pre-commit');
25
+ if (existsSync(preCommit_File_Path_Default)) {
26
+ fs.writeFileSync(preCommit_File_Path_Default, `#!/bin/sh${getCustomPrecommitHookPath()}`);
27
+ fs.renameSync(preCommit_File_Path_Default, preCommit_File_Path);
28
+ return;
29
+ }
30
+ if (existsSync(preCommit_File_Path)) {
31
+ fs.appendFileSync(preCommit_File_Path, `\n${getCustomPrecommitHookPath()}`);
32
+ return;
33
+ }
34
+ }
35
+ module.exports = {
36
+ updateGitFile
37
+ };
package/changeLog.md CHANGED
@@ -4,6 +4,9 @@
4
4
  1. Testing version of the library
5
5
 
6
6
  # 0.0.1
7
+ - This version was mistakenly published without the build folder which will be corrected in the next release
8
+
9
+ # 0.0.2 - first stable release of the library
7
10
  1. Sets up a custom precommit hook using husky
8
11
  2. clones the common linter configuration repository in developer branch
9
12
  3. Based on the cloned configuration folder creates a eslint config file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/codestandard-validator",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "library to enforce code standard using eslint",
5
5
  "main": "index.js",
6
6
  "scripts": {