Skip to content

Commit

Permalink
fix(build): add worker specific build config
Browse files Browse the repository at this point in the history
  • Loading branch information
lsagetlethias committed Oct 25, 2022
1 parent 91fd6fc commit 96cf0d9
Show file tree
Hide file tree
Showing 50 changed files with 874 additions and 289 deletions.
4 changes: 3 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TRACKER_MATOMO_ID_SITE=
TRACKER_MATOMO_URL=https://matomo.fabrique.social.gouv.fr
# matomo | posthog | noop (noop = no tracker) | debug | delegating
# matomo | posthog | noop (noop = no tracker) | debug | delegating / default=noop
# delegating = "delegating:<provider1>,<provider2>,...,<providerN>" = multiple tracker
TRACKER_PROVIDER=noop
TRACKER_POSTHOG_API_KEY=
Expand All @@ -11,3 +11,5 @@ SENTRY_ORG=incubateur
SENTRY_URL=https://sentry.fabrique.social.gouv.fr
# if "prodcution" build, skip uploading sourcemaps to Sentry
SKIP_SENTRY_UPLOAD=1
# file | localdb | inmemory / default=file
CACHE_PROVIDER=file
1 change: 1 addition & 0 deletions .github/workflows/branch-test-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_URL: ${{ secrets.SENTRY_URL }}
CACHE_PROVIDER: file
- name: Archive dist
uses: actions/upload-artifact@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_URL: ${{ secrets.SENTRY_URL }}
CACHE_PROVIDER: file

- name: Archive dist
uses: actions/upload-artifact@v2
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_URL: ${{ secrets.SENTRY_URL }}
CACHE_PROVIDER: file

- name: Lint
run: yarn lint && yarn lint:test
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jobs:
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_URL: ${{ secrets.SENTRY_URL }}
CACHE_PROVIDER: localdb
- name: Archive dist
uses: actions/upload-artifact@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [x] coverage
- [x] generate build
## Services
- [ ] Logger (winston + sentry + console + "renderer transport to main")
- [x] Logger (winston + sentry + console + "renderer transport to main")
- [x] Tracker (posthog)
- [x] global and user config (shared over ipc)
- [x] i18n (i18next + react-i18next (start [here](./src/common/i18n/)))
Expand Down Expand Up @@ -46,5 +46,5 @@
- [ ] pst extractor indexes => descriptorId
- [ ] eml export parallel ops
- [x] test csv export
- [ ] windows installer appId shortcut regedit
- [ ] windows open pst error (db?)
- [x] windows installer appId shortcut regedit
- [x] windows open pst error (db?)
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare const __static: string;

declare namespace NodeJS {
interface ProcessEnv {
CACHE_PROVIDER: string;
SENTRY_DSN: string;
SENTRY_ORG: string;
SENTRY_URL: string;
Expand Down
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"dev": "electron-webpack dev",
"debug": "cross-env 'ELECTRON_ARGS=[\"--inspect-brk=9229\"]' yarn dev",
"compile": "electron-webpack",
"compile": "electron-webpack && webpack --config webpack.workers.config.js",
"dist:win": "electron-builder --x64 --win portable msi nsis",
"dist:mac": "electron-builder --mac dmg zip",
"dist:mac-local": "CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder --mac dmg",
Expand Down Expand Up @@ -55,16 +55,19 @@
"@nivo/core",
"@nivo/generators",
"@sentry/electron",
"@socialgouv/archimail-pst-extractor",
"d3",
"electron-log",
"electron-store",
"i18next",
"lodash",
"normalize.css",
"posthog-js",
"react",
"react-dom",
"react-dropzone",
"react-i18next",
"source-map-support",
"tarn",
"uuid",
"zustand"
]
},
Expand Down Expand Up @@ -98,6 +101,13 @@
"filter": [
"**/*"
]
},
{
"from": "node_modules",
"to": "natives/",
"filter": [
"**/*.node"
]
}
],
"directories": {
Expand Down Expand Up @@ -189,6 +199,7 @@
"sass": "^1.49.0",
"sass-loader": "10",
"semantic-release": "^19.0.3",
"string-replace-loader": "^2",
"stylelint": "^14.9.1",
"stylelint-config-sass-guidelines": "^9.0.1",
"stylelint-config-standard": "^26.0.0",
Expand All @@ -207,14 +218,15 @@
"@nivo/generators": "^0.79.0",
"@sentry/electron": "^3.0.7",
"@socialgouv/archimail-pst-extractor": "^0.2.0",
"classic-level": "^1.2.0",
"d3": "^7.3.0",
"electron-log": "^4.4.8",
"electron-store": "^8.0.2",
"electron-updater": "^5.0.5",
"electron-util": "^0.17.2",
"fs-extra": "^10.1.0",
"i18next": "^21.8.10",
"json2csv": "^5.0.6",
"level": "^8.0.0",
"lodash": "^4.17.21",
"normalize.css": "^8.0.1",
"posthog-js": "~1.24.0",
Expand Down
13 changes: 13 additions & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { app, ipcMain, ipcRenderer } = IS_WORKER

export interface WorkerConfig {
APP_CACHE: string;
APP_DATA: string;
IS_DEV: boolean;
IS_DIST_MODE: boolean;
IS_E2E: boolean;
Expand Down Expand Up @@ -51,13 +52,17 @@ export const IS_MAC = localWorkerConfig.IS_MAC ?? process.platform === "darwin";
export const IS_WIN = localWorkerConfig.IS_WIN ?? process.platform === "win32";
const IS_PACKAGE_EVENT = "config.IS_PACKAGED";
const APP_CACHE_EVENT = "config.APP_CACHE";
const APP_DATA_EVENT = "config.APP_DATA";
if (IS_MAIN && !IS_WORKER) {
ipcMain.on(IS_PACKAGE_EVENT, (event) => {
event.returnValue = app.isPackaged;
});
ipcMain.on(APP_CACHE_EVENT, (event) => {
event.returnValue = APP_CACHE();
});
ipcMain.on(APP_DATA_EVENT, (event) => {
event.returnValue = APP_DATA();
});
}

if (IS_DEV && IS_MAIN && !IS_WORKER) {
Expand Down Expand Up @@ -87,6 +92,13 @@ export const APP_CACHE = (): string => {
} else return ipcRenderer.sendSync(APP_CACHE_EVENT) as string;
};

export const APP_DATA = (): string => {
if (IS_WORKER) return localWorkerConfig.APP_DATA!;
if (IS_MAIN) {
return app.getPath("userData");
} else return ipcRenderer.sendSync(APP_DATA_EVENT) as string;
};

export const IS_DIST_MODE =
localWorkerConfig.IS_DIST_MODE ??
(!IS_PACKAGED() && !process.env.ELECTRON_WEBPACK_WDS_PORT);
Expand All @@ -100,6 +112,7 @@ export const STATIC_PATH =

export const workerConfig: WorkerConfig = {
APP_CACHE: APP_CACHE(),
APP_DATA: APP_DATA(),
IS_DEV,
IS_DIST_MODE,
IS_E2E,
Expand Down
5 changes: 3 additions & 2 deletions src/common/lib/ModuleManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logger } from "../logger";
import type { Module } from "../modules/Module";
import { ModuleError } from "../modules/Module";
import { AppError } from "./error/AppError";
Expand All @@ -8,7 +9,7 @@ import { AppError } from "./error/AppError";
export const loadModules = async (...mods: Module[]): pvoid => {
await Promise.all(
mods.map(async (mod) => {
console.log(`<MODULE_LOADER> ${mod.constructor.name} loading !`);
logger.log(`<MODULE_LOADER> ${mod.constructor.name} loading !`);
await mod.init().catch((error) => {
throw new ModuleError(
`<MODULE_ERROR> ${mod.constructor.name} failed init.`,
Expand All @@ -27,7 +28,7 @@ export const loadModules = async (...mods: Module[]): pvoid => {
export const unloadModules = async (...mods: Module[]): pvoid => {
await Promise.all(
mods.map(async (mod) => {
console.warn(
logger.warn(
`<MODULE_UNLOADER> ${mod.constructor.name} unloading !`
);
await mod.uninit().catch((error) => {
Expand Down
11 changes: 6 additions & 5 deletions src/common/lib/event/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { noop } from "lodash";
import { v4 as randomUuid } from "uuid";

import { IS_MAIN } from "../../config";
import { logger } from "../../logger";
import { IsomorphicService } from "../../modules/ContainerModule";
import { ipcMain, ipcRenderer } from "../ipc";
import type {
Expand Down Expand Up @@ -51,7 +52,7 @@ export class PubSub extends IsomorphicService {
// the trigger with an ipc event (PUBSUB_TRIGGER_EVENT)
// also save the "unsubscriber" returned function for later usage
ipcMain.on(PUBSUB_SUBSCRIBE_EVENT, (ipcEvent, id, uuid) => {
console.log("[pubsub] renderer asks for subscribe", {
logger.log("[pubsub] renderer asks for subscribe", {
id,
uuid,
});
Expand All @@ -68,7 +69,7 @@ export class PubSub extends IsomorphicService {
// get and trigger "unsubscriber" functions (which call the
// PUBSUB_UNSUBSCRIBE_EVENT back to ipc) and delete them
ipcMain.on(PUBSUB_UNSUBSCRIBE_EVENT, (event, uuid) => {
console.log("[pubsub] renderer asks for unsub", { uuid });
logger.log("[pubsub] renderer asks for unsub", { uuid });
this.unsubscribersInRenderer.get(uuid)?.();
event.returnValue = this.unsubscribersInRenderer.delete(uuid);
});
Expand All @@ -77,7 +78,7 @@ export class PubSub extends IsomorphicService {
// - basically when a publish is done on main - propagate the
// trigger to registered renderer listeners
ipcRenderer.on(PUBSUB_TRIGGER_EVENT, (_, id, event) => {
console.log("[pubsub] main triggered an event", {
logger.log("[pubsub] main triggered an event", {
event,
id,
});
Expand All @@ -98,7 +99,7 @@ export class PubSub extends IsomorphicService {
*/
public async uninit(): pvoid {
this.unsubscribersInRenderer.forEach((unsubscribe, uuid) => {
console.log("[pubsub] call unsubscribe for ", uuid);
logger.log("[pubsub] call unsubscribe for ", uuid);
unsubscribe();
});
this.unsubscribersInRenderer.clear();
Expand Down Expand Up @@ -141,7 +142,7 @@ export class PubSub extends IsomorphicService {
PUBSUB_UNSUBSCRIBE_EVENT,
uuid
);
console.log("[pubsub] unsubscribe in main !", { done });
logger.log("[pubsub] unsubscribe in main !", { done });
};
this.unsubscribersInRenderer.set(uuid, unsubscribe);
}
Expand Down
26 changes: 26 additions & 0 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { create } from "electron-log";
import path from "path";

import { APP_DATA, IS_DEV, IS_MAIN, IS_TEST, IS_WORKER } from "./config";
import { name } from "./utils/package";

const logger = create(name);
if (!IS_TEST) {
logger.transports.file.resolvePath = ({ fileName }) =>
path.resolve(APP_DATA(), "logs", fileName ?? "default.log");

if (!IS_MAIN && !IS_WORKER) {
// renderer only config
if (logger.transports.ipc) logger.transports.ipc.level = false;
}

if (IS_DEV) {
logger.transports.file.level = false;
}

if (IS_MAIN && !IS_WORKER) {
// main only config
}
}

export { logger };
5 changes: 3 additions & 2 deletions src/common/modules/ContainerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";

import { IS_PACKAGED } from "../config";
import { AppError } from "../lib/error/AppError";
import { logger } from "../logger";
import type { UnknownMapping } from "../utils/type";
import type {
ReturnServiceType,
Expand Down Expand Up @@ -80,7 +81,7 @@ class ContainerModule extends IsomorphicModule {
await Promise.all(
[...isomorphicServiceMap.values(), ...serviceMap.values()]
.map((service) => {
console.info(
logger.info(
`[ContainerModule] ${
(service as Service).name
} loading !`
Expand All @@ -102,7 +103,7 @@ class ContainerModule extends IsomorphicModule {
await Promise.all(
[...isomorphicServiceMap.values(), ...serviceMap.values()]
.map((service) => {
console.warn(
logger.warn(
`[ContainerModule] ${
(service as Service).name
} unloading !`
Expand Down
9 changes: 5 additions & 4 deletions src/common/modules/I18nModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "../i18n/raw";
import type { PubSub } from "../lib/event/PubSub";
import type { Event, Unsubscriber } from "../lib/event/type";
import { logger } from "../logger";
import { WaitableTrait } from "../utils/WaitableTrait";
import { IsomorphicService } from "./ContainerModule";
import { IsomorphicModule } from "./Module";
Expand Down Expand Up @@ -139,7 +140,7 @@ export class I18nModule extends IsomorphicModule {
this.unsubscriberConfigUpdate = this.pubSub.subscribe(
"event.userconfig.updated",
(event) => {
console.log(
logger.log(
"[i18nModule] pubsub trigger",
this.userConfigChangedFromHere,
event
Expand All @@ -157,7 +158,7 @@ export class I18nModule extends IsomorphicModule {
}

public async uninit(): pvoid {
console.info("[I18nModule] uninit ", this.unsubscriberConfigUpdate);
logger.info("[I18nModule] uninit ", this.unsubscriberConfigUpdate);
this.unsubscriberConfigUpdate?.();
return Promise.resolve();
}
Expand Down Expand Up @@ -209,7 +210,7 @@ export class I18nModule extends IsomorphicModule {
ipcMain.handle(
I18N_CHANGE_LANGUAGE_EVENT,
async (_evt, lng: Locale) => {
console.log("[I18nModule] change asked from renderer", lng);
logger.log("[I18nModule] change asked from renderer", lng);
const oldLng = i18next.language;
await i18next.changeLanguage(lng);
this.userConfigChangedFromHere = true;
Expand All @@ -231,7 +232,7 @@ export class I18nModule extends IsomorphicModule {
ipcRenderer.on(
I18N_CHANGE_LANGUAGE_CALLBACK_EVENT,
async (_evt, lng: Locale) => {
console.log("[I18nModule] change triggered by main", lng);
logger.log("[I18nModule] change triggered by main", lng);
const oldLng = i18next.language;
await i18next.changeLanguage(lng);
this.triggerLanguageChangedListeners(lng, oldLng as Locale);
Expand Down
7 changes: 5 additions & 2 deletions src/common/modules/TrackerModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IS_DEV } from "../config";
import type { PubSub } from "../lib/event/PubSub";
import { DebugProvider } from "../tracker/provider/DebugProvider";
import { DelegatingProvider } from "../tracker/provider/DelegatingProvider";
import { NoopProvider } from "../tracker/provider/NoopProvider";
import type { TrackerProvider } from "../tracker/provider/TrackerProvider";
Expand Down Expand Up @@ -70,8 +72,9 @@ export class TrackerModule extends IsomorphicModule {
);
return new DelegatingProvider(appId, disabled, foundProviders);
}
return new (providers.find((p) => p.trackerName === name) ??
NoopProvider)(appId, disabled) as TrackerProvider;
return new (providers.find((p) => p.trackerName === name) ?? IS_DEV
? DebugProvider
: NoopProvider)(appId, disabled) as TrackerProvider;
}

get service(): TrackerService {
Expand Down
Loading

0 comments on commit 96cf0d9

Please sign in to comment.