-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Create config directory and file if not exist, exit with code 0 if …
…already exist.
- Loading branch information
Showing
1 changed file
with
39 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |