Move away from smartling CLI

This commit is contained in:
Fedor Indutny 2024-03-21 11:31:31 -07:00 committed by GitHub
parent e90553b3b3
commit f55e6e3407
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
76 changed files with 121916 additions and 244506 deletions

View file

@ -1,29 +1,71 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { execSync } from 'child_process';
import { randomBytes } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { API_BASE, PROJECT_ID, authenticate } from '../util/smartling';
const { SMARTLING_USER, SMARTLING_SECRET } = process.env;
if (!SMARTLING_USER) {
console.error('Need to set SMARTLING_USER environment variable!');
process.exit(1);
}
if (!SMARTLING_SECRET) {
console.error('Need to set SMARTLING_SECRET environment variable!');
process.exit(1);
async function main() {
if (!SMARTLING_USER) {
console.error('Need to set SMARTLING_USER environment variable!');
process.exit(1);
}
if (!SMARTLING_SECRET) {
console.error('Need to set SMARTLING_SECRET environment variable!');
process.exit(1);
}
console.log('Authenticating with Smartling');
const headers = await authenticate({
userIdentifier: SMARTLING_USER,
userSecret: SMARTLING_SECRET,
});
const boundaryString = randomBytes(32).toString('hex');
headers.set(
'content-type',
`multipart/form-data; boundary=${boundaryString}`
);
const url = new URL(`./files-api/v2/projects/${PROJECT_ID}/file`, API_BASE);
const body = [
`--${boundaryString}`,
'Content-Disposition: form-data; name="fileUri"',
'Content-Type: text/plain',
'',
'_locales/en/messages.json',
`--${boundaryString}`,
'Content-Disposition: form-data; name="fileType"',
'Content-Type: text/plain',
'',
'json',
`--${boundaryString}`,
'Content-Disposition: form-data; name="file"',
'Content-Type: text/plain',
'',
await readFile('_locales/en/messages.json', 'utf8'),
`--${boundaryString}`,
'',
];
console.log('Pushing strings');
const res = await fetch(url, {
method: 'POST',
headers,
body: body.join('\r\n'),
});
if (!res.ok) {
throw new Error(`Failed to push strings: ${await res.text()}`);
}
}
console.log('Pushing latest strings!');
console.log();
execSync(
'smartling-cli' +
` --user "${SMARTLING_USER}"` +
` --secret "${SMARTLING_SECRET}"` +
' --config .smartling.yml' +
' --verbose' +
' files push _locales/en/messages.json',
{
stdio: [null, process.stdout, process.stderr],
}
);
main().catch(err => {
console.error(err);
process.exit(1);
});