刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
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`);