Skip to content

Commit

Permalink
Merge pull request #25 from bradgarropy/error
Browse files Browse the repository at this point in the history
🚨 error handling
  • Loading branch information
bradgarropy committed Jan 2, 2020
2 parents 9217210 + e04a366 commit dfaa29d
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 142 deletions.
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Brad Garropy
Copyright (c) 2020 Brad Garropy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "labman",
"version": "0.4.0",
"version": "0.4.1",
"description": "👨🏼‍🔬 github label manager cli",
"keywords": [
"github",
Expand Down Expand Up @@ -32,15 +32,15 @@
"start": "node src/cli/index.js"
},
"dependencies": {
"@octokit/rest": "^16.35.2",
"@octokit/rest": "^16.36.0",
"chalk": "^3.0.0",
"conf": "^6.2.0",
"yargs": "^15.0.2"
"yargs": "^15.1.0"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
"eslint": "^6.8.0",
"eslint-config-bradgarropy": "^1.2.0",
"eslint-config-bradgarropy": "^1.3.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-react-hooks": "^2.3.0",
Expand Down
70 changes: 52 additions & 18 deletions src/cli/default.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const conf = require("conf")
const chalk = require("chalk")
const copy = require("../copy")
const {createOctokit} = require("../octokit")
const {
errorTokenNotFound,
errorInvalidToken,
errorRepoNotFound,
} = require("../errors")
const {
createOctokit,
validToken,
validRepo,
getLabels,
deleteLabels,
createLabels,
} = require("../github")

const config = new conf()

Expand All @@ -21,29 +31,53 @@ const handler = async argv => {
const {source, destination, labels, clobber} = argv
const token = config.get("token")

// validate token
if (!token) {
console.log(
`\nYou are not logged in, please run the ${chalk.cyanBright(
"login",
)} command.\n`,
)
errorTokenNotFound()
return
}

const isValidToken = await validToken(token)

// validate token
if (!isValidToken) {
errorInvalidToken()
return
}

const isValidSource = await validRepo(source)

console.log(chalk.cyanBright("labman login <username> <token>\n"))
// validate source
if (!isValidSource) {
errorRepoNotFound(source)
return
}

const isValidDestination = await validRepo(destination)

// validate destination
if (!isValidDestination) {
errorRepoNotFound(destination)
return
}

createOctokit(token)

try {
await copy(source, destination, labels, clobber)
} catch (error) {
console.log(
`\n${chalk.redBright(
"Invalid token!",
)} Please run the ${chalk.cyanBright("login")} command again.\n`,
)
console.log(chalk.cyanBright("labman login <username> <token>\n"))
// delete existing labels
if (clobber) {
const oldLabels = await getLabels(destination)
await deleteLabels(oldLabels, destination)
}

// get new labels
const sourceLabels = await getLabels(source)

const newLabels = labels.length
? sourceLabels.filter(label => labels.includes(label.name))
: sourceLabels

// create new labels
await createLabels(newLabels, destination)
}

module.exports = {
Expand Down
Empty file modified src/cli/index.js
100644 → 100755
Empty file.
10 changes: 7 additions & 3 deletions src/cli/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const conf = require("conf")
const chalk = require("chalk")
const {validToken} = require("../github")
const {errorLoginFailed} = require("../errors")

const config = new conf()

Expand All @@ -21,19 +22,22 @@ const handler = async argv => {
const storedToken = config.get("token")

if (!force && storedToken) {
console.log("\nYou are already logged in!\n")
console.log()
console.log("You are already logged in!")
return
}

const valid = await validToken(token)

if (!valid) {
console.log(`\n${chalk.redBright("Login failed!")} Please try again.\n`)
errorLoginFailed()
return
}

config.set({username, token})
console.log(chalk.greenBright("\nLogin successful!\n"))

console.log()
console.log(chalk.greenBright("Login successful!"))

return
}
Expand Down
11 changes: 10 additions & 1 deletion src/cli/logout.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const conf = require("conf")
const chalk = require("chalk")

const config = new conf()

const command = "logout"
const description = "Remove GitHub credentials"
const builder = {}
const handler = () => config.clear()

const handler = () => {
config.clear()

console.log()
console.log(chalk.greenBright("Logout successful!"))

return
}

module.exports = {
command,
Expand Down
23 changes: 0 additions & 23 deletions src/copy.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const chalk = require("chalk")

const errorTokenNotFound = () => {
const commandText = chalk.cyanBright("login")

console.log()
console.log(`You are not logged in, please run the ${commandText} command.`)
console.log()
console.log(chalk.cyanBright("labman login <username> <token>"))
}

const errorInvalidToken = () => {
const errorText = chalk.redBright("Invalid token!")
const commandText = chalk.cyanBright("login")

console.log()
console.log(`${errorText} Please run the ${commandText} command again.`)
console.log()
console.log(chalk.cyanBright("labman login <username> <token>"))
}

const errorLoginFailed = () => {
const errorText = chalk.redBright("Login failed!")

console.log()
console.log(`${errorText} Please try again.`)
}

const errorRepoNotFound = repo => {
const repoText = chalk.bold.cyanBright(repo)
const errorText = chalk.bold.redBright(
`Repository ${repoText} does not exist!`,
)

console.log()
console.log(errorText)
}

const errorLabelExists = label => {
const labelText = chalk.bold.cyanBright(label)
const errorText = chalk.bold.redBright(
` x Label ${labelText} already exists!`,
)

console.log(errorText)
}

module.exports = {
errorTokenNotFound,
errorInvalidToken,
errorLoginFailed,
errorRepoNotFound,
errorLabelExists,
}
Loading

0 comments on commit dfaa29d

Please sign in to comment.