From ec7af2f463120310fd717b57f79dbafa5977371a Mon Sep 17 00:00:00 2001 From: Craig Spence Date: Wed, 11 Sep 2024 00:44:20 +1200 Subject: [PATCH] =?UTF-8?q?refactor(betterer=20=F0=9F=94=A7):=20try=20to?= =?UTF-8?q?=20fix=20knip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/betterer/src/utils.ts | 2 +- packages/fixture/src/logging.ts | 8 ++++---- packages/knip/src/fragile-knip.ts | 2 +- packages/knip/src/knip.ts | 9 ++++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/betterer/src/utils.ts b/packages/betterer/src/utils.ts index 93dec7e90..c637a9789 100644 --- a/packages/betterer/src/utils.ts +++ b/packages/betterer/src/utils.ts @@ -26,7 +26,7 @@ export function isUndefined(value: unknown): value is undefined { } export function normalisedPath(filePath: string): string { - return path.sep === path.posix.sep ? filePath : filePath.split(path.sep).join(path.posix.sep); + return filePath.split(path.sep).join(path.posix.sep); } type Resolve = (value: T) => void; diff --git a/packages/fixture/src/logging.ts b/packages/fixture/src/logging.ts index da96c6728..ea94824ec 100644 --- a/packages/fixture/src/logging.ts +++ b/packages/fixture/src/logging.ts @@ -6,7 +6,7 @@ import { getStdOutΔ } from '@betterer/render'; import ansiRegex from 'ansi-regex'; const ANSI_REGEX = ansiRegex(); -const PROJECT_REGEXP = new RegExp(normalisePaths(process.cwd()), 'g'); +const PROJECT_REGEXP = new RegExp(normalisedPath(process.cwd()), 'g'); const STACK_TRACK_LINE_REGEXP = /^\s+at\s+/; const FIXTURE_LOGS_MAP: FixtureLogsMap = {}; @@ -28,7 +28,7 @@ export function createFixtureLogs(fixtureName: string, options: FixtureOptions = const lines = message.replace(/\r/g, '').split('\n'); const filteredLines = lines.filter((line) => !isStackTraceLine(line)); const formattedLines = filteredLines.map((line) => { - line = replaceProjectPath(normalisePaths(line)); + line = replaceProjectPath(normalisedPath(line)); line = line.trimEnd(); return line; }); @@ -78,6 +78,6 @@ function replaceProjectPath(str: string): string { return str.replace(PROJECT_REGEXP, ''); } -function normalisePaths(str: string): string { - return str.split(path.win32.sep).join(path.posix.sep); +function normalisedPath(str: string): string { + return str.split(path.sep).join(path.posix.sep); } diff --git a/packages/knip/src/fragile-knip.ts b/packages/knip/src/fragile-knip.ts index 3524cab1a..c9c89d6df 100644 --- a/packages/knip/src/fragile-knip.ts +++ b/packages/knip/src/fragile-knip.ts @@ -22,7 +22,7 @@ export function toArgs(options: KnipCLIOptions) { if (result === false) { return `--no-${param}`; } - return `--${kebabCase(name)}=${String(result)}`; + return `--${kebabCase(name)}="${String(result)}"`; }) .join(' '); } diff --git a/packages/knip/src/knip.ts b/packages/knip/src/knip.ts index 2de756355..ca5a33712 100644 --- a/packages/knip/src/knip.ts +++ b/packages/knip/src/knip.ts @@ -36,7 +36,7 @@ const asyncExec = promisify(exec); * * @param configFilePath - The relative path to a knip.json file. * @param extraConfiguration - Additional {@link https://knip.dev/overview/configuration | **Knip** configuration } - * to enable. This will be merge with the existing configuration in the config file, + * to enable. This will be merged with the existing configuration in the config file, * overwriting any existing existing properties. * @param extraCLIOptions - Additional CLI flags to enable. * @@ -73,23 +73,22 @@ export function knip( const { dir, base } = path.parse(absoluteConfigFilePath); - let stdout: string; - const tmpJSONPath = await resolver.tmp(base); const absoluteTmpJSONPath = resolver.resolve(tmpJSONPath); const relativeTmpJSONPath = path.relative(dir, absoluteTmpJSONPath); + let stdout: string; + try { const finalConfig = { ...config, ...extraConfiguration }; await fs.writeFile(absoluteTmpJSONPath, JSON.stringify(finalConfig), 'utf-8'); const finalArgs = toArgs({ ...extraCliOptions, - directory: dir, config: relativeTmpJSONPath, noExitCode: true, reporter: ['json'] }); - const buffers = await asyncExec(`${binPath} ${finalArgs}`); + const buffers = await asyncExec(`node "${binPath}" ${finalArgs}`, { cwd: dir }); stdout = buffers.stdout; } catch (error) { throw new BettererError(`Couldn't run knip. ❌`, error as Error);