upload.js 1.4 KB

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