From 2674e2a0493feef893df8b73a1982c04e1bb99c4 Mon Sep 17 00:00:00 2001 From: Bas Date: Wed, 13 Nov 2024 14:19:00 +0200 Subject: [PATCH] feat(worker): Add sentry to xapian worker scope --- .eslintrc.json | 14 ++++++ .gitignore | 4 ++ package-lock.json | 7 +-- package.json | 6 ++- src/app/sentry.ts | 7 ++- src/app/xapian/index.worker.ts | 13 +++--- src/build/add-sentry.js | 15 ------- src/build/gen-env.js | 66 ++++++++++++++++++++++++++++ src/build/post-build.js | 7 --- src/build/pre-build.js | 9 ++-- src/environments/environment.prod.ts | 13 +++--- src/environments/environment.ts | 16 +++---- 12 files changed, 125 insertions(+), 52 deletions(-) delete mode 100644 src/build/add-sentry.js create mode 100644 src/build/gen-env.js diff --git a/.eslintrc.json b/.eslintrc.json index 510268b6e..82e4da4c9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -50,6 +50,20 @@ "rules": { "@angular-eslint/template/eqeqeq": "warn" } + }, + { + "files": [ + "./src/build/*.js" + ], + "parserOptions": { + "ecmaVersion": 2015 + }, + "extends": [ + "eslint:recommended" + ], + "env": { + "node": true + } } ] } diff --git a/.gitignore b/.gitignore index 0e9a7255b..4b42e2f64 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,7 @@ mockserver.log # CI artifacts cypress/videos + +# environment +src/environments/env.ts + diff --git a/package-lock.json b/package-lock.json index d919f1364..d83343c77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -80,6 +80,9 @@ "tslib": "^2.5.0", "typescript": "~4.9.5", "webdriver-manager": "~12.1.9" + }, + "peerDependencies": { + "debug": "^2.6.9" } }, "node_modules/@ampproject/remapping": { @@ -9336,7 +9339,6 @@ "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "dev": true, "dependencies": { "ms": "2.0.0" } @@ -14306,8 +14308,7 @@ "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", - "devOptional": true + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "node_modules/multicast-dns": { "version": "7.2.5", diff --git a/package.json b/package.json index fb3235879..a2fe78edb 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "start-e2e-server": "start-test mockserver 15000 start-use-mockserver", "run-cypress-tests": "cypress run --headless", "cypress-e2e": "start-test start-e2e-server 4201 run-cypress-tests", - "ci-tests": "DISPLAY='' CHOKIDAR_USEPOLLING=1 node run-ci-tests.js" + "ci-tests": "DISPLAY='' CHOKIDAR_USEPOLLING=1 node run-ci-tests.js", + "postinstall": "node src/build/gen-env.js" }, "dependencies": { "@angular/animations": "15.1.3", @@ -97,5 +98,8 @@ "tslib": "^2.5.0", "typescript": "~4.9.5", "webdriver-manager": "~12.1.9" + }, + "peerDependencies": { + "debug": "^2.6.9" } } diff --git a/src/app/sentry.ts b/src/app/sentry.ts index 426a62f43..b5b4aa221 100644 --- a/src/app/sentry.ts +++ b/src/app/sentry.ts @@ -17,4 +17,9 @@ // along with Runbox 7. If not, see . // ---------- END RUNBOX LICENSE ---------- -// ADD SENTRY HERE +import { environment } from '../environments/environment' +import * as Sentry from '@sentry/browser'; + +Sentry.init({ + dsn: environment.SENTRY_DSN +}); diff --git a/src/app/xapian/index.worker.ts b/src/app/xapian/index.worker.ts index 797a40763..6da839dc6 100644 --- a/src/app/xapian/index.worker.ts +++ b/src/app/xapian/index.worker.ts @@ -1,23 +1,26 @@ // --------- BEGIN RUNBOX LICENSE --------- // Copyright (C) 2016-2022 Runbox Solutions AS (runbox.com). -// +// // This file is part of Runbox 7. -// +// // Runbox 7 is free software: You can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation, either version 3 of the License, or (at your // option) any later version. -// +// // Runbox 7 is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with Runbox 7. If not, see . // ---------- END RUNBOX LICENSE ---------- /// + +import '../sentry'; + import { Observer, Observable, of, from, AsyncSubject } from 'rxjs'; import { mergeMap, map, filter, catchError, tap, take, bufferCount } from 'rxjs/operators'; @@ -250,7 +253,7 @@ class SearchIndexService { } this.localSearchActivated = false; - + return new Observable((observer) => { console.log('Worker: Closing xapian database'); if (this.api) { diff --git a/src/build/add-sentry.js b/src/build/add-sentry.js deleted file mode 100644 index a19a968d0..000000000 --- a/src/build/add-sentry.js +++ /dev/null @@ -1,15 +0,0 @@ -const cp = require('child_process'); -const fs = require('fs'); - -const sentry_code = ` -import * as Sentry from "@sentry/browser"; - -Sentry.init({ - dsn: '${process.env.SENTRY_DSN}' -}); -`; - -let app = fs.readFileSync('src/app/sentry.ts').toString(); -app = app.replace('// ADD SENTRY HERE', sentry_code.trim()); -fs.writeFileSync('src/app/sentry.ts', app); -console.log('sentry.ts updated'); diff --git a/src/build/gen-env.js b/src/build/gen-env.js new file mode 100644 index 000000000..3477338ff --- /dev/null +++ b/src/build/gen-env.js @@ -0,0 +1,66 @@ +#!/usr/bin/env node + +const Debug = require('debug') +const fs = require('fs'); + +const target = 'src/environments/env.ts' +const debug = Debug('runbox:gen-env') + +debug(`Writing env file to ${target}.`) + +const env = [ + ['SENTRY_DSN', tryCatch(assertString, orNull)], +] + +function assertString(input) { + if (typeof input !== 'string') { + throw new TypeError(`Expected a string, but received ${typeof input}`); + } + return input; +} + +function tryCatch (tryFn, catchFn) { + return value => { + try { + return tryFn(value); + } catch (error) { + debug(error) + return catchFn(error, value) + } + } +} + +function orNull () { + return null +} + +fs.writeFileSync('src/environments/env.ts', `// --------- BEGIN RUNBOX LICENSE --------- +// Copyright (C) 2016-2018 Runbox Solutions AS (runbox.com). +// +// This file is part of Runbox 7. +// +// Runbox 7 is free software: You can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// Runbox 7 is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Runbox 7. If not, see . +// ---------- END RUNBOX LICENSE ---------- + +// This file is auto-generated. + +export default ${JSON.stringify( + env.reduce((acc, [name, fn]) => { + acc[name] = fn(process.env[name], name) + + return acc + }, {}) +, null, 2)}`); + +debug(`Wrote env file to ${target}.`) diff --git a/src/build/post-build.js b/src/build/post-build.js index 50adca24f..28df92fb5 100644 --- a/src/build/post-build.js +++ b/src/build/post-build.js @@ -13,10 +13,3 @@ if (changelogUpdated) { console.log('Run the following command to commit it:\n'); console.log('git commit src/app/changelog/changes.ts -em "docs(changelog): Update changelog"'); } - -if (!process.env.SENTRY_DSN) { - console.log(chalk.bold.yellow('This build did NOT use Sentry error reporting.')); - console.log('Re-run with SENTRY_DSN environment variable set to enable it'); -} else { - execSync('git checkout src/app/sentry.ts'); -} diff --git a/src/build/pre-build.js b/src/build/pre-build.js index eb20f441d..51b7bf8ac 100644 --- a/src/build/pre-build.js +++ b/src/build/pre-build.js @@ -4,8 +4,8 @@ const fs = require('fs'); console.log('WARNING: Reverting to committed package-lock.json. If that means your changes are lost you should rebuild it.') cp.execSync('git checkout package-lock.json'); -const packageLockJSON = JSON.parse(fs.readFileSync('package-lock.json')); -const packageJSON = JSON.parse(fs.readFileSync('package.json')); +// const packageLockJSON = JSON.parse(fs.readFileSync('package-lock.json')); +// const packageJSON = JSON.parse(fs.readFileSync('package.json')); // FIXME: is this necessary? the package.json & package-lock.json format has changed, package.json no longer // contains a _integrity field, package-lock.json not uses the field packages instead of dependencies. @@ -26,10 +26,7 @@ console.log('Updating build timestamp'); const build_time = new Date().toJSON(); fs.writeFileSync('src/app/buildtimestamp.ts', "export const BUILD_TIMESTAMP = '" + build_time + "';\n"); -if (process.env.SENTRY_DSN) { - console.log('Adding Sentry setup to app.component.ts'); - console.log(cp.execFileSync('node', ['src/build/add-sentry.js']).toString().trim()) -} +console.log(cp.execFileSync('node', ['src/build/gen-env.js']).toString().trim()) if (!process.env.SKIP_CHANGELOG) { console.log('Updating changelog'); diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index cfafb8259..cf62cf7e2 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -1,22 +1,25 @@ // --------- BEGIN RUNBOX LICENSE --------- // Copyright (C) 2016-2018 Runbox Solutions AS (runbox.com). -// +// // This file is part of Runbox 7. -// +// // Runbox 7 is free software: You can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation, either version 3 of the License, or (at your // option) any later version. -// +// // Runbox 7 is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with Runbox 7. If not, see . // ---------- END RUNBOX LICENSE ---------- +import env from './env' + export const environment = { + ...env, production: true -}; +} diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 250d35b4c..3d2dfca83 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -1,27 +1,25 @@ // --------- BEGIN RUNBOX LICENSE --------- // Copyright (C) 2016-2018 Runbox Solutions AS (runbox.com). -// +// // This file is part of Runbox 7. -// +// // Runbox 7 is free software: You can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation, either version 3 of the License, or (at your // option) any later version. -// +// // Runbox 7 is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with Runbox 7. If not, see . // ---------- END RUNBOX LICENSE ---------- -// The file contents for the current environment will overwrite these during build. -// The build system defaults to the dev environment which uses `environment.ts`, but if you do -// `ng build --env=prod` then `environment.prod.ts` will be used instead. -// The list of which env maps to which file can be found in `.angular-cli.json`. +import env from './env' export const environment = { - production: false + ...env, + production: false, };