Skip to content

Universal DevTools Kit SDK


Universal DevTools Kit SDK / @u-devtools/utils-node / FileSystemService

Class: FileSystemService ​

Defined in: file-system.ts:35

File system service for safe file operations within a project root.

Example ​

typescript
import { FileSystemService } from '@u-devtools/utils-node';

const fs = new FileSystemService('/path/to/project');

// Read file
const content = await fs.read('path/to/file.txt');

// Read JSON
const data = await fs.readJson('path/to/data.json');

// Write file
await fs.write('path/to/file.txt', content);

// Write JSON
await fs.writeJson('path/to/data.json', data, 2); // 2 = indentation

// Read directory
const entries = await fs.readdir('path/to/dir', { withFileTypes: true });

// Create directory
await fs.mkdir('path/to/dir', true); // true = recursive

Constructors ​

Constructor ​

new FileSystemService(root): FileSystemService

Defined in: file-system.ts:36

Parameters ​

ParameterType
rootstring

Returns ​

FileSystemService

Methods ​

access() ​

access(filePath): Promise<void>

Defined in: file-system.ts:116

Проверка доступа к файлу (аналог fs.access). Выбрасывает ошибку, если файл недоступен.

Parameters ​

ParameterType
filePathstring

Returns ​

Promise<void>


exists() ​

exists(filePath): boolean

Defined in: file-system.ts:103

Проверка существования файла/директории.

Parameters ​

ParameterType
filePathstring

Returns ​

boolean


mkdir() ​

mkdir(dirPath, recursive): Promise<void>

Defined in: file-system.ts:129

Создание директории (рекурсивно).

Parameters ​

ParameterTypeDefault value
dirPathstringundefined
recursivebooleantrue

Returns ​

Promise<void>

Example ​

typescript
await fs.mkdir('path/to/dir', true); // true = recursive

read() ​

read(filePath): Promise<string>

Defined in: file-system.ts:46

Безопасное чтение файла. Выбрасывает ошибку, если путь выходит за пределы root.

Parameters ​

ParameterType
filePathstring

Returns ​

Promise<string>

Example ​

typescript
const content = await fs.read('path/to/file.txt');

readdir() ​

readdir(dirPath, options?): Promise<string[] | Dirent<string>[]>

Defined in: file-system.ts:142

Чтение директории.

Parameters ​

ParameterType
dirPathstring
options?{ withFileTypes?: boolean; }
options.withFileTypes?boolean

Returns ​

Promise<string[] | Dirent<string>[]>

Example ​

typescript
const entries = await fs.readdir('path/to/dir', { withFileTypes: true });

readJson() ​

readJson<T>(filePath): Promise<T | null>

Defined in: file-system.ts:59

Безопасное чтение JSON.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterType
filePathstring

Returns ​

Promise<T | null>

Example ​

typescript
const data = await fs.readJson('path/to/data.json');

remove() ​

remove(filePath): Promise<void>

Defined in: file-system.ts:153

Удаление файла или директории.

Parameters ​

ParameterType
filePathstring

Returns ​

Promise<void>


stat() ​

stat(filePath): Promise<Stats>

Defined in: file-system.ts:167

Получение информации о файле/директории.

Parameters ​

ParameterType
filePathstring

Returns ​

Promise<Stats>


write() ​

write(filePath, content): Promise<void>

Defined in: file-system.ts:76

Безопасная запись файла. Автоматически создает директории.

Parameters ​

ParameterType
filePathstring
contentstring

Returns ​

Promise<void>

Example ​

typescript
await fs.write('path/to/file.txt', content);

writeJson() ​

writeJson(filePath, data, indent): Promise<void>

Defined in: file-system.ts:95

Безопасная запись JSON файла. Автоматически создает директории.

Parameters ​

ParameterTypeDefault value
filePathstringundefined
dataunknownundefined
indentnumber2

Returns ​

Promise<void>

Example ​

typescript
await fs.writeJson('path/to/data.json', data, 2); // 2 = indentation

Released under the MIT License.