vite-plugin-spire.officejs-test 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs CHANGED
@@ -1,113 +1,456 @@
1
- import { fileURLToPath as n } from "node:url";
2
- import { dirname as l } from "node:path";
3
- import f from "path";
4
- import m from "./utils/initserver.mjs";
5
- import u from "./utils/bundle.mjs";
6
- import { bundleCoAuthServer as d, developCoAuthServer as b } from "./utils/coAuthServer.mjs";
7
- l(n(import.meta.url));
8
- function S(a = {}) {
9
- const r = [
10
- {
11
- package: "spire.officejs-web-word",
12
- path: "/spireword",
13
- route: "/service/spireword",
14
- name: "spireword",
15
- build: "/service/spireword"
16
- },
17
- {
18
- package: "spire.officejs-web-excel",
19
- path: "/spireexcel",
20
- route: "/service/spireexcel",
21
- name: "spireexcel",
22
- build: "/service/spireexcel"
23
- },
24
- {
25
- package: "spire.officejs-web-powerpoint",
26
- path: "/spirepowerpoint",
27
- route: "/service/spirepowerpoint",
28
- name: "spirepowerpoint",
29
- build: "/service/spirepowerpoint"
30
- },
31
- {
32
- package: "spire.officejs-web-pdf",
33
- path: "/spireword",
34
- route: "/service/spireword",
35
- name: "pdf",
36
- build: "/service/spireword"
37
- },
38
- {
39
- package: "spire.officejs-web-common",
40
- path: "/lib",
41
- route: "/service/spirecommon",
42
- name: "spirecommon",
43
- build: "/service/spirecommon"
44
- },
45
- {
46
- package: "spire.officejs-web-editors",
47
- path: "/lib",
48
- route: "/editors",
49
- name: "editors",
50
- build: "/editors"
51
- },
52
- {
53
- package: "spire.officejs-web-externs",
54
- path: "/lib",
55
- route: "/externs",
56
- name: "externs",
57
- build: "/externs"
58
- },
59
- {
60
- package: "spire.officejs-web-fontsweb",
61
- path: "/lib",
62
- route: "/fontsweb",
63
- name: "fontsweb",
64
- build: "/fontsweb"
65
- }
66
- ], o = [
67
- {
68
- package: "spire.officejs-service-win-test",
69
- path: "/lib",
70
- name: "service-windows",
71
- platform: "win32",
72
- build: "/"
73
- },
74
- {
75
- package: "spire.officejs-service-linux-test",
76
- path: "/lib",
77
- name: "service-linux",
78
- platform: "linux",
79
- build: "/"
80
- },
81
- {
82
- package: "spire.officejs-service-arm-test",
83
- path: "/lib",
84
- name: "service-arm",
85
- platform: "arm",
86
- build: "/"
87
- }
88
- ], {
89
- port: c = 7e3,
90
- name: p = "vite-plugin-spire.officejs",
91
- coAuthor: s = !1
92
- } = a;
93
- let t = null, i = "";
1
+ import { fileURLToPath } from "node:url";
2
+ import { dirname, resolve, join } from "node:path";
3
+ import path, { join as join$1, relative, dirname as dirname$1 } from "path";
4
+ import { createServer } from "node:http";
5
+ import { access, constants } from "node:fs/promises";
6
+ import { statSync, createReadStream, existsSync, mkdirSync, chmodSync, readFileSync } from "node:fs";
7
+ import { readdir, mkdir, cp } from "fs/promises";
8
+ import { spawn } from "node:child_process";
9
+ const web = [{ "package": "spire.officejs-web-word", "path": "/spireword", "route": "/service/spireword", "name": "spireword", "build": "/service/spireword", "identify": "word" }, { "package": "spire.officejs-web-excel", "path": "/spireexcel", "route": "/service/spireexcel", "name": "spireexcel", "build": "/service/spireexcel", "identify": "excel" }, { "package": "spire.officejs-web-powerpoint", "path": "/spirepowerpoint", "route": "/service/spirepowerpoint", "name": "spirepowerpoint", "build": "/service/spirepowerpoint", "identify": "ppt" }, { "package": "spire.officejs-web-pdf", "path": "/spireword", "route": "/service/spireword", "name": "pdf", "build": "/service/spireword", "identify": "pdf" }, { "package": "spire.officejs-web-common", "path": "/lib", "route": "/service/spirecommon", "name": "spirecommon", "build": "/service/spirecommon", "identify": "spirecommon" }, { "package": "spire.officejs-web-editors", "path": "/lib", "route": "/editors", "name": "editors", "build": "/editors", "identify": "editors" }, { "package": "spire.officejs-web-externs", "path": "/lib", "route": "/externs", "name": "externs", "build": "/externs", "identify": "externs" }, { "package": "spire.officejs-web-fontsweb", "path": "/lib", "route": "/fontsweb", "name": "fontsweb", "build": "/fontsweb", "identify": "fontsweb" }];
10
+ const service = [{ "package": "spire.officejs-service-win-test", "path": "/lib", "name": "service-windows", "platform": "win32", "build": "/", "identify": "windows" }, { "package": "spire.officejs-service-linux-test", "path": "/lib", "name": "service-linux", "platform": "linux", "build": "/", "identify": "linux" }, { "package": "spire.officejs-service-arm-test", "path": "/lib", "name": "service-arm", "platform": "arm", "build": "/", "identify": "arm" }];
11
+ const configJSon = {
12
+ web,
13
+ service
14
+ };
15
+ let currentServer = null;
16
+ const mappings$2 = configJSon.web;
17
+ const initServer = (viteServer, port, name) => {
18
+ var _a;
19
+ if (currentServer) {
20
+ currentServer.close();
21
+ currentServer = null;
22
+ }
23
+ const server = createServer(async (req, res) => {
24
+ var _a2;
25
+ const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
26
+ const requestPathname = parsedUrl.pathname;
27
+ let mapping = {};
28
+ if (requestPathname.includes("Spire.Pdf.Base")) {
29
+ mapping = mappings$2.find((m) => m.name == "pdf");
30
+ } else {
31
+ mapping = mappings$2.find(
32
+ (m) => requestPathname.startsWith(m.route)
33
+ );
34
+ }
35
+ if (!mapping) {
36
+ res.writeHead(404, { "Content-Type": "text/plain" });
37
+ res.end("Resource not found");
38
+ return;
39
+ }
40
+ res.setHeader("Access-Control-Allow-Origin", "*");
41
+ try {
42
+ let packagePath2;
43
+ try {
44
+ const packageMain = await import.meta.resolve(
45
+ `${mapping.package}/package.json`
46
+ );
47
+ packagePath2 = dirname(packageMain);
48
+ } catch (resolveErr) {
49
+ packagePath2 = resolve(process.cwd(), "node_modules", mapping.package);
50
+ }
51
+ if (!packagePath2.startsWith("file://")) {
52
+ if (packagePath2.startsWith("/") || packagePath2.match(/^[A-Za-z]:/)) {
53
+ packagePath2 = "file://" + (packagePath2.startsWith("/") ? "" : "/") + packagePath2;
54
+ }
55
+ }
56
+ const localPackagePath = fileURLToPath(packagePath2);
57
+ const relativePath = requestPathname.replace(mapping.route, "") || "/index.html";
58
+ const fullPath = join(localPackagePath, mapping.path, relativePath);
59
+ await access(fullPath);
60
+ const stat = statSync(fullPath);
61
+ const ext = (_a2 = fullPath.split(".").pop()) == null ? void 0 : _a2.toLowerCase();
62
+ const mimeTypes = {
63
+ html: "text/html",
64
+ js: "application/javascript",
65
+ css: "text/css",
66
+ json: "application/json",
67
+ png: "image/png",
68
+ jpg: "image/jpeg",
69
+ svg: "image/svg+xml",
70
+ wasm: "application/wasm"
71
+ };
72
+ res.writeHead(200, {
73
+ "Content-Type": ext && mimeTypes[ext] || "application/octet-stream",
74
+ "Content-Length": stat.size
75
+ });
76
+ const stream = createReadStream(fullPath);
77
+ stream.pipe(res);
78
+ stream.on("error", (err) => {
79
+ console.error(`Stream error for ${fullPath}:`, err);
80
+ res.writeHead(500);
81
+ res.end("Internal server error");
82
+ });
83
+ } catch (error) {
84
+ if (error.code === "ENOENT") {
85
+ res.writeHead(404, { "Content-Type": "text/plain" });
86
+ res.end(`File not found: ${req.url}`);
87
+ } else {
88
+ console.error(`Error serving ${req.url}:`, error);
89
+ res.writeHead(500);
90
+ res.end("Internal server error");
91
+ }
92
+ }
93
+ });
94
+ currentServer = server;
95
+ server.listen(port, () => {
96
+ console.log(
97
+ `📦 [${name}] The Spire.OfficeJS static resource service has been started.`
98
+ );
99
+ console.log(` - Service address: http://localhost:${port}`);
100
+ mappings$2.forEach((m) => {
101
+ console.log(
102
+ ` - Mapping ${m.package}${m.path} -> http://localhost:${port}${m.route}`
103
+ );
104
+ });
105
+ });
106
+ server.on("error", (err) => {
107
+ if (err.code === "EADDRINUSE") {
108
+ console.warn(
109
+ `⚠️ [${name}] Port ${port} is already in use, retrying in 1s...`
110
+ );
111
+ setTimeout(() => {
112
+ server.close();
113
+ server.listen(port, () => {
114
+ console.log(
115
+ `📦 [${name}] The Spire.OfficeJS static resource service has been restarted on port ${port}.`
116
+ );
117
+ });
118
+ }, 1e3);
119
+ } else {
120
+ console.error(`❌ [${name}] Server error:`, err.message);
121
+ }
122
+ });
123
+ (_a = viteServer.httpServer) == null ? void 0 : _a.on("close", () => {
124
+ if (currentServer) {
125
+ currentServer.close();
126
+ currentServer = null;
127
+ }
128
+ });
129
+ };
130
+ const copySingleFile = async (src, dest) => {
131
+ const destDir = dirname$1(dest);
132
+ await mkdir(destDir, { recursive: true });
133
+ await cp(src, dest, { recursive: true });
134
+ };
135
+ const copyFile = async (src, dest, excludes) => {
136
+ const entries = await readdir(src, { withFileTypes: true });
137
+ await mkdir(dest, { recursive: true });
138
+ for (const entry of entries) {
139
+ const srcPath = join$1(src, entry.name);
140
+ const destPath = join$1(dest, entry.name);
141
+ const relativePath = relative(src, srcPath);
142
+ const shouldExclude = excludes.some((pattern) => {
143
+ if (typeof pattern === "string") {
144
+ return relativePath.includes(pattern) || entry.name === pattern;
145
+ }
146
+ if (pattern instanceof RegExp) {
147
+ return pattern.test(relativePath);
148
+ }
149
+ return false;
150
+ });
151
+ if (shouldExclude) {
152
+ continue;
153
+ }
154
+ if (entry.isDirectory()) {
155
+ await copyFile(srcPath, destPath, excludes);
156
+ } else {
157
+ await cp(srcPath, destPath, { recursive: true });
158
+ }
159
+ }
160
+ };
161
+ const mappings$1 = configJSon.web;
162
+ const excludesFile_copy = [
163
+ "README.md",
164
+ "package.json",
165
+ "index.js",
166
+ "index.d.ts",
167
+ "**/SpireCloudEditor.module.js"
168
+ ];
169
+ const initBundle = async (options, projectRoot) => {
170
+ const buildOutDir = options.dir;
171
+ const targetStaticPrfix2 = "spire.officejs/web";
172
+ try {
173
+ const createRequire = await import("module").then((m) => m.createRequire);
174
+ const requireFunc = createRequire(projectRoot + "/node_modules/");
175
+ for (let i = 0; i < mappings$1.length; i++) {
176
+ let packageName = mappings$1[i].package;
177
+ let packagePath2 = mappings$1[i].path;
178
+ let buildDir = targetStaticPrfix2 + mappings$1[i].build;
179
+ const packageJsonPath = requireFunc.resolve(`${packageName}/package.json`);
180
+ const packageRoot = path.dirname(packageJsonPath) + packagePath2;
181
+ const targetStaticDir = path.join(buildOutDir, buildDir);
182
+ console.log(`copying ${packageName} ......`);
183
+ await copyFile(packageRoot, targetStaticDir, excludesFile_copy);
184
+ }
185
+ console.log(`✅ successfully copied to: ${targetStaticPrfix2}`);
186
+ } catch (error) {
187
+ throw error;
188
+ }
189
+ };
190
+ const mappings = configJSon.service;
191
+ const __filename$1 = fileURLToPath(import.meta.url);
192
+ dirname(__filename$1);
193
+ const targetStaticPrfix = "spire.officejs/service";
194
+ const devConfigName = "spire.officejs-server-config.json";
195
+ const prodConfigDir = "OfficeServer/config/";
196
+ const prodConfigName = "default.json";
197
+ const platformProps = {
198
+ windows: "win32",
199
+ linux: "linux",
200
+ arm: "arm"
201
+ };
202
+ const defaultPlatform = "win32";
203
+ let serverProcess = null;
204
+ let packagePath;
205
+ let ExeFileRootDir, ExeFileDir, ExeFileDirName, ExeFileName, ExeFilePath;
206
+ const startServer = async (root, options) => {
207
+ var _a, _b;
208
+ await stopServer();
209
+ const mapping = getMapping(mappings);
210
+ try {
211
+ try {
212
+ const packageMain = await import.meta.resolve(
213
+ `${mapping.package}/package.json`
214
+ );
215
+ packagePath = dirname(packageMain);
216
+ } catch (resolveErr) {
217
+ packagePath = resolve(process.cwd(), "node_modules", mapping.package);
218
+ }
219
+ if (!packagePath.startsWith("file://")) {
220
+ if (packagePath.startsWith("/") || packagePath.match(/^[A-Za-z]:/)) {
221
+ packagePath = "file://" + (packagePath.startsWith("/") ? "" : "/") + packagePath;
222
+ }
223
+ }
224
+ const localPackagePath = fileURLToPath(packagePath);
225
+ const configPath = resolve(root, devConfigName);
226
+ const platform = getSystemPlatform();
227
+ console.log("platform", platform);
228
+ if (platform != platformProps.windows) {
229
+ ExeFileDirName = "OfficeServer";
230
+ ExeFileName = "officeserver-linux";
231
+ } else {
232
+ ExeFileDirName = "OfficeServer";
233
+ ExeFileName = "officeserver-win.exe";
234
+ }
235
+ ExeFileRootDir = localPackagePath + mapping.path;
236
+ ExeFileDir = resolve(ExeFileRootDir, ExeFileDirName);
237
+ ExeFilePath = resolve(ExeFileDir, ExeFileName);
238
+ const NODE_CONFIG_DIR = await applyConfigBeforeStartSerever(
239
+ options,
240
+ root,
241
+ mapping
242
+ );
243
+ if (platform != platformProps.windows) {
244
+ try {
245
+ chmodSync(ExeFilePath, 493);
246
+ } catch (err) {
247
+ console.error(err);
248
+ }
249
+ }
250
+ serverProcess = spawn(ExeFilePath, [], {
251
+ stdio: "pipe",
252
+ detached: false,
253
+ cwd: ExeFileDir,
254
+ env: {
255
+ ...process.env,
256
+ NODE_CONFIG_DIR
257
+ }
258
+ });
259
+ console.log(`Officeserver started with PID: ${serverProcess.pid}`);
260
+ (_a = serverProcess.stdout) == null ? void 0 : _a.on("data", (data) => {
261
+ console.log(`[officeserver] ${data.toString().trim()}`);
262
+ });
263
+ (_b = serverProcess.stderr) == null ? void 0 : _b.on("data", (data) => {
264
+ console.error(`[${mapping.package}] ${data.toString().trim()}`);
265
+ });
266
+ serverProcess.on("close", (code) => {
267
+ console.log(`${mapping.package} process exited with code ${code}`);
268
+ serverProcess = null;
269
+ });
270
+ serverProcess.on("error", (err) => {
271
+ console.error("Failed to start ${mapping.package}:", err);
272
+ serverProcess = null;
273
+ });
274
+ } catch (error) {
275
+ console.error(
276
+ `${mapping.package} executable not found at ${ExeFilePath}:`,
277
+ error
278
+ );
279
+ }
280
+ };
281
+ const stopServer = async () => {
282
+ if (serverProcess) {
283
+ const p = serverProcess;
284
+ serverProcess = null;
285
+ if (process.platform === "win32") {
286
+ try {
287
+ const { execSync } = await import("node:child_process");
288
+ execSync(`taskkill /pid ${p.pid} /T /F`, { stdio: "ignore" });
289
+ } catch {
290
+ try {
291
+ p.kill("SIGKILL");
292
+ } catch {
293
+ }
294
+ }
295
+ } else {
296
+ p.kill("SIGTERM");
297
+ }
298
+ await new Promise((resolve2) => {
299
+ const timeout = setTimeout(() => {
300
+ try {
301
+ p.kill("SIGKILL");
302
+ } catch {
303
+ }
304
+ resolve2();
305
+ }, 5e3);
306
+ p.on("close", () => {
307
+ clearTimeout(timeout);
308
+ resolve2();
309
+ });
310
+ });
311
+ }
312
+ };
313
+ const getMapping = (mappings2) => {
314
+ let platform = getSystemPlatform();
315
+ let mapping = mappings2.filter((item) => {
316
+ return item.platform == platform;
317
+ })[0];
318
+ if (!mapping) {
319
+ mapping = mappings2.filter((item) => {
320
+ return item.platform == defaultPlatform;
321
+ })[0];
322
+ }
323
+ return mapping;
324
+ };
325
+ const getSystemPlatform = () => {
326
+ let platform = process && process.platform;
327
+ let arch = getSystemArch();
328
+ if (platform == platformProps.linux && (arch === "arm" || arch === "arm64"))
329
+ platform = platformProps.arm;
330
+ return platform;
331
+ };
332
+ const getSystemArch = () => {
333
+ return process && process.arch;
334
+ };
335
+ const applyConfigBeforeStartSerever = async (options, root, mapping) => {
336
+ getSystemPlatform();
337
+ const sourceFile = resolve(root, devConfigName);
338
+ const packageName = mapping.package;
339
+ const packagePath2 = mapping.path;
340
+ const createRequire = await import("module").then((m) => m.createRequire);
341
+ const requireFunc = createRequire(root + "/node_modules/");
342
+ const packageJsonPath = requireFunc.resolve(`${packageName}/package.json`);
343
+ const packageRoot = dirname(packageJsonPath) + packagePath2;
344
+ const targetDir = resolve(packageRoot, prodConfigDir);
345
+ const targetFile = resolve(targetDir, prodConfigName);
346
+ await createSqlDirInDev(targetDir, sourceFile);
347
+ await copySingleFile(sourceFile, targetFile);
348
+ return targetDir;
349
+ };
350
+ const createSqlDirInDev = async (targetDir, sourceFile) => {
351
+ try {
352
+ const dbPath = await getConfigDbPath(sourceFile);
353
+ const absoluteDir = dbPath && resolve(targetDir, "..", dbPath);
354
+ if (absoluteDir && !existsSync(absoluteDir)) {
355
+ mkdirSync(absoluteDir, { recursive: true });
356
+ }
357
+ } catch (error) {
358
+ console.error(error);
359
+ }
360
+ };
361
+ const createSqlDirInProd = async (root) => {
362
+ const configDir = resolve(root, "dist", targetStaticPrfix, prodConfigDir);
363
+ const configPath = resolve(configDir, prodConfigName);
364
+ const sqlDir = await getConfigDbPath(configPath);
365
+ const absoluteDir = sqlDir && resolve(configDir, "..", sqlDir);
366
+ try {
367
+ if (absoluteDir && !existsSync(absoluteDir)) {
368
+ mkdirSync(absoluteDir, { recursive: true });
369
+ }
370
+ } catch (error) {
371
+ console.error(error);
372
+ }
373
+ };
374
+ const getConfigDbPath = async (sourceFile) => {
375
+ var _a, _b, _c;
376
+ if (!sourceFile) return;
377
+ try {
378
+ await access(sourceFile, constants.F_OK);
379
+ const configContent = readFileSync(sourceFile, "utf8");
380
+ const config = JSON.parse(configContent);
381
+ const dbPath = (_c = (_b = (_a = config.ServicesInformation) == null ? void 0 : _a.OfficeServer) == null ? void 0 : _b.sqlite3) == null ? void 0 : _c.path;
382
+ if (!dbPath) {
383
+ return;
384
+ }
385
+ const dir = dirname(dbPath);
386
+ return dir;
387
+ } catch (error) {
388
+ console.error(error);
389
+ }
390
+ };
391
+ const developCoAuthServer = (viteServer, root, options) => {
392
+ var _a;
393
+ startServer(root, options);
394
+ (_a = viteServer.httpServer) == null ? void 0 : _a.on("close", () => {
395
+ stopServer();
396
+ });
397
+ };
398
+ const bundleCoAuthServer = async (options, projectRoot) => {
399
+ const mapping = getMapping(mappings);
400
+ getSystemPlatform();
401
+ const bundleConfigDirName = "/OfficeServer/config";
402
+ try {
403
+ const bundleConfigDir = targetStaticPrfix + bundleConfigDirName;
404
+ const bundleConfigFileName = bundleConfigDir + "/default.json";
405
+ const buildOutDir = options.dir;
406
+ const targetDir = resolve(buildOutDir, targetStaticPrfix);
407
+ const createRequire = await import("module").then((m) => m.createRequire);
408
+ const requireFunc = createRequire(projectRoot + "/node_modules/");
409
+ const configSourcetPath = resolve(projectRoot, devConfigName);
410
+ const configtargetPath = resolve(buildOutDir, bundleConfigFileName);
411
+ let packageName = mapping.package;
412
+ let packagePath2 = mapping.path;
413
+ let buildDir = targetStaticPrfix + mapping.build;
414
+ const packageJsonPath = requireFunc.resolve(`${packageName}/package.json`);
415
+ const packageRoot = dirname(packageJsonPath) + packagePath2;
416
+ const targetStaticDir = join(buildOutDir, buildDir);
417
+ console.log(`copying ${packageName} ......`);
418
+ await copyFile(packageRoot, targetStaticDir, ["logs", "data"]);
419
+ await copySingleFile(configSourcetPath, configtargetPath);
420
+ await createSqlDirInProd(projectRoot);
421
+ console.log(`✅ successfully copied to: ${targetDir}`);
422
+ } catch (error) {
423
+ throw new Error(error);
424
+ }
425
+ };
426
+ dirname(fileURLToPath(import.meta.url));
427
+ function spireOfficeJsPlugin(options = {}) {
428
+ const {
429
+ port = 7e3,
430
+ name = "vite-plugin-spire.officejs",
431
+ coAuthor = false
432
+ } = options;
433
+ let viteConfig = null;
434
+ let projectRoot = "";
94
435
  return {
95
- name: p,
96
- configResolved(e) {
97
- t = e, i = e.root, f.resolve(i, e.build.outDir);
436
+ name,
437
+ configResolved(config) {
438
+ viteConfig = config;
439
+ projectRoot = config.root;
440
+ path.resolve(projectRoot, config.build.outDir);
98
441
  },
99
- // 开发环境
100
- // 创建一个子进程来提供officeJS的静态资源
101
- configureServer(e) {
102
- m(e, r, c, p), s && b(e, i, o, t);
442
+ // 开发环境:创建子进程提供 officeJS 静态资源
443
+ configureServer(viteServer) {
444
+ initServer(viteServer, port, name);
445
+ if (coAuthor) developCoAuthServer(viteServer, projectRoot, viteConfig);
103
446
  },
104
- // 生产环境
105
- // 将officeJS的静态资源拷贝到输出目录中
106
- async writeBundle(e, w) {
107
- await u(e, i, r), s && await d(e, i, o);
447
+ // 生产环境:将 officeJS 静态资源拷贝到输出目录
448
+ async writeBundle(options2, bundle) {
449
+ await initBundle(options2, projectRoot);
450
+ if (coAuthor) await bundleCoAuthServer(options2, projectRoot);
108
451
  }
109
452
  };
110
453
  }
111
454
  export {
112
- S as default
455
+ spireOfficeJsPlugin as default
113
456
  };
@@ -0,0 +1,29 @@
1
+ import type { Plugin as Plugin_2 } from 'vite';
2
+
3
+ export declare interface PluginOptions {
4
+ port?: number;
5
+ name?: string;
6
+ coAuthor?: boolean;
7
+ }
8
+
9
+ export declare interface ServiceMapping {
10
+ package: string;
11
+ path: string;
12
+ name: string;
13
+ platform: string;
14
+ build: string;
15
+ }
16
+
17
+ declare function spireOfficeJsPlugin(options?: PluginOptions): Plugin_2;
18
+ export default spireOfficeJsPlugin;
19
+
20
+ export declare interface WebMapping {
21
+ package: string;
22
+ path: string;
23
+ route: string;
24
+ name: string;
25
+ build: string;
26
+ identify: string;
27
+ }
28
+
29
+ export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-spire.officejs-test",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "",
5
5
  "main": "./lib/index.mjs",
6
6
  "files": [
@@ -13,5 +13,5 @@
13
13
  "author": "",
14
14
  "license": "MIT",
15
15
  "type": "module",
16
- "types": "./lib/index.d.ts"
16
+ "types": "./lib/plugins.d.ts"
17
17
  }
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare const plugin: (...args: any[]) => any;
2
- export default plugin;
@@ -1,12 +0,0 @@
1
- interface interface_options {
2
- dir: string;
3
- }
4
- interface interface_mapping {
5
- package: string;
6
- path: string;
7
- route: string;
8
- name: string;
9
- build: string;
10
- }
11
- declare const _default: (options: interface_options, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
- export default _default;
@@ -1,25 +0,0 @@
1
- import r from "path";
2
- import { copyFile as h } from "./copyFile.mjs";
3
- const k = [
4
- "README.md",
5
- "package.json",
6
- "index.js",
7
- "index.d.ts",
8
- "**/SpireCloudEditor.module.js"
9
- ], y = async (i, s, t) => {
10
- const l = i.dir, o = "spire.officejs/web";
11
- try {
12
- const n = (await import("module").then((e) => e.createRequire))(s + "/node_modules/");
13
- for (let e = 0; e < t.length; e++) {
14
- let a = t[e].package, d = t[e].path, u = o + t[e].build;
15
- const p = n.resolve(`${a}/package.json`), g = r.dirname(p) + d, f = r.join(l, u);
16
- console.log(`copying ${a} ......`), await h(g, f, k);
17
- }
18
- console.log(`✅ successfully copied to: ${o}`);
19
- } catch (c) {
20
- throw c;
21
- }
22
- };
23
- export {
24
- y as default
25
- };
@@ -1,12 +0,0 @@
1
- interface interface_mapping {
2
- package: string;
3
- path: string;
4
- name: string;
5
- platform: string;
6
- build: string;
7
- }
8
- export declare const developCoAuthServer: (viteServer: any, root: string, serviceMapping: Array<interface_mapping>, options: any) => void;
9
- export declare const bundleCoAuthServer: (options: {
10
- dir: string;
11
- }, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
- export {};
@@ -1,12 +0,0 @@
1
- interface interface_mapping {
2
- package: string;
3
- path: string;
4
- name: string;
5
- platform: string;
6
- build: string;
7
- }
8
- export declare const developCoAuthServer: (viteServer: any, root: string, serviceMapping: Array<interface_mapping>, options: any) => void;
9
- export declare const bundleCoAuthServer: (options: {
10
- dir: string;
11
- }, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
- export {};
@@ -1,135 +0,0 @@
1
- import { fileURLToPath as b } from "node:url";
2
- import { dirname as u, resolve as s, join as L } from "node:path";
3
- import { spawn as T } from "node:child_process";
4
- import { copyFile as A, copySingleFile as q } from "./copyFile.mjs";
5
- import { chmodSync as G, readFileSync as J, existsSync as M, mkdirSync as W } from "node:fs";
6
- const K = b(import.meta.url);
7
- u(K);
8
- const v = "spire.officejs/service", x = "spire.officejs-server-config.json", S = {
9
- windows: "win32",
10
- linux: "linux",
11
- arm: "arm"
12
- }, z = "win32";
13
- let n = null, f, $, y, P, F, h;
14
- const B = async (t, r, c) => {
15
- var a, i;
16
- await R();
17
- const e = N(r);
18
- try {
19
- try {
20
- const o = await import.meta.resolve(
21
- `${e.package}/package.json`
22
- );
23
- f = u(o);
24
- } catch {
25
- f = s(process.cwd(), "node_modules", e.package);
26
- }
27
- f.startsWith("file://") || (f.startsWith("/") || f.match(/^[A-Za-z]:/)) && (f = "file://" + (f.startsWith("/") ? "" : "/") + f);
28
- const g = b(f), p = s(t, x), l = w();
29
- console.log("platform", l), l != S.windows ? (P = "OfficeServer", F = "officeserver-linux") : (P = "OfficeServer", F = "officeserver-win.exe"), $ = g + e.path, y = s($, P), h = s(y, F);
30
- const m = await Z(
31
- c,
32
- t,
33
- e
34
- );
35
- if (l != S.windows)
36
- try {
37
- G(h, 493);
38
- } catch (o) {
39
- console.error(o);
40
- }
41
- n = T(h, [], {
42
- stdio: "pipe",
43
- detached: !1,
44
- cwd: y,
45
- env: {
46
- ...process.env,
47
- NODE_CONFIG_DIR: m
48
- }
49
- }), console.log(`Officeserver started with PID: ${n.pid}`), (a = n.stdout) == null || a.on("data", (o) => {
50
- console.log(`[officeserver] ${o.toString().trim()}`);
51
- }), (i = n.stderr) == null || i.on("data", (o) => {
52
- console.error(`[${e.package}] ${o.toString().trim()}`);
53
- }), n.on("close", (o) => {
54
- console.log(`${e.package} process exited with code ${o}`), n = null;
55
- }), n.on("error", (o) => {
56
- console.error("Failed to start ${mapping.package}:", o), n = null;
57
- });
58
- } catch (g) {
59
- console.error(
60
- `${e.package} executable not found at ${h}:`,
61
- g
62
- );
63
- }
64
- }, R = async () => {
65
- if (n) {
66
- const t = n;
67
- if (n = null, process.platform === "win32")
68
- try {
69
- const { execSync: r } = await import("node:child_process");
70
- r(`taskkill /pid ${t.pid} /T /F`, { stdio: "ignore" });
71
- } catch {
72
- try {
73
- t.kill("SIGKILL");
74
- } catch {
75
- }
76
- }
77
- else
78
- t.kill("SIGTERM");
79
- await new Promise((r) => {
80
- const c = setTimeout(() => {
81
- try {
82
- t.kill("SIGKILL");
83
- } catch {
84
- }
85
- r();
86
- }, 5e3);
87
- t.on("close", () => {
88
- clearTimeout(c), r();
89
- });
90
- });
91
- }
92
- }, N = (t) => {
93
- let r = w(), c = t.filter((e) => e.platform == r)[0];
94
- return c || (c = t.filter((e) => e.platform == z)[0]), c;
95
- }, w = () => {
96
- let t = process && process.platform, r = U();
97
- return t == S.linux && (r === "arm" || r === "arm64") && (t = S.arm), t;
98
- }, U = () => process && process.arch, Z = async (t, r, c) => {
99
- w();
100
- const e = s(r, x), a = c.package, i = c.path, l = (await import("module").then((k) => k.createRequire))(r + "/node_modules/").resolve(`${a}/package.json`), m = u(l) + i;
101
- let o, d;
102
- return o = s(m, "OfficeServer/config"), d = s(o, "default.json"), await H(o, e), await q(e, d), o;
103
- }, H = async (t, r) => {
104
- var c, e, a;
105
- try {
106
- const i = J(r, "utf8"), p = (a = (e = (c = JSON.parse(i).ServicesInformation) == null ? void 0 : c.OfficeServer) == null ? void 0 : e.sqlite3) == null ? void 0 : a.path;
107
- if (!p)
108
- return;
109
- const l = u(p), m = s(t, "../", l);
110
- M(m) || W(m, { recursive: !0 });
111
- } catch (i) {
112
- console.error(i);
113
- }
114
- }, te = (t, r, c, e) => {
115
- var a;
116
- B(r, c, e), (a = t.httpServer) == null || a.on("close", () => {
117
- R();
118
- });
119
- }, re = async (t, r, c) => {
120
- const e = N(c);
121
- w();
122
- const a = "/OfficeServer/config";
123
- try {
124
- const g = v + a + "/default.json", p = t.dir, l = s(p, v), o = (await import("module").then((_) => _.createRequire))(r + "/node_modules/"), d = s(r, x), k = s(p, g);
125
- let D = e.package, O = e.path, C = v + e.build;
126
- const E = o.resolve(`${D}/package.json`), I = u(E) + O, j = L(p, C);
127
- console.log(`copying ${D} ......`), await A(I, j, ["logs"]), await q(d, k), console.log(`✅ successfully copied to: ${l}`);
128
- } catch (i) {
129
- throw new Error(i);
130
- }
131
- };
132
- export {
133
- re as bundleCoAuthServer,
134
- te as developCoAuthServer
135
- };
@@ -1,7 +0,0 @@
1
- /**
2
- * 复制单个文件
3
- * @param src 源文件路径
4
- * @param dest 目标文件路径
5
- */
6
- export declare const copySingleFile: (src: string, dest: string) => Promise<void>;
7
- export declare const copyFile: (src: string, dest: string, excludes: Array<string>) => Promise<void>;
@@ -1,17 +0,0 @@
1
- import { readdir as m, mkdir as u, cp as l } from "fs/promises";
2
- import { join as c, relative as d, dirname as y } from "path";
3
- const g = async (e, i) => {
4
- const r = y(i);
5
- await u(r, { recursive: !0 }), await l(e, i, { recursive: !0 });
6
- }, w = async (e, i, r) => {
7
- const f = await m(e, { withFileTypes: !0 });
8
- await u(i, { recursive: !0 });
9
- for (const o of f) {
10
- const s = c(e, o.name), n = c(i, o.name), a = d(e, s);
11
- r.some((t) => typeof t == "string" ? a.includes(t) || o.name === t : t instanceof RegExp ? t.test(a) : !1) || (o.isDirectory() ? await w(s, n, r) : await l(s, n, { recursive: !0 }));
12
- }
13
- };
14
- export {
15
- w as copyFile,
16
- g as copySingleFile
17
- };
@@ -1,8 +0,0 @@
1
- interface mapping_inter {
2
- package: string;
3
- path: string;
4
- route: string;
5
- name: string;
6
- }
7
- declare const _default: (viteServer: any, mappings: Array<mapping_inter>, port: Number, name: String) => void;
8
- export default _default;
@@ -1,68 +0,0 @@
1
- import { createServer as y } from "node:http";
2
- import { fileURLToPath as P } from "node:url";
3
- import { dirname as T, resolve as j, join as x } from "node:path";
4
- import { access as k } from "node:fs/promises";
5
- import { statSync as E, createReadStream as C } from "node:fs";
6
- let a = null;
7
- const W = (u, p, s, c) => {
8
- var h;
9
- a && (a.close(), a = null);
10
- const n = y(async (t, r) => {
11
- var f;
12
- const d = new URL(t.url, `http://${t.headers.host}`).pathname;
13
- let o = {};
14
- if (d.includes("Spire.Pdf.Base") ? o = p.find((e) => e.name == "pdf") : o = p.find((e) => d.startsWith(e.route)), !o) {
15
- r.writeHead(404, { "Content-Type": "text/plain" }), r.end("Resource not found");
16
- return;
17
- }
18
- r.setHeader("Access-Control-Allow-Origin", "*");
19
- try {
20
- let e;
21
- try {
22
- const l = await import.meta.resolve(`${o.package}/package.json`);
23
- e = T(l);
24
- } catch {
25
- e = j(process.cwd(), "node_modules", o.package);
26
- }
27
- e.startsWith("file://") || (e.startsWith("/") || e.match(/^[A-Za-z]:/)) && (e = "file://" + (e.startsWith("/") ? "" : "/") + e);
28
- const v = P(e), $ = d.replace(o.route, "") || "/index.html", i = x(v, o.path, $);
29
- await k(i);
30
- const S = E(i), m = (f = i.split(".").pop()) == null ? void 0 : f.toLowerCase(), w = {
31
- html: "text/html",
32
- js: "application/javascript",
33
- css: "text/css",
34
- json: "application/json",
35
- png: "image/png",
36
- jpg: "image/jpeg",
37
- svg: "image/svg+xml",
38
- wasm: "application/wasm"
39
- };
40
- r.writeHead(200, {
41
- "Content-Type": m && w[m] || "application/octet-stream",
42
- "Content-Length": S.size
43
- });
44
- const g = C(i);
45
- g.pipe(r), g.on("error", (l) => {
46
- console.error(`Stream error for ${i}:`, l), r.writeHead(500), r.end("Internal server error");
47
- });
48
- } catch (e) {
49
- e.code === "ENOENT" ? (r.writeHead(404, { "Content-Type": "text/plain" }), r.end(`File not found: ${t.url}`)) : (console.error(`Error serving ${t.url}:`, e), r.writeHead(500), r.end("Internal server error"));
50
- }
51
- });
52
- a = n, n.listen(s, () => {
53
- console.log(`📦 [${c}] The Spire.OfficeJS static resource service has been started.`), console.log(` - Service address: http://localhost:${s}`), p.forEach((t) => {
54
- console.log(` - Mapping ${t.package}${t.path} -> http://localhost:${s}${t.route}`);
55
- });
56
- }), n.on("error", (t) => {
57
- t.code === "EADDRINUSE" ? (console.warn(`⚠️ [${c}] Port ${s} is already in use, retrying in 1s...`), setTimeout(() => {
58
- n.close(), n.listen(s, () => {
59
- console.log(`📦 [${c}] The Spire.OfficeJS static resource service has been restarted on port ${s}.`);
60
- });
61
- }, 1e3)) : console.error(`❌ [${c}] Server error:`, t.message);
62
- }), (h = u.httpServer) == null || h.on("close", () => {
63
- a && (a.close(), a = null);
64
- });
65
- };
66
- export {
67
- W as default
68
- };