Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taufik-nurrohman/file
File system utility.
https://github.com/taufik-nurrohman/file
file helper node utility
Last synced: 3 months ago
JSON representation
File system utility.
- Host: GitHub
- URL: https://github.com/taufik-nurrohman/file
- Owner: taufik-nurrohman
- License: mit
- Created: 2020-12-18T02:33:09.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-01T13:09:40.000Z (over 2 years ago)
- Last Synced: 2024-09-29T06:08:52.900Z (3 months ago)
- Topics: file, helper, node, utility
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
File Utility
============Utility functions of native file system API in Node.js. Not to be used in the browser.
Usage
-----### CommonJS
~~~ js
const {getContent, setContent} = require('@taufik-nurrohman/file');let content = getContent('./package.json');
if (null !== content) {
setContent('./package.json', content.replace(/"/g, "'"));
}
~~~### ECMAScript
~~~ js
import {getContent, setContent} from '@taufik-nurrohman/file';let content = getContent('./package.json');
if (null !== content) {
setContent('./package.json', content.replace(/"/g, "'"));
}
~~~Methods
-------### copy(from, to, name)
Copy a file.
~~~ js
copy('./package.json', './foo/bar/baz');
copy('./package.json', './foo/bar/baz', 'package.json.bak');
~~~### get(path)
Check if file/folder does exist.
~~~ js
if (false !== get('./package.json')) {
// …
}
~~~### getContent(path)
Get file content as string.
~~~ js
let content = getContent('./package.json');if (null !== content) {
// …
}
~~~### isFile(path)
Check if path is a file.
~~~ js
if (false !== isFile('./foo/bar/baz.qux')) {
// …
}
~~~### move(from, to, name)
Delete or move a file.
~~~ js
// Delete
move('./package.json', false);// Delete
move('./package.json');// Move
move('./package.json', './foo/bar/baz');// Move
move('./package.json', './foo/bar/baz', 'package.json.bak');
~~~### name(path, x = false)
Get file name from file path.
~~~ js
console.log(name('./foo/bar/baz.qux'));
console.log(name('./foo/bar/baz.qux', true));
console.log(name('./foo/bar/baz.qux', 'asdf'));
~~~### parent(path)
Get parent path from file path.
~~~ js
console.log(parent('./foo/bar/baz.qux'));
console.log(parent('./foo/bar/baz'));
console.log(parent('./'));
console.log(parent('.'));
console.log(parent(""));
~~~### parseContent(content, data, pattern = '%\\\\((\\\\S+?)\\\\)', separator = '.')
Convert embedded variables with format such as `%(foo.bar)` in `content` with `data`.
~~~ js
let content = 'foo %(bar) %(baz.qux)';
let data = {
bar: 'bar',
baz: {
qux: 'baz qux'
}
};console.log(parseContent(content, data));
~~~### set(path)
Create an empty file if it does not exist.
~~~ js
set('./foo/bar/baz.qux');
~~~### setContent(path, content)
Create or overwrite a file.
~~~ js
setContent('./foo/bar/baz.qux', 'foo bar baz');
~~~### x(path)
Get file extension from file path.
~~~ js
console.log(x('./foo/bar/baz.qux'));
console.log(x('./foo/bar/baz'));
console.log(x('./foo/bar/baz.'));
console.log(x('./foo/bar/.baz'));
console.log(x('./foo/bar/.baz.qux'));
console.log(x('./'));
console.log(x('.'));
console.log(x(""));
~~~