-
Notifications
You must be signed in to change notification settings - Fork 142
/
content-for-config.ts
67 lines (60 loc) · 2.14 KB
/
content-for-config.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
import Plugin from 'broccoli-plugin';
import type { Node } from 'broccoli-node-api';
import { readFileSync } from 'fs-extra';
import { join } from 'path';
export default class ContentForConfig extends Plugin {
// The object keys are the content types and each value is the HTML
// code that should replace the corresponding {{content-for}}
// Example: { body: '<p>This snippet replaces content-for \"body\" in the app index.html</p>' }
private contentFor: any;
private defaultContentForTypes = [
'head',
'test-head',
'head-footer',
'test-head-footer',
'body',
'test-body',
'body-footer',
'test-body-footer',
'config-module',
'app-boot',
];
constructor(configTree: Node, private options: any) {
super([configTree], {
annotation: 'embroider:content-for-config',
persistentOutput: true,
needsCache: false,
});
}
readContents() {
if (!this.contentFor) {
throw new Error(`ContentForConfig not available until after the build`);
}
return this.contentFor;
}
build() {
if (!this.contentFor) this.contentFor = {};
const availableContentForTypes = this.options.availableContentForTypes ?? [];
const extendedContentTypes = new Set([...this.defaultContentForTypes, ...availableContentForTypes]);
let appConfig = this.getAppConfig();
appConfig.forEach((configPath: { file: string; json: any }) => {
extendedContentTypes.forEach(contentType => {
const matchExp = this.options.pattern.match;
if (!this.contentFor[configPath.file]) this.contentFor[configPath.file] = {};
if (!this.contentFor[configPath.file][contentType]) {
let contents = this.options.pattern.replacement.call(null, configPath.json, matchExp, contentType);
this.contentFor[configPath.file][contentType] = contents;
}
});
});
}
getAppConfig() {
return this.options.configPaths.map((configPath: { file: string; path: string }) => {
let config = readFileSync(join(this.inputPaths[0], configPath.path), { encoding: 'utf8' });
return {
file: configPath.file,
json: JSON.parse(config),
};
});
}
}