mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	chore(dx/desktop): clean up package.json
This commit is contained in:
		@@ -30,7 +30,8 @@
 | 
			
		||||
    "prebuild-install": "^7.1.1"
 | 
			
		||||
  },  
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "dev": "tsx scripts/start.mts",
 | 
			
		||||
    "dev": "cross-env TRILIUM_DATA_DIR=data tsx scripts/start.mts",
 | 
			
		||||
    "start-no-dir": "cross-env tsx scripts/start.mts",
 | 
			
		||||
    "build": "tsx scripts/build.ts",
 | 
			
		||||
    "start-prod": "pnpm build && cross-env TRILIUM_DATA_DIR=data TRILIUM_PORT=37841 electron dist",
 | 
			
		||||
    "electron-forge:make": "pnpm build && electron-forge make dist",
 | 
			
		||||
@@ -43,98 +44,5 @@
 | 
			
		||||
    "name": "Trilium Notes Team",
 | 
			
		||||
    "email": "contact@eliandoran.me",
 | 
			
		||||
    "url": "https://github.com/TriliumNext/Notes"
 | 
			
		||||
  },
 | 
			
		||||
  "nx": {
 | 
			
		||||
    "name": "desktop",
 | 
			
		||||
    "targets": {
 | 
			
		||||
      "build": {
 | 
			
		||||
        "executor": "@nx/esbuild:esbuild",
 | 
			
		||||
        "outputs": [
 | 
			
		||||
          "{options.outputPath}"
 | 
			
		||||
        ],
 | 
			
		||||
        "defaultConfiguration": "production",
 | 
			
		||||
        "configurations": {
 | 
			
		||||
          "production": {
 | 
			
		||||
            "minify": true,
 | 
			
		||||
            "sourcemap": false
 | 
			
		||||
          },
 | 
			
		||||
          "development": {
 | 
			
		||||
            "minify": false,
 | 
			
		||||
            "sourcemap": true
 | 
			
		||||
          }
 | 
			
		||||
        },
 | 
			
		||||
        "options": {
 | 
			
		||||
          "main": "apps/desktop/src/electron-main.ts",
 | 
			
		||||
          "outputPath": "apps/desktop/dist",
 | 
			
		||||
          "tsConfig": "apps/desktop/tsconfig.app.json",
 | 
			
		||||
          "external": [
 | 
			
		||||
            "electron",
 | 
			
		||||
            "@electron/remote",
 | 
			
		||||
            "better-sqlite3",
 | 
			
		||||
            "./xhr-sync-worker.js"
 | 
			
		||||
          ],
 | 
			
		||||
          "thirdParty": true,
 | 
			
		||||
          "esbuildOptions": {
 | 
			
		||||
            "splitting": false,
 | 
			
		||||
            "loader": {
 | 
			
		||||
              ".css": "text"
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          "declarationRootDir": "apps/desktop/src"
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      "rebuild-deps": {
 | 
			
		||||
        "executor": "nx:run-commands",
 | 
			
		||||
        "dependsOn": [
 | 
			
		||||
          "build"
 | 
			
		||||
        ],
 | 
			
		||||
        "defaultConfiguration": "default",
 | 
			
		||||
        "cache": false,
 | 
			
		||||
        "configurations": {
 | 
			
		||||
          "default": {
 | 
			
		||||
            "command": "cross-env DEBUG=* tsx scripts/electron-rebuild.mts {projectRoot}/dist"
 | 
			
		||||
          },
 | 
			
		||||
          "nixos": {
 | 
			
		||||
            "command": "cross-env DEBUG=* tsx scripts/electron-rebuild.mts {projectRoot}/dist $(nix-shell -p electron_35 --run \"electron --version\")"
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      "serve": {
 | 
			
		||||
        "executor": "nx:run-commands",
 | 
			
		||||
        "dependsOn": [
 | 
			
		||||
          "rebuild-deps"
 | 
			
		||||
        ],
 | 
			
		||||
        "defaultConfiguration": "default",
 | 
			
		||||
        "configurations": {
 | 
			
		||||
          "default": {
 | 
			
		||||
            "command": "electron main.cjs",
 | 
			
		||||
            "cwd": "{projectRoot}/dist"
 | 
			
		||||
          },
 | 
			
		||||
          "nixos": {
 | 
			
		||||
            "command": "nix-shell -p electron_35 --run \"electron {projectRoot}/dist/main.cjs\"",
 | 
			
		||||
            "cwd": ".",
 | 
			
		||||
            "forwardAllArgs": false
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      "serve-nodir": {
 | 
			
		||||
        "executor": "nx:run-commands",
 | 
			
		||||
        "dependsOn": [
 | 
			
		||||
          "rebuild-deps"
 | 
			
		||||
        ],
 | 
			
		||||
        "defaultConfiguration": "default",
 | 
			
		||||
        "configurations": {
 | 
			
		||||
          "default": {
 | 
			
		||||
            "command": "electron main.cjs",
 | 
			
		||||
            "cwd": "{projectRoot}/dist"
 | 
			
		||||
          },
 | 
			
		||||
          "nixos": {
 | 
			
		||||
            "command": "nix-shell -p electron_35 --run \"electron {projectRoot}/dist/main.cjs\"",
 | 
			
		||||
            "cwd": ".",
 | 
			
		||||
            "forwardAllArgs": false
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -20,7 +20,6 @@ execSync(`${electronPath} ./src/main.ts`, {
 | 
			
		||||
        NODE_OPTIONS: "--import tsx",
 | 
			
		||||
        NODE_ENV: "development",
 | 
			
		||||
        TRILIUM_ENV: "dev",
 | 
			
		||||
        TRILIUM_DATA_DIR: "data",
 | 
			
		||||
        TRILIUM_RESOURCE_DIR: "../server/src",
 | 
			
		||||
        BETTERSQLITE3_NATIVE_PATH: join(projectRoot, "node_modules/better-sqlite3/build/Release/better_sqlite3.node"),
 | 
			
		||||
        LD_LIBRARY_PATH
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@
 | 
			
		||||
  "main": "./src/main.ts",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "dev": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_DATA_DIR=data TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
 | 
			
		||||
    "dev-no-dir": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
 | 
			
		||||
    "start-no-dir": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
 | 
			
		||||
    "edit-integration-db": "cross-env NODE_ENV=development TRILIUM_PORT=8086 TRILIUM_ENV=dev TRILIUM_DATA_DIR=spec/db TRILIUM_INTEGRATION_TEST=edit TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
 | 
			
		||||
    "build": "tsx scripts/build.ts",
 | 
			
		||||
    "package": "pnpm build && bash scripts/build-server.sh",
 | 
			
		||||
 
 | 
			
		||||
@@ -1,89 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * @module
 | 
			
		||||
 *
 | 
			
		||||
 * This script is used internally by the `rebuild-deps` target of the `desktop`. Normally we could use
 | 
			
		||||
 * `electron-rebuild` CLI directly, but it would rebuild the monorepo-level dependencies and breaks
 | 
			
		||||
 * the server build (and it doesn't expose a CLI option to override this).
 | 
			
		||||
 * 
 | 
			
		||||
 * A side purpose is to generate a fake `package.json` file in the `dist` directory
 | 
			
		||||
 * that contains only the native dependencies. This is used by `electron-forge`.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import { join, resolve } from "path";
 | 
			
		||||
import { rebuild } from "@electron/rebuild"
 | 
			
		||||
import { readFileSync, rmSync, writeFileSync } from "fs";
 | 
			
		||||
 | 
			
		||||
const nativeDependencies = [
 | 
			
		||||
    "better-sqlite3"
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
function parsePackageJson(distDir: string) {
 | 
			
		||||
    const packageJsonPath = join(distDir, "../package.json");
 | 
			
		||||
    const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
 | 
			
		||||
    let electronVersion: string;
 | 
			
		||||
 | 
			
		||||
    if (process.argv[3]) {
 | 
			
		||||
        electronVersion = process.argv[3];
 | 
			
		||||
    } else {
 | 
			
		||||
        electronVersion = packageJson?.devDependencies?.electron ?? packageJson?.dependencies?.electron;
 | 
			
		||||
        if (!electronVersion) {
 | 
			
		||||
            console.error(`Unable to retrieve Electron version in '${resolve(packageJsonPath)}'.`);
 | 
			
		||||
            process.exit(3);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return {
 | 
			
		||||
        electronVersion,
 | 
			
		||||
        packageJson
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function createFakePackageJson(distPath: string, packageJson: any) {
 | 
			
		||||
    const finalDependencies = {};
 | 
			
		||||
    for (const dep of nativeDependencies) {
 | 
			
		||||
        finalDependencies[dep] = packageJson.dependencies[dep];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const fakePackageJson: any = {
 | 
			
		||||
        name: "trilium",
 | 
			
		||||
        version: packageJson.version,
 | 
			
		||||
        main: packageJson.main,
 | 
			
		||||
        author: packageJson.author,
 | 
			
		||||
        license: packageJson.license,
 | 
			
		||||
        description: packageJson.description,
 | 
			
		||||
        dependencies: finalDependencies,
 | 
			
		||||
        devDependencies: {
 | 
			
		||||
            "electron": packageJson.devDependencies?.electron || packageJson.dependencies?.electron,
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    if (packageJson?.config?.forge) {
 | 
			
		||||
        fakePackageJson.config = {
 | 
			
		||||
            forge: join("..", packageJson.config.forge)
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    writeFileSync(distPath, JSON.stringify(fakePackageJson, null, 2), "utf-8");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function main() {
 | 
			
		||||
    const distDir = resolve(process.argv[2]);
 | 
			
		||||
    if (!distDir) {
 | 
			
		||||
        console.error("Missing root dir as argument.");
 | 
			
		||||
        process.exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const { electronVersion, packageJson } = parsePackageJson(distDir);
 | 
			
		||||
    const packageJsonPath = join(distDir, "package.json");
 | 
			
		||||
    createFakePackageJson(packageJsonPath, packageJson);
 | 
			
		||||
 | 
			
		||||
    console.log(`Rebuilding ${distDir} with version ${electronVersion}...`);
 | 
			
		||||
 | 
			
		||||
    rebuild({
 | 
			
		||||
        // We force the project root path to avoid electron-rebuild from rebuilding the monorepo-level dependency and breaking the server.
 | 
			
		||||
        projectRootPath: distDir,
 | 
			
		||||
        buildPath: distDir,
 | 
			
		||||
        force: true,
 | 
			
		||||
        electronVersion,
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main();
 | 
			
		||||
		Reference in New Issue
	
	Block a user