Skip to content

Commit 28074a9

Browse files
committed
refactor: show tag on log messages
1 parent f2453e2 commit 28074a9

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/commands/findPHPStanConfigPath.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { findPHPStanConfigPath as find } from "../utils/phpstan";
33
import { isAbsolute, join } from "path";
44

55
export default async function findPHPStanConfigPath(ext: Ext) {
6-
const { settings, outputChannel, cwd: rootPath } = ext;
6+
const { settings, cwd } = ext;
77
const configPath = settings.configPath
88
? isAbsolute(settings.configPath)
99
? settings.configPath
10-
: join(rootPath, settings.configPath)
10+
: join(cwd, settings.configPath)
1111
: await find(ext.cwd);
1212

1313
if (!configPath) throw new Error(`Config path not found.`);
14-
outputChannel.appendLine(`# Config path: ${configPath}`);
14+
ext.log({ tag: "configPath", message: configPath });
1515
return configPath;
1616
}

src/commands/loadPHPStanConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export default async function loadPHPStanConfig(ext: Ext) {
88
currentWorkingDirectory: ext.cwd,
99
rootDir: normalize(dirname(join(ext.cwd, ext.settings.path))),
1010
});
11-
ext.outputChannel.appendLine(`# Config:\n${JSON.stringify(config, null, 2)}`);
11+
ext.log({ tag: "config", message: JSON.stringify(config, null, 2) });
1212
return config;
1313
}

src/extension.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,18 @@ export class Ext<
102102
}
103103
}
104104

105-
log(data: string | Buffer | Error) {
105+
log(data: { tag: string; message: string } | string | Buffer | Error) {
106106
if (typeof data === "string") {
107107
this.outputChannel.appendLine(data);
108108
} else if (data instanceof Error) {
109-
this.outputChannel.appendLine(
110-
`# [error] ${data.stack ?? data.message ?? data}`
111-
);
112-
} else {
109+
this.log({
110+
tag: "error",
111+
message: `${data.stack ?? data.message ?? data}`,
112+
});
113+
} else if (Buffer.isBuffer(data)) {
113114
this.outputChannel.appendLine(uncolorize(data.toString()));
115+
} else {
116+
this.outputChannel.appendLine(`# [${data.tag}] ${data.message}`);
114117
}
115118
}
116119

@@ -124,7 +127,7 @@ export class Ext<
124127

125128
async call(cb: () => unknown, name = getFunctionName(cb)) {
126129
try {
127-
this.log(`[call:${name}]`);
130+
this.log({ tag: "call", message: name });
128131
return await cb();
129132
} catch (error) {
130133
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -197,11 +200,17 @@ export class Ext<
197200
if (this.settings.configFileWatcher)
198201
this.fileWatchers.register(
199202
this.settings.configPath ??
200-
new RelativePattern(this.cwd, `{phpstan.neon,phpstan.neon.dist}`),
203+
new RelativePattern(
204+
getWorkspacePath(),
205+
`{phpstan.neon,phpstan.neon.dist}`
206+
),
201207
(uri, eventName) => {
202208
if (!this.store.fileWatcher.enabled) return;
203209
const path = sanitizeFsPath(uri.fsPath);
204-
this.log(`[event:${eventName}] ${path}`);
210+
this.log({
211+
tag: `event:${eventName}`,
212+
message: path,
213+
});
205214
this.store.reactivate.timeout(() => this.reactivate());
206215
}
207216
);
@@ -214,7 +223,10 @@ export class Ext<
214223
for (const patternPath of config.parameters?.paths || []) {
215224
const path = sanitizeFsPath(uri.fsPath);
216225
if (path.startsWith(patternPath)) {
217-
this.log(`[event:${eventName}] ${path}`);
226+
this.log({
227+
tag: `event:${eventName}`,
228+
message: path,
229+
});
218230
return await this.options.commands.analyse(this);
219231
}
220232
}

0 commit comments

Comments
 (0)