| 
									
										
										
										
											2022-09-27 14:01:06 -07:00
										 |  |  | // Copyright 2021 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07:00
										 |  |  | import { randomBytes } from 'node:crypto'; | 
					
						
							|  |  |  | import { readFile } from 'node:fs/promises'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { API_BASE, PROJECT_ID, authenticate } from '../util/smartling'; | 
					
						
							| 
									
										
										
										
											2022-09-27 14:01:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const { SMARTLING_USER, SMARTLING_SECRET } = process.env; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07: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, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-09-27 14:01:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07: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 21:14:12 +02:00
										 |  |  |     'Content-Disposition: form-data; name="file"; filename="_locales/en/messages.json"', | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07:00
										 |  |  |     'Content-Type: text/plain', | 
					
						
							|  |  |  |     '', | 
					
						
							|  |  |  |     await readFile('_locales/en/messages.json', 'utf8'), | 
					
						
							| 
									
										
										
										
											2024-04-09 21:14:12 +02:00
										 |  |  |     `--${boundaryString}--`, | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07: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()}`); | 
					
						
							| 
									
										
										
										
											2022-09-27 14:01:06 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-21 11:31:31 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main().catch(err => { | 
					
						
							|  |  |  |   console.error(err); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							|  |  |  | }); |