|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const currentDir = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | + |
| 9 | +type Browser = "chrome" | "firefox"; |
| 10 | +type Environment = "development" | "production"; |
| 11 | + |
| 12 | +interface ManifestConfig { |
| 13 | + browser: Browser; |
| 14 | + environment: Environment; |
| 15 | +} |
| 16 | + |
| 17 | +function generateManifest(config: ManifestConfig): object { |
| 18 | + // Read the template |
| 19 | + const templatePath = path.join(currentDir, "manifest.template.json"); |
| 20 | + const template = JSON.parse(fs.readFileSync(templatePath, "utf8")); |
| 21 | + |
| 22 | + // Clone the template |
| 23 | + const manifest = JSON.parse(JSON.stringify(template)); |
| 24 | + |
| 25 | + // Add browser-specific settings |
| 26 | + if (config.browser === "firefox") { |
| 27 | + manifest.browser_specific_settings = { |
| 28 | + gecko: { |
| 29 | + id: "{6384c57b-f03b-4de7-b146-d0159cde0ca2}", |
| 30 | + strict_min_version: "130.0", |
| 31 | + }, |
| 32 | + }; |
| 33 | + manifest.background = { |
| 34 | + scripts: ["background.js"], |
| 35 | + }; |
| 36 | + } else { |
| 37 | + manifest.background = { |
| 38 | + service_worker: "background.js", |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + // Add environment-specific settings |
| 43 | + if (config.environment === "development") { |
| 44 | + manifest.host_permissions = ["http://localhost/*", "https://api.threadloaf.com/*"]; |
| 45 | + manifest.content_scripts.push({ |
| 46 | + matches: ["http://localhost/auth/callback*", "https://api.threadloaf.com/auth/callback*"], |
| 47 | + js: ["oauth_callback.js"], |
| 48 | + }); |
| 49 | + } else { |
| 50 | + manifest.host_permissions = ["https://api.threadloaf.com/*"]; |
| 51 | + manifest.content_scripts.push({ |
| 52 | + matches: ["https://api.threadloaf.com/auth/callback*"], |
| 53 | + js: ["oauth_callback.js"], |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return manifest; |
| 58 | +} |
| 59 | + |
| 60 | +function main(): void { |
| 61 | + const args = process.argv.slice(2); |
| 62 | + |
| 63 | + if (args.length !== 3) { |
| 64 | + console.error("Usage: generate-manifest.ts <browser> <environment> <output-file>"); |
| 65 | + console.error("Browser: chrome | firefox"); |
| 66 | + console.error("Environment: development | production"); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + const [browser, environment, outputFile] = args; |
| 71 | + |
| 72 | + if (!["chrome", "firefox"].includes(browser)) { |
| 73 | + console.error("Invalid browser. Must be chrome or firefox"); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + if (!["development", "production"].includes(environment)) { |
| 78 | + console.error("Invalid environment. Must be development or production"); |
| 79 | + process.exit(1); |
| 80 | + } |
| 81 | + |
| 82 | + const config: ManifestConfig = { |
| 83 | + browser: browser as Browser, |
| 84 | + environment: environment as Environment, |
| 85 | + }; |
| 86 | + |
| 87 | + const manifest = generateManifest(config); |
| 88 | + fs.writeFileSync(outputFile, JSON.stringify(manifest, null, 2)); |
| 89 | + console.log(`Generated ${browser} ${environment} manifest: ${outputFile}`); |
| 90 | +} |
| 91 | + |
| 92 | +// Run main if this file is being executed directly |
| 93 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 94 | + main(); |
| 95 | +} |
0 commit comments