@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
151 lines (149 loc) • 3.92 kB
JavaScript
import { getParentPath } from "./get-parent-path.mjs";
import { relativePath } from "@stryke/path/file-path-fns";
import { cwd } from "@stryke/path/cwd";
import { findWorkspaceRootSafe } from "@storm-software/config-tools";
import { isSystemRoot } from "@stryke/path/is-root-dir";
//#region src/get-workspace-root.ts
const WORKSPACE_ROOT_CONTENT = [
".all-contributorsrc",
".commitlintrc",
".github",
".git",
".husky",
".huskyrc",
".lintstagedrc",
".log4brains.yml",
".npmrc",
".nx",
".storm-workspace.js",
".storm-workspace.json",
".storm-workspace.ts",
".storm-workspace.yaml",
".storm-workspace.yml",
".vscode",
".whitesource",
"bun.lock",
"bun.lockb",
"lefthook.yaml",
"lefthook.yml",
"lerna.json",
"npm-lock.json",
"npm-lock.yaml",
"npm-lock.yml",
"npm-workspace.json",
"npm-workspace.yaml",
"npm-workspace.yml",
"nx.json",
"package-lock.json",
"patches",
"pnpm-lock.json",
"pnpm-lock.yaml",
"pnpm-lock.yml",
"pnpm-workspace.json",
"pnpm-workspace.yaml",
"pnpm-workspace.yml",
"socket.yaml",
"storm-workspace.js",
"storm-workspace.json",
"storm-workspace.ts",
"storm-workspace.yaml",
"storm-workspace.yml",
"syncpack.config.js",
"syncpack.json",
"turbo.json",
"yarn-lock.json",
"yarn-lock.yaml",
"yarn-lock.yml",
"yarn-workspace.json",
"yarn-workspace.yaml",
"yarn-workspace.yml",
"yarn.lock"
];
const PROJECT_ROOT_CONTENT = [
".powerlines",
".storm",
"package.json",
"powerlines.json",
"powerlines.yaml",
"powerlines.yml",
"powerlines.toml",
"powerlines.config.js",
"powerlines.config.ts",
"project.json"
];
/**
* Get the workspace root path
*
* @param dir - A directory to start the search from
* @returns The workspace root path
*/
function getWorkspaceRoot(dir = cwd()) {
if (process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH) return process.env.STORM_WORKSPACE_ROOT || process.env.NX_WORKSPACE_ROOT_PATH;
const root = findWorkspaceRootSafe(dir);
if (root) return root;
let result = getParentPath(WORKSPACE_ROOT_CONTENT, dir);
if (result) return result;
result = dir;
while (result && !isSystemRoot(result)) {
result = getParentPath("storm-workspace.json", result, {
skipCwd: true,
includeNameInResults: false
});
if (result) return result;
}
return dir;
}
/**
* Check if the given directory is the workspace root
*
* @param dir - A directory to check
* @returns True if the directory is the workspace root, false otherwise
*/
function isWorkspaceRoot(dir = cwd()) {
const workspaceRoot = getWorkspaceRoot(dir);
if (workspaceRoot) return workspaceRoot === dir;
return false;
}
/**
* Get the project root path
*
* @param dir - A directory to start the search from
* @returns The project root path
*/
function getProjectRoot(dir = cwd()) {
const result = getParentPath(PROJECT_ROOT_CONTENT, dir);
if (result) return result;
return dir;
}
/**
* Check if the given directory is the project root
*
* @param dir - A directory to check
* @returns True if the directory is the project root, false otherwise
*/
function isProjectRoot(dir = cwd()) {
const projectRoot = getProjectRoot(dir);
if (projectRoot) return projectRoot === dir;
return false;
}
/**
* Find the file path relative to the workspace root path.
*
* @param filePath - The file path to process
* @returns The file path relative to the workspace root
*/
function relativeToWorkspaceRoot(filePath) {
return relativePath(filePath, getWorkspaceRoot());
}
/**
* Find the file path relative to the project root path.
*
* @param filePath - The file path to process
* @returns The file path relative to the project root
*/
function relativeToProjectRoot(filePath) {
return relativePath(filePath, getProjectRoot());
}
//#endregion
export { PROJECT_ROOT_CONTENT, WORKSPACE_ROOT_CONTENT, getProjectRoot, getWorkspaceRoot, isProjectRoot, isWorkspaceRoot, relativeToProjectRoot, relativeToWorkspaceRoot };
//# sourceMappingURL=get-workspace-root.mjs.map