Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@kwooshung/files
Advanced tools
Readme
normalized
, so you don't have to worry about the format of the paths.tree-shaking
as an esm
module, so you don't need to worry about the size after packaging.commonjs
(cjs
) version available for those using the commonjs
specification.fs/promises
.hound
, which relies on FileHound for file searching. If you don't need it, you can consider it as 0
third-party dependencies.npm install @kwooshung/files
yarn add @kwooshung/files
pnpm add @kwooshung/files
import { normalize } from '@kwooshung/files';
normalize('a/b/c'); // a/b/c
normalize('a//b/////c'); // a/b/c
normalize('a\\b\\c'); // a/b/c
import { exists } from '@kwooshung/files';
async () => {
await exists('a/b/c'); // true
};
import { notExists } from '@kwooshung/files';
async () => {
await notExists('a/b/c/aNotExists'); // true
};
import { isFile } from '@kwooshung/files';
async () => {
await isFile('a/b/c/test.txt'); // true
};
import { isDir } from '@kwooshung/files';
async () => {
await isDir('a/b/c'); // true
};
import { makeDir } from '@kwooshung/files';
async () => {
await makeDir('a/b/c'); // Create a single directory
await makeDir(['a/b/c/1', 'd/e/f/2']); // Create multiple directories
};
import { getDir } from '@kwooshung/files';
async () => {
await getDir('a/b/c'); // Returns an array of file or directory paths
};
import { read } from '@kwooshung/files';
async () => {
await read('a/b/c/test.txt'); // Returns the file content, the encoding is utf8 by default
await read('a/b/c/test.txt', 'utf8'); // Returns the file content, specifying the encoding
};
{ append: false, overwrite: true, encoding: 'utf8' }
ts
, then the types of the third parameter append
and overwrite
are mutually exclusive, i.e.: when append
is true
, overwrite
is false
, and vice versa.import { write } from '@kwooshung/files';
async () => {
await write('a/b/c/test.txt', 'hello world'); // true
await write('a/b/c/test.txt', 'hello world', {
append: true, // Append
overwrite: false, // Not Overwrite
encoding: 'utf8' // Specify encoding
}); // true
await write('a/b/c/test.txt', 'hello world', {
append: false, // Not Append
overwrite: true // Overwrite, if the file does not exist, it will be created
encoding: 'utf8' // Specify encoding
}); // true
await write('a/b/c/test.txt', 'hello world', {
append: false, // Not Append
overwrite: true // Overwrite, if the file does not exist, it will be created
}); // true
await write('a/b/c/test.txt', 'hello world', {
encoding: 'binary' // Specify encoding
}); // true
};
As long as no exception is thrown, the copy is successful, even if the target file already exists.
import { copyFile } from '@kwooshung/files';
async () => {
await copyFile('a/b/c/1.txt', 'd/e/f/2.txt'); // 表示覆盖目标文件
await copyFile('a/b/c/1.txt', 'd/e/f/2.txt', false); // 即使目标文件已存在,虽然也不会覆盖,但是表示执行成功
};
false
as the third argument to prevent overwriting.import { copyDir } from '@kwooshung/files';
async () => {
await copyDir('a/b/c', 'd/e/f');
await copyDir('a/b/c', 'd/e/f', false);
};
Moving a file essentially copies the file and then deletes the source file. This approach ensures that if the move fails, the source file remains intact. It's a combination of copy/file
and remove
.
import { moveFile } from '@kwooshung/files';
async () => {
await moveFile('a/b/c/1.txt', 'd/e/f/2.txt'); // 表示覆盖目标文件
await moveFile('a/b/c/1.txt', 'd/e/f/2.txt', false); // 即使目标文件已存在,虽然也不会覆盖,但是表示执行成功
};
Moving a directory combines the functionalities of copy/dir
and remove
, thus it also follows the merging logic of copy/dir
.
import { moveDir } from '@kwooshung/files';
async () => {
await moveDir('a/b/c', 'd/e/f');
await moveDir('a/b/c', 'd/e/f', false);
};
import { remove } from '@kwooshung/files';
async () => {
await remove('a/b/c'); // Delete a single file or directory
await remove(['a/b/c/1', 'd/e/f/2']); // Delete multiple files or directories
};
This will delete all empty directories under the specified path, including the path itself.
import { removeEmptyDirs } from '@kwooshung/files';
async () => {
await removeEmptyDirs('a/b/c'); // Delete a single file or directory
await removeEmptyDirs(['a/b/c/1', 'd/e/f/2']); // Delete multiple files or directories
};
import { size } from '@kwooshung/files';
async () => {
await size('a/b/c'); // [{path: 'a/b/c', size: 1200;}]
await size(['a/b/c', 'd/e/f']); // [{path: 'a/b/c', size: 1200;}, {path: 'd/e/f', size: 2024;}]
};
import { sizeHuman } from '@kwooshung/files';
sizeHuman(1024); // {size: '1.25', unit: 'KB'}
sizeHuman(1024, 3); // {size: '1.234', unit: 'KB'}
recommended to use with size
import { sizeUnit } from '@kwooshung/files';
async () => {
await sizeUnit({path: 'a/b/c'; size: 1024;}); // [{path: 'a/b/c', size: '1200', unit: {size: '1.2', unit: 'KB'}}]
await sizeUnit([{path: 'a/b/c'; size: 1024;}, {path: 'd/e/f'; size: 2024;}]); // [{path: 'a/b/c', size: '1200', unit: {size: '1.2', unit: 'KB'}}, {path: 'd/e/f', size: '2024', unit: {size: '2.02', unit: 'KB'}}]
};
FileHound.any
, which means: multiple FileHound
instances can be passed in, and the results will be merged;import { hound } from '@kwooshung/files';
const fh1 = hound();
fh1
.modified("< 2 days")
.find()
.each(console.log);
const fh2 = FileHound.create();
filehound
.addFilter(customFilter)
.find()
.each(console.log);
hound(fh1, fh2);
FAQs
A convenient and efficient library for file operations.
The npm package @kwooshung/files receives a total of 18 weekly downloads. As such, @kwooshung/files popularity was classified as not popular.
We found that @kwooshung/files demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.