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
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
/**
 * This file generates data needed for static site compilation.
 * Pulls data from Airtable, and store them ni data/airtable/ directory.
 * Pages will be given a filename equivalent of the `id` column
 * in airtable - unless otherwise specified by 18n plugin when
 * doing localization.
 * 
 */

let Airtable = require('airtable');
var base = new Airtable({apiKey: 'keyCxb3aqtS38ft3N'}).base('appI3wmrF0dN0d3ls')
let fsextra = require('fs-extra')
let fs = require('fs')
let axios = require('axios')
let jsyaml = require('js-yaml')

// Each row in airtable is stored as an object here
let airtableRows = []

// Locale we're compiling for
let locale = process.env.locale || 'en'

// Will hold internal links for each langcode
let internalLinks = {}

// Will hold our directory tree (just paths, actually)
let tree = []

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)
      
      /* 
       * Each dynamic generated page (has a `group` in airtable)
       * from a yaml file, must have a "metadata.langcode = ita".
       * This is  simply to create internal links.
       * Internal links are at the bottom of each SEO page and they point
       * to other SEO pages about the same language. It's awful we
       * have to have this anomaly here when generating the site.
       * Maybe they should resite in a separate Airtable sheet.
       * This file was much simpler before the requiement to create
       * internal SEO links and put them on every page.
       * https://tabbit.org/dbbUSgGwt
       */
      
      let langcode = null
      if (group) {        
        langcode = jsyaml.safeLoad(remoteFile.data, 'utf8').metadata.langcode
        if (Array.isArray(internalLinks[langcode])) {
          internalLinks[langcode].push( id )
        } else {
          internalLinks[langcode] = []
          internalLinks[langcode].push( id )
        }
      }
      
      
      // We will loop these and write to disk later
      tree.push({
        path: filePath,        
        yaml: '__', //remoteFile.data,
        langcode: langcode
      })



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

    }

//fs.writeFileSync(`${filePath}/index.yaml`, remoteFile.data , 'utf-8')
    
    for (let file of tree) {
      if (internalLinks[file.langcode]) {
        file.yaml = `internal-links: ${internalLinks[file.langcode].join(" ")}\n`
      }
    }
    

    console.log(internalLinks)
    console.log(tree)
  }
);