From a18004493a280f4cd5008d21bf178c42a22ba4ff Mon Sep 17 00:00:00 2001 From: Daniel Aschwanden Date: Wed, 7 Feb 2024 09:03:37 +0100 Subject: [PATCH] feat: Cleanup githooks --- .gitignore | 4 +- .pre-commit-config.yaml | 20 ++++ ui/package.json | 12 +-- ui/src/service-worker.ts | 85 ----------------- ui/src/serviceWorkerRegistration.ts | 142 ---------------------------- ui/yarn.lock | 56 ----------- 6 files changed, 25 insertions(+), 294 deletions(-) create mode 100644 .pre-commit-config.yaml delete mode 100644 ui/src/service-worker.ts delete mode 100644 ui/src/serviceWorkerRegistration.ts diff --git a/.gitignore b/.gitignore index c6f4c50a..aeb91381 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies +.idea +.vscode +node_modules ui/node_modules ui/.pnp.* ui/.yarn/* @@ -82,4 +85,3 @@ ui/*.tgz # Yarn Integrity file ui/.yarn-integrity - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..5932a697 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/dnephin/pre-commit-golang + rev: v0.5.1 + hooks: + - id: go-fmt + - id: go-imports + - id: no-go-testing + - id: golangci-lint + - id: go-unit-tests + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "v3.1.0" + hooks: + - id: prettier diff --git a/ui/package.json b/ui/package.json index 35876443..0e1640e8 100644 --- a/ui/package.json +++ b/ui/package.json @@ -10,7 +10,6 @@ "@fortawesome/free-solid-svg-icons": "^6.5.1", "@fortawesome/react-fontawesome": "^0.2.0", "@mapbox/mapbox-gl-draw": "^1.3.0", - "@testing-library/dom": "^9.3.4", "@turf/bearing": "^6.5.0", "@turf/center": "^6.5.0", "@turf/helpers": "^6.5.0", @@ -34,10 +33,7 @@ "react-markdown": "^8.0.7", "react-router-dom": "^6.21.3", "usehooks-ts": "^2.10.0", - "web-vitals": "^3.5.1", - "workbox-core": "^7.0.0", - "workbox-expiration": "^7.0.0", - "workbox-precaching": "^7.0.0" + "web-vitals": "^3.5.1" }, "scripts": { "analyze": "source-map-explorer 'build/static/js/*.js'", @@ -62,17 +58,13 @@ "last 1 safari version" ] }, - "husky": { - "hooks": { - "pre-commit": "lint-staged" - } - }, "lint-staged": { "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [ "prettier --write" ] }, "devDependencies": { + "@testing-library/dom": "^9.3.4", "@testing-library/jest-dom": "^6.4.2", "@testing-library/react": "^14.2.1", "@testing-library/user-event": "^14.5.2", diff --git a/ui/src/service-worker.ts b/ui/src/service-worker.ts deleted file mode 100644 index 928312dc..00000000 --- a/ui/src/service-worker.ts +++ /dev/null @@ -1,85 +0,0 @@ -/// -/* eslint-disable no-restricted-globals */ - -// This service worker can be customized! -// See https://developers.google.com/web/tools/workbox/modules -// for the list of available Workbox modules, or add any other -// code you'd like. -// You can also remove this file if you'd prefer not to use a -// service worker, and the Workbox build step will be skipped. - -import { clientsClaim } from 'workbox-core'; -import { ExpirationPlugin } from 'workbox-expiration'; -import { createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching'; -import { registerRoute } from 'workbox-routing'; -import { StaleWhileRevalidate } from 'workbox-strategies'; - -declare const self: ServiceWorkerGlobalScope; - -clientsClaim(); - -// Precache all of the assets generated by your build process. -// Their URLs are injected into the manifest variable below. -// This variable must be present somewhere in your service worker file, -// even if you decide not to use precaching. See https://cra.link/PWA -precacheAndRoute(self.__WB_MANIFEST); - -// Set up App Shell-style routing, so that all navigation requests -// are fulfilled with your index.html shell. Learn more at -// https://developers.google.com/web/fundamentals/architecture/app-shell -const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); -registerRoute( - // Return false to exempt requests from being fulfilled by index.html. - ({ request, url }: { request: Request; url: URL }) => { - // If this isn't a navigation, skip. - if (request.mode !== 'navigate') { - return false; - } - - // If this is a URL that starts with /_, skip. - if (url.pathname.startsWith('/_')) { - return false; - } - - // If this is a URL that starts with /oauth2, skip. - if (url.pathname.startsWith('/oauth2')) { - return false; - } - - // If this looks like a URL for a resource, because it contains - // a file extension, skip. - if (url.pathname.match(fileExtensionRegexp)) { - return false; - } - - // Return true to signal that we want to use the handler. - return true; - }, - createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') -); - -// An example runtime caching route for requests that aren't handled by the -// precache, in this case same-origin .png requests like those from in public/ -registerRoute( - // Add in any other file extensions or routing criteria as needed. - ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), - // Customize this strategy as needed, e.g., by changing to CacheFirst. - new StaleWhileRevalidate({ - cacheName: 'images', - plugins: [ - // Ensure that once this runtime cache reaches a maximum size the - // least-recently used images are removed. - new ExpirationPlugin({ maxEntries: 50 }), - ], - }) -); - -// This allows the web app to trigger skipWaiting via -// registration.waiting.postMessage({type: 'SKIP_WAITING'}) -self.addEventListener('message', (event) => { - if (event.data && event.data.type === 'SKIP_WAITING') { - self.skipWaiting(); - } -}); - -// Any other custom service worker logic can go here. diff --git a/ui/src/serviceWorkerRegistration.ts b/ui/src/serviceWorkerRegistration.ts deleted file mode 100644 index d2a3fc38..00000000 --- a/ui/src/serviceWorkerRegistration.ts +++ /dev/null @@ -1,142 +0,0 @@ -// This optional code is used to register a service worker. -// register() is not called by default. - -// This lets the app load faster on subsequent visits in production, and gives -// it offline capabilities. However, it also means that developers (and users) -// will only see deployed updates on subsequent visits to a page, after all the -// existing tabs open on the page have been closed, since previously cached -// resources are updated in the background. - -// To learn more about the benefits of this model and instructions on how to -// opt-in, read https://cra.link/PWA - -const isLocalhost = Boolean( - window.location.hostname === 'localhost' || - // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.0/8 are considered localhost for IPv4. - window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) -); - -type Config = { - onSuccess?: (registration: ServiceWorkerRegistration) => void; - onUpdate?: (registration: ServiceWorkerRegistration) => void; -}; - -export function register(config?: Config) { - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - // The URL constructor is available in all browsers that support SW. - const publicUrl = new URL(import.meta.env.BASE_URL, window.location.href); - if (publicUrl.origin !== window.location.origin) { - // Our service worker won't work if PUBLIC_URL is on a different origin - // from what our page is served on. This might happen if a CDN is used to - // serve assets; see https://github.com/facebook/create-react-app/issues/2374 - return; - } - - window.addEventListener('load', () => { - const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; - - if (isLocalhost) { - // This is running on localhost. Let's check if a service worker still exists or not. - checkValidServiceWorker(swUrl, config); - - // Add some additional logging to localhost, pointing developers to the - // service worker/PWA documentation. - navigator.serviceWorker.ready.then(() => { - console.log( - 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit https://cra.link/PWA' - ); - }); - } else { - // Is not localhost. Just register service worker - registerValidSW(swUrl, config); - } - }); - } -} - -function registerValidSW(swUrl: string, config?: Config) { - navigator.serviceWorker - .register(swUrl) - .then((registration) => { - registration.onupdatefound = () => { - const installingWorker = registration.installing; - if (installingWorker == null) { - return; - } - installingWorker.onstatechange = () => { - if (installingWorker.state === 'installed') { - if (navigator.serviceWorker.controller) { - // At this point, the updated precached content has been fetched, - // but the previous service worker will still serve the older - // content until all client tabs are closed. - console.log( - 'New content is available and will be used when all ' + - 'tabs for this page are closed. See https://cra.link/PWA.' - ); - - // Execute callback - if (config && config.onUpdate) { - config.onUpdate(registration); - } - } else { - // At this point, everything has been precached. - // It's the perfect time to display a - // "Content is cached for offline use." message. - console.log('Content is cached for offline use.'); - - // Execute callback - if (config && config.onSuccess) { - config.onSuccess(registration); - } - } - } - }; - }; - }) - .catch((error) => { - console.error('Error during service worker registration:', error); - }); -} - -function checkValidServiceWorker(swUrl: string, config?: Config) { - // Check if the service worker can be found. If it can't reload the page. - fetch(swUrl, { - headers: { 'Service-Worker': 'script' }, - }) - .then((response) => { - // Ensure service worker exists, and that we really are getting a JS file. - const contentType = response.headers.get('content-type'); - if ( - response.status === 404 || - (contentType != null && contentType.indexOf('javascript') === -1) - ) { - // No service worker found. Probably a different app. Reload the page. - navigator.serviceWorker.ready.then((registration) => { - registration.unregister().then(() => { - window.location.reload(); - }); - }); - } else { - // Service worker found. Proceed as normal. - registerValidSW(swUrl, config); - } - }) - .catch(() => { - console.log('No internet connection found. App is running in offline mode.'); - }); -} - -export function unregister() { - if ('serviceWorker' in navigator) { - navigator.serviceWorker.ready - .then((registration) => { - registration.unregister(); - }) - .catch((error) => { - console.error(error.message); - }); - } -} diff --git a/ui/yarn.lock b/ui/yarn.lock index 5a7ff7b7..efd6676d 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -7230,13 +7230,6 @@ __metadata: languageName: node linkType: hard -"idb@npm:^7.0.1": - version: 7.1.1 - resolution: "idb@npm:7.1.1" - checksum: 10/8e33eaebf21055129864acb89932e0739b8c96788e559df24c253ce114d8c6deb977a3b30ea47a9bb8a2ae8a55964861c3df65f360d95745e341cee40d5c17f4 - languageName: node - linkType: hard - "ieee754@npm:^1.1.12": version: 1.2.1 resolution: "ieee754@npm:1.2.1" @@ -10910,9 +10903,6 @@ __metadata: vite-tsconfig-paths: "npm:^4.3.1" vitest: "npm:^1.2.2" web-vitals: "npm:^3.5.1" - workbox-core: "npm:^7.0.0" - workbox-expiration: "npm:^7.0.0" - workbox-precaching: "npm:^7.0.0" languageName: unknown linkType: soft @@ -12438,52 +12428,6 @@ __metadata: languageName: node linkType: hard -"workbox-core@npm:7.0.0, workbox-core@npm:^7.0.0": - version: 7.0.0 - resolution: "workbox-core@npm:7.0.0" - checksum: 10/680c65e926517a6cd7b515243b9a33a5f4cdc45de31e060fbdc89e28f79b10a2fa211576802a29790cd37fa8a801f3fccfb9cbe371acaa8095c858c9afefc7e6 - languageName: node - linkType: hard - -"workbox-expiration@npm:^7.0.0": - version: 7.0.0 - resolution: "workbox-expiration@npm:7.0.0" - dependencies: - idb: "npm:^7.0.1" - workbox-core: "npm:7.0.0" - checksum: 10/a9b23c7c76cbabe8f04567e603428db939cb169cf40c1e5ba1c5a1eb966f7b8a4d0182dffdd3d77917b5edca966be0d6e347b7eff274292256fe6ea9a40fa754 - languageName: node - linkType: hard - -"workbox-precaching@npm:^7.0.0": - version: 7.0.0 - resolution: "workbox-precaching@npm:7.0.0" - dependencies: - workbox-core: "npm:7.0.0" - workbox-routing: "npm:7.0.0" - workbox-strategies: "npm:7.0.0" - checksum: 10/8882d5ba888b08aba5e82656b26cd2f293c5adbb858bdff5e17cb4bf1823063cf51d16d3d6640716de96f11cf458f8aaf33b5ffef1ef07d327df8680711bf02e - languageName: node - linkType: hard - -"workbox-routing@npm:7.0.0": - version: 7.0.0 - resolution: "workbox-routing@npm:7.0.0" - dependencies: - workbox-core: "npm:7.0.0" - checksum: 10/294c4b0f136e39c44678caa736195bd99bf93e55ba3605d0a880e36e7ffc9a4b6be36c2c37bca3ee1f905615641167d5b2a0eab742f46b94536f2c5eaac0e832 - languageName: node - linkType: hard - -"workbox-strategies@npm:7.0.0": - version: 7.0.0 - resolution: "workbox-strategies@npm:7.0.0" - dependencies: - workbox-core: "npm:7.0.0" - checksum: 10/7d7dbe9dff54c22e01d01238dbf0b3ba259b85c08cbd8b617b9f5104efef948b760848fcf36385d26fd651a2fe57c4faa3d6a4fc2739e05fc684da0a7eb2197a - languageName: node - linkType: hard - "wrap-ansi@npm:^7.0.0": version: 7.0.0 resolution: "wrap-ansi@npm:7.0.0"