Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">=24.0.0"
},
"bin": {
"rust-coverage-check": "./src/main.mjs"
"rust-coverage-check": "src/main.mjs"
},
"exports": {
".": "./src/index.mjs"
Expand Down
34 changes: 30 additions & 4 deletions src/workspace-relative-crate.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import { relative, sep } from "node:path";
import { posix, win32 } from "node:path";

/**
* @param {string} workspaceRoot
* @returns {typeof posix | typeof win32}
*/
function pathModuleFor(workspaceRoot) {
Comment thread
mcharytoniuk marked this conversation as resolved.
return posix.isAbsolute(workspaceRoot) ? posix : win32;
Comment thread
mcharytoniuk marked this conversation as resolved.
Comment thread
mcharytoniuk marked this conversation as resolved.
}

/**
* @param {string} filename
* @param {string} workspaceRoot
* @returns {string | null}
*/
export function workspaceRelativeCrate(filename, workspaceRoot) {
if (!filename.startsWith(workspaceRoot + sep)) {
if (!posix.isAbsolute(workspaceRoot) && !win32.isAbsolute(workspaceRoot)) {
throw new Error(
`workspaceRoot must be an absolute path, got ${workspaceRoot}`,
);
}

const pathModule = pathModuleFor(workspaceRoot);
Comment thread
mcharytoniuk marked this conversation as resolved.

if (!pathModule.isAbsolute(filename)) {
return null;
}

const relativePath = relative(workspaceRoot, filename);
const relativePath = pathModule.relative(workspaceRoot, filename);

if (pathModule.isAbsolute(relativePath)) {
return null;
}

const [firstSegment] = relativePath.split(pathModule.sep);

if (firstSegment === "" || firstSegment === "..") {
return null;
}

return relativePath.split(sep)[0];
return firstSegment;
}
Loading