()
| 107 | } |
| 108 | |
| 109 | async function main() { |
| 110 | const { outputPath, pretty } = parseArgs(process.argv.slice(2)); |
| 111 | const writeToStdout = !outputPath; |
| 112 | const log = (...args) => { |
| 113 | if (writeToStdout) { |
| 114 | console.error(...args); |
| 115 | } else { |
| 116 | console.log(...args); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | const { results, errors } = await loadEntries(); |
| 121 | |
| 122 | results.sort((a, b) => { |
| 123 | const typeCompare = a.type.localeCompare(b.type); |
| 124 | if (typeCompare !== 0) { |
| 125 | return typeCompare; |
| 126 | } |
| 127 | return a.productId.localeCompare(b.productId); |
| 128 | }); |
| 129 | |
| 130 | if (errors.length > 0) { |
| 131 | for (const error of errors) { |
| 132 | log(error); |
| 133 | } |
| 134 | log(`\n⚠️ Skipped ${errors.length} invalid entr${errors.length === 1 ? 'y' : 'ies'}.`); |
| 135 | } |
| 136 | |
| 137 | const json = JSON.stringify(results, null, pretty ? 2 : 0); |
| 138 | |
| 139 | if (outputPath) { |
| 140 | const outputDir = path.dirname(outputPath); |
| 141 | if (outputDir && outputDir !== '.') { |
| 142 | fs.mkdirSync(outputDir, { recursive: true }); |
| 143 | } |
| 144 | fs.writeFileSync(outputPath, json); |
| 145 | log(`Saved ${results.length} entries to ${outputPath}`); |
| 146 | } else { |
| 147 | process.stdout.write(json); |
| 148 | } |
| 149 | |
| 150 | if (errors.length > 0) { |
| 151 | process.exit(1); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | main().catch((err) => { |
| 156 | console.error('Export failed:', err.message); |
no test coverage detected