-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompile-partial.js
More file actions
208 lines (177 loc) · 6.01 KB
/
compile-partial.js
File metadata and controls
208 lines (177 loc) · 6.01 KB
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
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
const https = require('https');
const yaml = require('js-yaml');
const helpersDir = path.join(__dirname, 'src', 'helpers');
if (fs.existsSync(helpersDir)) {
fs.readdirSync(helpersDir).forEach((file) => {
if (file.endsWith('.js')) {
const name = path.basename(file, '.js');
const helperFn = require(path.join(helpersDir, file));
if (typeof helperFn !== 'function') {
console.warn(`⚠️ Skipping ${file}: not a function`);
return;
}
Handlebars.registerHelper(name, helperFn);
}
});
}
function fetchRemoteAntoraVersion() {
const url = 'https://raw.githubusercontent.com/redpanda-data/docs/main/antora.yml'
return new Promise((resolve, reject) => {
https.get(url, res => {
if (res.statusCode !== 200) {
return reject(new Error(`Failed to fetch antora.yml: ${res.statusCode}`))
}
let body = ''
res.on('data', chunk => (body += chunk))
res.on('end', () => {
try {
const cfg = yaml.load(body)
if (cfg.version == null) {
throw new Error('version field missing')
}
const version = String(cfg.version).trim()
resolve(version)
} catch (err) {
reject(err)
}
})
}).on('error', reject)
})
}
function readJson(filepath) {
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (err) {
console.warn(`⚠️ Failed to read context JSON (${filepath}), using empty context.`);
return {};
}
}
// Parse args
const args = process.argv.slice(2);
const [partialName, jsonPathOrFlag, ...rest] = args;
if (!partialName) {
console.error('❌ Usage: node compile-partial.js <partial-name> [context.json] [--jsScripts=\'["a.js"]\']');
process.exit(1);
}
// Flags
let jsonPath = null;
let jsScripts = [];
for (const arg of [jsonPathOrFlag, ...rest]) {
if (arg?.startsWith('--jsScripts=')) {
try {
jsScripts = JSON.parse(arg.split('=')[1]);
} catch {
console.warn('⚠️ Invalid --jsScripts argument. Skipping.');
}
} else if (arg && !arg.startsWith('--')) {
jsonPath = arg;
}
}
// Paths
const baseDir = __dirname;
const partialsDir = path.join(baseDir, 'src', 'partials');
const cssSrcDir = path.join(baseDir, 'src', 'css');
const jsSrcDir = path.join(baseDir, 'src', 'js');
const outDir = path.join(baseDir, 'src', 'static', 'assets', 'widgets');
const cssOutDir = path.join(outDir, 'css');
const jsOutDir = path.join(outDir, 'js');
// Load partial template
const templatePath = path.join(partialsDir, `${partialName}.hbs`);
if (!fs.existsSync(templatePath)) {
console.error(`❌ Partial not found: ${templatePath}`);
process.exit(1);
}
// Register all partials
fs.readdirSync(partialsDir).forEach((file) => {
if (file.endsWith('.hbs')) {
const name = path.basename(file, '.hbs');
const template = fs.readFileSync(path.join(partialsDir, file), 'utf8');
Handlebars.registerPartial(name, template);
}
});
async function ensureRootVersion(context) {
// Ensure the nested structure exists
context.site = context.site || {};
context.site.components = context.site.components || {};
context.site.components.ROOT = context.site.components.ROOT || {};
const rootComponent = context.site.components.ROOT;
// Always hard-code the title
rootComponent.title = 'Self-Managed';
// Use env var if available
const envVersion = process.env.LATEST_ENTERPRISE;
if (envVersion) {
rootComponent.latest = rootComponent.latest || {};
rootComponent.latest.version = envVersion.trim();
return;
}
// Fallback: remote fetch
try {
const remoteVersion = await fetchRemoteAntoraVersion();
rootComponent.latest = rootComponent.latest || {};
rootComponent.latest.version = remoteVersion;
} catch (err) {
console.warn(`⚠️ Could not fetch Antora version: ${err.message}`);
}
}
(async () => {
const templateSrc = fs.readFileSync(templatePath, 'utf8');
const template = Handlebars.compile(templateSrc);
const contextFromFile = jsonPath ? readJson(path.resolve(jsonPath)) : {};
const context = {
...contextFromFile,
env: {
ALGOLIA_API_KEY: process.env.ALGOLIA_API_KEY,
ALGOLIA_APP_ID: process.env.ALGOLIA_APP_ID,
ALGOLIA_INDEX_NAME: process.env.ALGOLIA_INDEX_NAME,
},
};
await ensureRootVersion(context);
let html = template(context);
// 🔽 Inject CSS if exists
const allCssFiles = fs.readdirSync(cssSrcDir)
.filter(f => f.endsWith('-bump.css') && f !== `${partialName}-bump.css`);
allCssFiles.push(`${partialName}-bump.css`);
const cssTags = [];
fs.mkdirSync(cssOutDir, { recursive: true });
allCssFiles.forEach(cssFile => {
const srcPath = path.join(cssSrcDir, cssFile);
const outPath = path.join(cssOutDir, cssFile);
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, outPath);
cssTags.push(`<link rel="stylesheet" href="/assets/widgets/css/${cssFile}">`);
} else if (cssFile !== `${partialName}-bump.css`) {
console.warn(`⚠️ Shared CSS not found: ${cssFile}`);
}
});
if (partialName === 'head-bump') {
// Prepend styles
if (cssTags.length) {
html = `${html}\n${cssTags.join('\n')}`;
}
}
// 🔽 Inject JS scripts
if (Array.isArray(jsScripts)) {
fs.mkdirSync(jsOutDir, { recursive: true });
const scriptTags = jsScripts.map((jsFile) => {
const jsSrcPath = path.join(jsSrcDir, jsFile);
const jsOutPath = path.join(jsOutDir, jsFile);
if (fs.existsSync(jsSrcPath)) {
fs.copyFileSync(jsSrcPath, jsOutPath);
return `<script src="/assets/widgets/js/${jsFile}"></script>`;
} else {
console.warn(`⚠️ JavaScript not found: ${jsSrcPath}`);
return '';
}
}).filter(Boolean).join('\n ');
if (scriptTags) {
html = `${html}\n${scriptTags}`;
}
}
// Output HTML
fs.mkdirSync(outDir, { recursive: true });
const outFile = path.join(outDir, `${partialName}.html`);
fs.writeFileSync(outFile, html);
})();