@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
159 lines (157 loc) • 4.58 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_get_parent_path = require('./get-parent-path.cjs');
let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
let _stryke_path_cwd = require("@stryke/path/cwd");
let _storm_software_config_tools = require("@storm-software/config-tools");
let _stryke_path_is_root_dir = require("@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 = (0, _stryke_path_cwd.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 = (0, _storm_software_config_tools.findWorkspaceRootSafe)(dir);
if (root) return root;
let result = require_get_parent_path.getParentPath(WORKSPACE_ROOT_CONTENT, dir);
if (result) return result;
result = dir;
while (result && !(0, _stryke_path_is_root_dir.isSystemRoot)(result)) {
result = require_get_parent_path.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 = (0, _stryke_path_cwd.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 = (0, _stryke_path_cwd.cwd)()) {
const result = require_get_parent_path.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 = (0, _stryke_path_cwd.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 (0, _stryke_path_file_path_fns.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 (0, _stryke_path_file_path_fns.relativePath)(filePath, getProjectRoot());
}
//#endregion
exports.PROJECT_ROOT_CONTENT = PROJECT_ROOT_CONTENT;
exports.WORKSPACE_ROOT_CONTENT = WORKSPACE_ROOT_CONTENT;
exports.getProjectRoot = getProjectRoot;
exports.getWorkspaceRoot = getWorkspaceRoot;
exports.isProjectRoot = isProjectRoot;
exports.isWorkspaceRoot = isWorkspaceRoot;
exports.relativeToProjectRoot = relativeToProjectRoot;
exports.relativeToWorkspaceRoot = relativeToWorkspaceRoot;