ryanhex53 / gpt-po

command tool for translate po files by using openai api
71 stars 13 forks source link

Use current project path instead of home directory for config files #1

Closed magicoli closed 1 year ago

magicoli commented 1 year ago

gpt-po uses home directory as base instead of the git repo I am currently in, or the current working directory to save systemprompt and dictionary. That makes customizations saved globally instead of specific to each project.

To be useable in a multi projects context, they should be saved in the git repo if found, or in the current directory, again, as hidden files or in a hidden folder (translation is rarely "the" project, it's a feature added to it).

To at least use these file if they exist, without altering your current behaviour otherwise, something like this could do the trick:

// ...
import { homedir } from "os";
import { join } from "path";
import fs from "fs";
import gitRootDir from "git-root-dir";

// ...

const getFirstFound = (fileName) => {
  const currentDir = process.cwd();
  const gitRoot = gitRootDir() || currentDir;
  const home = homedir();

  const filePaths = [
    join(currentDir, ".gpt-po", fileName), // (current directory)/.gpt-po/
    join(gitRoot, ".gpt-po", fileName), // (git repository root directory)/.gpt-po/
    join(home, ".gpt-po", fileName), // ~/.gpt-po/
    join("/etc/gpt-po", fileName), // /etc/gpt-po/
  ];

  for (const filePath of filePaths) {
    if (fs.existsSync(filePath)) {
      return filePath;
    }
  }

  // Return join(home, fileName) as fallback if nothing is found
  return join(home, fileName);
};

// ... (Rest of the code)
//
//     const promptHome = getFirstFound("systemprompt.txt");
//
// ... (Rest of the code)

As I have multiple projects (and translation management is not supposed to be included in them), I installed gpt-po globally, but I saw in the code that homedir() was used to save the files anyway.

And IMHO an application should never save config as visible files in home directory anyway (particularly with names as vague as "dictionary.json"). This is a place we have a hard time to maintain organized, so global config and preference files should be saved as dot files (.gpt-po/systemprompt) or in the standard ~/.config/ directory (~/.config/gpt-po/systemprompt).

That said, it's a great project, it helps me a lot.

ryanhex53 commented 1 year ago

The new version of the code (1.0.6) has implemented the corresponding features based on your recommendations. Please check.

magicoli commented 1 year ago

Great! It works like a charm in 1.0.7. Thank you for the inclusion.