Skip to content

Commit

Permalink
bundle mammoth in core (#281)
Browse files Browse the repository at this point in the history
* bundle mammoth

* don't copy mammoth in node
  • Loading branch information
pelikhan authored Mar 21, 2024
1 parent bd2d11d commit 11701e3
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"optionalDependencies": {
"llamaindex": "0.1.21",
"llm-code-highlighter": "0.0.13",
"mammoth": "^1.7.0",
"pdfjs-dist": "4.0.379"
},
"engines": {
"node": ">=20.0.0"
},
"devDependencies": {
"mammoth": "^1.7.0",
"commander": "^12.0.0",
"dotenv": "^16.4.5",
"execa": "^8.0.1",
Expand All @@ -58,7 +58,7 @@
"zx": "^7.2.3"
},
"scripts": {
"compile": "esbuild src/main.ts --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:pdfjs-dist --external:mammoth --external:llamaindex --external:llm-code-highlighter && echo '#!/usr/bin/env node' | cat - built/genaiscript.cjs > temp && mv temp built/genaiscript.cjs",
"compile": "esbuild src/main.ts --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:pdfjs-dist --external:llamaindex --external:llm-code-highlighter && echo '#!/usr/bin/env node' | cat - built/genaiscript.cjs > temp && mv temp built/genaiscript.cjs",
"postcompile": "cp built/genaiscript.cjs ../vscode/genaiscript.cjs && node built/genaiscript.cjs help-all > ../../docs/src/content/docs/reference/cli/commands.md",
"go": "yarn compile && node built/genaiscript.cjs",
"test": "node --import tsx --test src/**.test.ts",
Expand Down
23 changes: 2 additions & 21 deletions packages/core/src/docx.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import { host } from "./host"
import { MarkdownTrace } from "./trace"
import { installImport } from "./import"
import { logError } from "./util"
import { MAMMOTH_VERSION } from "./version"

async function tryImportMammoth(trace?: MarkdownTrace) {
try {
const mod = await import("mammoth")
return mod
} catch (e) {
trace?.error(`mammoth not found, installing ${MAMMOTH_VERSION}...`)
try {
await installImport("mammoth", MAMMOTH_VERSION, trace)
const mod = await import("mammoth")
return mod
} catch (e) {
trace?.error("mammoth failed to load")
return undefined
}
}
}
import { extractRawText } from "mammoth"

/**
* parses docx, require mammoth to be installed
Expand All @@ -33,11 +15,10 @@ export async function DOCXTryParse(
): Promise<string> {
const { trace } = options || {}
try {
const mammoth = await tryImportMammoth(trace)
const path = !/^\//.test(file)
? host.path.join(host.projectFolder(), file)
: file
const results = await mammoth.extractRawText({ path })
const results = await extractRawText({ path })
return results.value
} catch (error) {
logError(error.message)
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ export const CORE_VERSION = packageJson.version
export const GITHUB_REPO = packageJson.repository.url

export const PDFJS_DIST_VERSION = packageJson.optionalDependencies["pdfjs-dist"]
export const MAMMOTH_VERSION = packageJson.optionalDependencies["mammoth"]
2 changes: 1 addition & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
"scripts": {
"vscode:update-dts": "npx @vscode/dts dev",
"vscode:prepublish": "yarn run compile",
"compile:extension": "esbuild src/extension.ts --sourcemap --bundle --format=cjs --platform=node --target=node20 --outfile=built/extension.js --external:vscode --external:pdfjs-dist --external:mammoth --external:llamaindex",
"compile:extension": "esbuild src/extension.ts --sourcemap --bundle --format=cjs --platform=node --target=node20 --outfile=built/extension.js --external:vscode --external:pdfjs-dist --external:llamaindex",
"compile": "yarn compile:extension",
"lint": "eslint src --ext ts",
"run-in-browser": "vscode-test-web --browserType=chromium --extensionDevelopmentPath=. ../sample",
Expand Down
1 change: 0 additions & 1 deletion packages/vscode/postpackage.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'zx/globals'

await $`rm -Rf ./node_modules/pdfjs-dist`
await $`rm -Rf ./node_modules/mammoth`

const pkg = await fs.readJSON('./package.json')
pkg.enabledApiProposals = pkg._enabledApiProposals
Expand Down
3 changes: 0 additions & 3 deletions packages/vscode/prepackage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import 'zx/globals'
await fs.copy('../../node_modules/pdfjs-dist/package.json', './node_modules/pdfjs-dist/package.json', { overwrite: true })
await $`cp -R ../../node_modules/pdfjs-dist/build ./node_modules/pdfjs-dist`

await fs.copy('../../node_modules/mammoth/package.json', './node_modules/mammoth/package.json', { overwrite: true })
await $`cp -R ../../node_modules/mammoth/lib ./node_modules/mammoth`

const pkg = await fs.readJSON('./package.json')
pkg._enabledApiProposals = pkg.enabledApiProposals
pkg.displayName = "GenAIScript"
Expand Down

0 comments on commit 11701e3

Please sign in to comment.