feat(runtime): add native office preview and download plugin
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { readFileSync, writeFileSync, copyFileSync } from 'node:fs';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
|
||||
export const target = 'import { createConverter } from "@deepseek-ai/libreoffice-kit";';
|
||||
export function patchOffice(source, hostVersion, packageVersion) {
|
||||
if (hostVersion !== '0.2.0-rc.2' || packageVersion !== '0.2.0-rc.2') throw new Error('unsupported Office preview version');
|
||||
if (source.split(target).length !== 2) throw new Error('expected exactly one original converter import');
|
||||
return source.replace(target, 'import { createConverter } from "./dsh-native-office.mjs";');
|
||||
}
|
||||
export function install(root = '/usr/local/lib/node_modules/@deepseek-ai/dsh') {
|
||||
const dir = join(root, 'node_modules/@deepseek-ai/dsh-office-to-pdf');
|
||||
const version = path => JSON.parse(readFileSync(join(path, 'package.json'), 'utf8')).version;
|
||||
const file = join(dir, 'lib/index.js');
|
||||
const patched = patchOffice(readFileSync(file, 'utf8'), version(root), version(dir));
|
||||
for (const [source, name] of [['native-office.mjs', 'dsh-native-office.mjs'], ['validate-office.py', 'validate-office.py'], ['office-worker.py', 'office-worker.py']]) {
|
||||
copyFileSync(new URL(source, import.meta.url), join(dir, 'lib', name));
|
||||
}
|
||||
writeFileSync(file, patched);
|
||||
}
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) install();
|
||||
Reference in New Issue
Block a user