signal-desktop/ts/scripts/push-strings.ts

72 lines
1.8 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-03-21 18:31:31 +00:00
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;
2024-03-21 18:31:31 +00:00
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,
});
2024-03-21 18:31:31 +00:00
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}`,
2024-04-09 19:14:12 +00:00
'Content-Disposition: form-data; name="file"; filename="_locales/en/messages.json"',
2024-03-21 18:31:31 +00:00
'Content-Type: text/plain',
'',
await readFile('_locales/en/messages.json', 'utf8'),
2024-04-09 19:14:12 +00:00
`--${boundaryString}--`,
2024-03-21 18:31:31 +00:00
'',
];
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()}`);
}
2024-03-21 18:31:31 +00:00
}
main().catch(err => {
console.error(err);
process.exit(1);
});