Home / File/ searchRoot.ts — astro Source File

searchRoot.ts — astro Source File

Architecture documentation for searchRoot.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 2 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  f146dc60_74d2_b1d6_cb66_a8216ff4ea82["searchRoot.ts"]
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  f146dc60_74d2_b1d6_cb66_a8216ff4ea82 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  f146dc60_74d2_b1d6_cb66_a8216ff4ea82 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  style f146dc60_74d2_b1d6_cb66_a8216ff4ea82 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// Taken from: https://github.com/vitejs/vite/blob/1a76300cd16827f0640924fdc21747ce140c35fb/packages/vite/src/node/server/searchRoot.ts
// MIT license
// See https://github.com/vitejs/vite/blob/1a76300cd16827f0640924fdc21747ce140c35fb/LICENSE
import fs from 'node:fs';
import { dirname, join } from 'node:path';

// https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
const ROOT_FILES = [
	// '.git',

	// https://pnpm.io/workspaces/
	'pnpm-workspace.yaml',

	// https://rushjs.io/pages/advanced/config_files/
	// 'rush.json',

	// https://nx.dev/latest/react/getting-started/nx-setup
	// 'workspace.json',
	// 'nx.json',

	// https://github.com/lerna/lerna#lernajson
	'lerna.json',
];

function tryStatSync(file: string): fs.Stats | undefined {
	try {
		// The "throwIfNoEntry" is a performance optimization for cases where the file does not exist
		return fs.statSync(file, {
			throwIfNoEntry: false,
		});
	} catch {
		// Ignore errors
	}
}

function isFileReadable(filename: string): boolean {
	if (!tryStatSync(filename)) {
		return false;
	}

	try {
		// Check if current process has read permission to the file
		fs.accessSync(filename, fs.constants.R_OK);

		return true;
	} catch {
		return false;
	}
}

// npm: https://docs.npmjs.com/cli/v7/using-npm/workspaces#installing-workspaces
// yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
function hasWorkspacePackageJSON(root: string): boolean {
	const path = join(root, 'package.json');
	if (!isFileReadable(path)) {
		return false;
	}
	try {
		const content = JSON.parse(fs.readFileSync(path, 'utf-8')) || {};
		return !!content.workspaces;
	} catch {
		return false;
	}
}

function hasRootFile(root: string): boolean {
	return ROOT_FILES.some((file) => fs.existsSync(join(root, file)));
}

function hasPackageJSON(root: string) {
	const path = join(root, 'package.json');
	return fs.existsSync(path);
}

/**
 * Search up for the nearest `package.json`
 */
function searchForPackageRoot(current: string, root = current): string {
	if (hasPackageJSON(current)) return current;

	const dir = dirname(current);
	// reach the fs root
	if (!dir || dir === current) return root;

	return searchForPackageRoot(dir, root);
}

/**
 * Search up for the nearest workspace root
 */
export function searchForWorkspaceRoot(
	current: string,
	root = searchForPackageRoot(current),
): string {
	if (hasRootFile(current)) return current;
	if (hasWorkspacePackageJSON(current)) return current;

	const dir = dirname(current);
	// reach the fs root
	if (!dir || dir === current) return root;

	return searchForWorkspaceRoot(dir, root);
}

Domain

Subdomains

Dependencies

  • node:fs
  • node:path

Frequently Asked Questions

What does searchRoot.ts do?
searchRoot.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in searchRoot.ts?
searchRoot.ts defines 7 function(s): hasPackageJSON, hasRootFile, hasWorkspacePackageJSON, isFileReadable, searchForPackageRoot, searchForWorkspaceRoot, tryStatSync.
What does searchRoot.ts depend on?
searchRoot.ts imports 2 module(s): node:fs, node:path.
Where is searchRoot.ts in the architecture?
searchRoot.ts is located at packages/integrations/vercel/src/lib/searchRoot.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/vercel/src/lib).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free