@bemoje/fs
v2.0.0
Published
File system utilities for reading, updating, and managing files and directories.
Maintainers
Readme
@bemoje/fs
File system utilities for reading, updating, and managing files and directories.
Exports
- deleteOlderThan: Delete files older than a given timestamp
- getFileAge: Retrieves the age of a file in milliseconds.
- getFirstFileInDir found in a directory.
- readFileFirstLine: Reads the first line of a file asynchronously.
- removeDataUrlSchemePrefix: Removes the data URL scheme prefix from a given string.
- updateFile: Updates a file by reading its content, applying a transformation function, and writing back the result. Creates the file and directories if they don't exist.
- updateFileLines or an array of lines. Creates the file and directories if they don't exist.
- updateFileLinesSync: Synchronous version of
updateFileLines. - updateFileSync: Synchronous version of
updateFile. - updateJsonFile: Updates a JSON file by applying a transformation function to the parsed content. If the file doesn't exist or can't be parsed, uses the default value. Creates the file and directories if they don't exist.
- updateJsonFileSync: Synchronous version of
updateJsonFile. - walkDirectory: Walk a directory recursively and return an array of paths.
Installation
npm install @bemoje/fsUsage
Walk a Directory
Recursively traverse a directory tree:
import { walkDirectory } from '@bemoje/fs'
// Get all file paths
const files = walkDirectory('./src', { only: 'files' })
// Get paths with stats
const entries = walkDirectory('./src', { stats: true })
entries.forEach(([path, stats]) => {
console.log(path, stats.size)
})
// Filter directories and limit depth
const shallow = walkDirectory('./src', { maxDepth: 2, filter: (dirpath, basename) => basename !== 'node_modules' })Update Files
Read, transform, and write files in one operation:
import { updateFile, updateFileSync } from '@bemoje/fs'
// Async
await updateFile('config.json', (content) => {
return content.replace(/localhost/g, 'production.host')
})
// Sync
updateFileSync('config.json', (content) => {
return content.replace(/localhost/g, 'production.host')
})Update File Lines
Transform files line-by-line:
import { updateFileLines, updateFileLinesSync } from '@bemoje/fs'
await updateFileLines('data.csv', (lines) => {
return lines.filter((line) => line.trim() !== '')
})
updateFileLinesSync('data.csv', (lines) => {
return lines.map((line) => line.toUpperCase())
})Update JSON Files
Read, transform, and write JSON files:
import { updateJsonFile, updateJsonFileSync } from '@bemoje/fs'
await updateJsonFile('package.json', (pkg) => {
pkg.version = '2.0.0'
return pkg
})
updateJsonFileSync('tsconfig.json', (config) => {
config.compilerOptions.strict = true
return config
})File Utilities
import { getFileAge, deleteOlderThan, readFileFirstLine, getFirstFileInDir } from '@bemoje/fs'
// Get file age in milliseconds
const age = await getFileAge('./data.json')
// Delete files older than 24 hours
await deleteOlderThan('./cache', 24 * 60 * 60 * 1000)
// Read only the first line of a file
const header = await readFileFirstLine('./data.csv')
// Get the first file in a directory
const firstFile = await getFirstFileInDir('./uploads')Data URL
import { removeDataUrlSchemePrefix } from '@bemoje/fs'
removeDataUrlSchemePrefix('data:image/png;base64,iVBOR...')
// 'iVBOR...'