This commit is contained in:
Pan Qiancheng 2025-04-03 18:45:04 +08:00
parent 2f2da071fa
commit e271ec7985
1 changed files with 68 additions and 173 deletions

241
README.md
View File

@ -1,195 +1,90 @@
# Nodejs工程模版 基于TypeScript
# PDF 银行流水转 Excel 工具
Nodejs+Typescript+Eslint+Prettier+Husky项目构建
这是一个 Node.js 命令行工具,用于将银行 PDF 流水文件转换为 Excel 文件。
[原文链接](https://blog.csdn.net/sdpyly/article/details/132453357)
## 功能概述
# 准备工作
- 解析 PDF 银行流水文件,提取交易记录。
- 将提取的交易记录转换为 Excel 格式。
- 支持自定义输出 Excel 文件名。
确保已经安装了git以及Node.js和npm通过`git -v`、`node -v`和`npm -v`检查是否安装。
# 初始化项目
```sh
# 进入你的目录
cd your-directory
## 依赖项
# 初始化 npm 项目
npm init -y
- dayjs
- pdf2json
- node-xlsx
- yargs
# 安装 TypeScript 和 ts-node
npm install --save-dev typescript ts-node
## 如何使用
# 初始化 TypeScript 配置
npx tsc --init
1. **安装依赖项:**
# 创建 src 目录
mkdir src
```bash
npm install
```
# 创建你的 TypeScript 文件
touch src/index.ts
```
在新建的tsconfig.json中修改target、module、和outDir其中outDir设置为`./dist`,其他根据情况设置。
# Eslint安装和配置
```sh
# 安装 ESLint
npm install --save-dev eslint
2. **运行工具:**
# 初始化 ESLint 配置
npx eslint --init
```
根据情况选择配置:
```text
You can also run this command directly using 'npm init @eslint/config'.
Need to install the following packages:
@eslint/create-config@0.4.6
Ok to proceed? (y) y
✔ How would you like to use ESLint? · style
✔ What type of modules does your project use? · commonjs
✔ Which framework does your project use? · none
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · node
✔ How would you like to define a style for your project? · guide
✔ Which style guide do you want to follow? · standard-with-typescript
✔ What format do you want your config file to be in? · JavaScript
Checking peerDependencies of eslint-config-standard-with-typescript@latest
The config that you've selected requires the following dependencies:
```bash
node index.js -i <input.pdf> [-o <output.xlsx>]
```
eslint-config-standard-with-typescript@latest @typescript-eslint/eslint-plugin@^6.1.0 eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 || ^16.0.0 eslint-plugin-promise@^6.0.0 typescript@*
✔ Would you like to install them now? · No / Yes
✔ Which package manager do you want to use? · npm
Installing eslint-config-standard-with-typescript@latest, @typescript-eslint/eslint-plugin@^6.1.0, eslint@^8.0.1, eslint-plugin-import@^2.25.2, eslint-plugin-n@^15.0.0 || ^16.0.0 , eslint-plugin-promise@^6.0.0, typescript@*
- `-i <input.pdf>`:指定输入的 PDF 文件名(必需)。
- `-o <output.xlsx>`:指定输出的 Excel 文件名(可选,默认为 `transactions.xlsx`)。
added 113 packages in 5s
```
# Prettier安装和配置
1. 首先,安装 Prettier在项目目录中运行
```sh
npm install --save-dev prettier
```
例如:
2. 创建一个 .prettierrc 文件来配置 Prettier。你可以在这个文件中设置你的格式化选项例如
```json
{
"semi": true,
"singleQuote": true,
"tabWidth": 4
}
```
# 在Eslint中使用Prettier插件
安装 eslint-plugin-prettier 和 eslint-config-prettier
```bash
node index.js -i bank_statement.pdf -o transactions.xlsx
```
```sh
npm install --save-dev eslint-plugin-prettier eslint-config-prettier
```
3. **构建项目(可选):**
编辑.eslintrc.js文件
```js
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es2022": true
},
"extends": ["standard-with-typescript","prettier"],
"plugins": ["prettier"],
"overrides": [
{
"env": {
"node": true
},
"files": [
".eslintrc.{js,cjs}"
],
"parserOptions": {
"sourceType": "script"
}
}
],
"parserOptions": {
"ecmaVersion": "latest",
"project": "./tsconfig.json"
},
"rules": {
"prettier/prettier": "error"
}
}
如果您使用 TypeScript 并且需要构建项目,请运行:
```bash
npm run build
```
然后,您可以使用以下命令运行构建后的 JavaScript 文件:
```bash
node ./dist/index.js -i <input.pdf> [-o <output.xlsx>]
```
## 项目结构
```
# Husky安装和配置
```sh
# 安装 Husky和commitlint
npm install --save-dev @commitlint/cli @commitlint/config-conventional husky
# 创建git仓库
git init
# 可修改分支名
git branch -m 旧名字 新名字
# 初始化 Husky
npx husky install
```
创建一个名为 commitlint.config.js 的文件,内容如下:
```js
module.exports = {extends: ['@commitlint/config-conventional']};
项目根目录/
├── index.js # 主入口文件
├── package.json # 项目配置文件
├── README.md # 项目说明文档
└── ... # 其他文件
```
提交前检查代码风格和提交格式:
```sh
npx husky add .husky/pre-commit "npm run lint"
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
```
# 修改tsconfig.json
可根据需要灵活配置
## 注意事项
## 启用`@`表示src目录
tsconfig.json需添加设置
```json
{
...
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
},
...
}
```
执行`npm install tsconfig-paths`
- 该工具仅适用于特定格式的银行 PDF 流水文件。如果您的 PDF 文件格式不同,可能需要修改代码中的正则表达式和交易类型列表。
- 如果 PDF 文件包含加密或复杂的布局,解析可能会失败。
# 修改package.json
将scripts修改为
```json
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "ts-node -r tsconfig-paths/register src/index.ts",
"debug": "node --inspect-brk dist/index.js",
"lint": "eslint ./src --ext .ts --fix",
"format": "prettier --write ./src",
"prepare": "husky install"
},
```
## 贡献
# 设置vscode调试
可参考以下的launch.json
```json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/index.ts",
"sourceMaps": true,
"runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"console": "integratedTerminal"
}
]
}
```
欢迎贡献代码!请按照以下步骤:
1. Fork 本仓库。
2. 创建你的 Feature Branch (`git checkout -b feature/YourFeature`)。
3. 提交你的修改 (`git commit -m 'Add some YourFeature'`)。
4. 推送到远程分支 (`git push origin feature/YourFeature`)。
5. 发起 Pull Request。
## 许可证
[请在此处添加你的项目许可证,例如 MIT、Apache 2.0 等]
## 作者
[请在此处添加你的姓名或组织名称]
## 联系方式
[请在此处添加你的联系方式例如邮箱、GitHub 链接等]