刘光辉
12 小时以前 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
66
67
68
69
70
71
72
73
74
import { realpathSync, writeFileSync } from 'node:fs';
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
import process from 'node:process';
 
function fail(message) {
  throw new Error(message);
}
 
const publicBaseUrl = process.env.ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL?.replace(/\/+$/, '');
const apiBaseUrl = process.env.ONLYOFFICE_PLUGIN_API_BASE_URL?.replace(/\/+$/, '');
const outputValue = process.env.ONLYOFFICE_PLUGIN_CONFIG_OUTPUT;
const releaseRootValue = process.env.ONLYOFFICE_RELEASE_ROOT;
const release = process.env.ONLYOFFICE_PLUGIN_RELEASE || '0.1.0';
 
if (!publicBaseUrl) fail('ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL is required');
if (!apiBaseUrl) fail('ONLYOFFICE_PLUGIN_API_BASE_URL is required');
if (!outputValue || !isAbsolute(outputValue)) fail('ONLYOFFICE_PLUGIN_CONFIG_OUTPUT must be an absolute path');
if (!releaseRootValue || !isAbsolute(releaseRootValue)) fail('ONLYOFFICE_RELEASE_ROOT must be an absolute path');
if (release !== '0.1.0' && release !== '0.1.0-dev') fail('ONLYOFFICE_PLUGIN_RELEASE must be 0.1.0 or 0.1.0-dev');
 
let parsedBaseUrl;
try {
  parsedBaseUrl = new URL(publicBaseUrl);
} catch {
  fail('ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL must be a valid http/https URL');
}
if (!['http:', 'https:'].includes(parsedBaseUrl.protocol) || parsedBaseUrl.search || parsedBaseUrl.hash) {
  fail('ONLYOFFICE_PLUGIN_PUBLIC_BASE_URL must be a valid http/https URL');
}
let parsedApiBaseUrl;
try {
  parsedApiBaseUrl = new URL(apiBaseUrl);
} catch {
  fail('ONLYOFFICE_PLUGIN_API_BASE_URL must be a valid http/https URL');
}
if (!['http:', 'https:'].includes(parsedApiBaseUrl.protocol) || parsedApiBaseUrl.search || parsedApiBaseUrl.hash) {
  fail('ONLYOFFICE_PLUGIN_API_BASE_URL must be a valid http/https URL');
}
 
const releaseRoot = realpathSync(releaseRootValue);
const output = resolve(outputValue);
const outputParent = realpathSync(dirname(output));
const outputRealPath = resolve(outputParent, output.slice(dirname(output).length + 1));
const relativeToReleaseRoot = relative(releaseRoot, outputRealPath);
if (relativeToReleaseRoot === '' || (!relativeToReleaseRoot.startsWith(`..${sep}`) && relativeToReleaseRoot !== '..')) {
  fail('ONLYOFFICE_PLUGIN_CONFIG_OUTPUT must be outside ONLYOFFICE_RELEASE_ROOT');
}
 
function quote(value) {
  return `'${value.replaceAll("'", "''")}'`;
}
 
const yaml = [
  'plugins:',
  '  definitions:',
  '    eln-template-annotator:',
  "      guid: 'asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}'",
  "      version: '0.1.0'",
  `      public-base-url: ${quote(publicBaseUrl)}`,
  `      release: ${quote(release)}`,
  `      api-base-url: ${quote(apiBaseUrl)}`,
  '  scenes:',
  '    - biz-scene: eln.template.annotate',
  '      biz-module: eln.template',
  '      plugin-codes:',
  '        - eln-template-annotator',
  '      autostart-plugin-code: eln-template-annotator',
  '      disabled-plugin-guids:',
  "        - 'asc.{AA2EA9B6-9EC2-415F-9762-634EE8D9A95E}'",
  '',
].join('\n');
 
writeFileSync(outputRealPath, yaml, { encoding: 'utf8' });
process.stdout.write(`${outputRealPath}\n`);