-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.ts
401 lines (383 loc) · 14.7 KB
/
build.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import {
AngularExpressionReplaceCommand,
ClassDomReplaceCommand,
ContentStepConfig,
CopyStep,
CopyStepConfig,
DomReplaceCommand,
FileContents,
FileUtil,
HtAccessToNetlifyConfigReplaceCommand,
HtmlLinks,
HtmlMeta,
OutputFunc,
Ssg,
SsgConfig,
SsgContext,
SsiEchoVarReplaceCommand,
SsiIfReplaceCommand,
SsiIncludeReplaceCommand,
SsiIncludeReplaceCommandTransformer,
SsiLastModifiedReplaceCommand,
SsiSetVarReplaceCommand,
StringEchoVarReplaceCommand
} from "ssg-api"
import { WitnessReplacerFactory } from "./people/witness/WitnessReplacerFactory.js"
import { AuthorReplaceCommand } from "./people/author/AuthorReplaceCommand.js"
import { rr0DefaultCopyright } from "./RR0DefaultCopyright.js"
import { MetaLinkReplaceCommand } from "./MetaLinkReplaceCommand.js"
import { ImageCommand } from "./ImageCommand.js"
import { BaseReplaceCommand } from "./BaseReplaceCommand.js"
import { DescriptionReplaceCommand } from "./DescriptionReplaceCommand.js"
import path from "path"
import { CodeReplacerFactory } from "./tech/info/soft/proj/impl/lang/CodeReplacerFactory.js"
import { DefaultContentVisitor } from "./DefaultContentVisitor.js"
import fs from "fs"
import {
AllDataService,
AnchorReplaceCommand,
APIFactory,
BookContentVisitor,
BookDirectoryStep,
CaseAnchorHandler,
CaseDirectoryStep,
CaseFactory,
CaseService,
CaseSummaryRenderer,
ChronologyReplacerActions,
ChronologyReplacerFactory,
CLI,
ContentVisitor,
CsvMapper,
DataAnchorHandler,
EventReplacer,
EventReplacerFactory,
GooglePlaceService,
HtmlRR0SsgContext,
HtmlTable,
HttpSource,
IndexedReplacerFactory,
LanguageReplaceCommand,
NoteFileCounter,
NoteRenderer,
NoteReplacer,
NoteReplacerFactory,
OpenGraphCommand,
OrganizationFactory,
OrganizationService,
OutlineReplaceCommand,
PeopleDirectoryStepFactory,
PeopleFactory,
PeopleReplacerFactory,
PeopleService,
PersistentSourceRegistry,
PlaceReplacerFactory,
RR0ContentStep,
RR0EventFactory,
RR0Mapping,
RR0SsgContextImpl,
SearchIndexStep,
SearchVisitor,
SourceFileCounter,
SourceIndexStep,
SourceRenderer,
SourceReplacer,
SourceReplacerFactory,
SsiTitleReplaceCommand,
Time,
TimeElementFactory,
TimeLinkDefaultHandler,
TimeReplacer,
TimeReplacerFactory,
TimeService,
TimeTextBuilder,
TypedDataFactory,
UnitReplaceCommand
} from "@rr0/cms"
interface RR0BuildArgs {
/**
* Configuration file
*/
config?: string
/**
* If the search index must be regenerated or not.
* For ex: "pages,sources"
*/
reindex?: string[]
/**
* Comma-separated list of file patterns to parse as contents.
*/
contents?: string[]
/**
* Comma-separated list of file patterns to copy to out dir.
*/
copies?: string[]
/**
* Comma-separated list of file patterns to books to generate TOCs for.
*/
books?: string
/**
* Force re-generation even if file has not changed.
*/
force?: string
}
console.time("ssg")
let args = new CLI().getArgs<RR0BuildArgs>()
const configFile = args.config
if (configFile) {
args = JSON.parse(FileContents.read(configFile).contents)
}
const cliContents = args.contents
console.debug("contents", cliContents)
const mandatoryRoots = ["people/*.html", "science/crypto/ufo/enquete/dossier/*.html"]
const contentRoots = cliContents
? cliContents.concat(mandatoryRoots)
: [
"croyance/**/*.html",
"index.html", "404.html", "googlebe03dcf00678bb7c.html", "Contact.html", "Copyright.html", "preambule.html", "FAQ.html", "Referencement.html",
"time/**/*.html",
"book/**/*.html",
"droit/**/*.html",
"org/**/*.html",
"people/**/*.html",
"place/**/*.html",
"politique/**/*.html",
"science/**/*.html",
"tech/**/*.html",
"udb/*.html",
"js/**/*.html"
]
const copiesArg = args.copies
const copies = copiesArg ? copiesArg : [
"favicon.ico", "manifest.json", "opensearch.xml", "apple-touch-icon.png", "apple-touch-icon_400x400.png", "screenshot1.jpg",
"rr0.css", "map.css", "diagram.css", "print.css", "figure.css", "section.css", "table.css", "nav.css",
// "**/*.png", "**/*.jpg", "**/*.gif", "**/*.webp", "!out/**/*",
"**/*.cmmn", "**/*.bpmn",
"tech/info/soft/reseau/protocole/index.js", "tech/info/soft/reseau/protocole/ports.json", "tech/info/soft/reseau/protocole/index.css",
"tech/info/soft/data/doc/index.js", "tech/info/soft/data/doc/index.json", "tech/info/soft/data/doc/index.css",
"people/index.js", "people/index.css", "people/witness/index.css",
"search/SearchComponent.mjs", "search/index.json", "search/search.css",
"source/index.css", "note/index.css",
"link.css", "quote.css",
"time/DualRangeComponent.mjs",
"index/index.js", "lang/form.js", "lang/form.css", "lang/speech.js", "lang/speech.css",
"croyance/divin/theisme/mono/livre/islam/coran/index.js"
]
const outDir = "out"
const config: SsgConfig = {
getOutputPath(context: SsgContext): string {
return path.join(outDir, context.file.name)
}
}
const outputFunc: OutputFunc
= async (context: SsgContext, outFile: FileContents): Promise<void> => {
try {
context.log("Writing", outFile.name)
await outFile.write()
context.file.contents = outFile.contents
} catch (e) {
context.error(outFile.name, e)
}
}
const timeFormat: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "numeric",
weekday: "long",
hour: "2-digit",
minute: "2-digit"
}
const timeContext = new TimeContext()
const context = new RR0SsgContextImpl("fr", timeContext, config)
context.setVar("mail", "rr0@rr0.org")
const siteBaseUrl = "https://rr0.org/"
const htAccessToNetlifyConfig: ContentStepConfig = {
replacements: [new HtAccessToNetlifyConfigReplaceCommand(siteBaseUrl)],
roots: [".htaccess"],
getOutputPath(_context: SsgContext): string {
return path.join(outDir, "netlify.toml")
}
}
const eventFactory = new RR0EventFactory()
const sightingFactory = new TypedDataFactory(eventFactory, "sighting", ["index"])
const orgFactory = new OrganizationFactory(eventFactory)
const caseFactory = new CaseFactory(eventFactory)
const peopleFactory = new PeopleFactory(eventFactory)
const apiFactory = new APIFactory(eventFactory)
const bookFactory = new TypedDataFactory(eventFactory, "book")
const articleFactory = new TypedDataFactory(eventFactory, "article")
const dataService = new AllDataService(
[orgFactory, caseFactory, peopleFactory, bookFactory, articleFactory, sightingFactory, apiFactory])
dataService.getFromDir("", ["people", "case"]).then(data => {
console.debug(data)
})
const timeTextBuilder = new TimeTextBuilder(timeFormat)
const timeService = new TimeService(dataService, timeTextBuilder)
const peopleService = new PeopleService(dataService, peopleFactory)
const apiKey = process.env.GOOGLE_MAPS_API_KEY
if (!apiKey) {
throw Error("GOOGLE_MAPS_API_KEY is required")
}
context.setVar("mapsApiKey", apiKey)
const placeService = new GooglePlaceService("place", apiKey)
const orgService = new OrganizationService([], "org", undefined)
timeService.getFiles().then(async (timeFiles) => {
context.setVar("timeFilesCount", timeFiles.length)
const timeElementFactory = new TimeElementFactory(timeService.renderer)
const caseService = new CaseService(dataService, caseFactory, timeElementFactory)
const timeReplacer = new TimeReplacer(timeElementFactory)
const peopleList = await peopleService.getAll()
context.setVar("peopleFilesCount", peopleList.length)
const bookMeta = new Map<string, HtmlMeta>()
const bookLinks = new Map<string, HtmlLinks>()
const ufoCasesStep = await CaseDirectoryStep.create(outputFunc, config, caseService)
const peopleDirectoryFactory = new PeopleDirectoryStepFactory(outputFunc, config, peopleService)
const peopleSteps = await peopleDirectoryFactory.create()
// Publish case.json files so that vraiufo.com will find them
copies.push(...(ufoCasesStep.config.rootDirs).map(dir => path.join(dir, "case.json")))
await FileUtil.writeFile(path.join(outDir, "casesDirs.json"), JSON.stringify(ufoCasesStep.config.rootDirs), "utf-8")
copies.push(...(peopleSteps.reduce((rootDirs, peopleStep) => {
rootDirs.push(...peopleStep.config.rootDirs)
return rootDirs
}, [])).map(dir => path.join(dir, "people.json")))
await FileUtil.writeFile(path.join(outDir, "peopleDirs.json"),
JSON.stringify(peopleList.map(people => people.dirName)), "utf-8")
const searchVisitor = new SearchVisitor(
{notIndexedUrls: ["404.html", "Referencement.html"], indexWords: false}, timeTextBuilder
)
const sourceRenderer = new SourceRenderer(timeTextBuilder)
const sourceRegistryFileName = "source/index.json"
const baseUrl = "https://rr0.org"
const http = new HttpSource()
const sourceFactory = new PersistentSourceRegistry(dataService, http, baseUrl, sourceRegistryFileName, timeFormat)
const noteCounter = new NoteFileCounter()
const noteRenderer = new NoteRenderer(noteCounter)
const caseRenderer = new CaseSummaryRenderer(noteRenderer, sourceFactory, sourceRenderer, timeElementFactory)
// const actions: ChronologyReplacerActions = {read: ["backup", "fetch"], write: ["backup", "pages"]}
// const actions: ChronologyReplacerActions = {read: [], write: ["backup"]}
const actions: ChronologyReplacerActions = {read: ["fetch"], write: ["backup"]}
const rr0Mapping = new RR0Mapping(actions)
const databaseAggregationCommand = new DomReplaceCommand(".contents ul",
new ChronologyReplacerFactory(timeService,
[rr0Mapping /*new GeipanRR0Mapping(actions)
/*, baseOvniFranceRR0Mapping, fuforaRR0Mapping, nuforcRR0Mapping, urecatRR0Mapping*/
], caseRenderer)
)
const timeDefaultHandler = (context: HtmlRR0SsgContext): string | undefined => {
let title: string | undefined
title = Time.titleFromFile(context, context.file.name, timeTextBuilder)
return title
}
const pageReplaceCommands = [
new BaseReplaceCommand("/"),
new LanguageReplaceCommand(),
new SsiEchoVarReplaceCommand("copyright", [rr0DefaultCopyright]),
new StringEchoVarReplaceCommand(),
new AngularExpressionReplaceCommand(),
new SsiIfReplaceCommand(),
new SsiSetVarReplaceCommand("title", (_match: string, ...args: any[]) => `<title>${args[0]}</title>`),
new SsiSetVarReplaceCommand("url",
(_match: string, ...args: any[]) => `<meta name="url" content="${args[0]}"/>`),
new SsiLastModifiedReplaceCommand(timeFormat),
new SsiTitleReplaceCommand([timeDefaultHandler]),
new DescriptionReplaceCommand("UFO data for french-reading people", "abstract"),
new AuthorReplaceCommand(timeService)
]
// const sourceCounter = new SourceFileCounter()
const sourceCounter = new SourceFileCounter()
const sourceReplacer = new SourceReplacer(sourceRenderer, sourceFactory, sourceCounter)
const sourceReplacerFactory = new SourceReplacerFactory(sourceReplacer)
const noteReplacer = new NoteReplacer(noteRenderer)
const noteReplacerFactory = new NoteReplacerFactory(noteReplacer)
const eventReplacer = new EventReplacer(caseRenderer, dataService)
const contentsReplaceCommand = [
new ClassDomReplaceCommand(new EventReplacerFactory(eventReplacer), "event"),
new ClassDomReplaceCommand(sourceReplacerFactory, "source"),
new DomReplaceCommand("time", new TimeReplacerFactory(timeReplacer)),
new DomReplaceCommand("code", new CodeReplacerFactory()),
new ClassDomReplaceCommand(new PeopleReplacerFactory(peopleService), "people"),
new ClassDomReplaceCommand(new PlaceReplacerFactory(), "place"),
new ClassDomReplaceCommand(new WitnessReplacerFactory(), "temoin", "temoin1", "temoin2", "temoin3"),
new ClassDomReplaceCommand(noteReplacerFactory, "note"),
new ClassDomReplaceCommand(new IndexedReplacerFactory(), "indexed"),
new UnitReplaceCommand(),
new MetaLinkReplaceCommand(new TimeLinkDefaultHandler(timeFiles, timeTextBuilder)),
databaseAggregationCommand
]
const ssg = new Ssg(config)
const getOutputPath = (context: SsgContext): string => path.join(outDir, context.file.name)
const force = args.force === "true"
const toProcess = new Set<string>(
["people/index.html", "people/witness/index.html", "people/militaires.html", "people/scientifiques.html", "people/astronomes.html", "people/politicians.html", "people/dirigeants.html", "people/pilotes.html", "people/contactes.html", "people/ufologues.html", "tech/info/Personnes.html", "people/Contributeurs.html"])
const csvTransformer = new class implements SsiIncludeReplaceCommandTransformer {
transform(context: SsgContext, file: FileContents): string | undefined {
const fileName = file.name
if (!fileName.endsWith(".csv")) {
return undefined
}
const csv = fs.readFileSync(fileName, {encoding: "utf-8"})
const headers = []
const obj: any[] = new CsvMapper().parse(csv, headers)
return HtmlTable.create(obj, headers)
}
}()
const includeStep = new RR0ContentStep(
[htAccessToNetlifyConfig, {
roots: contentRoots,
replacements: [new SsiIncludeReplaceCommand([csvTransformer])],
getOutputPath
}],
outputFunc, [], [], force, "content includes", toProcess
)
ssg.add(includeStep)
ssg.add(ufoCasesStep)
ssg.add(...peopleSteps)
if (contentRoots) {
const contentVisitor = new DefaultContentVisitor(dataService, caseRenderer, timeElementFactory)
const contentVisitors: ContentVisitor[] = [contentVisitor, searchVisitor]
if (args.books) {
contentVisitors.push(new BookContentVisitor(bookMeta, bookLinks))
}
const contentReplacements = [
...pageReplaceCommands,
...contentsReplaceCommand,
new OutlineReplaceCommand(),
new AnchorReplaceCommand(siteBaseUrl,
[new CaseAnchorHandler(caseService, timeTextBuilder), new DataAnchorHandler(dataService)]),
new ImageCommand(outDir, 275, 500),
new OpenGraphCommand(outDir, timeFiles, baseUrl, timeTextBuilder)
]
ssg.add(new RR0ContentStep([{roots: contentRoots, replacements: contentReplacements, getOutputPath}],
outputFunc, [], contentVisitors, force, "contents replacements", toProcess))
}
if (args.books) {
ssg.add(await BookDirectoryStep.create(outputFunc, config, bookMeta, bookLinks))
}
const reindex = args.reindex
if (reindex?.includes("search")) {
ssg.add(new SearchIndexStep("search/index.json", searchVisitor))
}
if (reindex?.includes("sources")) {
ssg.add(new SourceIndexStep(sourceRegistryFileName, sourceFactory))
}
if (copies) {
const copyConfig: CopyStepConfig = {
getOutputPath,
sourcePatterns: copies,
options: {ignore: ["node_modules/**", "out/**"]}
}
ssg.add(new CopyStep(copyConfig))
}
try {
const result = await ssg.start(context)
context.log("Completed", result)
} catch (err) {
try {
context.error(err, context.file.name)
} catch (e) {
context.error(err)
}
} finally {
console.timeEnd("ssg")
}
})