108 lines
3.1 KiB
JavaScript
108 lines
3.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.mkdocsConfigService = void 0;
|
|
const promises_1 = require("fs/promises");
|
|
const env_1 = require("../../config/env");
|
|
const logger_1 = require("../../utils/logger");
|
|
const yaml_1 = require("yaml");
|
|
/**
|
|
* Custom YAML tag schema to preserve !!python/name: and !!python/object: tags.
|
|
* Without this, the yaml library would reject these custom tags.
|
|
*/
|
|
const pythonNameTag = {
|
|
identify: () => false,
|
|
tag: '!python/name',
|
|
collection: undefined,
|
|
resolve: (str) => str,
|
|
};
|
|
const pythonObjectTag = {
|
|
identify: () => false,
|
|
tag: '!python/object',
|
|
collection: undefined,
|
|
resolve: (str) => str,
|
|
};
|
|
/**
|
|
* Parse mkdocs.yml content with support for !!python/name: tags.
|
|
* Returns a yaml Document that preserves comments and formatting.
|
|
*/
|
|
function parseConfig(content) {
|
|
return (0, yaml_1.parseDocument)(content, {
|
|
customTags: (tags) => [
|
|
...tags,
|
|
pythonNameTag,
|
|
pythonObjectTag,
|
|
],
|
|
keepSourceTokens: true,
|
|
});
|
|
}
|
|
/**
|
|
* Validate that a string is valid YAML (with python tags support).
|
|
* Returns null if valid, error message if invalid.
|
|
*/
|
|
function validateYaml(content) {
|
|
try {
|
|
const doc = parseConfig(content);
|
|
const errors = doc.errors;
|
|
if (errors.length > 0) {
|
|
return errors.map(e => e.message).join('; ');
|
|
}
|
|
return null;
|
|
}
|
|
catch (err) {
|
|
return err.message;
|
|
}
|
|
}
|
|
async function readConfig() {
|
|
return (0, promises_1.readFile)(env_1.env.MKDOCS_CONFIG_PATH, 'utf-8');
|
|
}
|
|
async function writeConfig(content) {
|
|
// Validate YAML first
|
|
const error = validateYaml(content);
|
|
if (error) {
|
|
throw new Error(`Invalid YAML: ${error}`);
|
|
}
|
|
// Create backup
|
|
try {
|
|
await (0, promises_1.copyFile)(env_1.env.MKDOCS_CONFIG_PATH, `${env_1.env.MKDOCS_CONFIG_PATH}.bak`);
|
|
}
|
|
catch {
|
|
logger_1.logger.warn('Could not create backup of mkdocs.yml');
|
|
}
|
|
await (0, promises_1.writeFile)(env_1.env.MKDOCS_CONFIG_PATH, content, 'utf-8');
|
|
}
|
|
/**
|
|
* Trigger `mkdocs build --clean` via the build trigger HTTP server
|
|
* running inside the MkDocs container on port 8001.
|
|
*/
|
|
async function triggerBuild() {
|
|
const buildUrl = `${env_1.env.MKDOCS_PREVIEW_URL.replace(':8000', ':8001')}/build`;
|
|
const startTime = Date.now();
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 130_000);
|
|
const response = await fetch(buildUrl, {
|
|
method: 'POST',
|
|
signal: controller.signal,
|
|
});
|
|
clearTimeout(timeout);
|
|
const data = await response.json();
|
|
return data;
|
|
}
|
|
catch (err) {
|
|
const duration = Date.now() - startTime;
|
|
logger_1.logger.error('MkDocs build failed', err);
|
|
return {
|
|
success: false,
|
|
output: `Build error: ${err.message}`,
|
|
duration,
|
|
};
|
|
}
|
|
}
|
|
exports.mkdocsConfigService = {
|
|
readConfig,
|
|
writeConfig,
|
|
validateYaml,
|
|
parseConfig,
|
|
triggerBuild,
|
|
};
|
|
//# sourceMappingURL=mkdocs-config.service.js.map
|