@blueid/access-cli 0.19.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,9 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs'
3
3
 
4
+ import binaryToIntelHex from './binaryToIntelHex.js'
5
+ import crc16 from './crc16.js'
6
+
4
7
  const bufferToCArray = (buffer) => {
5
8
  const hexString = buffer.toString('hex')
6
9
  const hexArray = hexString.match(/.{1,2}/g)
@@ -13,9 +16,41 @@ const bufferToProtoArray = (buffer) => {
13
16
  return hexArray.map((hex) => `\\x${hex}`).join('')
14
17
  }
15
18
 
19
+ const bufferToIntelHex = (buffer, hexStartAddress, hexSize, hexCrc16, hexCrc16Magic) => {
20
+ if (typeof hexStartAddress !== 'number') {
21
+ throw new Error('Invalid hexStartAddress')
22
+ }
23
+
24
+ if (typeof hexSize !== 'number') {
25
+ throw new Error('Invalid hexSize')
26
+ }
27
+
28
+ if (typeof hexCrc16 !== 'boolean') {
29
+ throw new Error('Invalid hexCrc16')
30
+ }
31
+
32
+ if (typeof hexCrc16Magic !== 'boolean') {
33
+ throw new Error('Invalid hexCrc16Magic')
34
+ }
35
+
36
+ const binaryBuffer = Buffer.alloc(hexSize)
37
+
38
+ if (hexCrc16) {
39
+ buffer.copy(binaryBuffer, 2, 0)
40
+ const crc16_ = crc16(binaryBuffer, 2, hexCrc16Magic)
41
+ binaryBuffer.writeUInt16LE(crc16_, 0)
42
+ console.log(`Intel Hex, startAddress=${hexStartAddress}, size=${hexSize}, crc16=${crc16_}`)
43
+ } else {
44
+ buffer.copy(binaryBuffer, 0, 0)
45
+ console.log(`Intel Hex, startAddress=${hexStartAddress}, size=${hexSize}`)
46
+ }
47
+
48
+ return binaryToIntelHex(hexStartAddress, true, binaryBuffer)
49
+ }
50
+
16
51
  export const outputFormats = ['hex', 'cpphex', 'protohex', 'base64']
17
52
 
18
- export default (buffer, format, filename) => {
53
+ export default (buffer, format, filename, settings = {}) => {
19
54
  let result = null
20
55
 
21
56
  switch (format) {
@@ -31,13 +66,34 @@ export default (buffer, format, filename) => {
31
66
  case 'base64':
32
67
  result = buffer.toString('base64')
33
68
  break
34
- default:
69
+ case 'file':
35
70
  {
36
71
  const filepath = path.resolve(filename)
37
72
  fs.writeFileSync(filepath, buffer)
38
73
  result = `Written to file <${filepath}>`
39
74
  }
40
75
  break
76
+ case 'file_hex':
77
+ {
78
+ const [startAddress, filename_] = filename.split(':')
79
+
80
+ const startAddressNo = Number.parseInt(startAddress)
81
+ if (Number.isNaN(startAddressNo)) {
82
+ throw new Error(`Invalid startAddress ${startAddress}`)
83
+ }
84
+
85
+ const { fileHexSize, fileHexCrc16, fileHexCrc16Magic } = settings
86
+
87
+ const hexData = bufferToIntelHex(buffer, startAddressNo, fileHexSize, fileHexCrc16, fileHexCrc16Magic)
88
+
89
+ const filepath = path.resolve(filename_)
90
+ fs.writeFileSync(filepath, hexData)
91
+
92
+ result = `Written to file <${filepath}>`
93
+ }
94
+ break
95
+ default:
96
+ throw new Error(`Unknown format ${format}`)
41
97
  }
42
98
 
43
99
  console.log(`(${format} with ${buffer.length.toString()} bytes)\n`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueid/access-cli",
3
- "version": "0.19.0",
3
+ "version": "0.22.0",
4
4
  "description": "BlueID Access CLI",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -14,7 +14,7 @@
14
14
  "package.json"
15
15
  ],
16
16
  "scripts": {
17
- "sync-proto": "sh syncproto.sh",
17
+ "sync-proto": "sh syncproto.sh || exit 0",
18
18
  "preinstall": "npm run sync-proto",
19
19
  "prepack": "npm run sync-proto"
20
20
  },