diff --git a/README.md b/README.md index 7bd79e9..287ffbe 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ The function can be called from both main process and renderer process. export default function openAboutWindow(info: { icon_path: string; product_name?: string; + package_json?: PackageJson; package_json_dir?: string; about_page_dir?: string; bug_report_url?: string; @@ -82,6 +83,7 @@ $ npm run debug | Name | Description | Type | |------|-------------|------| | `icon_path` | Path to icon file of the application. The path is passed to `src` property of `` element. **Required** | string | +| `package_json` | the package.json file, if it is present, `package_json_dir` property will be ignored. **Optional** | string | | `package_json_dir` | Path to directory which contains package.json. If not specified, it will try to detect a path to package.json. If also failed, it gives up and show less information in 'About This App' window. **Optional** | string | | `bug_report_url` | URL to bug report page. If not specified, 'bugs' entry in package.json is used. **Optional** | string | | `copyright` | Copyright notice shown in window. If not specified, it is replaced with license description generated by 'license' entry of package.json. **Optional** | string | diff --git a/src/index.ts b/src/index.ts index 3eaa37a..027a446 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,6 +23,8 @@ export interface AboutWindowInfo { copyright?: string; homepage?: string; description?: string; + // if this param is present, we ignore package_json_dir param. + package_json?: PackageJson; package_json_dir?: string; about_page_dir?: string; license?: string; @@ -48,8 +50,12 @@ declare namespace NodeJS { function loadPackageJson(pkg_path: string): PackageJson { try { + // some build systems(vite, rollup) may not be able to transform this line into a valid es module statement. + // in this case, even the user pass in the right folder, it will throw an error when load the json. + // even worse, the error will be caught and handled here, which make it hard to debug. return require(pkg_path); } catch (e) { + console.warn(e); return null; } } @@ -91,8 +97,13 @@ function detectPackageJson(specified_dir: string, app: Electron.App) { } function injectInfoFromPackageJson(info: AboutWindowInfo, app: Electron.App) { - const pkg = detectPackageJson(info.package_json_dir, app); - if (pkg === null) { + let pkg: PackageJson | null | undefined; + if (info.package_json) { + pkg = info.package_json; + } else { + pkg = detectPackageJson(info.package_json_dir, app); + } + if (pkg == null) { // Note: Give up. return info; }