Skip to content

Commit

Permalink
feat: allow relative patterns in configPath
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Sep 21, 2022
1 parent 28074a9 commit c4feb94
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
},
"phpstan.configPath": {
"type": "string",
"description": "PHPStan config path"
"description": "PHPStan config path",
"default": "{phpstan.neon,phpstan.neon.dist}"
},
"phpstan.analysedDelay": {
"type": "integer",
Expand Down
17 changes: 8 additions & 9 deletions src/commands/findPHPStanConfigPath.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Ext } from "../extension";
import { findPHPStanConfigPath as find } from "../utils/phpstan";
import { isAbsolute, join } from "path";
import { RelativePattern, workspace } from "vscode";

export default async function findPHPStanConfigPath(ext: Ext) {
const { settings, cwd } = ext;
const configPath = settings.configPath
? isAbsolute(settings.configPath)
? settings.configPath
: join(cwd, settings.configPath)
: await find(ext.cwd);

if (!configPath) throw new Error(`Config path not found.`);
const [configUri] = await workspace.findFiles(
new RelativePattern(cwd, settings.configPath),
null,
1
);
if (!configUri) throw new Error(`Config path not found.`);
const configPath = configUri.fsPath;
ext.log({ tag: "configPath", message: configPath });
return configPath;
}
6 changes: 1 addition & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,7 @@ export class Ext<

if (this.settings.configFileWatcher)
this.fileWatchers.register(
this.settings.configPath ??
new RelativePattern(
getWorkspacePath(),
`{phpstan.neon,phpstan.neon.dist}`
),
new RelativePattern(getWorkspacePath(), this.settings.configPath),
(uri, eventName) => {
if (!this.store.fileWatcher.enabled) return;
const path = sanitizeFsPath(uri.fsPath);
Expand Down
14 changes: 0 additions & 14 deletions src/utils/phpstan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { checkFile } from "./fs";
import { parseNeonFile } from "./neon";
import { resolvePath } from "./path";
import { join } from "path";

export type PHPStanAnalyseResult = {
totals: {
Expand Down Expand Up @@ -49,18 +47,6 @@ export async function parsePHPStanConfigFile(
return normalizePHPStanConfig(config, env.currentWorkingDirectory);
}

export async function findPHPStanConfigPath(
cwd: string
): Promise<string | undefined> {
const baseNames = ["phpstan.neon", "phpstan.neon.dist"];
for (const basename of baseNames) {
const path = join(cwd, basename);
if (await checkFile(path)) {
return path;
}
}
}

export function normalizePHPStanConfig(
config: PHPStanConfig,
cwd: string
Expand Down

0 comments on commit c4feb94

Please sign in to comment.