/** * @requires fs */ const fs = require('fs') /** * @requires ./config.js * @description Loads the application configuration, containing the fileStore value */ const config = require('./config.js') /** * @constant * @type {object} * @description Points to the location of the assessment JSON files from config().fileStore */ const fileStore = './src' + config().fileStore /** * @constant * @type {array} * @description A list of assessment JSON files in the fileStore */ const files = fs.readdirSync(fileStore) /** * @requires lunr */ const lunr = require('lunr') /** * @constant * @type {integer} * @default 2007 * @description This is the first year we want to create items for in the assessmentsByYear object. It represents when EASA time began */ const startYear = 2007 var assessments = new Array /** * This block iterates through the assessment JSON files read from the fileStore location from config() * For each file, it parses the JSON and reads the fields needed for the index and search outputs * It places these into an object corresponding to the assessment type */ for (const key in files) { if (files[key] != 'index.json' && files[key] != 'search.json') { var data = JSON.parse(fs.readFileSync((fileStore + files[key]))) var assessmentType = Object.keys(data)[0] /** * @type {object} * @description Because lunr interprets '-' as a special character, for the 'easa' assessment type we need a search field with it stripped out. This is in assessment.numberNoDash */ var assessment = new Object switch (assessmentType) { case 'easa': var easa = eval('data.' + assessmentType) assessment.id = easa.id + '_v' + easa.revision assessment.number = easa.id assessment.numberNoDash = easa.id.replace(/-/g, '') assessment.agency = easa.agency assessment.title = easa.solution_name assessment.solution_name = easa.easa_name + ' ' + easa.solution_name assessment.date = easa.completion_date assessment.description = easa.brief_description + ' ' + easa.additional_description + ' ' + easa.changes break; case 'tsa': var tsa = eval('data.' + assessmentType) assessment.id = tsa.id assessment.number = tsa.id assessment.numberNoDash = tsa.id.replace(/-/g, '') assessment.agency = tsa.agency assessment.title = tsa.clarity_data.idea_name assessment.solution_name = tsa.clarity_data.idea_name assessment.date = tsa.completion_date assessment.description = tsa.description + ' ' + tsa.clarity_data.idea_objective break; default: break; } assessments[key] = assessment } } /** * @type {object} * @description This is the lunr search index, created by adding the appropriate fields from the assessments array and writing it to search.json */ var idx = lunr(function () { this.ref('id') this.field('numberNoDash') this.field('agency') this.field('solution_name') this.field('description') for (const assessment of assessments) { this.add(assessment) } }) fs.writeFileSync(fileStore + 'search.json', JSON.stringify(idx)) /** * @type {object} * @description For the index that index.vue parses, we need to split the assessments array into years, starting with startYear. We then write this to index.json */ var assessmentsByYear = new Object for (let index = startYear; index <= new Date().getFullYear(); index++) { assessmentsByYear[index.toString()] = assessments.filter((assessment) => assessment.id.slice(0,4) == index) } fs.writeFileSync(fileStore + 'index.json', JSON.stringify(assessmentsByYear))