(data: Record<string, any>)
| 6 | import prettyjson from 'prettyjson'; |
| 7 | |
| 8 | const writeOutput = (data: Record<string, any>) => { |
| 9 | if (isEmpty(data)) return; |
| 10 | const argv = process.argv.slice(2); |
| 11 | const argvData = utils.parseArgv(argv); |
| 12 | const outputFile = get(argvData, 'output-file'); |
| 13 | if (!outputFile) return; |
| 14 | const filePath = utils.getAbsolutePath(outputFile); |
| 15 | const output = get(argvData, 'output') || get(argvData, 'output-format'); |
| 16 | const newMap = { |
| 17 | [IOutput.JSON]: JSON.stringify(data, null, 2), |
| 18 | [IOutput.YAML]: yaml.dump(data), |
| 19 | [IOutput.RAW]: JSON.stringify(data), |
| 20 | }; |
| 21 | const defaultValue = prettyjson.render(data, { keysColor: 'bold', emptyArrayMsg: '[]' }); |
| 22 | fs.writeFileSync(filePath, get(newMap, output, defaultValue), 'utf-8'); |
| 23 | // 使用cat命令查看文件时,文件的末尾会显示一个%符号,可能是由于文件的最后一行缺少换行符(\n)导致的 |
| 24 | fs.appendFileSync(filePath, '\n'); |
| 25 | }; |
| 26 | |
| 27 | export default writeOutput; |
no outgoing calls
no test coverage detected