Skip to content

Commit

Permalink
✨ Create config directory and file if not exist, exit with code 0 if …
Browse files Browse the repository at this point in the history
…already exist.
  • Loading branch information
JohannLai committed Mar 21, 2023
1 parent c7946b6 commit dc97183
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions scripts/createConfig.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import fs from 'fs'
import os from 'os'
import path from 'path'

const homeDir =
process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME']
const homeDir = os.homedir()

const configDir = path.join(homeDir, '.config', 'gpt-cli')
const pluginsDir = path.join(configDir, 'plugins')
const configFile = path.join(configDir, '.gptrc')

// if not config directory, create it
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true })
}
try {
// if not config directory, create it
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true })
}

// if not plugin directory, create it
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true })
}
// if not plugin directory, create it
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true })
}

// if exits, do nothing, process exits with code 0
if (fs.existsSync(configFile)) {
process.exit(0)
}
// if exits, do nothing, process exits with code 0
if (fs.existsSync(configFile)) {
process.exit(0)
}

fs.writeFileSync(configFile, '', { mode: 0o600 })

console.log(`${configDir} directory and ${configFile} file were created.`)
} catch (e) {
console.error(`
Error occurred while creating config directory and file.
${e}
fs.writeFileSync(configFile, '', { mode: 0o600 })
`)

console.log(`${configDir} directory and ${configFile} file were created.`)
console.log(`
Please create the following directory and file manually:
mkdir -p ${configDir}
mkdir -p ${pluginsDir}
touch ${configFile}
if cannot fix the issue, please open an issue on GitHub:
https://github.com/JohannLai/gptcli/issues/new , thanks!
`)

process.exit(0)
}

0 comments on commit dc97183

Please sign in to comment.