Skip to content

Commit

Permalink
refactor: add InfoType enumeration to supplement the missing ts synta…
Browse files Browse the repository at this point in the history
…x type (#4220)

Co-authored-by: 142vip <[email protected]>
  • Loading branch information
mmdapl and 142vip authored Dec 15, 2024
1 parent e1a7b26 commit 378cf39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
26 changes: 13 additions & 13 deletions packages/info/src/infoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
MidwayConfigService,
MidwayEnvironmentService,
} from '@midwayjs/core';
import { InfoValueType, TypeInfo } from './interface';
import { InfoType, InfoValueType, TypeInfo } from './interface';
import { bitToMB, renderToHtml, safeContent, safeRequire } from './utils';
import {
hostname,
Expand All @@ -39,7 +39,7 @@ export class InfoService {
environment: MidwayEnvironmentService;

@Config('info.title')
titleConfig;
titleConfig: string;

@Config('info.hiddenKey')
defaultHiddenKey: string[];
Expand Down Expand Up @@ -93,7 +93,7 @@ export class InfoService {

projectInfo(): TypeInfo {
return {
type: 'Project',
type: InfoType.PROJECT,
info: {
Project: this.midwayInformationService.getProjectName(),
AppDir: this.midwayInformationService.getAppDir(),
Expand All @@ -107,7 +107,7 @@ export class InfoService {
systemInfo(): TypeInfo {
const _platform = process.platform;
return {
type: 'System',
type: InfoType.SYSTEM,
info: {
Platform: _platform === 'win32' ? 'Windows' : _platform,
Node: process.versions.node,
Expand All @@ -126,7 +126,7 @@ export class InfoService {
const memory = process.memoryUsage();
const cpu = cpus();
return {
type: 'Memory & CPU',
type: InfoType.MEMORY_CPU,
info: {
'Memory Total Occupy': bitToMB(memory.rss),
'Heap Total Occupy': bitToMB(memory.heapTotal),
Expand Down Expand Up @@ -171,7 +171,7 @@ export class InfoService {
}
}
return {
type: 'Software',
type: InfoType.SOFTWARE,
info,
};
}
Expand All @@ -182,15 +182,15 @@ export class InfoService {
env[envName] = this.filterSecretContent(envName, process.env[envName]);
});
return {
type: 'Environment Variable',
type: InfoType.ENVIRONMENT_VARIABLE,
info: env,
};
}

timeInfo(): TypeInfo {
const t = new Date().toString().split(' ');
return {
type: 'Time',
type: InfoType.TIME,
info: {
Current: Date.now(),
Uptime: uptime(),
Expand Down Expand Up @@ -229,7 +229,7 @@ export class InfoService {
.join(' / ');
});
return {
type: 'Network',
type: InfoType.NETWORK,
info,
};
}
Expand All @@ -245,7 +245,7 @@ export class InfoService {
})`;
});
return {
type: 'Dependencies',
type: InfoType.DEPENDENCIES,
info,
};
}
Expand All @@ -263,7 +263,7 @@ export class InfoService {
}

return {
type: 'Midway Service',
type: InfoType.MIDWAY_SERVICE,
info,
};
}
Expand All @@ -275,12 +275,12 @@ export class InfoService {
info[key] = this.safeJson(this.filterSecretContent(key, config[key]));
});
return {
type: 'Midway Config',
type: InfoType.MIDWAY_CONFIG,
info,
};
}

protected filterSecretContent(key, value) {
protected filterSecretContent(key: string, value: any) {
if (typeof value === 'string') {
const find = this.secretMatchList.some(isMatch => {
return isMatch(key.toLowerCase());
Expand Down
15 changes: 15 additions & 0 deletions packages/info/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ export interface InfoConfigOptions {
hiddenKey: Array<string>;
ignoreKey: Array<string>
}


export enum InfoType{
PROJECT='Project',
SYSTEM='System',
MEMORY_CPU='Memory & CPU',
SOFTWARE='Software',
ENVIRONMENT_VARIABLE='Environment Variable',
TIME='Time',
NETWORK='Network',
RESOURCE='Resource',
DEPENDENCIES='Dependencies',
MIDWAY_SERVICE='Midway Service',
MIDWAY_CONFIG='Midway Config',
}
2 changes: 1 addition & 1 deletion packages/info/src/middleware/info.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InfoService } from '../infoService';
@Middleware()
export class InfoMiddleware {
@Config('info.infoPath')
protected infoPath;
protected infoPath: string;

@Inject()
protected infoService: InfoService;
Expand Down
2 changes: 1 addition & 1 deletion packages/info/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function bitToMB(bit: number): string {
return Number((bit || 0) / 1024 / 1024).toFixed(2) + ' MB';
}

export function renderToHtml(infoList: TypeInfo[], title): string {
export function renderToHtml(infoList: TypeInfo[], title: string): string {
let html = `<div style="padding: 24px; font-size: 36px;background: #9999cb;font-weight: bold;">${title}</div>`;
html += infoList
.map(info => {
Expand Down

0 comments on commit 378cf39

Please sign in to comment.