upload.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const Path = require('path')
  2. const { promises: fs } = require('fs')
  3. const { promisify } = require('util')
  4. const oneSky = require('@brainly/onesky-utils')
  5. const { withAuth } = require('./config')
  6. const sleep = promisify(setTimeout)
  7. async function uploadLocales() {
  8. // Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/file.md#upload---upload-a-file
  9. const blob = await oneSky.postFile(
  10. withAuth({
  11. fileName: 'en-US.json',
  12. language: 'en-GB',
  13. format: 'HIERARCHICAL_JSON',
  14. content: await fs.readFile(
  15. Path.join(__dirname, '/../../locales/en.json')
  16. ),
  17. keepStrings: false, // deprecate locales that no longer exist in en.json
  18. })
  19. )
  20. return JSON.parse(blob).data.import.id
  21. }
  22. async function getImportTask(importId) {
  23. // Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/import_task.md
  24. const blob = await oneSky.getImportTask(withAuth({ importId }))
  25. return JSON.parse(blob).data
  26. }
  27. async function pollUploadStatus(importId) {
  28. let task
  29. while ((task = await getImportTask(importId)).status === 'in-progress') {
  30. console.log('onesky is processing the import ...')
  31. await sleep(5000)
  32. }
  33. if (task.status === 'failed') {
  34. console.error(JSON.stringify({ task }, null, 2))
  35. throw new Error('upload failed')
  36. }
  37. }
  38. async function uploadOnce() {
  39. const importId = await uploadLocales()
  40. await pollUploadStatus(importId)
  41. }
  42. async function main() {
  43. try {
  44. await uploadOnce()
  45. } catch (err) {
  46. console.error('--- upload failed once ---')
  47. console.error(err)
  48. console.error('--- upload failed once ---')
  49. console.log('retrying upload in 30s')
  50. await sleep(30_000)
  51. await uploadOnce()
  52. }
  53. }
  54. main().catch(error => {
  55. console.error({ error })
  56. process.exit(1)
  57. })