From 2634334b6f5fd5be3b57971526b3645ae8538dd8 Mon Sep 17 00:00:00 2001 From: Dawid Urbas Date: Sun, 17 Dec 2023 19:47:51 +0000 Subject: [PATCH] Test workflow --- .env.example | 13 ++++++ .github/workflows/test.yaml | 8 ++-- package.json | 10 ++++- scripts/extension/background.ts | 22 ++++++++++ scripts/extension/tsconfig.json | 13 ++++++ scripts/project/removeExportFromScript.cjs | 18 ++++++++ .../project/removeInlineScript.cjs | 2 +- scripts/project/replaceGeckoId.cjs | 41 +++++++++++++++++++ static/manifest.json | 15 +++---- 9 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 .env.example create mode 100644 scripts/extension/background.ts create mode 100644 scripts/extension/tsconfig.json create mode 100644 scripts/project/removeExportFromScript.cjs rename removeInlineScript.cjs => scripts/project/removeInlineScript.cjs (95%) create mode 100644 scripts/project/replaceGeckoId.cjs diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f56dc4c --- /dev/null +++ b/.env.example @@ -0,0 +1,13 @@ +CHROME_ID=your_chrome_id +CHROME_CLIENT_ID=your_chrome_client_id +CHROME_CLIENT_SECRET=your_chrome_client_secret +CHROME_REFRESH_TOKEN=your_chrome_refresh_token +EDGE_ID=your_edge_id +EDGE_CLIENT_ID=your_edge_client_id +EDGE_CLIENT_SECRET=your_edge_client_secret +EDGE_ACCESS_TOKEN_URL=your_edge_access_token_url +FF_ID=your_firefox_id +FF_JWT_ISSUER=your_firefox_jwt_issuer +FF_JWT_SECRET=your_firefox_jwt_secret + + diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0c4118e..c880dec 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -25,7 +25,7 @@ jobs: run: pnpm install - name: Build extension - run: pnpm run build + run: FF_ID=${{ env.FF_ID }} pnpm build:prod - name: Zip the build run: | @@ -41,21 +41,21 @@ jobs: client_id: ${{ secrets.CHROME_CLIENT_ID }} client_secret: ${{ secrets.CHROME_CLIENT_SECRET }} refresh_token: ${{ secrets.CHROME_REFRESH_TOKEN }} - extension_id: eggfhkdnfdhdpmkfpihjjbnncgmhihce + extension_id: ${{ env.CHROME_ID }} zip_file: build.zip - name: Firefox upload & release uses: cardinalby/webext-buildtools-firefox-addons-action@1.0.9 with: zipFilePath: build.zip - extensionId: holo-key-manager@holo.host + extensionId: ${{ env.FF_ID }} jwtIssuer: ${{ secrets.FF_JWT_ISSUER }} jwtSecret: ${{ secrets.FF_JWT_SECRET }} - name: Edge upload & release uses: wdzeng/edge-addon@v1.2.1 with: - product-id: 70d8655d-2c91-4b81-99b1-35afa863ea8c + product-id: ${{ env.EDGE_ID }} zip-path: build.zip client-id: ${{ secrets.EDGE_CLIENT_ID }} client-secret: ${{ secrets.EDGE_CLIENT_SECRET }} diff --git a/package.json b/package.json index 72b36ce..adaa3df 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,13 @@ "version": "0.0.3", "private": true, "scripts": { - "dev": "vite build --watch", - "build": "vite build && node removeInlineScript.cjs", + "build": "pnpm build:vite && pnpm build:script && pnpm build:removeExport && pnpm build:removeInline", + "build:prod": "pnpm build:replaceGeckoId -- FF_ID && pnpm build:dev", + "build:vite": "vite build", + "build:script": "tsc --p scripts/extension/tsconfig.json", + "build:removeExport": "node scripts/project/removeExportFromScript.cjs", + "build:removeInline": "node scripts/project/removeInlineScript.cjs", + "build:replaceGeckoId": "node scripts/project/replaceGeckoId.cjs", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", @@ -15,6 +20,7 @@ "devDependencies": { "@sveltejs/adapter-static": "^2.0.3", "@sveltejs/kit": "^1.27.7", + "@tsconfig/node-lts": "^20.1.0", "@types/chrome": "^0.0.254", "@types/file-saver": "^2.0.7", "@typescript-eslint/eslint-plugin": "^6.13.2", diff --git a/scripts/extension/background.ts b/scripts/extension/background.ts new file mode 100644 index 0000000..456348e --- /dev/null +++ b/scripts/extension/background.ts @@ -0,0 +1,22 @@ +import type { Message } from '../../types/index'; + +console.log('background script loaded'); + +chrome.runtime.onMessageExternal.addListener((message: Message, sender, sendResponse) => { + if (message.action === 'openWindow') { + try { + chrome.windows.create({ + url: 'sign.html', + type: 'popup', + height: 600, + width: 375, + top: 0, + left: screen.width - 375 + }); + } catch (error) { + console.error('Error creating window:', error); + return; + } + sendResponse({ success: true, message: 'Window opened' }); + } +}); diff --git a/scripts/extension/tsconfig.json b/scripts/extension/tsconfig.json new file mode 100644 index 0000000..f6d81a9 --- /dev/null +++ b/scripts/extension/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "outDir": "../../build/scripts", + "allowJs": true, + "checkJs": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "strict": true, + "target": "esnext", + "types": ["chrome"] + }, + "include": ["background.ts"] +} diff --git a/scripts/project/removeExportFromScript.cjs b/scripts/project/removeExportFromScript.cjs new file mode 100644 index 0000000..2a67a9a --- /dev/null +++ b/scripts/project/removeExportFromScript.cjs @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +// removeExportFromScript.cjs +const path = require('path'); +const fs = require('fs'); + +function removeExportStatement(directory, fileName) { + console.log('Removing "export {};" from script'); + const filePath = path.join(directory, fileName); + let fileContent = fs.readFileSync(filePath, 'utf8'); + + // Replace "export {};" with an empty string + fileContent = fileContent.replace('export {};', ''); + + fs.writeFileSync(filePath, fileContent); + console.log(`"export {};" removed from: ${filePath}`); +} + +removeExportStatement(path.resolve(__dirname, '../../build/scripts'), 'background.js'); diff --git a/removeInlineScript.cjs b/scripts/project/removeInlineScript.cjs similarity index 95% rename from removeInlineScript.cjs rename to scripts/project/removeInlineScript.cjs index dcbe7fc..dac980c 100644 --- a/removeInlineScript.cjs +++ b/scripts/project/removeInlineScript.cjs @@ -44,4 +44,4 @@ async function removeInlineScript(directory) { }); } -removeInlineScript(path.resolve(__dirname, 'build')); +removeInlineScript(path.resolve(__dirname, '../../build')); diff --git a/scripts/project/replaceGeckoId.cjs b/scripts/project/replaceGeckoId.cjs new file mode 100644 index 0000000..6b795ba --- /dev/null +++ b/scripts/project/replaceGeckoId.cjs @@ -0,0 +1,41 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +// replaceGeckoId.js +const fs = require('fs'); +const path = require('path'); + +const manifestPath = path.resolve(__dirname, '../../static/manifest.json'); + +const args = process.argv.slice(2); +const geckoId = args[0]; + +if (!geckoId) { + console.error('Gecko ID is not provided as an argument.'); + process.exit(1); +} + +fs.readFile(manifestPath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading the manifest file:', err); + return; + } + + const manifest = JSON.parse(data); + const updatedManifest = { + ...manifest, + browser_specific_settings: { + ...manifest.browser_specific_settings, + gecko: { + id: geckoId + } + } + }; + + fs.writeFile(manifestPath, JSON.stringify(updatedManifest, null, 2), 'utf8', (err) => { + if (err) { + console.error('Error writing the manifest file:', err); + return; + } + + console.log('Manifest file updated successfully.'); + }); +}); diff --git a/static/manifest.json b/static/manifest.json index a1a68ec..3b56eec 100644 --- a/static/manifest.json +++ b/static/manifest.json @@ -1,13 +1,8 @@ { "name": "Holo key manager", "description": "A browser extension to manage holo keys", - "version": "0.0.27", + "version": "0.0.28", "manifest_version": 3, - "browser_specific_settings": { - "gecko": { - "id": "holo-key-manager@holo.host" - } - }, "action": { "default_title": "Holo key manager", "default_icon": "favicon.png", @@ -16,5 +11,11 @@ "content_security_policy": { "extension_pages": "default-src 'self' 'wasm-unsafe-eval'" }, - "permissions": ["storage"] + "background": { + "service_worker": "scripts/background.js" + }, + "permissions": ["activeTab", "storage", "tabs", "nativeMessaging"], + "externally_connectable": { + "matches": ["*://localhost/*"] + } }