|
11 | 11 | * |
12 | 12 | * Output: |
13 | 13 | * - team-workspaces.json: Contains all team workspaces metadata |
14 | | - * - workspace-admins.json: Contains workspace IDs, names, types, and admin user IDs |
| 14 | + * - workspace-admins-report.json: Contains workspace IDs, names, types, and admin user details |
| 15 | + * - admin-report.html: HTML report of workspace admins rendered using the Handlebars template |
15 | 16 | */ |
16 | 17 |
|
17 | 18 | const axios = require('axios'); |
18 | 19 | const fs = require('fs'); |
19 | 20 | const path = require('path'); |
| 21 | +const handlebars = require('handlebars'); |
20 | 22 | let curReportItem = {}; |
21 | 23 | let adminReport = []; |
22 | 24 | let finalReport = {}; |
@@ -87,6 +89,107 @@ async function getTeamWorkspaces() { |
87 | 89 | } |
88 | 90 | } |
89 | 91 |
|
| 92 | +/** |
| 93 | + * Generate a static HTML report from the workspace admins report data |
| 94 | + * @param {string} reportJsonPath - Path to the workspace admins JSON report |
| 95 | + * @param {string} templatePath - Path to the Handlebars template file |
| 96 | + * @param {string} outputPath - Path where the HTML report should be saved |
| 97 | + */ |
| 98 | +async function generateHtmlReport(reportJsonPath, templatePath, outputPath) { |
| 99 | + try { |
| 100 | + console.log('Generating HTML report...'); |
| 101 | + |
| 102 | + // Check if report JSON exists |
| 103 | + if (!fs.existsSync(reportJsonPath)) { |
| 104 | + console.error(`Error: Report JSON file not found at ${reportJsonPath}`); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + // Check if template exists |
| 109 | + if (!fs.existsSync(templatePath)) { |
| 110 | + console.error(`Error: Template file not found at ${templatePath}`); |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + // Ensure the output directory exists |
| 115 | + const outputDir = path.dirname(outputPath); |
| 116 | + if (!fs.existsSync(outputDir)) { |
| 117 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 118 | + console.log(`Created output directory: ${outputDir}`); |
| 119 | + } |
| 120 | + |
| 121 | + // Read the template and report data |
| 122 | + const styleTemplate = fs.readFileSync(templatePath, 'utf-8'); |
| 123 | + const reportData = JSON.parse(fs.readFileSync(reportJsonPath, 'utf-8')); |
| 124 | + |
| 125 | + // Create a complete HTML template with the styles |
| 126 | + const fullTemplate = ` |
| 127 | +<!DOCTYPE html> |
| 128 | +<html lang="en"> |
| 129 | +<head> |
| 130 | + <meta charset="UTF-8"> |
| 131 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 132 | + <title>Postman Workspace Admins Report</title> |
| 133 | + ${styleTemplate} |
| 134 | +</head> |
| 135 | +<body> |
| 136 | + <h1>Postman Workspace Admins Report</h1> |
| 137 | + <p>Generated: {{generated_at}}</p> |
| 138 | + |
| 139 | + <table class="tftable"> |
| 140 | + <tr> |
| 141 | + <th>Workspace</th> |
| 142 | + <th>Type</th> |
| 143 | + <th>Visibility</th> |
| 144 | + <th>Admin User</th> |
| 145 | + <th>Username</th> |
| 146 | + <th>Email</th> |
| 147 | + </tr> |
| 148 | + {{#each workspaces}} |
| 149 | + {{#each admin_users}} |
| 150 | + <tr> |
| 151 | + {{#if @index}} |
| 152 | + <td></td> |
| 153 | + <td></td> |
| 154 | + <td></td> |
| 155 | + {{else}} |
| 156 | + <td>{{../workspace_name}}</td> |
| 157 | + <td>{{../workspace_type}}</td> |
| 158 | + <td>{{../workspace_visibility}}</td> |
| 159 | + {{/if}} |
| 160 | + <td>{{user_name}}</td> |
| 161 | + <td>{{user_username}}</td> |
| 162 | + <td>{{user_email}}</td> |
| 163 | + </tr> |
| 164 | + {{else}} |
| 165 | + <tr> |
| 166 | + <td>{{workspace_name}}</td> |
| 167 | + <td>{{workspace_type}}</td> |
| 168 | + <td>{{workspace_visibility}}</td> |
| 169 | + <td colspan="3">No admin users found</td> |
| 170 | + </tr> |
| 171 | + {{/each}} |
| 172 | + {{/each}} |
| 173 | + </table> |
| 174 | +</body> |
| 175 | +</html> |
| 176 | +`; |
| 177 | + |
| 178 | + // Compile and render the template |
| 179 | + const template = handlebars.compile(fullTemplate); |
| 180 | + const htmlOutput = template(reportData); |
| 181 | + |
| 182 | + // Write the HTML output to the file |
| 183 | + fs.writeFileSync(outputPath, htmlOutput); |
| 184 | + console.log(`HTML report generated successfully: ${outputPath}`); |
| 185 | + |
| 186 | + return true; |
| 187 | + } catch (error) { |
| 188 | + console.error('Error generating HTML report:', error.message); |
| 189 | + return false; |
| 190 | + } |
| 191 | +} |
| 192 | + |
90 | 193 | /** |
91 | 194 | * Get admin users for a specific workspace with detailed user information |
92 | 195 | * @param {string} workspaceId - The ID of the workspace to get admins for |
@@ -216,6 +319,11 @@ async function main() { |
216 | 319 | fs.writeFileSync(adminsFilePath, JSON.stringify(finalReport, null, 2)); |
217 | 320 | console.log(`\nSaved workspace admin report to: ${adminsFilePath}`); |
218 | 321 |
|
| 322 | + // Generate HTML report |
| 323 | + const templatePath = path.join(__dirname, 'report-template.hbs'); |
| 324 | + const htmlOutputPath = path.join(outputDir, 'admin-report.html'); |
| 325 | + await generateHtmlReport(adminsFilePath, templatePath, htmlOutputPath); |
| 326 | + |
219 | 327 | } catch (error) { |
220 | 328 | console.error('Error in main execution:', error); |
221 | 329 | process.exit(1); |
|
0 commit comments