Skip to content

Commit

Permalink
frontend: separate env folders
Browse files Browse the repository at this point in the history
now we will use different folders depending on the environment we run the app (prod, dev & test)
  • Loading branch information
horacioh committed Sep 12, 2023
1 parent 1e47089 commit 4a41c23
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 285 deletions.
9 changes: 8 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ use nix --max-jobs auto
watch_file shell.nix build/nix/**/*.nix

export TAMAGUI_TARGET="web"

export APP_FOLDER_NAME="Mintter.dev"

export VITE_BACKEND_HTTP_PORT='55002'
export VITE_BACKEND_GRPC_PORT="56002"
export VITE_BACKEND_P2P_PORT="56003"

export NEXT_PUBLIC_GRPC_HOST="https://gateway.mintter.com"
# export NEXT_PUBLIC_GRPC_HOST="http://localhost:56001"
export NEXT_PUBLIC_LN_HOST="https://ln.mintter.com"
export MINTTER_IS_GATEWAY="1"

export MINTTER_GATEWAY_URL="https://mintter.com"
export VITE_MINTTER_GATEWAY_URL="https://mintter.com"

export APP_VERSION="0.0.100"

Expand Down
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def main():

@cmd(cmds, "frontend-test", "Tests frontend code")
def frontend_test(args):
run("yarn test")
run("APP_FOLDER_NAME=Mintter.test && yarn test")

@cmd(cmds, "run-backend", "Build and run mintterd binary for the current platform.")
def run_backend(args):
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"dev": "electron-forge start",
"devtools": "react-devtools",
"test": "node test/pre-test.js && yarn e2e && node test/post-test.js",
"test": "yarn e2e",
"e2e": "xvfb-maybe -- playwright test",
"format": "prettier \"**/*.{ts,tsx,md,mdx,json}\" --check --ignore-path ../../../.prettierignore",
"lint": "echo TODO",
Expand Down
2 changes: 2 additions & 0 deletions frontend/apps/desktop/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'path'
import Store from 'electron-store'
import {NavRoute} from '@mintter/app/src/utils/navigation'
import {childLogger, error, log} from './logger'
import {APP_USER_DATA_PATH} from './app-paths'

const t = initTRPC.create({isServer: true, transformer: superjson})

Expand Down Expand Up @@ -81,6 +82,7 @@ export function updateGoDaemonState(state: GoDaemonState) {

const store = new Store({
name: 'AppStore',
cwd: APP_USER_DATA_PATH,
})

type AppWindow = {
Expand Down
26 changes: 26 additions & 0 deletions frontend/apps/desktop/src/app-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {app} from 'electron'
import path from 'path'

export const APP_FOLDER_NAME =
process.env.NODE_ENV != 'production' && process.env.APP_FOLDER_NAME
? process.env.APP_FOLDER_NAME
: 'Mintter'

export const APP_USER_DATA_PATH = path.join(
app.getPath('appData'),
APP_FOLDER_NAME,
)

console.log(`== ~ APP_FOLDER_NAME:`, APP_FOLDER_NAME)

export function initPaths() {
app.setPath('userData', APP_USER_DATA_PATH)
console.log('===== userData', app.getPath('userData'))
}

export function setupTests() {
app.setPath('userData', APP_USER_DATA_PATH)
console.log('===== userData', app.getPath('userData'))
}

console.log(`== ~ process.env.NODE_ENV:`, process.env.NODE_ENV)
7 changes: 4 additions & 3 deletions frontend/apps/desktop/src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {join} from 'path'
import {updateGoDaemonState} from './api'
import {childLogger, log} from './logger'
import {color} from 'console-log-colors'
import {APP_USER_DATA_PATH} from './app-paths'

const logger = childLogger(color.cyan('Go Daemon'))

Expand Down Expand Up @@ -39,15 +40,15 @@ const prodDaemonBinaryPath = join(
`mintterd-${getPlatformTriple()}`,
)

const userDataDir = join(app.getPath('userData'), 'daemon')

console.log(`== ~ userDataDir:`, userDataDir)
const userDataDir = join(APP_USER_DATA_PATH, 'daemon')

let goDaemonExecutablePath =
process.env.NODE_ENV == 'development'
? devDaemonBinaryPath
: prodDaemonBinaryPath

console.log(`== ~ goDaemonExecutablePath:`, goDaemonExecutablePath)

const daemonArguments = [
'-http-port',
String(BACKEND_HTTP_PORT),
Expand Down
4 changes: 3 additions & 1 deletion frontend/apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import log from 'electron-log/main'
// import updater from 'update-electron-app'
import squirrelStartup from 'electron-squirrel-startup'
import {mainMenu, trpc} from './api'
import {initPaths} from './app-paths'
import {mainDaemon} from './daemon'
import {saveCidAsFile} from './save-cid-as-file'
import {openLink} from './open-link'

if (squirrelStartup) {
app.quit()
}

initPaths()

mainDaemon

Menu.setApplicationMenu(mainMenu)
Expand Down
13 changes: 10 additions & 3 deletions frontend/apps/desktop/test/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import {
ElectronApplication,
Page,
_electron as electron,
expect,
test,
} from '@playwright/test'
import os from 'os'

import fs from 'fs'
import path from 'path'
import {findLatestBuild, parseElectronApp} from 'electron-playwright-helpers'

let electronApp: ElectronApplication

test.beforeAll(async () => {
// remove the app data:

fs.rm(
path.join(os.homedir(), 'Library', 'Application Support', 'Mintter.test'),
{recursive: true},
(...args) => {
console.log('== DELETE??', args)
},
)
// find the latest build in the out directory
const latestBuild = findLatestBuild()

Expand Down
10 changes: 5 additions & 5 deletions frontend/packages/app/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
interface ImportMetaEnv {
readonly MINTTER_GATEWAY_URL: string
readonly BACKEND_HTTP_PORT: string
readonly BACKEND_GRPC_PORT: string
readonly BACKEND_P2P_PORT: string
readonly BACKEND_HOSTNAME: string
readonly VITE_MINTTER_GATEWAY_URL: string
readonly VITE_BACKEND_HTTP_PORT: string
readonly VITE_BACKEND_GRPC_PORT: string
readonly VITE_BACKEND_P2P_PORT: string
readonly VITE_BACKEND_HOSTNAME: string
}

interface ImportMeta {
Expand Down
27 changes: 22 additions & 5 deletions frontend/packages/app/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
/// <reference types="vite/client" />

// we are using this ternary ugly thing with `import.meta.env` and `process.env` because this variables will be loaded in different runtimes, and not in all runtines both "ways" are available.

export const MINTTER_GATEWAY_URL =
import.meta.env.MINTTER_GATEWAY_URL || 'https://mintter.com/'
export const BACKEND_HTTP_PORT = import.meta.env.BACKEND_HTTP_PORT || '55001'
export const BACKEND_GRPC_PORT = import.meta.env.BACKEND_GRPC_PORT || '56001'
export const BACKEND_P2P_PORT = import.meta.env.BACKEND_P2P_PORT || '56002'
import.meta.env.VITE_MINTTER_GATEWAY_URL ||
process.env.VITE_MINTTER_GATEWAY_URL ||
'https://mintter.com/'
export const BACKEND_HTTP_PORT =
import.meta.env.VITE_BACKEND_HTTP_PORT ||
process.env.VITE_BACKEND_HTTP_PORT ||
'55001'

export const BACKEND_GRPC_PORT =
import.meta.env.VITE_BACKEND_GRPC_PORT ||
process.env.VITE_BACKEND_GRPC_PORT ||
'56001'

export const BACKEND_P2P_PORT =
import.meta.env.VITE_BACKEND_P2P_PORT ||
process.env.VITE_BACKEND_P2P_PORT ||
'56002'

export const BACKEND_HOSTNAME =
import.meta.env.BACKEND_HOSTNAME || 'http://localhost'
import.meta.env.VITE_BACKEND_HOSTNAME ||
process.env.VITE_BACKEND_HOSTNAME ||
'http://localhost'

export const BACKEND_HTTP_URL = `${BACKEND_HOSTNAME}:${BACKEND_HTTP_PORT}`
export const BACKEND_GRPC_URL = `${BACKEND_HOSTNAME}:${BACKEND_GRPC_PORT}`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"lint:fix": "turbo lint --continue -- --fix --cache --cache-location 'node_modules/.cache/.eslintcache'",
"format": "turbo format --continue -- --cache --cache-location='node_modules/.cache/.prettiercache'",
"format:fix": "turbo format --continue -- --write --cache --cache-location='node_modules/.cache/.prettiercache'",
"test": "turbo test --continue",
"test": "APP_FOLDER_NAME=Mintter.test && turbo test --continue",
"validate": "yarn lint && yarn format",
"validate:fix": "yarn lint:fix && yarn format:fix",
"site": "yarn workspace @mintter/site dev",
Expand Down
Loading

0 comments on commit 4a41c23

Please sign in to comment.