Move remaining strings for Environment & Platform to type-checked values.
This commit is contained in:
12
index.html
12
index.html
@@ -15,17 +15,21 @@
|
||||
<script type="module">
|
||||
const platform = process.env.VITE_PLATFORM;
|
||||
switch (platform) {
|
||||
case 'capacitor':
|
||||
case 'capacitor': // BuildPlatform.Capacitor
|
||||
import('./src/main.capacitor.ts');
|
||||
break;
|
||||
case 'electron':
|
||||
case 'electron': // BuildPlatform.Electron
|
||||
import('./src/main.electron.ts');
|
||||
break;
|
||||
case 'pywebview':
|
||||
case 'pywebview': // BuildPlatform.PyWebView
|
||||
import('./src/main.pywebview.ts');
|
||||
break;
|
||||
default:
|
||||
case 'web': // BuildPlatform.Web
|
||||
import('./src/main.web.ts');
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown platform: ${platform}`);
|
||||
throw new Error(`Unknown platform: ${platform}`);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
"build:electron": "npm run clean:electron && tsc -p tsconfig.electron.json && vite build --config vite.config.electron.mts && node scripts/build-electron.js",
|
||||
"build:electron-linux": "npm run build:electron && electron-builder --linux AppImage",
|
||||
"build:electron-linux-deb": "npm run build:electron && electron-builder --linux deb",
|
||||
"build:electron-linux-prod": "NODE_ENV=production npm run build:electron && electron-builder --linux AppImage",
|
||||
"build:electron-linux-prod": "NODE_ENV=prod npm run build:electron && electron-builder --linux AppImage",
|
||||
"build:electron-mac": "npm run build:electron-prod && electron-builder --mac",
|
||||
"build:electron-mac-universal": "npm run build:electron-prod && electron-builder --mac --universal",
|
||||
"build:electron-prod": "NODE_ENV=production npm run build:electron",
|
||||
"build:electron-prod": "NODE_ENV=prod npm run build:electron",
|
||||
"build:pywebview": "vite build --config vite.config.pywebview.mts",
|
||||
"build:web": "VITE_GIT_HASH=`git log -1 --pretty=format:%h` vite build --config vite.config.web.mts",
|
||||
"check:android-device": "adb devices | grep -w 'device' || (echo 'No Android device connected' && exit 1)",
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
const { contextBridge, ipcRenderer } = require("electron");
|
||||
import { NodeEnv, BuildPlatform } from "@/interfaces/build";
|
||||
|
||||
const logger = {
|
||||
log: (message, ...args) => {
|
||||
// Always log in development, log with context in production
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (process.env.NODE_ENV !== NodeEnv.Prod) {
|
||||
/* eslint-disable no-console */
|
||||
console.log(`[Preload] ${message}`, ...args);
|
||||
/* eslint-enable no-console */
|
||||
@@ -23,7 +24,7 @@ const logger = {
|
||||
},
|
||||
info: (message, ...args) => {
|
||||
// Always log info in development, log with context in production
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (process.env.NODE_ENV !== NodeEnv.Prod) {
|
||||
/* eslint-disable no-console */
|
||||
console.info(`[Preload] ${message}`, ...args);
|
||||
/* eslint-enable no-console */
|
||||
@@ -53,7 +54,7 @@ const getPath = (pathType) => {
|
||||
logger.info("Preload script starting...");
|
||||
|
||||
// Force electron platform in the renderer process
|
||||
window.process = { env: { VITE_PLATFORM: "electron" } };
|
||||
window.process = { env: { VITE_PLATFORM: BuildPlatform.Electron } };
|
||||
|
||||
try {
|
||||
contextBridge.exposeInMainWorld("electronAPI", {
|
||||
@@ -76,12 +77,12 @@ try {
|
||||
// Environment info
|
||||
env: {
|
||||
isElectron: true,
|
||||
isDev: process.env.NODE_ENV === "development",
|
||||
platform: "electron", // Explicitly set platform
|
||||
isDev: process.env.NODE_ENV === NodeEnv.Dev,
|
||||
platform: BuildPlatform.Electron, // Explicitly set platform
|
||||
},
|
||||
// Path utilities
|
||||
getBasePath: () => {
|
||||
return process.env.NODE_ENV === "development" ? "/" : "./";
|
||||
return process.env.NODE_ENV === NodeEnv.Dev ? "/" : "./";
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
21
src/interfaces/build.ts
Normal file
21
src/interfaces/build.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export const NodeEnv = {
|
||||
Dev: "dev",
|
||||
Test: "test",
|
||||
Prod: "prod",
|
||||
} as const;
|
||||
export type NodeEnv = typeof NodeEnv[keyof typeof NodeEnv];
|
||||
|
||||
export const BuildEnv = {
|
||||
Development: "development",
|
||||
Testing: "testing",
|
||||
Production: "production",
|
||||
} as const;
|
||||
export type BuildEnv = typeof BuildEnv[keyof typeof BuildEnv];
|
||||
|
||||
export const BuildPlatform = {
|
||||
Web: "web",
|
||||
Electron: "electron",
|
||||
Capacitor: "capacitor",
|
||||
PyWebView: "pywebview",
|
||||
} as const;
|
||||
export type BuildPlatform = typeof BuildPlatform[keyof typeof BuildPlatform];
|
||||
@@ -1,12 +1,14 @@
|
||||
import { initBackend } from "absurd-sql/dist/indexeddb-main-thread";
|
||||
|
||||
import { initializeApp } from "./main.common";
|
||||
import { logger } from "./utils/logger";
|
||||
import { BuildPlatform } from "@/interfaces/build";
|
||||
|
||||
const platform = process.env.VITE_PLATFORM;
|
||||
const pwa_enabled = process.env.VITE_PWA_ENABLED === "true";
|
||||
|
||||
// Only import service worker for web builds
|
||||
if (platform !== "electron" && pwa_enabled) {
|
||||
if (platform !== BuildPlatform.Electron && pwa_enabled) {
|
||||
import("./registerServiceWorker"); // Web PWA support
|
||||
}
|
||||
|
||||
@@ -25,7 +27,7 @@ function sqlInit() {
|
||||
// workers through the main thread
|
||||
initBackend(worker);
|
||||
}
|
||||
if (platform === "web" || platform === "development") {
|
||||
if (platform === BuildPlatform.Web) {
|
||||
sqlInit();
|
||||
} else {
|
||||
logger.warn("[Web] SQL not initialized for platform", { platform });
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
import { register } from "register-service-worker";
|
||||
import { NodeEnv, BuildPlatform } from "@/interfaces/build";
|
||||
|
||||
// Check if we're in an Electron environment
|
||||
const isElectron =
|
||||
process.env.VITE_PLATFORM === "electron" ||
|
||||
process.env.VITE_PLATFORM === BuildPlatform.Electron ||
|
||||
process.env.VITE_DISABLE_PWA === "true" ||
|
||||
window.navigator.userAgent.toLowerCase().includes("electron");
|
||||
|
||||
@@ -15,7 +16,7 @@ const isElectron =
|
||||
if (
|
||||
!isElectron &&
|
||||
process.env.VITE_PWA_ENABLED === "true" &&
|
||||
process.env.NODE_ENV === "production"
|
||||
process.env.NODE_ENV === NodeEnv.Prod
|
||||
) {
|
||||
register(`${process.env.BASE_URL}sw.js`, {
|
||||
ready() {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { WebPlatformService } from "./platforms/WebPlatformService";
|
||||
import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
|
||||
import { ElectronPlatformService } from "./platforms/ElectronPlatformService";
|
||||
import { PyWebViewPlatformService } from "./platforms/PyWebViewPlatformService";
|
||||
import { BuildPlatform } from "@/interfaces/build";
|
||||
|
||||
/**
|
||||
* Factory class for creating platform-specific service implementations.
|
||||
@@ -35,19 +36,19 @@ export class PlatformServiceFactory {
|
||||
return PlatformServiceFactory.instance;
|
||||
}
|
||||
|
||||
const platform = process.env.VITE_PLATFORM || "web";
|
||||
const platform = process.env.VITE_PLATFORM || BuildPlatform.Web;
|
||||
|
||||
switch (platform) {
|
||||
case "capacitor":
|
||||
case BuildPlatform.Capacitor:
|
||||
PlatformServiceFactory.instance = new CapacitorPlatformService();
|
||||
break;
|
||||
case "electron":
|
||||
case BuildPlatform.Electron:
|
||||
PlatformServiceFactory.instance = new ElectronPlatformService();
|
||||
break;
|
||||
case "pywebview":
|
||||
case BuildPlatform.PyWebView:
|
||||
PlatformServiceFactory.instance = new PyWebViewPlatformService();
|
||||
break;
|
||||
case "web":
|
||||
case BuildPlatform.Web:
|
||||
default:
|
||||
PlatformServiceFactory.instance = new WebPlatformService();
|
||||
break;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import { AxiosError } from "axios";
|
||||
import { logger, safeStringify } from "../utils/logger";
|
||||
import { BuildPlatform } from "@/interfaces/build";
|
||||
|
||||
/**
|
||||
* Handles API errors with platform-specific logging and error processing.
|
||||
@@ -36,7 +37,7 @@ import { logger, safeStringify } from "../utils/logger";
|
||||
* ```
|
||||
*/
|
||||
export const handleApiError = (error: AxiosError, endpoint: string) => {
|
||||
if (process.env.VITE_PLATFORM === "capacitor") {
|
||||
if (process.env.VITE_PLATFORM === BuildPlatform.Capacitor) {
|
||||
const endpointStr = safeStringify(endpoint); // we've seen this as an object in deep links
|
||||
logger.error(`[Capacitor API Error] ${endpointStr}:`, {
|
||||
message: error.message,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { logToDb } from "../db/databaseUtil";
|
||||
import { BuildPlatform, NodeEnv } from "@/interfaces/build";
|
||||
|
||||
export function safeStringify(obj: unknown) {
|
||||
const seen = new WeakSet();
|
||||
@@ -21,7 +22,7 @@ export function safeStringify(obj: unknown) {
|
||||
|
||||
export const logger = {
|
||||
debug: (message: string, ...args: unknown[]) => {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (process.env.NODE_ENV !== NodeEnv.Prod) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.debug(message, ...args);
|
||||
// const argsString = args.length > 0 ? " - " + safeStringify(args) : "";
|
||||
@@ -30,8 +31,8 @@ export const logger = {
|
||||
},
|
||||
log: (message: string, ...args: unknown[]) => {
|
||||
if (
|
||||
process.env.NODE_ENV !== "production" ||
|
||||
process.env.VITE_PLATFORM === "capacitor"
|
||||
process.env.NODE_ENV !== NodeEnv.Prod ||
|
||||
process.env.VITE_PLATFORM === BuildPlatform.Capacitor
|
||||
) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(message, ...args);
|
||||
@@ -41,9 +42,9 @@ export const logger = {
|
||||
},
|
||||
info: (message: string, ...args: unknown[]) => {
|
||||
if (
|
||||
process.env.NODE_ENV !== "production" ||
|
||||
process.env.VITE_PLATFORM === "capacitor" ||
|
||||
process.env.VITE_PLATFORM === "electron"
|
||||
process.env.NODE_ENV !== NodeEnv.Prod ||
|
||||
process.env.VITE_PLATFORM === BuildPlatform.Capacitor ||
|
||||
process.env.VITE_PLATFORM === BuildPlatform.Electron
|
||||
) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.info(message, ...args);
|
||||
|
||||
@@ -100,6 +100,7 @@ import { Component, Vue } from "vue-facing-decorator";
|
||||
import { RouteLocationNormalizedLoaded, Router } from "vue-router";
|
||||
|
||||
import { APP_SERVER } from "@/constants/app";
|
||||
import { NodeEnv } from "@/interfaces/build";
|
||||
import { logger } from "@/utils/logger";
|
||||
import { errorStringForLog } from "@/libs/endorserServer";
|
||||
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory";
|
||||
@@ -148,7 +149,7 @@ export default class DeepLinkRedirectView extends Vue {
|
||||
this.deepLinkUrl = `timesafari://${fullPathWithQuery}`;
|
||||
this.webUrl = `${APP_SERVER}/${fullPathWithQuery}`;
|
||||
|
||||
this.isDevelopment = process.env.NODE_ENV !== "production";
|
||||
this.isDevelopment = process.env.NODE_ENV !== NodeEnv.Prod;
|
||||
this.userAgent = navigator.userAgent;
|
||||
|
||||
this.openDeepLink();
|
||||
|
||||
@@ -8,5 +8,5 @@
|
||||
"allowImportingTsExtensions": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["vite.config.*"]
|
||||
"include": ["vite.config.*", "./src/interfaces/build.ts"]
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { defineConfig } from "vite";
|
||||
import { createBuildConfig } from "./vite.config.common.mts";
|
||||
import { BuildPlatform } from "./src/interfaces/build.ts";
|
||||
|
||||
export default defineConfig(async () => createBuildConfig('capacitor'));
|
||||
export default defineConfig(async () => createBuildConfig(BuildPlatform.Capacitor));
|
||||
@@ -1,39 +1,40 @@
|
||||
import { defineConfig, UserConfig, Plugin } from "vite";
|
||||
import { defineConfig, UserConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
import dotenv from "dotenv";
|
||||
import { loadAppConfig } from "./vite.config.common-utils.mts";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
export type BuildMode = 'web' | 'electron' | 'capacitor' | 'pywebview';
|
||||
export type BuildEnv = 'dev' | 'test' | 'prod';
|
||||
import { NodeEnv, BuildEnv, BuildPlatform } from "./src/interfaces/build.ts";
|
||||
|
||||
// Load environment variables
|
||||
if (
|
||||
process.env.NODE_ENV === 'dev'
|
||||
|| process.env.NODE_ENV === 'test'
|
||||
|| process.env.NODE_ENV === 'prod'
|
||||
) {
|
||||
console.log(`NODE_ENV=${process.env.NODE_ENV}`);
|
||||
let buildEnv: BuildEnv;
|
||||
if (process.env.NODE_ENV === NodeEnv.Dev) {
|
||||
buildEnv = BuildEnv.Development;
|
||||
} else if (process.env.NODE_ENV === NodeEnv.Test) {
|
||||
buildEnv = BuildEnv.Testing;
|
||||
} else if (process.env.NODE_ENV === NodeEnv.Prod) {
|
||||
buildEnv = BuildEnv.Production;
|
||||
} else {
|
||||
console.error("NODE_ENV is not set. Invoke with NODE_ENV=dev|test|prod");
|
||||
throw new Error("NODE_ENV is not set. Invoke with NODE_ENV=dev|test|prod");
|
||||
// process.exit(1);
|
||||
console.error("NODE_ENV is not set. Invoke with NODE_ENV=" + Object.values(NodeEnv).join("|"));
|
||||
throw new Error("NODE_ENV is not set. Invoke with NODE_ENV=" + Object.values(NodeEnv).join("|"));
|
||||
}
|
||||
console.log(`Environment: ${buildEnv}`);
|
||||
|
||||
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });
|
||||
dotenv.config({ path: `.env.${buildEnv}` });
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export async function createBuildConfig(mode: BuildMode): Promise<UserConfig> {
|
||||
export async function createBuildConfig(platform: BuildPlatform): Promise<UserConfig> {
|
||||
const appConfig = await loadAppConfig();
|
||||
const isElectron = mode === "electron";
|
||||
const isCapacitor = mode === "capacitor";
|
||||
const isPyWebView = mode === "pywebview";
|
||||
|
||||
console.log(`Platform: ${platform}`);
|
||||
const isElectron = platform === BuildPlatform.Electron;
|
||||
const isCapacitor = platform === BuildPlatform.Capacitor;
|
||||
const isPyWebView = platform === BuildPlatform.PyWebView;
|
||||
|
||||
// Explicitly set platform and disable PWA for Electron
|
||||
process.env.VITE_PLATFORM = mode;
|
||||
process.env.VITE_PLATFORM = platform;
|
||||
process.env.VITE_PWA_ENABLED = (isElectron || isPyWebView || isCapacitor)
|
||||
? 'false'
|
||||
: 'true';
|
||||
@@ -69,7 +70,7 @@ export async function createBuildConfig(mode: BuildMode): Promise<UserConfig> {
|
||||
},
|
||||
define: {
|
||||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
||||
'process.env.VITE_PLATFORM': JSON.stringify(mode),
|
||||
'process.env.VITE_PLATFORM': JSON.stringify(platform),
|
||||
'process.env.VITE_PWA_ENABLED': JSON.stringify(!isElectron),
|
||||
'process.env.VITE_DISABLE_PWA': JSON.stringify(isElectron),
|
||||
__dirname: isElectron ? JSON.stringify(process.cwd()) : '""',
|
||||
@@ -91,10 +92,10 @@ export async function createBuildConfig(mode: BuildMode): Promise<UserConfig> {
|
||||
'path': path.resolve(__dirname, './src/utils/node-modules/path.js'),
|
||||
'fs': path.resolve(__dirname, './src/utils/node-modules/fs.js'),
|
||||
'crypto': path.resolve(__dirname, './src/utils/node-modules/crypto.js'),
|
||||
'nostr-tools/nip06': mode === 'development'
|
||||
'nostr-tools/nip06': buildEnv === BuildEnv.Development
|
||||
? 'nostr-tools/nip06'
|
||||
: path.resolve(__dirname, 'node_modules/nostr-tools/nip06'),
|
||||
'nostr-tools/core': mode === 'development'
|
||||
'nostr-tools/core': buildEnv === BuildEnv.Development
|
||||
? 'nostr-tools'
|
||||
: path.resolve(__dirname, 'node_modules/nostr-tools'),
|
||||
'nostr-tools': path.resolve(__dirname, 'node_modules/nostr-tools'),
|
||||
@@ -121,4 +122,4 @@ export async function createBuildConfig(mode: BuildMode): Promise<UserConfig> {
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig(async () => createBuildConfig('web'));
|
||||
export default defineConfig(async () => createBuildConfig(BuildPlatform.Web));
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { defineConfig, mergeConfig } from "vite";
|
||||
import { createBuildConfig } from "./vite.config.common.mts";
|
||||
import path from 'path';
|
||||
import { BuildPlatform } from "./src/interfaces/build.ts";
|
||||
|
||||
export default defineConfig(async () => {
|
||||
const baseConfig = await createBuildConfig('electron');
|
||||
const baseConfig = await createBuildConfig(BuildPlatform.Electron);
|
||||
|
||||
return mergeConfig(baseConfig, {
|
||||
build: {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { defineConfig } from "vite";
|
||||
import { createBuildConfig } from "./vite.config.common.mts";
|
||||
import { BuildPlatform } from "./src/interfaces/build.ts";
|
||||
|
||||
export default defineConfig(async () => createBuildConfig('pywebview'));
|
||||
export default defineConfig(async () => createBuildConfig(BuildPlatform.PyWebView));
|
||||
@@ -2,9 +2,10 @@ import { defineConfig, mergeConfig } from "vite";
|
||||
import { VitePWA } from "vite-plugin-pwa";
|
||||
import { createBuildConfig } from "./vite.config.common.mts";
|
||||
import { loadPwaConfig } from "./vite.config.common-utils.mts";
|
||||
import { BuildPlatform } from "./src/interfaces/build.ts";
|
||||
|
||||
export default defineConfig(async () => {
|
||||
const baseConfig = await createBuildConfig('web');
|
||||
const baseConfig = await createBuildConfig(BuildPlatform.Web);
|
||||
const pwaConfig = await loadPwaConfig();
|
||||
|
||||
return mergeConfig(baseConfig, {
|
||||
|
||||
Reference in New Issue
Block a user