Skip to content

Commit

Permalink
feat: support convert docx to txt
Browse files Browse the repository at this point in the history
  • Loading branch information
DemoMacro committed Jun 30, 2023
1 parent 8c8fd68 commit 4315366
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@funish/githooks": "edge",
"@funish/githooks-config": "edge",
"@funish/lint": "edge",
"@types/node": "20.3.2",
"prettier": "2.8.8",
"rome": "12.1.3",
"unbuild": "1.2.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/docen/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: true,
entries: ["src/"],
entries: ["src/index", "src/cli"],
});
10 changes: 9 additions & 1 deletion packages/docen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"files": [
"dist"
],
"bin": {
"docen": "dist/cli.mjs"
},
"scripts": {
"prepack": "unbuild"
},
Expand All @@ -27,5 +30,10 @@
"bugs": {
"url": "https://github.com/DemoMacro/docen/issues"
},
"homepage": "https://github.com/DemoMacro/docen#readme"
"homepage": "https://github.com/DemoMacro/docen#readme",
"dependencies": {
"@funish/cli": "0.0.4",
"file-type": "18.5.0",
"mammoth": "1.6.0"
}
}
30 changes: 30 additions & 0 deletions packages/docen/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CLI } from "@funish/cli";
import { docen } from ".";

const cli = new CLI("docen");

cli.command({
options: [
{
name: "source",
alias: "s",
description: "Source directory",
},
{
name: "target",
alias: "t",
description: "Target directory",
},
],
action: (argv) => {
if (argv.source && argv.target) {
docen(argv.source as string, argv.target as string);
} else {
cli.help();
}
},
});

cli.version();

cli.help();
17 changes: 17 additions & 0 deletions packages/docen/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFileSync, writeFileSync } from "fs";
import { extractRawText } from "mammoth";
import { extname } from "path";
import { fileTypeFromBuffer } from "file-type";

export async function docen(source: string, target: string): Promise<void> {
const sourceContent = readFileSync(source);
const sourceType = await fileTypeFromBuffer(sourceContent);

if (sourceType?.ext === "docx" && extname(target) === ".txt") {
extractRawText({ buffer: sourceContent }).then((result) => {
writeFileSync(target, result.value);
});
} else {
console.error(`Unsupported source file type: ${sourceType?.ext}`);
}
}
Loading

0 comments on commit 4315366

Please sign in to comment.