gpt-po 1.0.6 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {
package/lib/src/index.js CHANGED
@@ -11,8 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  const commander_1 = require("commander");
14
- const os_1 = require("os");
15
- const path_1 = require("path");
16
14
  const pkg = require("../package.json");
17
15
  const sync_1 = require("./sync");
18
16
  const translate_1 = require("./translate");
@@ -48,6 +46,11 @@ program
48
46
  if (key) {
49
47
  process.env.OPENAI_API_KEY = key;
50
48
  }
49
+ // process.env.OPENAI_API_KEY is not set, exit
50
+ if (!process.env.OPENAI_API_KEY) {
51
+ console.error("OPENAI_API_KEY is required");
52
+ process.exit(1);
53
+ }
51
54
  (0, translate_1.init)();
52
55
  if (po) {
53
56
  yield (0, translate_1.translatePo)(model, po, source, lang, verbose, output);
@@ -72,13 +75,18 @@ program
72
75
  program
73
76
  .command("systemprompt")
74
77
  .description("open/edit system prompt")
75
- .action(() => {
78
+ .option("--reset", "reset system prompt to default")
79
+ .action((args) => {
80
+ const { reset } = args;
76
81
  // open `systemprompt.txt` file by system text default editor
77
82
  const copyFile = __dirname + "/systemprompt.txt";
78
83
  // user home path
79
- const promptHome = (0, path_1.join)((0, os_1.homedir)(), "systemprompt.txt");
80
- (0, utils_1.copyFileIfNotExists)(promptHome, copyFile);
81
- (0, utils_1.openFileByDefault)(promptHome);
84
+ const promptFile = (0, utils_1.findConfig)("systemprompt.txt");
85
+ (0, utils_1.copyFileIfNotExists)(promptFile, copyFile, reset);
86
+ if (reset) {
87
+ console.log("systemprompt.txt reset to default");
88
+ }
89
+ (0, utils_1.openFileByDefault)(promptFile);
82
90
  });
83
91
  // program command `userdict` with help text `open/edit user dictionary`
84
92
  program
@@ -88,9 +96,9 @@ program
88
96
  // open `dictionary.json` file by system text default editor
89
97
  const copyFile = __dirname + "/dictionary.json";
90
98
  // user home path
91
- const dictHome = (0, path_1.join)((0, os_1.homedir)(), "dictionary.json");
92
- (0, utils_1.copyFileIfNotExists)(dictHome, copyFile);
93
- (0, utils_1.openFileByDefault)(dictHome);
99
+ const dictFile = (0, utils_1.findConfig)("dictionary.json");
100
+ (0, utils_1.copyFileIfNotExists)(dictFile, copyFile);
101
+ (0, utils_1.openFileByDefault)(dictFile);
94
102
  });
95
103
  program.parse(process.argv);
96
104
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- You are a translation expert, follow the user's instructions carefully, translate the text in a colloquial, professional and elegant manner without sounding like a machine translation. Remember to only translate the text and not further interpret it.
1
+ You are a translation expert, translate the text in a colloquial, professional and elegant manner without sounding like a machine translation. Remember to only translate the text and not interpret it further.
@@ -54,14 +54,9 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
54
54
  model,
55
55
  temperature: 0.1,
56
56
  messages: [
57
- { role: "system", content: _systemprompt },
58
57
  {
59
- role: "user",
60
- content: `Translate the ${src} content I will post later into ${lang}, and keep the untranslated parts such as symbols in the result.`,
61
- },
62
- {
63
- role: "assistant",
64
- content: "Sure, Please send me the content that needs to be translated.",
58
+ role: "system",
59
+ content: _systemprompt + ` Translate the ${src} user content into ${lang} language. Please translate it as a text, not as a table. The parts that cannot be translated will retain their original format.`
65
60
  },
66
61
  // add userdict here
67
62
  ...dicts,
@@ -136,6 +131,9 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
136
131
  }
137
132
  else {
138
133
  console.error(error.message);
134
+ if (error.code == "ECONNABORTED") {
135
+ console.log('you may need to set "HTTPS_PROXY" to reach openai api.');
136
+ }
139
137
  }
140
138
  }
141
139
  }
@@ -3,8 +3,9 @@ import { GetTextTranslations } from "gettext-parser";
3
3
  * copy source file to destination file if destination file does not exist
4
4
  * @param file destination file path
5
5
  * @param copyFile source file path
6
+ * @param force force copy file
6
7
  */
7
- export declare function copyFileIfNotExists(file: string, copyFile: string): void;
8
+ export declare function copyFileIfNotExists(file: string, copyFile: string, force?: boolean): void;
8
9
  export declare function openFileByDefault(filePath: string): void;
9
10
  export declare function parsePo(poFile: string, defaultCharset?: string): Promise<GetTextTranslations>;
10
11
  export declare function compilePo(data: GetTextTranslations, poFile: string): Promise<void>;
package/lib/src/utils.js CHANGED
@@ -10,15 +10,18 @@ const path = require("path");
10
10
  * copy source file to destination file if destination file does not exist
11
11
  * @param file destination file path
12
12
  * @param copyFile source file path
13
+ * @param force force copy file
13
14
  */
14
- function copyFileIfNotExists(file, copyFile) {
15
+ function copyFileIfNotExists(file, copyFile, force = false) {
15
16
  // make sure the directory exists
16
17
  fs.mkdirSync(path.dirname(file), { recursive: true });
17
18
  // check if file exists else create it
18
19
  try {
19
20
  fs.accessSync(file, fs.constants.F_OK);
20
- // check if the file is empty else copy the file
21
- fs.statSync(file).size === 0 && fs.copyFileSync(copyFile, file);
21
+ // check if the file is empty or force, copy the file
22
+ if (force || fs.statSync(file).size === 0) {
23
+ fs.copyFileSync(copyFile, file);
24
+ }
22
25
  }
23
26
  catch (err) {
24
27
  fs.copyFileSync(copyFile, file);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {