Skip to content

Commit

Permalink
fix(cli): fix options handler of file write
Browse files Browse the repository at this point in the history
  • Loading branch information
DM1-1 authored and ysfscream committed Apr 28, 2024
1 parent 5b2edc0 commit d60d4b4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ export class Commander {
'load the parameters from the local configuration file, which supports json and yaml format, default path is ./mqttx-cli-config.json',
)
// https://github.com/tj/commander.js/blob/master/examples/options-conflicts.js
.addOption(new Option('--file-write <PATH>', 'append received messages to a specified file').default(parseFileWrite).conflicts('fileSave'))
.addOption(new Option('--file-save <PATH>', 'save each received message to a new file').default(parseFileSave).conflicts('fileWrite'))
.addOption(new Option('--file-write <PATH>', 'append received messages to a specified file').argParser(parseFileWrite).conflicts('fileSave'))
.addOption(new Option('--file-save <PATH>', 'save each received message to a new file').argParser(parseFileSave).conflicts('fileWrite'))
.option(
'-Pp, --protobuf-path <PATH>',
'the path to the .proto file that defines the message format for Protocol Buffers (protobuf)',
Expand Down
4 changes: 2 additions & 2 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parseConnectOptions, parseSubscribeOptions, checkTopicExists } from '..
import delay from '../utils/delay'
import convertPayload from '../utils/convertPayload'
import { saveConfig, loadConfig } from '../utils/config'
import { writeFile, appendFile, getPathExtname } from '../utils/fileUtils'
import { writeFile, appendFile, getPathExtname, createNextNumberedFileName } from '../utils/fileUtils'
import { deserializeBufferToProtobuf } from '../utils/protobuf'
import isSupportedBinaryFormatForMQTT from '../utils/binaryFormats'
import * as Debug from 'debug'
Expand Down Expand Up @@ -114,7 +114,7 @@ const sub = (options: SubscribeOptions) => {

const receivedMessage = processReceivedMessage(payload, protobufPath, protobufMessageName, format)

const savePath = fileSave ?? fileWrite
const savePath = fileSave ? createNextNumberedFileName(fileSave) : fileWrite
if(savePath) {
fileSave && writeFile(savePath, receivedMessage)
fileWrite && appendFile(savePath, receivedMessage)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs'
import signale from '../utils/signale'
import { getSpecialTypesOption } from '../utils/generator'
import { createNextNumberedFileName, readFile, processPath, getPathExtname } from '../utils/fileUtils'
import { readFile, processPath } from '../utils/fileUtils'

import { IClientOptions, IClientPublishOptions, IClientSubscribeOptions } from 'mqtt'
import { getLocalScenarioList, getScenarioFilePath } from './simulate'
Expand Down Expand Up @@ -116,7 +116,7 @@ const parseFileRead = (value: string) => {
}

const parseFileSave = (value: string) => {
const filePath = createNextNumberedFileName(processPath(value))
const filePath = processPath(value)
if(!filePath) {
signale.error('A valid file path is required when saving to file.')
process.exit(1)
Expand Down

0 comments on commit d60d4b4

Please # to comment.