Skip to content

Commit

Permalink
feat: new runnable xhb binary (#294)
Browse files Browse the repository at this point in the history
* feat: new runnable xhb binary
* fix: chunks not being concatenated
* feat: update readme with cli example
  • Loading branch information
hertzg authored Nov 10, 2021
1 parent d7a148c commit b1497d1
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 21 deletions.
Binary file not shown.
Binary file not shown.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
# XHB file read/write for NodeJS
This package provides ability to read and modify xhb files created by HomeBank
in somewhat sane manner.

[HomeBank](http://homebank.free.fr/) is a personal finance and money management software application built and maintained by Maxime Doyen.
This package provides ability to read and modify xhb files created by HomeBank in somewhat sane manner.

## Installation and Usage
[HomeBank](http://homebank.free.fr/) is a personal finance and money management software application built and
maintained by Maxime Doyen.

## Command Line Usage
This package also includes a tiny cli utility `xhb` to convert between `.xhb` and `.json` formats

```bash
# Convert XHB to JSON
$ npx xhb parse < database.xhb > database.json

# Modify the database with common tools, just keep structure the same
$ mv database.json database-modified.json

# Convert JSON back to XHB
$ npx xhb serialize < database-modified.json > database-modified.xhb
```

## Programmatic Usage

```bash
$ npm install xhb --save
```

```typescript
import FS from 'fs'
import {parse, serialize} from 'xhb'
import { parse, serialize } from 'xhb'


const contents = FS.readFileSync('./homebank.xhb', {encoding:'utf8'})
const contents = FS.readFileSync('./homebank.xhb', { encoding: 'utf8' })
const xhb = parse(contents)

// modify / copy / clone the xhb object, whatever you need to do with it.

const modified = serialize(xhb);
FS.writeFileSync('./homebank-modified.xhb', modified, {encoding: 'utf8'})
const modified = serialize(xhb)
FS.writeFileSync('./homebank-modified.xhb', modified, { encoding: 'utf8' })
```

## API overview
Expand Down Expand Up @@ -58,12 +72,13 @@ export interface XHB {
For more information, please see type definitions for respective entities from source code.

### FAQ

Here are some questions that come up or most likely will come up.

##### Dates are in weird format, how do I convert them to js Dates?

The dates are in GLib specific julian day format (which is not "real" julian day count).
Consider using package [`gdate-julian`](https://github.com/hertzg/node-gdate-julian) package.
The dates are in GLib specific julian day format (which is not "real" julian day count). Consider using
package [`gdate-julian`](https://github.com/hertzg/node-gdate-julian) package.

---

Expand All @@ -89,9 +104,9 @@ HomeBank does not create them separately in XML. This package reads and write th

##### The XML produced is not "correct" xml and why do you use `sprintf` to create xml tags? That's stupid!

This is the way HomeBank deals with XML files. Don't believe me? Check the source code on launchpad.
The goal of this project is to be able to read and write files acceptable by that application so I mimic the way it
deals with the file format.
This is the way HomeBank deals with XML files. Don't believe me? Check the source code on launchpad. The goal of this
project is to be able to read and write files acceptable by that application so I mimic the way it deals with the file
format.

See: [https://bazaar.launchpad.net/~mdoyen/homebank/5.2.x/view/head:/src/hb-xml.c#L1214](https://bazaar.launchpad.net/~mdoyen/homebank/5.2.x/view/head:/src/hb-xml.c#L1214)

Expand Down
2 changes: 2 additions & 0 deletions bin/xhb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../dist/bin/xhb')
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"email": "georgedot@gmail.com"
},
"engines": {
"node": ">=8"
"node": ">=16"
},
"bin": "dist/bin/xhb",
"keywords": [
"xhb",
"file",
Expand Down Expand Up @@ -42,11 +43,13 @@
"semantic-release": "semantic-release"
},
"files": [
"bin/**",
"dist/**"
],
"dependencies": {
"printj": "^1.2.2",
"xml-parser": "^1.2.1"
"xml-parser": "^1.2.1",
"yargs": "^17.2.1"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.0",
Expand All @@ -56,10 +59,11 @@
"@semantic-release/github": "^8.0.0",
"@semantic-release/npm": "^8.0.0",
"@semantic-release/release-notes-generator": "^10.0.0",
"@tsconfig/node12": "^1.0.9",
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/node": "^16.0.0",
"@types/xml-parser": "^1.2.29",
"@types/yargs": "^17",
"conventional-changelog-conventionalcommits": "^4.5.0",
"jest": "^27.0.6",
"prettier": "^2.2.1",
Expand Down
51 changes: 51 additions & 0 deletions src/bin/xhb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { parse, serialize } from '../'

const _collect = async (stream: NodeJS.ReadStream): Promise<Buffer> => {
const chunks: Buffer[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
return Buffer.concat(chunks)
}

const parseAction = async () => {
const xhb = (await _collect(process.stdin)).toString('utf8')
const object = parse(xhb)
process.stdout.end(JSON.stringify(object))
}

const serializeAction = async () => {
const json = (await _collect(process.stdin)).toString('utf8')
const xhb = serialize(JSON.parse(json))
process.stdout.end(xhb)
}

const bootstrap = () => {
return Yargs(hideBin(process.argv), process.cwd())
.scriptName('xhb')
.version()
.command(
'parse',
'read xhb from stdin and write json to stdout',
parseAction,
)
.command(
'serialize',
'read json from stdin and write xhb to stdout',
serializeAction,
)
.help()
.wrap(Yargs.terminalWidth() !== null ? Yargs.terminalWidth() : 80)
.demandCommand()
.recommendCommands()
.strict()
}

const main = async () => {
const program = bootstrap()
await program.parseAsync()
}

main()
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"declaration": true
},
"exclude": ["**/__tests__"]
"exclude": ["node_modules", "dist", "**/__tests__"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"outDir": "dist",
"rootDir": "src",
"noImplicitAny": false
}
},
"exclude": ["./dist/**/*"]
}
32 changes: 30 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ __metadata:
languageName: node
linkType: hard

"@tsconfig/node12@npm:^1.0.7, @tsconfig/node12@npm:^1.0.9":
"@tsconfig/node12@npm:^1.0.7":
version: 1.0.9
resolution: "@tsconfig/node12@npm:1.0.9"
checksum: a01b2400ab3582b86b589c6d31dcd0c0656f333adecde85d6d7d4086adb059808b82692380bb169546d189bf771ae21d02544a75b57bd6da4a5dd95f8567bec9
Expand Down Expand Up @@ -1384,6 +1384,15 @@ __metadata:
languageName: node
linkType: hard

"@types/yargs@npm:^17":
version: 17.0.5
resolution: "@types/yargs@npm:17.0.5"
dependencies:
"@types/yargs-parser": "*"
checksum: 2be02640c84471c66aec58cfd415432fab0973283909765aa3becbdb76b5874d4770fe7f376e77b98833d5cc9d9bc258a905adce68a0baec13d430c866fce3b5
languageName: node
linkType: hard

"JSONStream@npm:^1.0.4":
version: 1.3.5
resolution: "JSONStream@npm:1.3.5"
Expand Down Expand Up @@ -7422,10 +7431,11 @@ __metadata:
"@semantic-release/github": ^8.0.0
"@semantic-release/npm": ^8.0.0
"@semantic-release/release-notes-generator": ^10.0.0
"@tsconfig/node12": ^1.0.9
"@tsconfig/node16": ^1.0.2
"@types/jest": ^27.0.2
"@types/node": ^16.0.0
"@types/xml-parser": ^1.2.29
"@types/yargs": ^17
conventional-changelog-conventionalcommits: ^4.5.0
jest: ^27.0.6
prettier: ^2.2.1
Expand All @@ -7436,6 +7446,9 @@ __metadata:
ts-node: ^10.4.0
typescript: ^4.1.3
xml-parser: ^1.2.1
yargs: ^17.2.1
bin:
xhb: dist/bin/xhb
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -7512,6 +7525,21 @@ __metadata:
languageName: node
linkType: hard

"yargs@npm:^17.2.1":
version: 17.2.1
resolution: "yargs@npm:17.2.1"
dependencies:
cliui: ^7.0.2
escalade: ^3.1.1
get-caller-file: ^2.0.5
require-directory: ^2.1.1
string-width: ^4.2.0
y18n: ^5.0.5
yargs-parser: ^20.2.2
checksum: 451aac46f82da776f436018feed0244bc0e7b4355f7e397bcb53d34c691b177c0d71db3dda9653760e1bc240254d8b763a252ff918ef9e235a8d202e2909c4eb
languageName: node
linkType: hard

"yn@npm:3.1.1":
version: 3.1.1
resolution: "yn@npm:3.1.1"
Expand Down

0 comments on commit b1497d1

Please sign in to comment.