CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-utility

A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.

Pending
Overview
Eval results
Files

fs.mddocs/

File System Operations

Asynchronous file system utilities for checking file existence with proper error handling, designed for robust file operations in Node.js applications.

Capabilities

File Existence Check

Asynchronously check if a file exists and return file statistics or false if it doesn't exist.

/**
 * Check if a file exists asynchronously
 * @param file - Path to file to check
 * @returns Promise resolving to Stats object if file exists, false otherwise
 */
function exists(file: string): Promise<Stats | false>;

Usage Examples:

import { exists } from "utility";
import { Stats } from "node:fs";

// Basic file existence check
const fileExists = await exists('./config.json');
if (fileExists) {
  console.log('File exists and has size:', fileExists.size);
  console.log('Last modified:', fileExists.mtime);
} else {
  console.log('File does not exist');
}

// Directory existence check
const dirExists = await exists('./uploads');
if (dirExists && dirExists.isDirectory()) {
  console.log('Directory exists');
} else if (dirExists && dirExists.isFile()) {
  console.log('Path exists but is a file, not directory');
} else {
  console.log('Directory does not exist');
}

// Conditional file operations
async function readConfigIfExists(configPath: string) {
  const stats = await exists(configPath);
  if (stats) {
    console.log(`Config file found (${stats.size} bytes)`);
    const content = await fs.readFile(configPath, 'utf8');
    return JSON.parse(content);
  } else {
    console.log('Config file not found, using defaults');
    return getDefaultConfig();
  }
}

// File validation before processing
async function processFile(filePath: string) {
  const stats = await exists(filePath);
  
  if (!stats) {
    throw new Error(`File not found: ${filePath}`);
  }
  
  if (stats.size === 0) {
    throw new Error(`File is empty: ${filePath}`);
  }
  
  if (stats.size > 10 * 1024 * 1024) { // 10MB limit
    throw new Error(`File too large: ${filePath} (${stats.size} bytes)`);
  }
  
  // File is valid, proceed with processing
  return processValidFile(filePath);
}

// Batch file existence checks
async function checkMultipleFiles(filePaths: string[]) {
  const results = await Promise.all(
    filePaths.map(async (path) => ({
      path,
      exists: await exists(path)
    }))
  );
  
  const existing = results.filter(r => r.exists);
  const missing = results.filter(r => !r.exists);
  
  return { existing, missing };
}

// Error handling (errors other than ENOENT are thrown)
try {
  const stats = await exists('/root/protected-file');
  if (stats) {
    console.log('File exists and accessible');
  }
} catch (error) {
  // Will catch permission errors, not ENOENT
  console.error('Access error:', error.message);
}

Install with Tessl CLI

npx tessl i tessl/npm-utility

docs

array.md

crypto.md

date.md

fs.md

function.md

index.md

json.md

number.md

object.md

optimize.md

string.md

timeout.md

web.md

tile.json