upload.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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({ task })
  35. throw new Error('upload failed')
  36. }
  37. }
  38. async function main() {
  39. const importId = await uploadLocales()
  40. await pollUploadStatus(importId)
  41. }
  42. main().catch(error => {
  43. console.error({ error })
  44. process.exit(1)
  45. })