From 08d097dd0ad3ae9bf987bb78c4316f08e1d5ef34 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sat, 27 Apr 2024 07:39:59 +0000 Subject: [PATCH] Automatically push up newest source strings to Transifex --- .gitlab-ci.yml | 34 ++++++++++++++++++++++------------ scripts/tx.js | 29 +++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4d18ab7..cb2cd3a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,8 +10,8 @@ audit: script: - yarn install - yarn audit - except: - - tags + rules: + - when: always allow_failure: true lint: @@ -19,16 +19,26 @@ lint: script: - yarn install - yarn lint - except: - - tags + rules: + - when: always test: stage: test script: - yarn install - yarn test --no-progress --no-watch --browsers=ChromiumHeadlessCI - except: - - tags + rules: + - when: always + +push-i18n-source: + stage: build + script: + - apk add npm + - npm install @transifex/api + - npm run tx push + rules: + - changes: + - src/assets/en.json build-branch: stage: build @@ -43,8 +53,8 @@ build-branch: artifacts: paths: - dist/ - only: - - branches + rules: + - if: $CI_COMMIT_BRANCH != "master" build-tag: stage: build @@ -58,8 +68,8 @@ build-tag: artifacts: paths: - dist/ - only: - - tags + rules: + - if: $CI_COMMIT_TAG deploy: stage: deploy @@ -83,5 +93,5 @@ deploy: - scp version-$CI_COMMIT_TAG.json openlp@$OPENLP_HOST:/home/openlp/sites/get.openlp.org/www/remote/ - ssh openlp@$OPENLP_HOST "rm /home/openlp/sites/get.openlp.org/www/remote/version.json" - scp version-$CI_COMMIT_TAG.json openlp@$OPENLP_HOST:/home/openlp/sites/get.openlp.org/www/remote/version.json - only: - - tags + rules: + - if: $CI_COMMIT_TAG diff --git a/scripts/tx.js b/scripts/tx.js index a12197c..2c6e6ed 100644 --- a/scripts/tx.js +++ b/scripts/tx.js @@ -5,6 +5,7 @@ const { parseArgs } = require('util'); const { transifexApi } = require('@transifex/api'); const axios = require('axios'); +const ACTIONS = ['push', 'upload', 'download']; // Parse the command line arguments function parseCliArgs() { @@ -29,7 +30,7 @@ function parseCliArgs() { console.log(`usage: tx [-h|--help] [-v|--verbose] [-t|--token TOKEN] positional arguments: - action the action to perform, one of 'upload' or 'download' + action the action to perform, one of 'push', 'upload' or 'download' options: -h, --help show this help message and exit @@ -47,8 +48,8 @@ options: else if (!values.token && process.env.TX_TOKEN) { values.token = process.env.TX_TOKEN; } - if (positionals.length < 1 || (positionals[0] != 'upload' && positionals[0] != 'download')) { - console.error("ERROR: Action is not valid, please use either 'upload' or 'download'"); + if (positionals.length < 1 || !ACTIONS.includes(positionals[0])) { + console.error("ERROR: Action is not valid, please use one of " + ACTIONS.join(", ")); process.exit(1); } return {token: values.token, action: positionals[0], verbose: values.verbose}; @@ -60,9 +61,26 @@ function getPercentage(attributes) { } +async function pushSource(resource, verbose) { + const filename = path.join('src', 'assets', 'en.json'); + if (!fs.existsSync(filename)) { + console.error(`Source file ${filename} does not exist!`); + process.exit(1); + } + if (verbose) { + console.log('Reading en.json...'); + } + const content = fs.readFileSync(filename); + console.log('Uploading en.json...'); + await transifexApi.ResourceStringsAsyncUpload.upload({ + resource: resource, + content: content.toString() + }); +} + async function uploadFiles(resource, languages, verbose) { for (const lang of languages) { - const filename = path.join('src', 'assets', 'i18n', `${lang.attributes.code}.json`) + const filename = path.join('src', 'assets', 'i18n', `${lang.attributes.code}.json`); if (!fs.existsSync(filename)) { continue; } @@ -129,6 +147,9 @@ async function main() { else if (action == 'download') { await downloadFiles(org, proj, resource, languages.data, verbose); } + else if (action == 'push') { + await pushSource(resource, verbose); + } } main();