createConfig.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import fs from 'fs'
  2. import os from 'os'
  3. import path from 'path'
  4. const homeDir = os.homedir()
  5. const configDir = path.join(homeDir, '.config', 'gpt-cli')
  6. const pluginsDir = path.join(configDir, 'plugins')
  7. const configFile = path.join(configDir, '.gptrc')
  8. try {
  9. // if not config directory, create it
  10. if (!fs.existsSync(configDir)) {
  11. fs.mkdirSync(configDir, { recursive: true })
  12. }
  13. // if not plugin directory, create it
  14. if (!fs.existsSync(pluginsDir)) {
  15. fs.mkdirSync(pluginsDir, { recursive: true })
  16. }
  17. // if exits, do nothing, process exits with code 0
  18. if (fs.existsSync(configFile)) {
  19. process.exit(0)
  20. }
  21. fs.writeFileSync(configFile, '', { mode: 0o600 })
  22. console.log(`${configDir} directory and ${configFile} file were created.`)
  23. } catch (e) {
  24. console.error(`
  25. Error occurred while creating config directory and file.
  26. ${e}
  27. `)
  28. console.log(`
  29. Please create the following directory and file manually:
  30. mkdir -p ${configDir}
  31. mkdir -p ${pluginsDir}
  32. touch ${configFile}
  33. if cannot fix the issue, please open an issue on GitHub:
  34. https://github.com/JohannLai/gptcli/issues/new , thanks!
  35. `)
  36. process.exit(0)
  37. }