Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 `<img>` 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 |
Expand Down
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down