generated from cloud-gov/pages-uswds-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fellow_files_creation_script.js
129 lines (109 loc) · 4.28 KB
/
fellow_files_creation_script.js
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
129
/**
* This script is used to generate the /fellows/ directory which stores the page definitions of the different combinations of filters applied to the list of fellows.
* It pulls the list of agencies/tracks/years from the /_data/ folder using js-yaml.
*
* To run, execute `node fellow_files_creation_script.js` from the project directory
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
/**
* This function returns the data from the given yaml file path
* @param {String} filePath path to yaml file
*/
const readYamlConstants = (filePath) => {
try {
const fileContents = fs.readFileSync(filePath, 'utf8');
const data = yaml.load(fileContents);
return data;
} catch (e) {
console.error(`Error reading YAML file: ${e}`);
}
}
/**
* This function takes a year, track, and agency and returns the appropriate page definition
* @param {Integer} year filter applied
* @param {Object} track filter applied
* @param {Object} agency filter applied
*/
const createFileContents = (year, track, agency) => {
const nonNullFilterTitles = [year, agency && agency.fullname, track && track.display_name].filter(filter => filter !== null);
const nonNullFilterTags = [year, agency && agency.name, track && track.url_name].filter(filter => filter !== null);
const title = nonNullFilterTitles.join(' ');
const filterTag = nonNullFilterTags.length === 1 ? `'${nonNullFilterTags[0]}'` : nonNullFilterTags.join(',');
return `---
layout: fellow-landing
title: ${title} Fellows
sidenav: true
banner: true
pagination:
enabled: true
collection: fellows
sort_field: 'title'
tag: ${filterTag}
---
{% include_cached fellows-list.html fellows=paginator.posts %}
`
}
/**
* This function takes a file path, year, track, and agency, and writes the appropriate page definition to the file
* @param {String} filePath path of file to write to
* @param {Integer} year filter applied
* @param {Object} track filter applied
* @param {Object} agency filter applied
*/
const createFile = (filePath, year, track, agency) => {
const content = createFileContents(year, track, agency);
fs.writeFileSync(filePath, content);
console.log(`Created file: ${filePath}`);
}
/**
* Creates nested directories of files representing page definitions with different combinations of filters applied
* @param {Array<Object>} agencies
* @param {Array<Object>} tracks
* @param {Array<Integer>} years
*/
const createFellowFilterDirectoriesAndFiles = (agencies, tracks, years) => {
const baseDir = path.join(__dirname, 'fellows');
fs.mkdirSync(baseDir, { recursive: true });
years.forEach(year => {
// Create filter for year
createFile(path.join(baseDir, `${year}.html`), year, null, null);
const yearDir = path.join(baseDir, `${year}`);
fs.mkdirSync(yearDir, { recursive: true });
agencies.forEach(agency => {
// Create filter for year + agency
createFile(path.join(yearDir, `${agency.name}.html`), year, null, agency);
});
tracks.forEach(track => {
// Create filter for year + track
createFile(path.join(yearDir, `${track.url_name}.html`), year, track, null);
const trackDir = path.join(yearDir, track.url_name);
fs.mkdirSync(trackDir, { recursive: true });
agencies.forEach(agency => {
// Create filter for year + track + agency
createFile(path.join(trackDir, `${agency.name}.html`), year, track, agency);
});
});
});
tracks.forEach(track => {
// Create filter for track
createFile(path.join(baseDir, `${track.url_name}.html`), null, track, null);
const trackDir = path.join(baseDir, track.url_name);
fs.mkdirSync(trackDir, { recursive: true });
agencies.forEach(agency => {
// Create filter for track + agency
createFile(path.join(trackDir, `${agency.name}.html`), null, track, agency);
});
});
agencies.forEach(agency => {
// Create filter for agency
createFile(path.join(baseDir, `${agency.name}.html`), null, null, agency);
});
}
// Read constants
const agencies = readYamlConstants('_data/agencies.yaml');
const tracks = readYamlConstants('_data/fellow-tracks.yml');
const years = readYamlConstants('_data/years.yml');
// Create directories and files
createFellowFilterDirectoriesAndFiles(agencies, tracks, years);