Repository navigation
Expand file tree
/
Copy pathcompile-partial.js
More file actions
223 lines (191 loc) · 7.12 KB
/
Copy pathcompile-partial.js
File metadata and controls
223 lines (191 loc) · 7.12 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
const https = require('https');
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);
}
});
}
// The latest Self-Managed version, as the live docs site states it in every
// page's <meta name="latest-redpanda-version">. The site is public, so this
// needs no token (redpanda-data/docs, which holds antora.yml, is private).
const LATEST_VERSION_URL = 'https://docs.redpanda.com/home/'
function fetchLatestVersion () {
return new Promise((resolve, reject) => {
// agent: false so no kept-alive socket holds the process open afterwards.
const req = https.get(LATEST_VERSION_URL, { agent: false }, (res) => {
if (res.statusCode !== 200) {
// Drain the response: an unread body keeps the socket, and the
// process, open, so the build would hang here.
res.resume()
return reject(new Error(`${LATEST_VERSION_URL} returned ${res.statusCode}`))
}
let body = ''
res.setEncoding('utf8')
// A connection dropped mid-body errors on res, not req.
res.on('error', reject)
res.on('data', (chunk) => (body += chunk))
res.on('end', () => {
const match = /<meta\s+name="latest-redpanda-version"\s+content="([^"]*)"/i.exec(body)
const version = match && match[1].trim()
if (!version || !/^\d+\.\d+$/.test(version)) {
return reject(new Error(`no latest-redpanda-version meta tag in ${LATEST_VERSION_URL}`))
}
resolve(version)
})
})
req.on('error', reject)
req.setTimeout(10000, () => req.destroy(new Error('timed out after 10 s')))
})
}
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: the live docs site
try {
const remoteVersion = await fetchLatestVersion();
rootComponent.latest = rootComponent.latest || {};
rootComponent.latest.version = remoteVersion;
} catch (err) {
// Without a version the Bump widgets ship "Self-Managed v" as the search
// tag and an empty latest-redpanda-version meta tag, so a release build
// (CI) stops here. Locally it only warns, so the build works offline.
if (process.env.CI === 'true') {
console.error(`❌ Could not get the latest Redpanda version: ${err.message}. Set LATEST_ENTERPRISE to build without it.`);
process.exit(1);
}
console.warn(`⚠️ Could not get the latest Redpanda 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);
})();