Tue Aug 13 2019
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

let Airtable = require('airtable');
var base = new Airtable({apiKey: 'keyCxb3aqtS38ft3N'}).base('appI3wmrF0dN0d3ls')
let fsextra = require('fs-extra')
let fs = require('fs')
let beautify = require('js-beautify')
let util = require('util')
let axios = require('axios')
let yaml = require('yamljs');
let airtableRows = []
let locale = process.env.locale || 'en'


console.log(`ⓘ  Generating for locale: ${locale}`)
base('translations').select({    
    view: "Grid view"
})
.eachPage( (records, fetchNextPage) => {       
    records.forEach(record => {
      airtableRows.push({
        id: record.get('id'),
        files: record.get(`files`),        
        group: record.get('group')
      });
    });    

    fetchNextPage();

  }, async function done(err) {
    if (err) { console.error(err); return; }         
    
    for (let row of airtableRows) {

      let { id, group, files } = row
      
      // The file's name format is locale-filename.yaml (en-learn-spanish.yaml, etc)
      let [ file ] = files.filter(f => f.filename.slice(0,2) == locale)               
      let remoteFile = await axios.get(file.url) 
      let datauri = `./data/airtable`

      // If this page belongs to a group, create the dirs to put it there      
      filePath = group ? `${datauri}/groups/${group}/${id}` : `${datauri}/${id}`     
      fsextra.ensureDirSync(filePath)

      // Write the yaml file
      fs.writeFileSync(`${filePath}/index.yaml`, remoteFile.data , 'utf-8')
      console.log(`✔  Created [ f ]: ${filePath}/index.js`)   
    }
  }
);