import { realpathSync, readFileSync, writeFileSync } from 'node:fs';
|
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
import { stringify } from 'yaml';
|
|
import { parseRegistry } from '../src/registry.mjs';
|
|
/** @param {string} message @returns {never} */
|
function fail(message) {
|
throw new Error(message);
|
}
|
|
/** @param {string | undefined} value @param {string} name */
|
function httpUrl(value, name) {
|
if (!value) fail(`${name} is required`);
|
/** @type {URL} */
|
let parsed;
|
try {
|
parsed = new URL(value.replace(/\/+$/u, ''));
|
} catch {
|
fail(`${name} must be a valid http/https URL`);
|
}
|
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.username || parsed.password || parsed.search || parsed.hash) {
|
fail(`${name} must be a valid http/https URL`);
|
}
|
return `${parsed.origin}${parsed.pathname.replace(/\/+$/u, '')}`;
|
}
|
|
const gatewayRoot = resolve(import.meta.dirname, '..');
|
const registry = parseRegistry(readFileSync(resolve(gatewayRoot, '../registry.yaml'), 'utf8'));
|
const publicBaseUrl = httpUrl(process.env.ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL, 'ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL');
|
const apiBaseUrl = httpUrl(process.env.ONLYOFFICE_PLUGIN_API_BASE_URL, 'ONLYOFFICE_PLUGIN_API_BASE_URL');
|
const release = process.env.ONLYOFFICE_PLUGIN_RELEASE ?? registry.release;
|
const outputValue = process.env.ONLYOFFICE_PLUGIN_CONFIG_OUTPUT;
|
const releaseRootValue = process.env.ONLYOFFICE_RELEASE_ROOT;
|
|
if (release !== '0.1.0' && release !== '0.1.0-dev') fail('ONLYOFFICE_PLUGIN_RELEASE must be 0.1.0 or 0.1.0-dev');
|
if (typeof outputValue !== 'string' || !isAbsolute(outputValue)) fail('ONLYOFFICE_PLUGIN_CONFIG_OUTPUT must be an absolute path');
|
if (typeof releaseRootValue !== 'string' || !isAbsolute(releaseRootValue)) fail('ONLYOFFICE_RELEASE_ROOT must be an absolute path');
|
|
const releaseRoot = realpathSync(releaseRootValue);
|
const outputParent = realpathSync(dirname(outputValue));
|
const output = resolve(outputParent, outputValue.slice(dirname(outputValue).length + 1));
|
const relativeToRelease = relative(releaseRoot, output);
|
if (relativeToRelease === '' || (!relativeToRelease.startsWith(`..${sep}`) && relativeToRelease !== '..')) {
|
fail('ONLYOFFICE_PLUGIN_CONFIG_OUTPUT must be outside ONLYOFFICE_RELEASE_ROOT');
|
}
|
|
const definitions = Object.fromEntries(registry.plugins.map((plugin) => [plugin.code, {
|
guid: plugin.guid,
|
version: plugin.version,
|
'public-base-url': publicBaseUrl,
|
release,
|
'api-base-url': apiBaseUrl,
|
}]));
|
const scenes = registry.plugins.map((plugin) => ({
|
'biz-scene': plugin.bizScene,
|
'biz-module': plugin.bizModule,
|
'plugin-codes': [plugin.code],
|
...(plugin.autostart ? { 'autostart-plugin-code': plugin.code } : {}),
|
...(plugin.disabledPluginGuids.length ? { 'disabled-plugin-guids': [...plugin.disabledPluginGuids] } : {}),
|
}));
|
|
writeFileSync(output, stringify({ plugins: { definitions, scenes } }, { defaultStringType: 'QUOTE_SINGLE', lineWidth: 0 }), 'utf8');
|
process.stdout.write(`${output}\n`);
|