Skip to content

Commit

Permalink
refactor: use named export for package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Oct 29, 2023
1 parent 0ddde57 commit 8d6865d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 34 deletions.
19 changes: 1 addition & 18 deletions src/info.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
import _packageJson from '../package.json';

interface MyPackageJson {
name: string;
displayName: string;
version: string;
description: string;
author: string;
repository: {
type: string;
url: string;
};
license: string;
bugs: string;
homepage: string;
}

export const packageJson: MyPackageJson = _packageJson;
export { name, displayName, version, description, license, bugs, homepage } from '../package.json';
6 changes: 3 additions & 3 deletions src/plugin/extension-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Messaging } from 'services/messaging';
import { Notification } from 'services/notification';
import { Http } from 'services/http';
import type { GithubRelease } from 'interface';
import { packageJson } from 'info';
import { version, displayName } from 'info';

@Service()
export class ExtensionUpdater {
Expand All @@ -33,7 +33,7 @@ export class ExtensionUpdater {
const response = await this.http.json<GithubRelease>(
'https://api.github.com/repos/EhTagTranslation/EhSyringe/releases/latest',
);
const current = `v${packageJson.version}`;
const current = `v${version}`;
const latest = response.tag_name;
if (typeof latest != 'string' || !latest.startsWith('v')) {
const e = new Error('响应格式错误');
Expand All @@ -45,7 +45,7 @@ export class ExtensionUpdater {
if (latest === current) return false;

this.notification.send({
title: packageJson.displayName,
title: displayName,
message: `发现新的版本 ${latest},点击跳转到下载页面。`,
action: () => openInTab(response.html_url),
});
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/popup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Messaging } from 'services/messaging';
import { DateTime } from 'services/date-time';
import { openInTab } from 'providers/utils';
import type { DownloadStatus } from 'plugin/database-updater';
import { packageJson } from 'info';
import { homepage, version } from 'info';
import type { MessageListener } from 'providers/common/messaging';

import './index.less';
Expand Down Expand Up @@ -348,7 +348,7 @@ export class Popup {
return html`<div id="ehs-main-panel" class="ehs-panel ${state.showSettingPanel ? 'ehs-hide' : ''}">
<div class="header">
<div>
<a href="${packageJson.homepage}" class="monospace minor">v${packageJson.version}</a>
<a href="${homepage}" class="monospace minor">v${version}</a>
</div>
<div class="cushion"></div>
<div>
Expand Down
4 changes: 2 additions & 2 deletions src/providers/web-ext/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Storage, Listener, ListenerId, SyncStorage } from '../common/storage';
import type { JsonValue } from 'type-fest';
import { packageJson } from 'info';
import { name } from 'info';

const listeners = new Map<string, Listener[]>();

Expand Down Expand Up @@ -42,7 +42,7 @@ export const storage: Storage = {
},
};

const mark = `${packageJson.name}.`;
const mark = `${name}.`;
const syncKey = (k: string): string => mark + k;

export const syncStorage: SyncStorage = {
Expand Down
4 changes: 2 additions & 2 deletions src/providers/web-ext/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NotificationInfo } from '../common/notification';
import type { Badge } from 'providers/common/badge';
import { packageJson } from 'info';
import { displayName } from 'info';

export function openInTab(url: string): void {
void browser.tabs.create({
Expand Down Expand Up @@ -38,7 +38,7 @@ export function setBadge(info: Badge): void {
if (typeof chrome.action.setBadgeText == 'function') {
void chrome.action.setBadgeText({ text: info.text });
} else if (typeof chrome.action.setTitle == 'function') {
const extname = packageJson.displayName;
const extname = displayName;
const title = info.text ? `${extname} (${info.text})` : extname;
void chrome.action.setTitle({ title });
}
Expand Down
8 changes: 4 additions & 4 deletions src/services/sync-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { syncStorage } from 'providers/storage';
import { Logger } from './logger';
import type { JsonValue } from 'type-fest';
import { type ConfigData, Storage } from './storage';
import { packageJson } from 'info';
import { version } from 'info';

export interface SyncStorageItems {
version: string;
Expand All @@ -19,9 +19,9 @@ export class SyncStorage {
readonly async: Storage,
) {
const oldVer = this.get('version');
if (packageJson.version !== oldVer) {
if (version !== oldVer) {
this.migrate();
this.set('version', packageJson.version);
this.set('version', version);
}
}

Expand Down Expand Up @@ -52,7 +52,7 @@ export class SyncStorage {
}

readonly defaults: Readonly<SyncStorageItems> = {
version: packageJson.version,
version: version,
databaseMap: undefined,
databaseSha: undefined,
config: this.async.defaults.config,
Expand Down
4 changes: 2 additions & 2 deletions src/user-script/popup-host.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container } from 'services';
import { isHathNetwork } from 'utils/hosts';
import { Popup } from 'plugin/popup';
import { packageJson } from 'info';
import { displayName } from 'info';
import { setBadge } from 'providers/utils';

import './popup-host.less';
Expand Down Expand Up @@ -113,7 +113,7 @@ export function createPopup(): void {
}
const button = document.body.appendChild(document.createElement('div'));
button.id = 'eh-syringe-popup-button';
button.title = packageJson.displayName;
button.title = displayName;
const badge = button.appendChild(document.createElement('div'));
badge.id = 'eh-syringe-popup-badge';
setBadge({ text: '' });
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const WebExtensionPlugin = _WebExtensionPlugin.default;

const __dirname = path.resolve(fileURLToPath(import.meta.url), '../');

/** @type { import('./src/info').packageJson & import('type-fest').PackageJson } */
/** @type { import('./src/info') & import('type-fest').PackageJson } */
const pkgJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json')));
const manifestJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './manifest.json')));

Expand Down

0 comments on commit 8d6865d

Please sign in to comment.