-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
183 lines (154 loc) · 4.92 KB
/
build.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// DEPENDENCIES
const Airtable = require('airtable');
const read = require('read-file');
const inflection = require( 'inflection' );
const mkdirp = require("mkdirp");
const rimraf = require('rimraf')
const fs = require("fs");
const Handlebars = require("handlebars");
const helpers = require('handlebars-helpers')({
handlebars: Handlebars
});
const airtable = new Airtable({apiKey: process.env.AIRTABLE_KEY}).base('appOHXPnlXEfqnWLd');
const _ = require("lodash");
const tocTemplate = Handlebars.compile(read.sync("./TABLE_OF_CONTENTS.handlebars", 'utf8'));
const makeTemplate = Handlebars.compile(read.sync("./.templates/vehicle-make.handlebars", 'utf8'));
const modelTemplate = Handlebars.compile(read.sync("./.templates/vehicle-model.handlebars", 'utf8'));
const vehicleTemplate = Handlebars.compile(read.sync("./.templates/vehicle.handlebars", 'utf8'));
const vehiclesTemplate = Handlebars.compile(read.sync("./.templates/vehicles.handlebars", 'utf8'));
// DATA TABLES
const makesTable = airtable('Vehicle Makes');
const modelsTable = airtable('Vehicle Models');
const vehiclesTable = airtable('Vehicles');
const makeSlug = function(str) {
return inflection.dasherize(str).replace('(','').replace(')','').toLowerCase();
}
const clean = function() {
// rimraf.sync('./vehicles/');
}
// FETCH METHODS
const getModels = function() {
return modelsTable.select({
view: "All Models"
}).all().then((models) => {
let vehicObject = {};
models.forEach((model) => {
let fields = model._rawJson.fields;
fields.slug = makeSlug(fields.Name);
vehicObject[model.id] = fields;
// return fields;
})
return vehicObject;
}).catch((err) => {
console.log(err);
});
}
const getMakes = function() {
return makesTable.select({
view: "All Compatible"
}).all().then((makes) => {
console.log(makes);
return makes.map((make) => {
let fields = make._rawJson.fields;
fields.slug = makeSlug(fields.Name);
return fields;
});
}).catch((err) => {
console.log(err);
});
}
const getVehicles = function() {
return vehiclesTable.select({
view: "All Vehicles Ungrouped"
}).all().then((vehicles) => {
let vehicObject = {};
vehicles.forEach((vehicle) => {
let fields = vehicle._rawJson.fields;
// console.log(fields);
fields.slug = makeSlug(fields.FullName);
if (fields.PRRepoNames && fields.PRRepoNames.length) {
fields.prLinks = fields.PRRepoNames.map((prName) => {
return `- [${prName}](https://github.com/${prName})`;
}).join("\n");
}
vehicObject[vehicle.id] = fields;
// return fields;
})
return vehicObject;
}).catch((err) => {
console.log(err);
});
}
// RENDER METHODS
const renderTocTemplate = (makes) => {
var output = tocTemplate(makes);
fs.writeFileSync(`./TABLE_OF_CONTENTS.md`,output);
}
const renderVehiclesTemplate = (makes) => {
var output = vehiclesTemplate(makes);
fs.writeFileSync(`./vehicles/README.md`,output);
}
const renderModelTemplate = (model) => {
var output = modelTemplate(model);
mkdirp.sync(`./vehicles/${model.make.slug}/${model.slug}/`);
fs.writeFileSync(`./vehicles/${model.make.slug}/${model.slug}.md`,output);
}
const renderVehicleTemplate = (vehicle) => {
var output = vehicleTemplate(vehicle);
mkdirp.sync(`./vehicles/${vehicle.make.slug}/${vehicle.model.slug}`);
fs.writeFileSync(`./vehicles/${vehicle.make.slug}/${vehicle.model.slug}/${vehicle.slug}.md`,output);
}
const renderMakeTemplate = (make) => {
var output = makeTemplate(make);
mkdirp.sync(`./vehicles/${make.slug}/`);
fs.writeFileSync(`./vehicles/${make.slug}/README.md`,output);
}
const renderTemplates = (data) => {
const makes = data.makes;
const vehicles = data.vehicles;
const models = data.models;
//CLEAN FIRST
clean();
makes.forEach((make) => {
make.Models = make.Models.map((modelId) => {
var model = models[modelId];
model.make = make;
model.Vehicles = model.Vehicles.map((vehicleId) => {
var vehicle = vehicles[vehicleId];
vehicle.model = model;
return vehicle;
});
return model;
});
make.Models = _.orderBy(make.Models,['Name'], ['asc']);
make.Vehicles = make.Vehicles.map((vehicleId) => {
var vehicle = vehicles[vehicleId];
vehicle.make = make;
return vehicle;
});
make.Vehicles = _.orderBy(make.Vehicles,['Model','Year'], ['asc','asc']);
renderMakeTemplate(make);
make.Models.forEach((model) => {
renderModelTemplate(model);
});
make.Vehicles.forEach((vehicle) => {
renderVehicleTemplate(vehicle);
});
});
renderVehiclesTemplate({
makes
});
renderTocTemplate({
makes
});
}
// CONCAT ALL THE DATA
Promise.all([getMakes(),getVehicles(), getModels()]).then((responses) => {
return {
makes: responses[0],
vehicles: responses[1],
models: responses[2]
};
}).catch((e) => {
console.log(e);
}).then(renderTemplates);