README
This commit is contained in:
parent
2f2da071fa
commit
e271ec7985
241
README.md
241
README.md
|
|
@ -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 项目
|
- dayjs
|
||||||
npm init -y
|
- pdf2json
|
||||||
|
- node-xlsx
|
||||||
|
- yargs
|
||||||
|
|
||||||
# 安装 TypeScript 和 ts-node
|
## 如何使用
|
||||||
npm install --save-dev typescript ts-node
|
|
||||||
|
|
||||||
# 初始化 TypeScript 配置
|
1. **安装依赖项:**
|
||||||
npx tsc --init
|
|
||||||
|
|
||||||
# 创建 src 目录
|
```bash
|
||||||
mkdir src
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
# 创建你的 TypeScript 文件
|
2. **运行工具:**
|
||||||
touch src/index.ts
|
|
||||||
```
|
|
||||||
在新建的tsconfig.json中,修改target、module、和outDir,其中outDir设置为`./dist`,其他根据情况设置。
|
|
||||||
# Eslint安装和配置
|
|
||||||
```sh
|
|
||||||
# 安装 ESLint
|
|
||||||
npm install --save-dev eslint
|
|
||||||
|
|
||||||
# 初始化 ESLint 配置
|
```bash
|
||||||
npx eslint --init
|
node index.js -i <input.pdf> [-o <output.xlsx>]
|
||||||
```
|
```
|
||||||
根据情况选择配置:
|
|
||||||
```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:
|
|
||||||
|
|
||||||
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 文件名(必需)。
|
||||||
✔ Would you like to install them now? · No / Yes
|
- `-o <output.xlsx>`:指定输出的 Excel 文件名(可选,默认为 `transactions.xlsx`)。
|
||||||
✔ 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@*
|
|
||||||
|
|
||||||
added 113 packages in 5s
|
例如:
|
||||||
```
|
|
||||||
# Prettier安装和配置
|
|
||||||
1. 首先,安装 Prettier,在项目目录中,运行
|
|
||||||
```sh
|
|
||||||
npm install --save-dev prettier
|
|
||||||
```
|
|
||||||
|
|
||||||
2. 创建一个 .prettierrc 文件来配置 Prettier。你可以在这个文件中设置你的格式化选项,例如:
|
```bash
|
||||||
```json
|
node index.js -i bank_statement.pdf -o transactions.xlsx
|
||||||
{
|
```
|
||||||
"semi": true,
|
|
||||||
"singleQuote": true,
|
|
||||||
"tabWidth": 4
|
|
||||||
}
|
|
||||||
```
|
|
||||||
# 在Eslint中使用Prettier插件
|
|
||||||
安装 eslint-plugin-prettier 和 eslint-config-prettier
|
|
||||||
|
|
||||||
```sh
|
3. **构建项目(可选):**
|
||||||
npm install --save-dev eslint-plugin-prettier eslint-config-prettier
|
|
||||||
```
|
|
||||||
|
|
||||||
编辑.eslintrc.js文件
|
如果您使用 TypeScript 并且需要构建项目,请运行:
|
||||||
```js
|
|
||||||
module.exports = {
|
```bash
|
||||||
"env": {
|
npm run build
|
||||||
"browser": true,
|
```
|
||||||
"commonjs": true,
|
|
||||||
"es2022": true
|
然后,您可以使用以下命令运行构建后的 JavaScript 文件:
|
||||||
},
|
|
||||||
"extends": ["standard-with-typescript","prettier"],
|
```bash
|
||||||
"plugins": ["prettier"],
|
node ./dist/index.js -i <input.pdf> [-o <output.xlsx>]
|
||||||
"overrides": [
|
```
|
||||||
{
|
|
||||||
"env": {
|
## 项目结构
|
||||||
"node": true
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
".eslintrc.{js,cjs}"
|
|
||||||
],
|
|
||||||
"parserOptions": {
|
|
||||||
"sourceType": "script"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"parserOptions": {
|
|
||||||
"ecmaVersion": "latest",
|
|
||||||
"project": "./tsconfig.json"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"prettier/prettier": "error"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
```
|
||||||
# Husky安装和配置
|
项目根目录/
|
||||||
```sh
|
├── index.js # 主入口文件
|
||||||
# 安装 Husky和commitlint
|
├── package.json # 项目配置文件
|
||||||
npm install --save-dev @commitlint/cli @commitlint/config-conventional husky
|
├── README.md # 项目说明文档
|
||||||
|
└── ... # 其他文件
|
||||||
# 创建git仓库
|
|
||||||
git init
|
|
||||||
|
|
||||||
# 可修改分支名
|
|
||||||
git branch -m 旧名字 新名字
|
|
||||||
|
|
||||||
# 初始化 Husky
|
|
||||||
npx husky install
|
|
||||||
```
|
|
||||||
创建一个名为 commitlint.config.js 的文件,内容如下:
|
|
||||||
```js
|
|
||||||
module.exports = {extends: ['@commitlint/config-conventional']};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
提交前检查代码风格和提交格式:
|
## 注意事项
|
||||||
```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目录
|
- 该工具仅适用于特定格式的银行 PDF 流水文件。如果您的 PDF 文件格式不同,可能需要修改代码中的正则表达式和交易类型列表。
|
||||||
tsconfig.json需添加设置
|
- 如果 PDF 文件包含加密或复杂的布局,解析可能会失败。
|
||||||
```json
|
|
||||||
{
|
|
||||||
...
|
|
||||||
"baseUrl": "./",
|
|
||||||
"paths": {
|
|
||||||
"@/*": ["src/*"]
|
|
||||||
},
|
|
||||||
...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
执行`npm install tsconfig-paths`
|
|
||||||
|
|
||||||
# 修改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
|
1. Fork 本仓库。
|
||||||
{
|
2. 创建你的 Feature Branch (`git checkout -b feature/YourFeature`)。
|
||||||
"version": "0.2.0",
|
3. 提交你的修改 (`git commit -m 'Add some YourFeature'`)。
|
||||||
"configurations": [
|
4. 推送到远程分支 (`git push origin feature/YourFeature`)。
|
||||||
{
|
5. 发起 Pull Request。
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
## 许可证
|
||||||
"name": "Launch Program",
|
|
||||||
"skipFiles": [
|
[请在此处添加你的项目许可证,例如 MIT、Apache 2.0 等]
|
||||||
"<node_internals>/**"
|
|
||||||
],
|
## 作者
|
||||||
"program": "${workspaceFolder}/src/index.ts",
|
|
||||||
"sourceMaps": true,
|
[请在此处添加你的姓名或组织名称]
|
||||||
"runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
|
|
||||||
"outFiles": [
|
## 联系方式
|
||||||
"${workspaceFolder}/**/*.js"
|
|
||||||
],
|
[请在此处添加你的联系方式,例如邮箱、GitHub 链接等]
|
||||||
"console": "integratedTerminal"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
Loading…
Reference in New Issue