* Process the given data and write it to a file if possible. If the user requested RAW output and * no output name is given, the file is directly written to stdout. The resulting Response contains * an otherwise empty message then to prevent writing other information to stdout. * * If an
(data: string | Buffer, output: string, defaultFileName: string)
| 151 | * @return an empty [Response] if written to stdout or a [Response] with the chosen output file otherwise. |
| 152 | */ |
| 153 | static async saveResultToFile(data: string | Buffer, output: string, defaultFileName: string) { |
| 154 | if ((output == null || output === "") && process.env.BW_RAW === "true") { |
| 155 | // No output is given and the user expects raw output. Since the command result is about content, |
| 156 | // we directly return the command result to stdout (and suppress further messages). |
| 157 | process.stdout.write(data); |
| 158 | return Response.success(); |
| 159 | } |
| 160 | |
| 161 | const filePath = await this.saveFile(data, output, defaultFileName); |
| 162 | const res = new MessageResponse("Saved " + filePath, null); |
| 163 | res.raw = filePath; |
| 164 | return Response.success(res); |
| 165 | } |
| 166 | |
| 167 | static readStdin(): Promise<string> { |
| 168 | return new Promise((resolve, reject) => { |
no test coverage detected