Skip to content

Commit

Permalink
Update: Code polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebiano committed Jul 25, 2022
1 parent e4196a2 commit 28a974c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 50 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<!-- TODO: Spaces 2 -->
<!-- TODO: Change all functions to arrow functions -->
<!-- TODO: Make code cleaner -->
<div align="center">
<!-- <img src="Stuff/AppIcon-readme.png" width="200" height="200"> -->
<pre>
Expand Down
3 changes: 1 addition & 2 deletions labeler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 7 additions & 11 deletions lib/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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.')
Expand All @@ -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}`)
Expand Down Expand Up @@ -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}`)
Expand Down
41 changes: 13 additions & 28 deletions lib/configstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
}
}

Expand All @@ -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
}
}
5 changes: 1 addition & 4 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
})
Expand Down
6 changes: 3 additions & 3 deletions lib/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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".'
}
Expand All @@ -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 --- */
Expand Down

0 comments on commit 28a974c

Please sign in to comment.