Skip to content

Commit

Permalink
refactor(betterer 🔧): try to fix knip
Browse files Browse the repository at this point in the history
  • Loading branch information
phenomnomnominal committed Sep 10, 2024
1 parent 588de2f commit ec7af2f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/betterer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = (value: T) => void;
Expand Down
8 changes: 4 additions & 4 deletions packages/fixture/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;
});
Expand Down Expand Up @@ -78,6 +78,6 @@ function replaceProjectPath(str: string): string {
return str.replace(PROJECT_REGEXP, '<project>');
}

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);
}
2 changes: 1 addition & 1 deletion packages/knip/src/fragile-knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}
9 changes: 4 additions & 5 deletions packages/knip/src/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ec7af2f

Please sign in to comment.