@officesdk/officesdk-plugin 0.0.0-stage → 1.0.4

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/bin/zcode.mjs ADDED
@@ -0,0 +1,91 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import os from 'node:os';
4
+ import {pathToFileURL} from 'node:url';
5
+ import {createHash} from 'node:crypto';
6
+ import {execFile} from 'node:child_process';
7
+ import {promisify} from 'node:util';
8
+
9
+ const run=promisify(execFile);
10
+ async function materializeWasm(staging,editor) {
11
+ const loader=await fs.readFile(path.join(staging,'resources/editor/zcode-editor.js'),'utf8');
12
+ const required=new Set([...loader.matchAll(/mcp-([0-9a-f]{64})\.wasm/g)].map(x=>x[1]));
13
+ if(!required.size)throw Error('Missing native module map for the ZCode viewer');
14
+ async function visit(dir) {
15
+ for(const entry of await fs.readdir(dir,{withFileTypes:true})){
16
+ if(!required.size)return;
17
+ const name=path.join(dir,entry.name);
18
+ if(entry.isDirectory()){await visit(name);continue;}
19
+ if(!entry.isFile()||!name.endsWith('.js'))continue;
20
+ const code=await fs.readFile(name,'utf8');
21
+ for(const [url] of code.matchAll(/data:application\/wasm;base64,[A-Za-z0-9+/=]+/g)){
22
+ const digest=createHash('sha256').update(url).digest('hex');
23
+ if(!required.has(digest))continue;
24
+ const wasm=Buffer.from(url.slice(url.indexOf(',')+1),'base64');
25
+ if(!wasm.subarray(0,4).equals(Buffer.from([0,97,115,109])))throw Error('Invalid OfficeSDK WebAssembly resource');
26
+ await fs.writeFile(path.join(editor,'mcp-'+digest+'.wasm'),wasm);
27
+ required.delete(digest);
28
+ }
29
+ }
30
+ }
31
+ await visit(editor);
32
+ if(required.size)throw Error('The verified engine lacks modules required by the ZCode viewer');
33
+ }
34
+ export async function prepareMarket(staging,target,engine,version) {
35
+ const editor=path.join(staging,'resources/office-kit');
36
+ const copy={recursive:true,mode:fs.constants.COPYFILE_FICLONE};
37
+ await fs.cp(engine.editor,editor,copy);
38
+ await fs.cp(engine.kit,path.join(staging,'resources/kit'),copy);
39
+ await materializeWasm(staging,editor);
40
+ const localeFile=path.join(staging,'install-locale.mjs');
41
+ const {enableHostEditorLocales}=await import(pathToFileURL(localeFile));
42
+ await enableHostEditorLocales(path.join(staging,'resources/editor/bundle/offline-editor.js'),path.join(staging,'resources/editor/zcode-editor.js'));
43
+ await fs.rm(localeFile);
44
+ const wrapper=staging+'-market';
45
+ await fs.mkdir(path.join(wrapper,'plugins'),{recursive:true});
46
+ const manifest={name:'officesdk',owner:{name:'OfficeSDK'},plugins:[{name:'officesdk',source:'./plugins/officesdk',version,description:'OfficeSDK',category:'productivity'}]};
47
+ await fs.writeFile(path.join(wrapper,'marketplace.json'),JSON.stringify(manifest,null,2)+'\n');
48
+ await fs.rename(staging,path.join(wrapper,'plugins/officesdk'));
49
+ await fs.rename(wrapper,staging);
50
+ }
51
+ export async function marketManager(target) {
52
+ const roots=['/Applications',path.join(os.homedir(),'Applications')];
53
+ let entry;
54
+ for(const dir of roots){
55
+ const file=path.join(dir,'ZCode.app/Contents/Resources/glm/zcode.cjs');
56
+ if(await fs.access(file).then(()=>true,()=>false)){entry=file;break;}
57
+ }
58
+ const command=entry?process.execPath:'zcode';
59
+ const manage=async(...args)=>(await run(command,[...(entry?[entry]:[]),'plugins',...args],{timeout:90000,maxBuffer:4*1024*1024})).stdout;
60
+ const list=async()=>JSON.parse(await manage('list','--json'));
61
+ const known=JSON.parse(await manage('marketplace','list','--json')).find(x=>x.name==='officesdk');
62
+ if(known && (known.source?.source!=='directory' || await fs.realpath(known.source.path)!==await fs.realpath(target)))throw Error('A different marketplace is registered as OfficeSDK');
63
+ const rows=await list(),previous=rows.find(x=>x.id==='officesdk@officesdk');
64
+ const legacy=rows.filter(x=>x.enabled&&['officesdk@officesdk-development','officesdk@officesdk-zcode-local'].includes(x.id));
65
+ let added=false,installed=false;const disabled=[];
66
+ return {
67
+ async activate(version){
68
+ if(!known){await manage('marketplace','add',target);added=true;}
69
+ else await manage('marketplace','update','officesdk');
70
+ if(previous)await manage('update','officesdk@officesdk');
71
+ else {await manage('install','officesdk@officesdk');installed=true;}
72
+ await manage('enable','officesdk@officesdk');
73
+ const plugin=(await list()).find(x=>x.id==='officesdk@officesdk');
74
+ if(!plugin?.enabled||plugin.version!==version||!plugin.declaredMcpServerNames?.includes('officesdk')||!plugin.mcpServerNames?.includes('plugin:officesdk:officesdk')||plugin.diagnostics?.length)throw Error('ZCode did not load OfficeSDK '+version);
75
+ const source=path.join(target,'plugins/officesdk');
76
+ if(!plugin.rootPath)throw Error('ZCode did not return the installed plugin location');
77
+ for(const relative of ['dist/server.mjs','dist/editor.html','resources/editor/zcode-editor.js']){
78
+ const candidate=await fs.readFile(path.join(source,relative));
79
+ const current=await fs.readFile(path.join(plugin.rootPath,relative));
80
+ if(!candidate.equals(current))throw Error('ZCode is still using another build of '+relative);
81
+ }
82
+ for(const item of legacy){await manage('disable',item.id);disabled.push(item.id);}
83
+ },
84
+ async rollback(){
85
+ for(const id of disabled)await manage('enable',id);
86
+ if(installed)await manage('uninstall','officesdk@officesdk','--force');
87
+ if(added)await manage('marketplace','remove','officesdk');
88
+ if(known&&previous){await manage('marketplace','update','officesdk');await manage('update',previous.id);await manage(previous.enabled?'enable':'disable',previous.id);}
89
+ }
90
+ };
91
+ }