UNPKG

quality-bot

Version:

QBot Online Code Review Application

101 lines (89 loc) 4.2 kB
const simpleGit = require('simple-git'); const fs = require('fs') const path = require("path"); const colors = require('./../common/colors'); const messageService = require('./message-service'); const commonService = require('./common-service'); const documentService = require('./document-service'); const readlineSync = require('readline-sync'); const localPrecommitPath= process.cwd() + "/.git/hooks"; const os = require('os'); const { exec } = require("child_process"); const git = simpleGit(); const configure = async () => { const fColor = colors.FgWhite; // Qbot Token With validation let tokenFromUser = readlineSync.question("Enter your QBot token: ", { hideEchoBack: true }); if (!tokenFromUser) { processError("ERROR! QBot token is must, Kindly enter the token"); } else { const availableHooksPath = await commonService.getExistingCoreHooksPath(); const availableLocalHooksPath = await commonService.getExistingLocalCoreHooksPath(); const availableGlobalHooksPath = await commonService.getExistingGlobalCoreHooksPath(); if (availableHooksPath) { if (availableLocalHooksPath && availableGlobalHooksPath) { await generateHooksFile(availableLocalHooksPath, tokenFromUser, 'local', false); } else if (availableLocalHooksPath) { await generateHooksFile(availableLocalHooksPath, tokenFromUser, 'local', false); } else if (availableGlobalHooksPath) { await generateHooksFile(availableGlobalHooksPath, tokenFromUser, 'global', false); } } else { let flag = readlineSync.question("Do you want to Configure this in local or global level(type 'local' or 'global' default is 'global'): "); if (tokenFromUser) { if (flag && flag === 'local') { console.log("Your precommit file path is : ", `${localPrecommitPath}`); generateHooksFile(localPrecommitPath, tokenFromUser, 'local', true); } else { let hooksPath = readlineSync.question("Enter any path to configure the core hooks: "); await generateHooksFile(hooksPath, tokenFromUser, 'global', false); } } } } } const generateHooksFile = async (hooksPath, tokenFromUser, flag, defalutFile) => { if (!hooksPath) { processError("ERROR! Core hooks path is must, Kindly enter the core hooks path"); } else { if (!fs.existsSync(hooksPath)) { processError("ERROR! Core hooks path is invalid or not exist in your system, Kindly provide the correct path"); } else { console.log(); // Local or Global Configuration if (defalutFile && flag === 'local') { await git.addConfig('user.qbot-token-local', tokenFromUser, append = false, scope = 'local'); } else if (flag === 'local') { await git.addConfig('user.qbot-token-local', tokenFromUser, append = false, scope = 'local'); await git.addConfig('core.hookspath', hooksPath, append = false, scope = 'local'); } else { await git.addConfig('core.hookspath', hooksPath, append = false, scope = 'global'); await git.addConfig('user.qbot-token-global', tokenFromUser, append = false, scope = 'global'); } documentService.createPreCommitFile(hooksPath); messageService.configurationSuccess(hooksPath, flag); if(os.platform() === "linux"){ await provideExecuteAccess(hooksPath); } process.exit(); } } } const processError = (errorMessage) => { messageService.configurationError(errorMessage); process.exit(1); } const provideExecuteAccess = async (hooksPath) => { exec("chmod 777 " + hooksPath+"/pre-commit", (error, data, getter) => { if (error) { console.log("Error occured while providing execute access to precommit file", error.message); return; } }); } module.exports = { configure }