56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
import { resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { runInNewContext } from 'node:vm';
|
|
import { format } from 'prettier';
|
|
|
|
const websiteDir = fileURLToPath(new URL('../', import.meta.url));
|
|
const outDir = resolve(websiteDir, 'docsearch-export');
|
|
let config;
|
|
|
|
// Evaluate our local configuration without instantiating an Algolia client.
|
|
// Shared functions and settings are expanded into a standalone dashboard file.
|
|
runInNewContext(
|
|
await readFile(resolve(websiteDir, 'docsearch.config.js'), 'utf8'),
|
|
{
|
|
Crawler: function (value) {
|
|
config = value;
|
|
}
|
|
}
|
|
);
|
|
if (!config) throw new Error('docsearch.config.js did not define a Crawler');
|
|
|
|
function serialize(value) {
|
|
if (typeof value === 'function') return `(${value.toString()})`;
|
|
if (Array.isArray(value)) return `[${value.map(serialize).join(',')}]`;
|
|
if (value !== null && typeof value === 'object') {
|
|
return `{${Object.entries(value)
|
|
.map(([key, item]) => `${JSON.stringify(key)}:${serialize(item)}`)
|
|
.join(',')}}`;
|
|
}
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
await mkdir(outDir, { recursive: true });
|
|
async function write(name, content) {
|
|
const path = resolve(outDir, name);
|
|
await writeFile(path, content);
|
|
console.log(path);
|
|
}
|
|
|
|
await write(
|
|
'crawler.config.js',
|
|
await format(
|
|
'// Generated by pnpm docsearch:export. Edit docsearch.config.js instead.\n' +
|
|
'// Keep the existing write key when pasting into the Algolia dashboard.\n' +
|
|
`new Crawler(${serialize(config)});\n`,
|
|
{ parser: 'babel', singleQuote: true, trailingComma: 'none' }
|
|
)
|
|
);
|
|
for (const [name, settings] of Object.entries(config.initialIndexSettings)) {
|
|
await write(
|
|
`${name}.settings.json`,
|
|
JSON.stringify(settings, null, 2) + '\n'
|
|
);
|
|
}
|
|
console.log('Export only: no changes were sent to Algolia.');
|