UNPKG

4.18 kBMarkdownView Raw
1# file-system — Simplified file system
2[![NPM](https://nodei.co/npm/file-system.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/file-system/)
3
4This module make file opertaion apis simple, you don't need to care the dir exits. and the api is same as node's filesystem. This is no exists time cost for this plugin.
5```js
6var fs = require('file-system');
7
8fs.mkdir('1/2/3/4/5', [mode], function(err) {});
9fs.mkdirSync('1/2/3/4/5', [mode]);
10fs.writeFile('path/test.txt', 'aaa', function(err) {})
11```
12
13### install
14```
15npm install file-system --save
16```
17
18## API
19### .fs
20file extend node fs origin methods, and overwrite some methods with next list chart
21```js
22var file = require('file-system');
23var fs = require('fs');
24
25file.readFile === fs.readFile // true
26```
27
28### .mkdir
29The api is same as node's mkdir
30
31### .mkdirSync
32The api is same as node's mkdir
33
34### .writeFile
35The api is same as node's writeFile
36
37### .writeFileSync
38The api is same as node's writeFile
39
40### .fileMatch
41The api equal [file-match](https://github.com/douzi8/file-match)
42
43### .copyFile(srcpath, destpath, options)
44Asynchronously copy a file into newpath
45* {string} ``srcpath`` required
46* {string} ``destpath`` required
47* {object} ``options``
48 * {string} ``options.encoding`` [options.encoding=utf8]
49 * {function} ``options.done(err)``
50 * {function} ``options.process(content)``
51 The process argument must return processed content
52```js
53fs.copyFile('deom.png', 'dest/demo.png', {
54 done: function(err) {
55 console.log('done');
56 }
57});
58```
59
60### .copyFileSync(srcpath, destpath, options)
61The api same as copyFile, but it's synchronous
62```js
63fs.copyFileSync('demo.png', 'dest/demo.png');
64fs.copyFileSync('demo.css', 'dest/demo.css', {
65 process: function(contents) {
66 return contents;
67 }
68})
69```
70
71### .recurse(dirpath, filter, callback)
72Recurse into a directory, executing callback for each file and folder.
73if the filename is undefiend, the callback is for folder, otherwise for file.
74* {string} ``dirpath`` required
75* {string|array|function} ``filter``
76If the filter is function, executing callback for all files and folder
77* {function} ``callback(filepath, filename)``
78```js
79fs.recurse('path', function(filepath, filename) { });
80
81fs.recurse('path', [
82 '*.css',
83 '**/*.js',
84 'path/*.html',
85 '!**/path/*.js'
86], function(filepath, filename) {
87 if (filename) {
88 // it's file
89 } else {
90 // it's folder
91 }
92});
93
94// Only using files
95fs.recurse('path', function(filepath, filename) {
96 if (!filename) return;
97});
98```
99[filter params description](https://github.com/douzi8/file-match#filter-description)
100
101### .recurseSync(dirpath, filter, callback)
102The api is same as recurse, but it is synchronous
103```js
104fs.recurseSync('path', function(filepath, filename) {
105
106});
107
108fs.recurseSync('path', ['**/*.js', 'path/**/*.html'], function(filepath, filename) {
109
110});
111```
112
113### .rmdirSync(dirpath)
114Recurse into a directory, remove all of the files and folder in this directory.
115```js
116fs.rmdirSync('path');
117```
118
119### .copySync(dirpath, destpath, options)
120Recurse into a directory, copy all files into dest.
121* {string} ``dirpath`` required
122* {string} ``destpath`` required
123* {object} ``options``
124 * {string|array} ``options.filter``
125 * {function} ``options.process(contents, filepath)``
126 If custom the destpath, return object, otherwise return content
127 * {string|array} ``options.noProcess``
128```js
129fs.copySync('path', 'dest', { clear: true });
130
131fs.copySync('src', 'dest/src');
132
133fs.copySync('src', 'dest/src', { filter: ['*.js', 'path/**/*.css'] });
134
135fs.copySync('path', 'dest', {
136 noProcess: '**/*.{jpg, png}', // Don't process images
137 process: function(contents, filepath) {
138 // only process file content
139 return contents;
140 // or custom destpath
141 return {
142 contents: '',
143 filepath: ''
144 };
145 }
146});
147
148//Handler self files
149fs.copySync('path', 'path', { filter: ['*.html.js'], process: function(contents, filepath) {} });
150```
151
152### .base64
153Deprecated, move to [base64](https://github.com/douzi8/base64-img#base64filename-callback)
154### .base64Sync
155Deprecated, move to [base64Sync](https://github.com/douzi8/base64-img#base64syncfilename)
\No newline at end of file