diff --git a/cli/src/index.ts b/cli/src/index.ts index f899beb16..7e1d9087f 100755 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -317,6 +317,7 @@ export class Commander { .argParser(parseFileSave) .conflicts('fileWrite'), ) + .option('--delimiter [CHARACTER]', 'append a delimiter to the end of each message, default is "\\n"') .option( '-Pp, --protobuf-path ', 'the path to the .proto file that defines the message format for Protocol Buffers (protobuf)', diff --git a/cli/src/lib/sub.ts b/cli/src/lib/sub.ts index 7b7c8382d..7e6dda75f 100644 --- a/cli/src/lib/sub.ts +++ b/cli/src/lib/sub.ts @@ -108,7 +108,7 @@ const sub = (options: SubscribeOptions) => { }) client.on('message', (topic, payload, packet) => { - const { format, protobufPath, protobufMessageName, fileSave, fileWrite } = options + const { format, protobufPath, protobufMessageName, fileSave, fileWrite, delimiter } = options const msgData: Record[] = [] @@ -117,7 +117,7 @@ const sub = (options: SubscribeOptions) => { const savePath = fileSave ? createNextNumberedFileName(fileSave) : fileWrite if (savePath) { fileSave && writeFile(savePath, receivedMessage) - fileWrite && appendFile(savePath, receivedMessage) + fileWrite && appendFile(savePath, receivedMessage, delimiter) } options.verbose && msgData.push({ label: 'mqtt-packet', value: packet }) diff --git a/cli/src/types/global.d.ts b/cli/src/types/global.d.ts index 144ee3d40..d7f435959 100644 --- a/cli/src/types/global.d.ts +++ b/cli/src/types/global.d.ts @@ -81,17 +81,19 @@ declare global { interface SubscribeOptions extends ConnectOptions { topic: string[] qos?: QoS[] - // properties of MQTT 5.0 - fileWrite?: string - fileSave?: string + // Start properties of MQTT 5.0 no_local?: boolean[] retainAsPublished?: boolean[] retainHandling?: QoS[] subscriptionIdentifier?: number[] + connUserProperties?: Record + // End properties of MQTT 5.0 + fileWrite?: string + fileSave?: string + delimiter?: string format?: FormatType outputMode?: OutputMode verbose: boolean - connUserProperties?: Record protobufPath?: string protobufMessageName?: string } @@ -119,7 +121,7 @@ declare global { type OmitSubscribeOptions = Omit< SubscribeOptions, - 'format' | 'outputMode' | 'protobufPath' | 'protobufMessageName' | 'debug' | 'fileWrite' | 'fileSave' + 'format' | 'outputMode' | 'protobufPath' | 'protobufMessageName' | 'debug' | 'fileWrite' | 'fileSave' | 'delimiter' > interface BenchSubscribeOptions extends OmitSubscribeOptions { diff --git a/cli/src/utils/fileUtils.ts b/cli/src/utils/fileUtils.ts index 0b3c5b348..cd0fc9ce5 100644 --- a/cli/src/utils/fileUtils.ts +++ b/cli/src/utils/fileUtils.ts @@ -52,9 +52,9 @@ const writeFile = (filePath: string, data: string | Buffer): void => { } } -const appendFile = (filePath: string, data: string | Buffer): void => { +const appendFile = (filePath: string, data: string | Buffer, delimiter = '\n'): void => { try { - fs.appendFileSync(filePath, `${data}\n`) + fs.appendFileSync(filePath, `${data}${delimiter}`) } catch (error) { signale.error(error) process.exit(1)