RED-7497: Localazy scripts

Update .gitlab-ci.yml file
Update .gitlab-ci.yml file
Update .gitlab-ci.yml file
Update .gitlab-ci.yml file
Update .gitlab-ci.yml file
localazy script

Update .gitlab-ci.yml file
Update .gitlab-ci.yml file
RED-7497: TS compile error

localazy debug

RED-7497: file import path

localazy test

localazy test

localazy test

localazy test

localazy test

localazy test
This commit is contained in:
Adina Țeudan 2023-09-06 12:45:51 +03:00
parent 8b5558b743
commit 75d2c9c869
10 changed files with 169 additions and 1 deletions

View File

@ -11,4 +11,22 @@ workflow:
include:
- project: 'gitlab/gitlab'
ref: 'main'
file: 'ci-templates/docker_build_nexus.yml'
file: 'ci-templates/docker_build_nexus_v2.yml'
localazy update:
image: node:20.5
cache:
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
script:
- cd tools/localazy
- yarn install --cache-folder .yarn-cache
- yarn start
rules:
- if: $LOCALAZY_RUN
- changes:
- tools/localazy/**/*
- red-ui/src/assets/i18n/**/*

View File

@ -11,6 +11,7 @@
"nx": "nx",
"start": "nx serve",
"update": "nx migrate latest",
"localazy": "ts-node tools/localazy/src/index.ts",
"migrate": "nx migrate --run-migrations",
"workspace-generator": "nx workspace-generator",
"analyze": "nx build --stats-json && webpack-bundle-analyzer dist/apps/red-ui/stats.json",
@ -75,6 +76,7 @@
"@angular/compiler-cli": "16.2.2",
"@angular/language-service": "16.2.2",
"@bartholomej/ngx-translate-extract": "^8.0.2",
"@localazy/ts-api": "^1.0.0",
"@nx/eslint-plugin": "16.7.4",
"@nx/jest": "16.7.4",
"@nx/linter": "16.7.4",

2
tools/localazy/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
yarn.lock
node_modules/

View File

@ -0,0 +1,15 @@
{
"name": "localazy",
"version": "1.0.0",
"dependencies": {
"@types/node": "^20.5.7"
},
"scripts": {
"start": "ts-node src/index.ts"
},
"devDependencies": {
"@localazy/ts-api": "^1.0.0",
"ts-node": "10.9.1",
"typescript": "5.1.3"
}
}

View File

@ -0,0 +1,2 @@
export const TOKEN: string = process.env.LOCALAZY_TOKEN || '';
export const FILENAME = 'i18n.json';

View File

@ -0,0 +1,63 @@
import { FILENAME, TOKEN } from './constants';
import LocalazyApi from '@localazy/ts-api';
import { Key } from '@localazy/ts-api/dist/models/responses/keys-in-file';
import { get, set } from './utils';
import * as fs from 'fs';
const api = LocalazyApi({
projectToken: TOKEN,
baseUrl: 'https://api.localazy.com',
});
export async function getProjectId(): Promise<string> {
const projects = await api.listProjects();
return projects[0].id;
}
export async function getFileId(projectId: string): Promise<string> {
const files = await api.listFiles({ projectId });
return files.find(file => file.name === FILENAME)!.id;
}
export async function uploadEn(projectId: string): Promise<void> {
const enFile = (await import('./../../../apps/red-ui/src/assets/i18n/redact/en.json')).default;
await api.import({
projectId,
files: [
{
name: FILENAME,
content: { type: 'json', features: ['plural_icu', 'filter_untranslated'], en: enFile },
},
],
deprecate: 'project',
});
}
export async function downloadLanguage(projectId: string, fileId: string, lang: 'en' | 'de'): Promise<any> {
const response = await api.getFileContents({ projectId, fileId, lang });
return JSON.parse(await response.text());
}
export async function listKeys(projectId: string, fileId: string, lang: 'en' | 'de'): Promise<Key[]> {
const response = await api.listKeysInFileForLanguage({ projectId, fileId, lang });
console.log(`Downloaded ${response.keys.length} keys for ${lang} language`);
return response.keys;
}
export async function updateLocalEn(keys: Key[]): Promise<void> {
const enFile = (await import('./../../../apps/red-ui/src/assets/i18n/redact/en.json')).default;
keys.forEach(({ key, value }) => {
if (get(enFile, key) !== undefined) {
set(enFile, key, value);
}
});
await fs.promises.writeFile('./../../apps/red-ui/src/assets/i18n/redact/en.json', JSON.stringify(enFile, null, 2));
}
export async function updateLocalDe(projectId: string, fileId: string): Promise<void> {
const deFile = await downloadLanguage(projectId, fileId, 'de');
await fs.promises.writeFile('./../../apps/red-ui/src/assets/i18n/redact/de.json', JSON.stringify(deFile, null, 2));
}

View File

@ -0,0 +1,33 @@
import { downloadLanguage, getFileId, getProjectId, listKeys, updateLocalDe, updateLocalEn, uploadEn } from './functions';
async function execute() {
const projectId = await getProjectId();
const fileId = await getFileId(projectId);
/** Update local en (source) file with potential remotely updated translations. */
const remoteKeys = await listKeys(projectId, fileId, 'en');
await updateLocalEn(remoteKeys);
/** Upload local en (source) file in order to add new keys and deprecate unused keys. */
await uploadEn(projectId);
/** Download updated de file. */
await updateLocalDe(projectId, fileId);
}
async function uploadLocals() {
const projectId = await getProjectId();
await uploadEn(projectId);
}
async function downloadDe() {
const projectId = await getProjectId();
const fileId = await getFileId(projectId);
console.log({ projectId, fileId });
console.log(await downloadLanguage(projectId, fileId, 'de'));
}
execute().then();
// uploadLocals().then();
// downloadDe().then();

View File

@ -0,0 +1,16 @@
export function get(object: any, path: string[]): any {
return path.reduce((o, k) => (o || {})[k], object);
}
export function set(object: any, path: string[], value: any): void {
path.reduce((o, k, i) => {
if (i === path.length - 1) {
o[k] = value;
} else {
if (o[k] === undefined) {
o[k] = {};
}
}
return o[k];
}, object);
}

View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6", "DOM"],
"outDir": "build",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}

View File

@ -2494,6 +2494,11 @@
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b"
integrity sha512-Hcv+nVC0kZnQ3tD9GVu5xSMR4VVYOteQIr/hwFPVEvPdlXqgGEuRjiheChHgdM+JyqdgNcmzZOX/tnl0JOiI7A==
"@localazy/ts-api@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@localazy/ts-api/-/ts-api-1.1.0.tgz#e36edbea775fa950a6dcd54693686ed63fa67218"
integrity sha512-0iLFWRxmKPkuruASye4A6CQoYMDGLqQmPLS76zKhPywwNTc89oIlaWrkl63c2EP70NzHxKu6BnpmJAjqFSeXrg==
"@materia-ui/ngx-monaco-editor@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@materia-ui/ngx-monaco-editor/-/ngx-monaco-editor-6.0.0.tgz#9ae93666019e9a6d4f787370b4373cbb63a04a38"