@intlayer/cli 3.5.4 → 3.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/audit.cjs +109 -0
- package/dist/cjs/audit.cjs.map +1 -0
- package/dist/cjs/cli.cjs +1 -1
- package/dist/cjs/cli.cjs.map +1 -1
- package/dist/cjs/config.cjs +1 -1
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/listContentDeclaration.cjs +1 -1
- package/dist/cjs/listContentDeclaration.cjs.map +1 -1
- package/dist/cjs/pull.cjs +8 -5
- package/dist/cjs/pull.cjs.map +1 -1
- package/dist/cjs/push.cjs +21 -12
- package/dist/cjs/push.cjs.map +1 -1
- package/dist/esm/audit.mjs +74 -0
- package/dist/esm/audit.mjs.map +1 -0
- package/dist/esm/cli.mjs +1 -1
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/config.mjs +2 -2
- package/dist/esm/config.mjs.map +1 -1
- package/dist/esm/listContentDeclaration.mjs +2 -2
- package/dist/esm/listContentDeclaration.mjs.map +1 -1
- package/dist/esm/pull.mjs +9 -6
- package/dist/esm/pull.mjs.map +1 -1
- package/dist/esm/push.mjs +22 -13
- package/dist/esm/push.mjs.map +1 -1
- package/dist/types/{audit/index.d.ts → audit.d.ts} +2 -2
- package/dist/types/audit.d.ts.map +1 -0
- package/dist/types/push.d.ts.map +1 -1
- package/package.json +10 -11
- package/dist/cjs/audit/CJS_FORMAT.md +0 -23
- package/dist/cjs/audit/JSON_FORMAT.md +0 -26
- package/dist/cjs/audit/JSX_FORMAT.md +0 -28
- package/dist/cjs/audit/MJS_FORMAT.md +0 -23
- package/dist/cjs/audit/PROMPT.md +0 -146
- package/dist/cjs/audit/TSX_FORMAT.md +0 -28
- package/dist/cjs/audit/TS_FORMAT.md +0 -22
- package/dist/cjs/audit/index.cjs +0 -131
- package/dist/cjs/audit/index.cjs.map +0 -1
- package/dist/esm/audit/CJS_FORMAT.md +0 -23
- package/dist/esm/audit/JSON_FORMAT.md +0 -26
- package/dist/esm/audit/JSX_FORMAT.md +0 -28
- package/dist/esm/audit/MJS_FORMAT.md +0 -23
- package/dist/esm/audit/PROMPT.md +0 -146
- package/dist/esm/audit/TSX_FORMAT.md +0 -28
- package/dist/esm/audit/TS_FORMAT.md +0 -22
- package/dist/esm/audit/index.mjs +0 -96
- package/dist/esm/audit/index.mjs.map +0 -1
- package/dist/types/audit/index.d.ts.map +0 -1
package/dist/esm/audit/index.mjs
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "fs";
|
|
2
|
-
import { join, relative } from "path";
|
|
3
|
-
import { getConfiguration } from "@intlayer/config";
|
|
4
|
-
import { getLocaleName } from "@intlayer/core";
|
|
5
|
-
import { OpenAI } from "openai";
|
|
6
|
-
import pLimit from "p-limit";
|
|
7
|
-
import { getContentDeclaration } from '../listContentDeclaration.mjs';
|
|
8
|
-
const projectPath = process.cwd();
|
|
9
|
-
const getFileContent = (filePath) => {
|
|
10
|
-
const fileContent = readFileSync(filePath, "utf-8");
|
|
11
|
-
return fileContent;
|
|
12
|
-
};
|
|
13
|
-
const writeFileContent = (filePath, content) => {
|
|
14
|
-
writeFileSync(filePath, content);
|
|
15
|
-
};
|
|
16
|
-
const getAbsolutePath = (filePath) => {
|
|
17
|
-
const absolutePath = join(__dirname, filePath);
|
|
18
|
-
return absolutePath;
|
|
19
|
-
};
|
|
20
|
-
const FILE_TEMPLATE = {
|
|
21
|
-
ts: getFileContent(getAbsolutePath("./TS_FORMAT.md")),
|
|
22
|
-
tsx: getFileContent(getAbsolutePath("./TSX_FORMAT.md")),
|
|
23
|
-
js: getFileContent(getAbsolutePath("./MJS_FORMAT.md")),
|
|
24
|
-
mjs: getFileContent(getAbsolutePath("./MJS_FORMAT.md")),
|
|
25
|
-
cjs: getFileContent(getAbsolutePath("./CJS_FORMAT.md")),
|
|
26
|
-
jsx: getFileContent(getAbsolutePath("./JSX_FORMAT.md")),
|
|
27
|
-
json: getFileContent(getAbsolutePath("./JSON_FORMAT.md"))
|
|
28
|
-
};
|
|
29
|
-
const CHAT_GPT_PROMPT = getFileContent(getAbsolutePath("./PROMPT.md"));
|
|
30
|
-
const formatLocaleWithName = (locale) => {
|
|
31
|
-
const localeName = getLocaleName(locale);
|
|
32
|
-
return `${locale}: ${localeName}`;
|
|
33
|
-
};
|
|
34
|
-
const auditFile = async (filePath, options) => {
|
|
35
|
-
try {
|
|
36
|
-
const relativePath = relative(projectPath, filePath);
|
|
37
|
-
console.info(`Auditing file: ${relativePath}`);
|
|
38
|
-
const { internationalization } = getConfiguration();
|
|
39
|
-
const { defaultLocale, locales } = internationalization;
|
|
40
|
-
const openai = new OpenAI({
|
|
41
|
-
apiKey: options?.openAiApiKey
|
|
42
|
-
});
|
|
43
|
-
const fileContent = getFileContent(filePath);
|
|
44
|
-
const splitted = filePath.split(".");
|
|
45
|
-
const fileExtension = splitted[splitted.length - 1];
|
|
46
|
-
const prompt = options?.customPrompt ?? CHAT_GPT_PROMPT.replace("{{filePath}}", filePath).replace(
|
|
47
|
-
"{{defaultLocale}}",
|
|
48
|
-
`{${formatLocaleWithName(defaultLocale)}}`
|
|
49
|
-
).replace(
|
|
50
|
-
"{{otherLocales}}",
|
|
51
|
-
`{${locales.map(formatLocaleWithName).join(", ")}}`
|
|
52
|
-
).replace(
|
|
53
|
-
"{{declarationsContentTemplate}}",
|
|
54
|
-
FILE_TEMPLATE[fileExtension]
|
|
55
|
-
).replace("{{fileContent}}", fileContent);
|
|
56
|
-
const chatCompletion = await openai.chat.completions.create({
|
|
57
|
-
model: options?.model ?? "gpt-4o-mini",
|
|
58
|
-
messages: [{ role: "system", content: prompt }]
|
|
59
|
-
});
|
|
60
|
-
const newContent = chatCompletion.choices[0].message?.content;
|
|
61
|
-
if (newContent) {
|
|
62
|
-
writeFileContent(filePath, newContent);
|
|
63
|
-
console.info(`${options?.logPrefix ?? ""}File ${relativePath} updated`);
|
|
64
|
-
}
|
|
65
|
-
console.info(
|
|
66
|
-
`${options?.logPrefix ?? ""}${chatCompletion.usage?.total_tokens} tokens used in the request`
|
|
67
|
-
);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error(error);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
const audit = async (options) => {
|
|
73
|
-
if (!options?.openAiApiKey) {
|
|
74
|
-
console.error(
|
|
75
|
-
"OpenAI API key is required to audit the content declaration files."
|
|
76
|
-
);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
let contentDeclarationFilesList = options?.files;
|
|
80
|
-
if (!contentDeclarationFilesList) {
|
|
81
|
-
const contentDeclarationFilesPath = getContentDeclaration({
|
|
82
|
-
exclude: options?.excludedGlobs
|
|
83
|
-
});
|
|
84
|
-
contentDeclarationFilesList = contentDeclarationFilesPath;
|
|
85
|
-
}
|
|
86
|
-
const limit = pLimit(options?.asyncLimit ? Number(options?.asyncLimit) : 5);
|
|
87
|
-
const pushPromises = contentDeclarationFilesList.map(
|
|
88
|
-
(filePath) => limit(() => auditFile(filePath, options))
|
|
89
|
-
);
|
|
90
|
-
await Promise.all(pushPromises);
|
|
91
|
-
};
|
|
92
|
-
export {
|
|
93
|
-
audit,
|
|
94
|
-
auditFile
|
|
95
|
-
};
|
|
96
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/audit/index.ts"],"sourcesContent":["import { readFileSync, writeFileSync } from 'fs';\nimport { join, relative } from 'path';\nimport { getConfiguration, Locales } from '@intlayer/config';\nimport { getLocaleName } from '@intlayer/core';\nimport { OpenAI } from 'openai';\nimport pLimit from 'p-limit';\nimport { getContentDeclaration } from '../listContentDeclaration';\n\n// Depending on your implementation, you may need the OpenAI API client.\n// For instance, you can use `openai` npm package (https://www.npmjs.com/package/openai).\n\ntype AuditOptions = {\n files?: string[];\n logPrefix?: string;\n model?: string;\n customPrompt?: string;\n asyncLimit?: number;\n openAiApiKey?: string;\n excludedGlobs?: string[];\n};\n\nconst projectPath = process.cwd();\n\n/**\n * Reads the content of a file synchronously.\n *\n * @function\n * @param filePath - The relative or absolute path to the target file.\n * @returns The entire contents of the specified file as a UTF-8 encoded string.\n */\nconst getFileContent = (filePath: string): string => {\n // Read the file content using Node.js fs module.\n const fileContent = readFileSync(filePath, 'utf-8');\n return fileContent;\n};\n\nconst writeFileContent = (filePath: string, content: string) => {\n writeFileSync(filePath, content);\n};\n\nconst getAbsolutePath = (filePath: string): string => {\n const absolutePath = join(__dirname, filePath);\n\n return absolutePath;\n};\n\nconst FILE_TEMPLATE: Record<string, string> = {\n ts: getFileContent(getAbsolutePath('./TS_FORMAT.md')),\n tsx: getFileContent(getAbsolutePath('./TSX_FORMAT.md')),\n js: getFileContent(getAbsolutePath('./MJS_FORMAT.md')),\n mjs: getFileContent(getAbsolutePath('./MJS_FORMAT.md')),\n cjs: getFileContent(getAbsolutePath('./CJS_FORMAT.md')),\n jsx: getFileContent(getAbsolutePath('./JSX_FORMAT.md')),\n json: getFileContent(getAbsolutePath('./JSON_FORMAT.md')),\n};\n\n// The prompt template to send to ChatGPT, requesting an audit of content declaration files.\nconst CHAT_GPT_PROMPT = getFileContent(getAbsolutePath('./PROMPT.md'));\n\n/**\n * Formats a locale with its full name and returns a string representation.\n *\n * @function\n * @param locale - A locale from the project's configuration (e.g., 'en-US', 'fr-FR').\n * @returns A formatted string combining the locale's name and code. Example: \"English (US): en-US\".\n */\nconst formatLocaleWithName = (locale: Locales): string => {\n // getLocaleName returns a human-readable name for the locale.\n const localeName = getLocaleName(locale);\n\n // Concatenate both the readable name and the locale code.\n return `${locale}: ${localeName}`;\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for ChatGPT.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies. It prints the prompt for each file,\n * and could be adapted to send requests to the ChatGPT model.\n *\n * @async\n * @function\n * @param filePath - The relative or absolute path to the target file.\n * @param options - Optional configuration for the audit process.\n * @returns This function returns a Promise that resolves once the audit is complete.\n */\nexport const auditFile = async (filePath: string, options?: AuditOptions) => {\n try {\n const relativePath = relative(projectPath, filePath);\n console.info(`Auditing file: ${relativePath}`);\n\n // Extract internationalization configuration, including locales and the default locale.\n const { internationalization } = getConfiguration();\n const { defaultLocale, locales } = internationalization;\n\n // Optionally, you could initialize and configure the OpenAI client here, if you intend to make API calls.\n // Uncomment and configure the following lines if you have `openai` installed and want to call the API:\n\n const openai = new OpenAI({\n apiKey: options?.openAiApiKey,\n });\n\n // Read the file's content.\n const fileContent = getFileContent(filePath);\n const splitted = filePath.split('.');\n const fileExtension = splitted[splitted.length - 1];\n\n // Prepare the prompt for ChatGPT by replacing placeholders with actual values.\n const prompt =\n options?.customPrompt ??\n CHAT_GPT_PROMPT.replace('{{filePath}}', filePath)\n .replace(\n '{{defaultLocale}}',\n `{${formatLocaleWithName(defaultLocale)}}`\n )\n .replace(\n '{{otherLocales}}',\n `{${locales.map(formatLocaleWithName).join(', ')}}`\n )\n .replace(\n '{{declarationsContentTemplate}}',\n FILE_TEMPLATE[fileExtension]\n )\n .replace('{{fileContent}}', fileContent);\n\n // Example of how you might request a completion from ChatGPT:\n const chatCompletion = await openai.chat.completions.create({\n model: options?.model ?? 'gpt-4o-mini',\n messages: [{ role: 'system', content: prompt }],\n });\n\n const newContent = chatCompletion.choices[0].message?.content;\n\n if (newContent) {\n writeFileContent(filePath, newContent);\n\n console.info(`${options?.logPrefix ?? ''}File ${relativePath} updated`);\n }\n\n console.info(\n `${options?.logPrefix ?? ''}${chatCompletion.usage?.total_tokens} tokens used in the request`\n );\n } catch (error) {\n console.error(error);\n }\n};\n\n/**\n * Audits the content declaration files by constructing a prompt for ChatGPT.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies. It prints the prompt for each file,\n * and could be adapted to send requests to the ChatGPT model.\n *\n * @async\n * @function\n * @param options - Optional configuration for the audit process.\n * @returns This function returns a Promise that resolves once the audit is complete.\n */\nexport const audit = async (options: AuditOptions) => {\n if (!options?.openAiApiKey) {\n console.error(\n 'OpenAI API key is required to audit the content declaration files.'\n );\n return;\n }\n\n let contentDeclarationFilesList = options?.files;\n\n if (!contentDeclarationFilesList) {\n // Retrieve all content declaration file paths using a helper function.\n const contentDeclarationFilesPath = getContentDeclaration({\n exclude: options?.excludedGlobs,\n });\n\n contentDeclarationFilesList = contentDeclarationFilesPath;\n }\n\n const limit = pLimit(options?.asyncLimit ? Number(options?.asyncLimit) : 5); // Limit the number of concurrent requests\n const pushPromises = contentDeclarationFilesList.map((filePath) =>\n limit(() => auditFile(filePath, options))\n );\n await Promise.all(pushPromises);\n};\n"],"mappings":"AAAA,SAAS,cAAc,qBAAqB;AAC5C,SAAS,MAAM,gBAAgB;AAC/B,SAAS,wBAAiC;AAC1C,SAAS,qBAAqB;AAC9B,SAAS,cAAc;AACvB,OAAO,YAAY;AACnB,SAAS,6BAA6B;AAetC,MAAM,cAAc,QAAQ,IAAI;AAShC,MAAM,iBAAiB,CAAC,aAA6B;AAEnD,QAAM,cAAc,aAAa,UAAU,OAAO;AAClD,SAAO;AACT;AAEA,MAAM,mBAAmB,CAAC,UAAkB,YAAoB;AAC9D,gBAAc,UAAU,OAAO;AACjC;AAEA,MAAM,kBAAkB,CAAC,aAA6B;AACpD,QAAM,eAAe,KAAK,WAAW,QAAQ;AAE7C,SAAO;AACT;AAEA,MAAM,gBAAwC;AAAA,EAC5C,IAAI,eAAe,gBAAgB,gBAAgB,CAAC;AAAA,EACpD,KAAK,eAAe,gBAAgB,iBAAiB,CAAC;AAAA,EACtD,IAAI,eAAe,gBAAgB,iBAAiB,CAAC;AAAA,EACrD,KAAK,eAAe,gBAAgB,iBAAiB,CAAC;AAAA,EACtD,KAAK,eAAe,gBAAgB,iBAAiB,CAAC;AAAA,EACtD,KAAK,eAAe,gBAAgB,iBAAiB,CAAC;AAAA,EACtD,MAAM,eAAe,gBAAgB,kBAAkB,CAAC;AAC1D;AAGA,MA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