()
| 121 | } |
| 122 | |
| 123 | function main() { |
| 124 | try { |
| 125 | const options = parseArgs(process.argv.slice(2)); |
| 126 | if (options.help) { |
| 127 | printHelp(); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!fs.existsSync(options.sidebar)) { |
| 132 | throw new Error(`Sidebar file not found: ${options.sidebar}`); |
| 133 | } |
| 134 | |
| 135 | let source = fs.readFileSync(options.sidebar, "utf8"); |
| 136 | const originalSource = source; |
| 137 | const reports = []; |
| 138 | |
| 139 | for (const target of createTargets(options)) { |
| 140 | const result = syncTarget(source, target); |
| 141 | source = result.source; |
| 142 | reports.push(result.report); |
| 143 | } |
| 144 | |
| 145 | const changed = source !== originalSource; |
| 146 | for (const report of reports) { |
| 147 | if (options.verbose || report.added.length > 0 || report.removed.length > 0 || report.warnings.length > 0) { |
| 148 | printReport(report); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (options.write && changed) { |
| 153 | fs.writeFileSync(options.sidebar, source); |
| 154 | console.log(`Updated ${path.relative(ROOT_DIR, options.sidebar)}`); |
| 155 | } else if (!changed) { |
| 156 | console.log("Sidebar is already in sync."); |
| 157 | } else { |
| 158 | console.log("Dry run only. Re-run with --write to update sidebar.ts."); |
| 159 | } |
| 160 | |
| 161 | if (options.check && changed) { |
| 162 | process.exitCode = 1; |
| 163 | } |
| 164 | } catch (error) { |
| 165 | console.error(`sync-sidebar: ${error.message}`); |
| 166 | process.exitCode = 1; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | function syncTarget(source, target) { |
| 171 | const dir = path.resolve(ROOT_DIR, target.dir); |
no test coverage detected