gtx-cli 0.0.1 → 1.0.0-alpha.1
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/README.md +4 -7
- package/dist/api/fetchTranslations.d.ts +10 -0
- package/dist/api/fetchTranslations.js +42 -0
- package/dist/api/sendUpdates.d.ts +18 -0
- package/dist/api/sendUpdates.js +72 -0
- package/dist/api/waitForUpdates.d.ts +11 -0
- package/dist/api/waitForUpdates.js +99 -0
- package/dist/cli/base.d.ts +9 -0
- package/dist/cli/base.js +191 -0
- package/dist/cli/next.d.ts +20 -0
- package/dist/cli/next.js +166 -0
- package/dist/cli/react.d.ts +31 -0
- package/dist/cli/react.js +477 -0
- package/dist/config/generateSettings.d.ts +7 -0
- package/dist/config/generateSettings.js +73 -0
- package/dist/console/console.d.ts +8 -0
- package/dist/console/console.js +65 -0
- package/dist/console/errors.d.ts +9 -0
- package/dist/console/errors.js +12 -0
- package/dist/console/index.d.ts +1 -0
- package/dist/console/index.js +2 -0
- package/dist/console/warnings.d.ts +7 -0
- package/dist/console/warnings.js +47 -0
- package/dist/formats/gt/save.d.ts +9 -0
- package/dist/formats/gt/save.js +27 -0
- package/dist/formats/json/save.d.ts +9 -0
- package/dist/formats/json/save.js +60 -0
- package/dist/formats/json/translate.d.ts +15 -0
- package/dist/formats/json/translate.js +61 -0
- package/dist/fs/config/loadConfig.d.ts +1 -0
- package/dist/fs/config/loadConfig.js +15 -0
- package/dist/fs/config/setupConfig.d.ts +13 -0
- package/dist/fs/config/setupConfig.js +45 -0
- package/dist/fs/config/updateConfig.d.ts +10 -0
- package/dist/fs/config/updateConfig.js +33 -0
- package/dist/fs/determineFramework.d.ts +2 -0
- package/dist/fs/determineFramework.js +46 -0
- package/dist/fs/findFilepath.d.ts +29 -0
- package/dist/fs/findFilepath.js +78 -0
- package/dist/fs/findJsxFilepath.d.ts +7 -0
- package/dist/fs/findJsxFilepath.js +36 -0
- package/dist/fs/index.d.ts +1 -0
- package/dist/fs/index.js +2 -0
- package/dist/fs/loadJSON.d.ts +6 -0
- package/dist/fs/loadJSON.js +23 -0
- package/dist/fs/saveJSON.d.ts +1 -0
- package/dist/fs/saveJSON.js +13 -0
- package/dist/fs/utils.d.ts +1 -0
- package/dist/fs/utils.js +19 -0
- package/dist/hooks/postProcess.d.ts +4 -0
- package/dist/hooks/postProcess.js +101 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +24 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +11 -0
- package/dist/next/config/parseNextConfig.d.ts +10 -0
- package/dist/next/config/parseNextConfig.js +55 -0
- package/dist/next/jsx/utils.d.ts +7 -0
- package/dist/next/jsx/utils.js +82 -0
- package/dist/next/parse/handleInitGT.d.ts +5 -0
- package/dist/next/parse/handleInitGT.js +167 -0
- package/dist/next/parse/index.d.ts +4 -0
- package/dist/next/parse/index.js +14 -0
- package/dist/next/parse/scanForContent.d.ts +13 -0
- package/dist/next/parse/scanForContent.js +189 -0
- package/dist/react/config/createESBuildConfig.d.ts +2 -0
- package/dist/react/config/createESBuildConfig.js +134 -0
- package/dist/react/data-_gt/addGTIdentifierToSyntaxTree.d.ts +1 -0
- package/dist/react/data-_gt/addGTIdentifierToSyntaxTree.js +92 -0
- package/dist/react/jsx/evaluateJsx.d.ts +17 -0
- package/dist/react/jsx/evaluateJsx.js +133 -0
- package/dist/react/jsx/parse/parseStringFunction.d.ts +12 -0
- package/dist/react/jsx/parse/parseStringFunction.js +120 -0
- package/dist/react/jsx/trimJsxStringChildren.d.ts +7 -0
- package/dist/react/jsx/trimJsxStringChildren.js +97 -0
- package/dist/react/jsx/utils/parseAst.d.ts +30 -0
- package/dist/react/jsx/utils/parseAst.js +320 -0
- package/dist/react/jsx/utils/parseJsx.d.ts +13 -0
- package/dist/react/jsx/utils/parseJsx.js +236 -0
- package/dist/react/jsx/utils/parseStringFunction.d.ts +12 -0
- package/dist/react/jsx/utils/parseStringFunction.js +120 -0
- package/dist/react/jsx/wrapJsx.d.ts +51 -0
- package/dist/react/jsx/wrapJsx.js +407 -0
- package/dist/react/parse/createDictionaryUpdates.d.ts +5 -0
- package/dist/react/parse/createDictionaryUpdates.js +77 -0
- package/dist/react/parse/createInlineUpdates.d.ts +5 -0
- package/dist/react/parse/createInlineUpdates.js +141 -0
- package/dist/react/parse/index.d.ts +3 -0
- package/dist/react/parse/index.js +12 -0
- package/dist/react/parse/scanForContent.d.ts +13 -0
- package/dist/react/parse/scanForContent.js +200 -0
- package/dist/react/updates/createDictionaryUpdates.d.ts +5 -0
- package/dist/react/updates/createDictionaryUpdates.js +76 -0
- package/dist/react/updates/createInlineUpdates.d.ts +5 -0
- package/dist/react/updates/createInlineUpdates.js +141 -0
- package/dist/react/updates/scanForContent.d.ts +13 -0
- package/dist/react/updates/scanForContent.js +200 -0
- package/dist/react/utils/flattenDictionary.d.ts +20 -0
- package/dist/react/utils/flattenDictionary.js +78 -0
- package/dist/react/utils/getEntryAndMetadata.d.ts +5 -0
- package/dist/react/utils/getEntryAndMetadata.js +14 -0
- package/dist/react/utils/getVariableName.d.ts +2 -0
- package/dist/react/utils/getVariableName.js +20 -0
- package/dist/types/api.d.ts +6 -0
- package/dist/types/api.js +2 -0
- package/dist/types/data.d.ts +31 -0
- package/dist/types/data.js +2 -0
- package/dist/types/index.d.ts +73 -0
- package/dist/types/index.js +2 -0
- package/package.json +11 -4
package/README.md
CHANGED
|
@@ -4,25 +4,22 @@
|
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
|
-
#
|
|
7
|
+
# gtx-cli: General Purpose CLI Tool for General Translation
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Additionally, it can scan your project for all content that needs to be translated, and automatically wrap them in `<T>` and `<Var>` components.
|
|
9
|
+
gtx-cli is a command-line tool used to interface with General Translation's AI-powered i18n platform.
|
|
12
10
|
|
|
13
11
|
See our [docs](https://generaltranslation.com/docs) for more information including guides, examples, and API references.
|
|
14
12
|
|
|
15
13
|
## Installation
|
|
16
14
|
|
|
17
15
|
```bash
|
|
18
|
-
npm install
|
|
16
|
+
npm install gtx-cli
|
|
19
17
|
```
|
|
20
18
|
|
|
21
19
|
## Usage
|
|
22
20
|
|
|
23
21
|
```bash
|
|
24
|
-
npx
|
|
25
|
-
npx gt-react-cli setup
|
|
22
|
+
npx gtx-cli translate
|
|
26
23
|
```
|
|
27
24
|
|
|
28
25
|
## Documentation
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RetrievedTranslations } from '../types/api';
|
|
2
|
+
/**
|
|
3
|
+
* Fetches translations from the API and saves them to a local directory
|
|
4
|
+
* @param baseUrl - The base URL for the API
|
|
5
|
+
* @param apiKey - The API key for the API
|
|
6
|
+
* @param versionId - The version ID of the project
|
|
7
|
+
* @param translationsDir - The directory to save the translations to
|
|
8
|
+
* @param fileType - The file type to save the translations as (file extension)
|
|
9
|
+
*/
|
|
10
|
+
export declare function fetchTranslations(baseUrl: string, apiKey: string, versionId: string): Promise<RetrievedTranslations>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.fetchTranslations = fetchTranslations;
|
|
16
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
+
/**
|
|
18
|
+
* Fetches translations from the API and saves them to a local directory
|
|
19
|
+
* @param baseUrl - The base URL for the API
|
|
20
|
+
* @param apiKey - The API key for the API
|
|
21
|
+
* @param versionId - The version ID of the project
|
|
22
|
+
* @param translationsDir - The directory to save the translations to
|
|
23
|
+
* @param fileType - The file type to save the translations as (file extension)
|
|
24
|
+
*/
|
|
25
|
+
function fetchTranslations(baseUrl, apiKey, versionId) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
// First fetch the translations from the API
|
|
28
|
+
const response = yield fetch(`${baseUrl}/v1/project/translations/info/${encodeURIComponent(versionId)}`, {
|
|
29
|
+
method: 'GET',
|
|
30
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, (apiKey && { 'x-gt-api-key': apiKey })),
|
|
31
|
+
});
|
|
32
|
+
if (response.ok) {
|
|
33
|
+
const data = yield response.json();
|
|
34
|
+
const translations = data.translations;
|
|
35
|
+
return translations;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.error(chalk_1.default.red('Failed to fetch translations'));
|
|
39
|
+
}
|
|
40
|
+
return [];
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Settings, Updates } from '../types';
|
|
2
|
+
import { DataFormat } from '../types/data';
|
|
3
|
+
type ApiOptions = Settings & {
|
|
4
|
+
publish: boolean;
|
|
5
|
+
wait: boolean;
|
|
6
|
+
timeout: string;
|
|
7
|
+
dataFormat: DataFormat;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Sends updates to the API
|
|
11
|
+
* @param updates - The updates to send
|
|
12
|
+
* @param options - The options for the API call
|
|
13
|
+
* @returns The versionId of the updated project
|
|
14
|
+
*/
|
|
15
|
+
export declare function sendUpdates(updates: Updates, options: ApiOptions): Promise<{
|
|
16
|
+
versionId: any;
|
|
17
|
+
} | undefined>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.sendUpdates = sendUpdates;
|
|
16
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
+
const console_1 = require("../console/console");
|
|
18
|
+
const updateConfig_1 = __importDefault(require("../fs/config/updateConfig"));
|
|
19
|
+
const waitForUpdates_1 = require("./waitForUpdates");
|
|
20
|
+
/**
|
|
21
|
+
* Sends updates to the API
|
|
22
|
+
* @param updates - The updates to send
|
|
23
|
+
* @param options - The options for the API call
|
|
24
|
+
* @returns The versionId of the updated project
|
|
25
|
+
*/
|
|
26
|
+
function sendUpdates(updates, options) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const { apiKey, projectId, defaultLocale, dataFormat } = options;
|
|
29
|
+
const globalMetadata = Object.assign(Object.assign({}, (projectId && { projectId })), (defaultLocale && { sourceLocale: defaultLocale }));
|
|
30
|
+
// If additionalLocales is provided, additionalLocales + project.current_locales will be translated
|
|
31
|
+
// If not, then options.locales will be translated
|
|
32
|
+
// If neither, then project.current_locales will be translated
|
|
33
|
+
const body = Object.assign(Object.assign(Object.assign(Object.assign({ updates }, (options.locales && { locales: options.locales })), { metadata: globalMetadata, publish: options.publish }), (dataFormat && { dataFormat })), (options.versionId && { versionId: options.versionId }));
|
|
34
|
+
const spinner = yield (0, console_1.displayLoadingAnimation)('Sending updates to General Translation API...');
|
|
35
|
+
try {
|
|
36
|
+
const startTime = Date.now();
|
|
37
|
+
const response = yield fetch(`${options.baseUrl}/v1/project/translations/update`, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, (apiKey && { 'x-gt-api-key': apiKey })),
|
|
40
|
+
body: JSON.stringify(body),
|
|
41
|
+
});
|
|
42
|
+
process.stdout.write('\n\n');
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
spinner.fail(yield response.text());
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
if (response.status === 204) {
|
|
48
|
+
spinner.succeed(yield response.text());
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const { versionId, message, locales } = yield response.json();
|
|
52
|
+
spinner.succeed(chalk_1.default.green(message));
|
|
53
|
+
if (options.config)
|
|
54
|
+
(0, updateConfig_1.default)({
|
|
55
|
+
configFilepath: options.config,
|
|
56
|
+
_versionId: versionId,
|
|
57
|
+
locales,
|
|
58
|
+
});
|
|
59
|
+
// Wait for translations if wait is true
|
|
60
|
+
if (options.wait && locales) {
|
|
61
|
+
// timeout was validated earlier
|
|
62
|
+
const timeout = parseInt(options.timeout) * 1000;
|
|
63
|
+
const result = yield (0, waitForUpdates_1.waitForUpdates)(apiKey, options.baseUrl, versionId, locales, startTime, timeout);
|
|
64
|
+
}
|
|
65
|
+
return { versionId };
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
spinner.fail(chalk_1.default.red('Failed to send updates'));
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits for translations to be deployed to the General Translation API
|
|
3
|
+
* @param apiKey - The API key for the General Translation API
|
|
4
|
+
* @param baseUrl - The base URL for the General Translation API
|
|
5
|
+
* @param versionId - The version ID of the project
|
|
6
|
+
* @param locales - The locales to wait for
|
|
7
|
+
* @param startTime - The start time of the wait
|
|
8
|
+
* @param timeoutDuration - The timeout duration for the wait
|
|
9
|
+
* @returns True if all translations are deployed, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
export declare const waitForUpdates: (apiKey: string, baseUrl: string, versionId: string, locales: string[], startTime: number, timeoutDuration: number) => Promise<boolean>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.waitForUpdates = void 0;
|
|
16
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
+
const console_1 = require("../console/console");
|
|
18
|
+
const generaltranslation_1 = require("generaltranslation");
|
|
19
|
+
/**
|
|
20
|
+
* Waits for translations to be deployed to the General Translation API
|
|
21
|
+
* @param apiKey - The API key for the General Translation API
|
|
22
|
+
* @param baseUrl - The base URL for the General Translation API
|
|
23
|
+
* @param versionId - The version ID of the project
|
|
24
|
+
* @param locales - The locales to wait for
|
|
25
|
+
* @param startTime - The start time of the wait
|
|
26
|
+
* @param timeoutDuration - The timeout duration for the wait
|
|
27
|
+
* @returns True if all translations are deployed, false otherwise
|
|
28
|
+
*/
|
|
29
|
+
const waitForUpdates = (apiKey, baseUrl, versionId, locales, startTime, timeoutDuration) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const spinner = yield (0, console_1.displayLoadingAnimation)('Waiting for translation...');
|
|
31
|
+
const availableLocales = [];
|
|
32
|
+
const checkDeployment = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
+
try {
|
|
34
|
+
const response = yield fetch(`${baseUrl}/v1/project/translations/status/${encodeURIComponent(versionId)}`, {
|
|
35
|
+
method: 'GET',
|
|
36
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, (apiKey && { 'x-gt-api-key': apiKey })),
|
|
37
|
+
});
|
|
38
|
+
if (response.ok) {
|
|
39
|
+
const data = yield response.json();
|
|
40
|
+
if (data.availableLocales) {
|
|
41
|
+
data.availableLocales.forEach((locale) => {
|
|
42
|
+
if (!availableLocales.includes(locale) &&
|
|
43
|
+
locales.includes(locale)) {
|
|
44
|
+
availableLocales.push(locale);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const newSuffixText = [
|
|
48
|
+
`\n\n` +
|
|
49
|
+
chalk_1.default.green(`${availableLocales.length}/${locales.length}`) +
|
|
50
|
+
` translations completed`,
|
|
51
|
+
...availableLocales.map((locale) => {
|
|
52
|
+
const localeProperties = (0, generaltranslation_1.getLocaleProperties)(locale);
|
|
53
|
+
return `Translation completed for ${chalk_1.default.green(localeProperties.name)} (${chalk_1.default.green(localeProperties.code)})`;
|
|
54
|
+
}),
|
|
55
|
+
];
|
|
56
|
+
spinner.suffixText = newSuffixText.join('\n');
|
|
57
|
+
}
|
|
58
|
+
if (locales.every((locale) => availableLocales.includes(locale))) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
// Calculate time until next 5-second interval since startTime
|
|
69
|
+
const msUntilNextInterval = Math.max(0, 5000 - ((Date.now() - startTime) % 5000));
|
|
70
|
+
// Do first check immediately
|
|
71
|
+
const initialCheck = yield checkDeployment();
|
|
72
|
+
if (initialCheck) {
|
|
73
|
+
spinner.succeed(chalk_1.default.green('All translations are live!'));
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return new Promise((resolve) => {
|
|
77
|
+
let intervalCheck;
|
|
78
|
+
// Start the interval aligned with the original request time
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
intervalCheck = setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
+
const isDeployed = yield checkDeployment();
|
|
82
|
+
const elapsed = Date.now() - startTime;
|
|
83
|
+
if (isDeployed || elapsed >= timeoutDuration) {
|
|
84
|
+
process.stdout.write('\n');
|
|
85
|
+
clearInterval(intervalCheck);
|
|
86
|
+
if (isDeployed) {
|
|
87
|
+
spinner.succeed(chalk_1.default.green('All translations are live!'));
|
|
88
|
+
resolve(true);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
spinner.fail(chalk_1.default.red('Timed out waiting for translations'));
|
|
92
|
+
resolve(false);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}), 5000);
|
|
96
|
+
}, msUntilNextInterval);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
exports.waitForUpdates = waitForUpdates;
|
package/dist/cli/base.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.BaseCLI = void 0;
|
|
49
|
+
const commander_1 = require("commander");
|
|
50
|
+
const console_1 = require("../console/console");
|
|
51
|
+
const console_2 = require("../console/console");
|
|
52
|
+
const setupConfig_1 = __importDefault(require("../fs/config/setupConfig"));
|
|
53
|
+
const prompts_1 = require("@inquirer/prompts");
|
|
54
|
+
const generaltranslation_1 = require("generaltranslation");
|
|
55
|
+
const findFilepath_1 = __importStar(require("../fs/findFilepath"));
|
|
56
|
+
const errors_1 = require("../console/errors");
|
|
57
|
+
const path_1 = __importDefault(require("path"));
|
|
58
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
59
|
+
const translate_1 = require("../formats/json/translate");
|
|
60
|
+
const utils_1 = require("../fs/utils");
|
|
61
|
+
const generateSettings_1 = require("../config/generateSettings");
|
|
62
|
+
const SUPPORTED_DATA_FORMATS = ['JSX', 'ICU', 'I18NEXT'];
|
|
63
|
+
class BaseCLI {
|
|
64
|
+
// Constructor is shared amongst all CLI class types
|
|
65
|
+
constructor(library) {
|
|
66
|
+
this.library = library;
|
|
67
|
+
this.setupInitCommand();
|
|
68
|
+
}
|
|
69
|
+
// Init is never called in a child class
|
|
70
|
+
init() {
|
|
71
|
+
this.setupGTCommand();
|
|
72
|
+
}
|
|
73
|
+
// Execute is called by the main program
|
|
74
|
+
execute() {
|
|
75
|
+
commander_1.program.parse();
|
|
76
|
+
}
|
|
77
|
+
setupGTCommand() {
|
|
78
|
+
commander_1.program
|
|
79
|
+
.command('translate')
|
|
80
|
+
.description('Translate your project using General Translation')
|
|
81
|
+
.option('-c, --config <path>', 'Filepath to config file, by default gt.config.json', (0, findFilepath_1.default)(['gt.config.json']))
|
|
82
|
+
.option('--api-key <key>', 'API key for General Translation cloud service')
|
|
83
|
+
.option('--project-id <id>', 'Project ID for the translation service', (0, utils_1.resolveProjectId)())
|
|
84
|
+
.option('--default-language, --default-locale <locale>', 'Default locale (e.g., en)')
|
|
85
|
+
.option('--new, --locales <locales...>', 'Space-separated list of locales (e.g., en fr es)')
|
|
86
|
+
.option('-t, --translations-dir, --translation-dir <path>', 'Directory containing your language files. Should be in the format path/to/translations/*.json or path/to/translations/*.yaml')
|
|
87
|
+
.action((options) => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
(0, console_1.displayAsciiTitle)();
|
|
89
|
+
(0, console_2.displayInitializingText)();
|
|
90
|
+
const settings = (0, generateSettings_1.generateSettings)(options);
|
|
91
|
+
if (!settings.locales) {
|
|
92
|
+
console.error(errors_1.noLocalesError);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
if (!settings.defaultLocale) {
|
|
96
|
+
console.error(errors_1.noDefaultLocaleError);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
if (!settings.translationsDir) {
|
|
100
|
+
console.error(errors_1.noTranslationsDirError);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
if (!settings.apiKey) {
|
|
104
|
+
console.error(errors_1.noApiKeyError);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
if (!settings.projectId) {
|
|
108
|
+
console.error(errors_1.noProjectIdError);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
111
|
+
// ---- CREATING UPDATES ---- //
|
|
112
|
+
// Find the source file in the translationsDir
|
|
113
|
+
const rawSource = (0, findFilepath_1.findFile)(settings.translationsDir, settings.defaultLocale);
|
|
114
|
+
if (!rawSource) {
|
|
115
|
+
console.error(errors_1.noSourceFileError);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
// Get the data format from the ending of the translationsDir
|
|
119
|
+
const fileExtension = settings.translationsDir
|
|
120
|
+
.split('.')
|
|
121
|
+
.pop();
|
|
122
|
+
const dataFormat = this.library === 'next-intl'
|
|
123
|
+
? 'ICU'
|
|
124
|
+
: this.library === 'react-i18next'
|
|
125
|
+
? 'I18NEXT'
|
|
126
|
+
: this.library === 'next-i18next'
|
|
127
|
+
? 'I18NEXT'
|
|
128
|
+
: 'JSX';
|
|
129
|
+
if (!dataFormat) {
|
|
130
|
+
console.error(errors_1.noDataFormatError);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
else if (!SUPPORTED_DATA_FORMATS.includes(dataFormat)) {
|
|
134
|
+
console.error(errors_1.noSupportedDataFormatError);
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
const source = fileExtension === 'json'
|
|
138
|
+
? JSON.parse(rawSource)
|
|
139
|
+
: yaml_1.default.parse(rawSource);
|
|
140
|
+
const result = yield (0, translate_1.translateJson)(source, settings, dataFormat, fileExtension);
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
setupInitCommand() {
|
|
144
|
+
commander_1.program
|
|
145
|
+
.command('init')
|
|
146
|
+
.description('Initialize project for General Translation')
|
|
147
|
+
.action(() => __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
(0, console_1.displayAsciiTitle)();
|
|
149
|
+
(0, console_2.displayInitializingText)();
|
|
150
|
+
// Ask where the translations are stored
|
|
151
|
+
const translationsDir = yield (0, prompts_1.input)({
|
|
152
|
+
message: 'Where is the directory containing your language files?',
|
|
153
|
+
});
|
|
154
|
+
// Ask for the default locale
|
|
155
|
+
const defaultLocale = yield (0, prompts_1.input)({
|
|
156
|
+
message: 'What is the default locale for your project?',
|
|
157
|
+
});
|
|
158
|
+
// Ask for the locales
|
|
159
|
+
const locales = yield (0, prompts_1.input)({
|
|
160
|
+
message: 'What locales would you like to translate using General Translation? (space-separated list)',
|
|
161
|
+
validate: (input) => {
|
|
162
|
+
const locales = input.split(' ');
|
|
163
|
+
if (locales.length === 0) {
|
|
164
|
+
return 'Please enter at least one locale';
|
|
165
|
+
}
|
|
166
|
+
for (const locale of locales) {
|
|
167
|
+
if (!(0, generaltranslation_1.isValidLocale)(locale)) {
|
|
168
|
+
return 'Please enter a valid locale (e.g., en, fr, es)';
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
const dataFormat = yield (0, prompts_1.select)({
|
|
175
|
+
message: 'What is the format of your language files?',
|
|
176
|
+
choices: ['.json', '.yaml'],
|
|
177
|
+
default: '.json',
|
|
178
|
+
});
|
|
179
|
+
// combine translationsDir and dataFormat into something like
|
|
180
|
+
// translationsDir/*[.json|.yaml]
|
|
181
|
+
const translationsDirWithFormat = path_1.default.join(translationsDir, `*${dataFormat}`);
|
|
182
|
+
// Create gt.config.json
|
|
183
|
+
(0, setupConfig_1.default)('gt.config.json', {
|
|
184
|
+
defaultLocale,
|
|
185
|
+
locales: locales.split(' '),
|
|
186
|
+
translationsDir: translationsDirWithFormat,
|
|
187
|
+
});
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
exports.BaseCLI = BaseCLI;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { WrapOptions, Options, Updates, SetupOptions, SupportedFrameworks } from '../types';
|
|
2
|
+
import { ReactCLI } from './react';
|
|
3
|
+
export declare class NextCLI extends ReactCLI {
|
|
4
|
+
constructor();
|
|
5
|
+
init(): void;
|
|
6
|
+
execute(): void;
|
|
7
|
+
protected scanForContent(options: WrapOptions, framework: SupportedFrameworks): Promise<{
|
|
8
|
+
errors: string[];
|
|
9
|
+
filesUpdated: string[];
|
|
10
|
+
warnings: string[];
|
|
11
|
+
}>;
|
|
12
|
+
protected createDictionaryUpdates(options: Options & {
|
|
13
|
+
dictionary: string;
|
|
14
|
+
}, esbuildConfig: any): Promise<Updates>;
|
|
15
|
+
protected createInlineUpdates(options: Options): Promise<{
|
|
16
|
+
updates: Updates;
|
|
17
|
+
errors: string[];
|
|
18
|
+
}>;
|
|
19
|
+
protected handleSetupCommand(options: SetupOptions): Promise<void>;
|
|
20
|
+
}
|