Skip to content

Commit

Permalink
fix: fixing user key storage system in auth command
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomart97 committed Mar 25, 2023
1 parent dd5df21 commit e33a32f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 0 additions & 1 deletion config.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"axios": "^1.3.4",
"commander": "^10.0.0",
"global-dirs": "^3.0.1",
"kleur": "^4.1.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/translate/translateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export const translateAction = async (options: GeneralOptions) => {

const openAIChatResponse = await openAIChat(params)

console.log(`\n> ${green(openAIChatResponse.data)}`)
console.log(`\n${green(openAIChatResponse.data)}`)
}
19 changes: 16 additions & 3 deletions src/helpers/authSystem.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import path from 'path'
import fs from 'fs'
import globalDirs from 'global-dirs'
import { red } from 'kleur/colors'

interface Config {
apiKey: string
}

const readConfig = (): Config => {
try {
const data = fs.readFileSync('config.json', 'utf8')
const packageDir = path.join(globalDirs.npm.packages, 'zonix')
const configPath = path.join(packageDir, 'config.json')
const data = fs.readFileSync(configPath, 'utf8')
return JSON.parse(data)
} catch {
return { apiKey: '' }
Expand All @@ -15,9 +20,17 @@ const readConfig = (): Config => {

const writeConfig = (config: Config) => {
try {
fs.writeFileSync('config.json', JSON.stringify(config))
const packageDir = path.join(globalDirs.npm.packages, 'zonix')
const configPath = path.join(packageDir, 'config.json')
const configDir = path.dirname(configPath)

if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true })
}

fs.writeFileSync(configPath, JSON.stringify(config))
} catch (error) {
console.error(`Failed to write config file: ${error}`)
console.log(red(`\nFailed to write config file: ${error}`))
}
}

Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

global-dirs@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-3.0.1.tgz#0c488971f066baceda21447aecb1a8b911d22485"
integrity sha512-NBcGGFbBA9s1VzD41QXDG+3++t9Mn5t1FpLdhESY6oKY4gYTFpX4wO3sqGUa0Srjtbfj3szX0RnemmrVRUdULA==
dependencies:
ini "2.0.0"

globals@^13.19.0:
version "13.20.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-13.20.0.tgz#ea276a1e508ffd4f1612888f9d1bad1e2717bf82"
Expand Down Expand Up @@ -456,6 +463,11 @@ inherits@2:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ini/-/ini-2.0.0.tgz#e5fd556ecdd5726be978fa1001862eacb0a94bc5"
integrity sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA==

is-extglob@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
Expand Down

0 comments on commit e33a32f

Please sign in to comment.