@neodx/fs
v0.0.12
Published
File system helpers
Downloads
5,807
Readme
@neodx/fs
File system enhancements and common stuff.
API`s overview:
scan(path, ['*.js', '*.module.ts', '!*.test.{ts,js}'])
- glob reader with multiple inputs supportensureDir(path)
,ensureFile(path)
- Safe file/dir creation with all required ancestorsexists(path)
,isFile(path)
,isDirectory(path)
- Safe path checksdeepReadDir(path)
- Deep version ofreaddir
parseJson(input)
,serializeJson(input)
- Safe JSON parser and serializer with JSONC supportgetFileHash(path)
,getHash(content)
- Returns file hash based on its content (HEX)
API
scan
Glob-based (via tiny-glob) multiple patterns scanner with exclusion support
import { scan } from '@neodx/fs';
await scan(process.cwd(), ['*.js', '!*.config.js']);
await scan(process.cwd(), '**/*.ts', '**/*.js');
ensureFile(path)
and ensureDir(path)
Recursively creates missed file or dir with all required ancestors if one of them is not exists.
Automatically avoid duplicated calls:
import { ensureFile, ensureDir } from '@neodx/fs';
// Everything works as expected
await Promise.all([
ensureDir('foo/baz'),
ensureFile('foo/bar/2.ts'),
ensureDir('foo/bar'),
ensureFile('foo/bar/1.ts'),
ensureDir('foo')
]);
isFile
, isDirectory
, exists
The following APIs are useful for safe paths checking.
exists(path)
- Returnstrue
if the path existsisFile(path)
- Returnstrue
if the path exists, and it's a fileisDirectory(path)
- Returnstrue
if the path exists, and it's a directory
deepReadDir(path, { absolute = false })
Returns flat list with all children's paths. Paths are relative by default.
import { deepReadDir, isFile } from '@neodx/fs';
const files = await deepReadDir(myPath);
for (const file of files) {
if (await isFile(file)) {
await doSmth(file);
}
}
parseJson
and serializeJson
import { parseJson, serializeJson } from '@neodx/fs';
import { writeFile, readFile } from 'fs/promises';
const json = parseJson(await readFile('tsconfig.json', 'utf-8'));
await writeFile(
'tsconfig.json',
serializeJson({
...json,
compilerOptions: {
...json.compilerOptions,
target: 'esnext'
}
}),
'utf-8'
);
Inspired by fs-extra and others.