From 28a974c4d60eaf5d44804de2dc90b461c8b9e70f Mon Sep 17 00:00:00 2001 From: Zebiano Date: Mon, 25 Jul 2022 22:45:14 +0100 Subject: [PATCH] Update: Code polish --- README.md | 2 -- labeler.js | 3 +-- lib/axios.js | 18 +++++++----------- lib/configstore.js | 41 +++++++++++++---------------------------- lib/helper.js | 5 +---- lib/inquirer.js | 6 +++--- 6 files changed, 25 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 046b603..5d4b1f4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ - -
diff --git a/labeler.js b/labeler.js
index bdb2f93..ac656fb 100755
--- a/labeler.js
+++ b/labeler.js
@@ -190,8 +190,7 @@ async function main() {
             if (cli.flags.uploadLabels) await helper.uploadLabels(token, owner, host, repo, cli, false) // Upload custom labels to repository
         }
         echo.success('Finished!', true)
-    }
-    else {
+    } else {
         if (cli.flags.deleteAllLabels) await helper.deleteAllLabels(token, owner, host, repository, cli, true) // Delete all labels from repository
         if (cli.flags.uploadLabels) await helper.uploadLabels(token, owner, host, repository, cli, true) // Upload custom labels to repository
     }
diff --git a/lib/axios.js b/lib/axios.js
index fb53408..d703300 100644
--- a/lib/axios.js
+++ b/lib/axios.js
@@ -28,7 +28,7 @@ export function getLabels(exit, token, owner, host, repository) {
                 accept: 'application/vnd.github.v3+json'
             }
         })
-        .catch(function (err) {
+        .catch(err => {
             if (err.response?.status === 404) {
                 echo.error('404: Not found.')
                 echo.error(`URL seems to be faulty: ${url}`)
@@ -65,10 +65,8 @@ export function saveLabel(exit, token, owner, host, repository, label) {
                 accept: 'application/vnd.github.v3+json'
             }
         })
-        .then(function () {
-            echo.upload(label.name)
-        })
-        .catch(function (err) {
+        .then(() => echo.upload(label.name))
+        .catch(err => {
             if (err.response?.status === 404) {
                 echo.error('404: Not found.')
                 echo.error(`URL seems to be faulty: ${url}`)
@@ -97,10 +95,8 @@ export function deleteLabel(exit, token, label) {
                 accept: 'application/vnd.github.v3+json'
             }
         })
-        .then(function () {
-            echo.remove(label.name)
-        })
-        .catch(function (err) {
+        .then(() => echo.remove(label.name))
+        .catch(err => {
             if (err.response?.status === 401) {
                 echo.error('401: Unauthorized.')
                 echo.error('Token has probably expired.')
@@ -126,7 +122,7 @@ export function headRepoList(exit, token, owner, host, perPage) {
                 accept: 'application/vnd.github.v3+json'
             }
         })
-        .catch(function (err) {
+        .catch(err => {
             if (err.response?.status === 404) {
                 echo.error('404: Not found.')
                 echo.error(`URL seems to be faulty: ${url}`)
@@ -155,7 +151,7 @@ export function getReposByPage(exit, token, owner, host, pageNum, perPage) {
                 accept: 'application/vnd.github.v3+json'
             }
         })
-        .catch(function (err) {
+        .catch(err => {
             if (err.response?.status === 404) {
                 echo.error('404: Not found.')
                 echo.error(`URL seems to be faulty: ${url}`)
diff --git a/lib/configstore.js b/lib/configstore.js
index 7137a0f..4d64af0 100644
--- a/lib/configstore.js
+++ b/lib/configstore.js
@@ -15,30 +15,23 @@ const labelsConfig = new Configstore(`${pkg.name}_labels`)
 // Check for key in config
 export function has(type, key) {
     switch (type) {
-        case 'config':
-            if (config.has(key)) return true
-            return false
-        case 'labels':
-            if (labelsConfig.has(key)) return true
-            return false
+        case 'config': return config.has(key)
+        case 'labels': return labelsConfig.has(key)
     }
 }
 
 // Get key
 export function get(type, key) {
     switch (type) {
-        case 'config':
-            return config.get(key)
-        case 'labels':
-            return labelsConfig.get(key)
+        case 'config': return config.get(key)
+        case 'labels': return labelsConfig.get(key)
     }
 }
 
 // Get all
 export function getAll(type) {
     switch (type) {
-        case 'config':
-            return config.all
+        case 'config': return config.all
         case 'labels':
             if (!has('labels', 'labels')) set('labels', { 'labels': defaultLabels })
             return labelsConfig.get('labels')
@@ -48,30 +41,24 @@ export function getAll(type) {
 // Set new key
 export function set(type, object) {
     switch (type) {
-        case 'config':
-            config.set(object)
-        case 'labels':
-            labelsConfig.set(object)
+        case 'config': config.set(object)
+        case 'labels': labelsConfig.set(object)
     }
 }
 
 // Delete key
 export function remove(type, key) {
     switch (type) {
-        case 'config':
-            config.delete(key)
-        case 'labels':
-            labelsConfig.delete(key)
+        case 'config': config.delete(key)
+        case 'labels': labelsConfig.delete(key)
     }
 }
 
 // Delete all items
 export function clear(type) {
     switch (type) {
-        case 'config':
-            config.clear()
-        case 'labels':
-            labelsConfig.clear()
+        case 'config': config.clear()
+        case 'labels': labelsConfig.clear()
     }
 }
 
@@ -83,9 +70,7 @@ export function resetLabels() {
 // Get path
 export function path(type) {
     switch (type) {
-        case 'config':
-            return config.path
-        case 'labels':
-            return labelsConfig.path
+        case 'config': return config.path
+        case 'labels': return labelsConfig.path
     }
 }
diff --git a/lib/helper.js b/lib/helper.js
index 1db1bf9..5cfdb74 100644
--- a/lib/helper.js
+++ b/lib/helper.js
@@ -127,7 +127,6 @@ export async function uploadLabels(token, owner, host, repository, cli, exit) {
         const answer = await inquirer.confirmUploadLabels(repository)
         if (!answer.uploadLabels) {
             if (cli.flags.bulkUpdate) return
-
             console.log()
             echo.abort(`Upload labels to ${repository}.`, true)
         }
@@ -158,7 +157,6 @@ export async function deleteAllLabels(token, owner, host, repository, cli, exit)
         const answer = await inquirer.confirmDeleteAllLabels(repository)
         if (!answer.deleteAllLabels) {
             if (cli.flags.bulkUpdate) return
-
             console.log()
             echo.abort(`Delete labels from ${repository}.`, true)
         }
@@ -203,13 +201,12 @@ export async function getRepositories(token, owner, host) {
     // To compute the actual number of necessary requests, divide the number of repos by the requested per_page limit (max 100)
     const numberOfRepos = getPageCountFromLinkHeader(response.headers.link)
     pageCount = Math.ceil(numberOfRepos / perPage)
-
     echo.info(Chalk.bold(`Fetching list of ${numberOfRepos} repository name(s) across ${pageCount} page(s), in organization ${owner}...`))
 
     // Push every repo into repos array
     for (let i = 1; i <= pageCount; i++) {
         const res = await axios.getReposByPage(true, token, owner, host, i, perPage)
-        res.data.forEach(function (repo) {
+        res.data.forEach(repo => {
             // Get the url for each repo object and extract just the name at the end of the url
             repos.push(repo.html_url.substring(repo.html_url.lastIndexOf('/') + 1))
         })
diff --git a/lib/inquirer.js b/lib/inquirer.js
index 1647ab5..42dd86c 100644
--- a/lib/inquirer.js
+++ b/lib/inquirer.js
@@ -37,7 +37,7 @@ const inputLabelName = {
     type: 'input',
     name: 'name',
     message: 'Enter Label name: ',
-    validate: function (value) {
+    validate: value => {
         if (value.length) return true
         else return 'Please enter a valid Label name. For example "Bug".'
     }
@@ -55,11 +55,11 @@ const inputLabelColor = {
     type: 'input',
     name: 'color',
     message: 'Enter Label Color:',
-    validate: function (value) {
+    validate: value => {
         if (value.length && /^([A-Fa-f0-9]{6})$/.test(value)) return true
         else return 'Please enter a valid Hex color. For example "D2DAE1".'
     },
-    transformer: function (color) { return `${Chalk.bgHex(`#${color}`)('  ')} ${color}` }
+    transformer: color => { return `${Chalk.bgHex(`#${color}`)('  ')} ${color}` }
 }
 
 /* --- List --- */