* Save the given data to a file and determine the target file if necessary. * If output is non-empty, it is used as target filename. Otherwise the target filename is * built from the current working directory and the given defaultFileName. * * @param data to be written to the file. *
(data: string | Buffer, output: string, defaultFileName: string)
| 102 | * @return the chosen output file. |
| 103 | */ |
| 104 | static saveFile(data: string | Buffer, output: string, defaultFileName: string) { |
| 105 | let p: string = null; |
| 106 | let mkdir = false; |
| 107 | if (output != null && output !== "") { |
| 108 | const osOutput = path.join(output); |
| 109 | if (osOutput.indexOf(path.sep) === -1) { |
| 110 | p = path.join(process.cwd(), osOutput); |
| 111 | } else { |
| 112 | mkdir = true; |
| 113 | if (osOutput.endsWith(path.sep)) { |
| 114 | p = path.join(osOutput, defaultFileName); |
| 115 | } else { |
| 116 | p = osOutput; |
| 117 | } |
| 118 | } |
| 119 | } else { |
| 120 | p = path.join(process.cwd(), defaultFileName); |
| 121 | } |
| 122 | |
| 123 | p = path.resolve(p); |
| 124 | if (mkdir) { |
| 125 | const dir = p.substring(0, p.lastIndexOf(path.sep)); |
| 126 | if (!fs.existsSync(dir)) { |
| 127 | NodeUtils.mkdirpSync(dir, "700"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return new Promise<string>((resolve, reject) => { |
| 132 | fs.writeFile(p, data, { encoding: "utf8", mode: 0o600 }, (err) => { |
| 133 | if (err != null) { |
| 134 | reject("Cannot save file to " + p); |
| 135 | } |
| 136 | resolve(p); |
| 137 | }); |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Process the given data and write it to a file if possible. If the user requested RAW output and |
no test coverage detected