From be00065aed56c80557da3b821c08a11ab75b637c Mon Sep 17 00:00:00 2001 From: monosans Date: Fri, 30 Jun 2023 18:39:18 +0300 Subject: [PATCH] Change logic of setting current working directory Fixes #128 --- package.json | 2 +- src/args.ts | 8 +++++++- src/runner.ts | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 39c73df..3e2d951 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "djlint.configuration": { "type": "string", "default": "", - "description": "Path to global configuration file in .djlintrc format. Requires djLint ≥ 1.13." + "description": "Path to global configuration file in .djlintrc format. The path can be relative to the workspace root. Requires djLint ≥ 1.13." }, "djlint.customBlocks": { "type": "array", diff --git a/src/args.ts b/src/args.ts index a55031f..9fb044d 100644 --- a/src/args.ts +++ b/src/args.ts @@ -117,8 +117,14 @@ class UseEditorIndentationArg extends CliArg { } } +export const configurationArg = new StringArg( + "configuration", + "--configuration", + "1.13" +); + const commonArgs: CliArg[] = [ - new StringArg("configuration", "--configuration", "1.13"), + configurationArg, new StringArrayArg("exclude", "--exclude", "1.25"), new StringArrayArg("extendExclude", "--extend-exclude", "1.25"), new ProfileArg(), diff --git a/src/runner.ts b/src/runner.ts index c370b6a..2b889f4 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,6 +1,6 @@ import { spawn } from "child_process"; import vscode from "vscode"; -import type { CliArg } from "./args"; +import { configurationArg, type CliArg } from "./args"; import { getErrorMsg } from "./errorHandler"; import { getPythonExec } from "./python"; @@ -17,8 +17,17 @@ export async function runDjlint( const childArgs = ["-m", "djlint", "-"].concat( args.flatMap((arg) => arg.build(config, document, formattingOptions)) ); - const cwd = vscode.Uri.joinPath(document.uri, ".."); - const childOptions = { cwd: cwd.fsPath }; + let childOptions; + if (childArgs.includes(configurationArg.cliName)) { + const cwd = vscode.workspace.getWorkspaceFolder(document.uri); + if (cwd) { + childOptions = { cwd: cwd.uri.fsPath }; + } + } + if (!childOptions) { + const cwd = vscode.Uri.joinPath(document.uri, ".."); + childOptions = { cwd: cwd.fsPath }; + } const child = spawn(pythonExec, childArgs, childOptions); child.stdin.write(document.getText()); child.stdin.end();