file-system
Advanced tools
Comparing version 1.2.9 to 2.0.0
298
index.js
@@ -29,7 +29,10 @@ /** | ||
function getDirs(filepath) { | ||
filepath = filepath.replace(/\/$/, '').replace(/\\$/, ''); | ||
filepath = util.path.unixifyPath(filepath); | ||
return filepath.split('/'); | ||
function getExists(filepath) { | ||
var exists = fs.existsSync(filepath); | ||
if (exists) { | ||
return filepath; | ||
} else { | ||
return getExists(path.dirname(filepath)); | ||
} | ||
} | ||
@@ -49,4 +52,3 @@ | ||
* @description | ||
* Create dir, if dir don't exists, it will not throw error. | ||
* And will mkdir for path, it is asynchronous. | ||
* Create dir, if dir exist, it will only invoke callback. | ||
* | ||
@@ -56,8 +58,8 @@ * @example | ||
* fs.mkdir('1/2/3/4/5', 511); | ||
* fs.mkdir('path/2/3', function(err) {}); | ||
* fs.mkdir('path/2/3', function() {}); | ||
* ``` | ||
*/ | ||
exports.mkdir = function(filepath, mode, callback) { | ||
var dirs = getDirs(filepath); | ||
var length = dirs.length; | ||
var root = getExists(filepath); | ||
var children = path.relative(root, filepath); | ||
@@ -75,27 +77,13 @@ if (util.isFunction(mode)) { | ||
while(length--) { | ||
exists = fs.existsSync(filepath); | ||
if (exists) { | ||
break; | ||
} else { | ||
item = dirs[length]; | ||
last = filepath.lastIndexOf(item); | ||
filepath = filepath.slice(0, last); | ||
} | ||
} | ||
if (!children) return callback(); | ||
dirs = dirs.slice(length + 1); | ||
children = children.split(path.sep); | ||
function create(filepath) { | ||
if (create.count == dirs.length) { | ||
var err; | ||
if (!create.count) { | ||
err = new Error("EEXIST mkdir '" + filepath + "'"); | ||
} | ||
return callback(err); | ||
if (create.count === children.length) { | ||
return callback(); | ||
} | ||
filepath = path.join(filepath, dirs[create.count]); | ||
filepath = path.join(filepath, children[create.count]); | ||
fs.mkdir(filepath, mode, function(err) { | ||
@@ -107,4 +95,4 @@ create.count++; | ||
create.count = 0; | ||
create(filepath); | ||
create.count = 0; | ||
create(root); | ||
}; | ||
@@ -117,20 +105,12 @@ | ||
exports.mkdirSync = function(filepath, mode) { | ||
var dirs = getDirs(filepath); | ||
var length = dirs.length; | ||
var item, last, exists; | ||
var root = getExists(filepath); | ||
var children = path.relative(root, filepath); | ||
while(length--) { | ||
exists = fs.existsSync(filepath); | ||
if (exists) { | ||
break; | ||
} else { | ||
item = dirs[length]; | ||
last = filepath.lastIndexOf(item); | ||
filepath = filepath.slice(0, last); | ||
} | ||
} | ||
if (!children) return; | ||
dirs.slice(length + 1).forEach(function(item) { | ||
filepath = path.join(filepath, item); | ||
fs.mkdirSync(filepath, mode); | ||
children = children.split(path.sep); | ||
children.forEach(function(item) { | ||
root = path.join(root, item); | ||
fs.mkdirSync(root, mode); | ||
}); | ||
@@ -175,2 +155,54 @@ }; | ||
* @description | ||
* Asynchronously copy a file | ||
* @example | ||
* file.copyFile('demo.txt', 'demo.dest.txt', { done: function() { }}) | ||
*/ | ||
exports.copyFile = function(srcpath, destpath, options) { | ||
options = util.extend({ | ||
encoding: 'utf8', | ||
done: util.noop | ||
}, options || {}); | ||
if (!options.process) { | ||
options.encoding = null; | ||
} | ||
fs.readFile(srcpath, { | ||
encoding: null | ||
}, function(err, contents) { | ||
if (err) return options.done(err); | ||
if (options.process) { | ||
contents = options.process(contents); | ||
} | ||
exports.writeFile(destpath, contents, options, options.done); | ||
}); | ||
}; | ||
/** | ||
* @description | ||
* Copy file to dest, if no process options, it will only copy file to dest | ||
* @example | ||
* file.copyFileSync('demo.txt', 'demo.dest.txt' { process: function(contents) { }}); | ||
* file.copyFileSync('demo.png', 'dest.png'); | ||
*/ | ||
exports.copyFileSync = function(srcpath, destpath, options) { | ||
options = util.extend({ | ||
encoding: 'utf8' | ||
}, options || {}); | ||
var contents; | ||
if (options.process) { | ||
contents = fs.readFileSync(srcpath, options); | ||
contents = options.process(contents); | ||
exports.writeFileSync(destpath, contents, options); | ||
} else { | ||
contents = fs.readFileSync(srcpath); | ||
exports.writeFileSync(destpath, contents); | ||
} | ||
}; | ||
/** | ||
* @description | ||
* Recurse into a directory, executing callback for each file and folder | ||
@@ -199,10 +231,10 @@ * if the filename is undefiend, the callback is for folder, otherwise for file. | ||
fs.stat(filepath, function(err, stats) { | ||
var relative = path.relative(rootpath, filepath); | ||
var flag = filterCb(relative); | ||
if (stats.isDirectory()) { | ||
recurse(filepath); | ||
callback(filepath); | ||
if (flag) callback(filepath); | ||
} else { | ||
var relative = path.relative(rootpath, filepath); | ||
if (filterCb(relative)) { | ||
callback(filepath, filename); | ||
} | ||
if (flag) callback(filepath, filename); | ||
} | ||
@@ -238,11 +270,10 @@ }); | ||
var stats = fs.statSync(filepath); | ||
var relative = path.relative(rootpath, filepath); | ||
var flag = filterCb(relative); | ||
if (stats.isDirectory()) { | ||
recurse(filepath); | ||
callback(filepath); | ||
if (flag) callback(filepath); | ||
} else { | ||
var relative = path.relative(rootpath, filepath); | ||
if (filterCb(relative)) { | ||
callback(filepath, filename); | ||
} | ||
if (flag) callback(filepath, filename); | ||
} | ||
@@ -264,21 +295,14 @@ }); | ||
* file.rmdirSync('path'); | ||
* file.rmdirSync('path/file.txt'); | ||
*/ | ||
exports.rmdirSync = function(dirpath) { | ||
var stats = fs.statSync(dirpath); | ||
exports.recurseSync(dirpath, function(filepath, filename) { | ||
// it is file, otherwise it's folder | ||
if (filename) { | ||
fs.unlinkSync(filepath); | ||
} else { | ||
fs.rmdirSync(filepath); | ||
} | ||
}); | ||
if (stats.isFile()) { | ||
fs.unlinkSync(dirpath); | ||
} else { | ||
exports.recurseSync(dirpath, function(filepath, filename) { | ||
// it is file, otherwise it's folder | ||
if (filename) { | ||
fs.unlinkSync(filepath); | ||
} else { | ||
fs.rmdirSync(filepath); | ||
} | ||
}); | ||
fs.rmdirSync(dirpath); | ||
} | ||
fs.rmdirSync(dirpath); | ||
}; | ||
@@ -298,97 +322,77 @@ | ||
exports.copySync = function(dirpath, destpath, options) { | ||
var defaults = { | ||
options = util.extend({ | ||
encoding: 'utf8', | ||
filter: null, | ||
noProcess: '' | ||
}; | ||
options = util.extend(defaults, options || {}); | ||
}, options || {}); | ||
var files = []; | ||
var folders = []; | ||
var files = []; | ||
exports.recurseSync(dirpath, options.filter, function(filepath, filename) { | ||
if (filename) { | ||
files.push(filepath); | ||
} else { | ||
folders.push(filepath); | ||
} | ||
if (!filename) return; | ||
files.push(filepath); | ||
folders.push(path.dirname(filepath)); | ||
}); | ||
// Clear empty folder | ||
if (options.clear) { | ||
folders = folders.filter(function(item) { | ||
var length = files.length; | ||
while(length--) { | ||
if (path.dirname(files[length]) === item) return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
var length = files.length; | ||
var noProcessCb = fileMatch(options.noProcess); | ||
folders = folders.filter(function(item, index) { | ||
var length = folders.length; | ||
// Make sure dest root | ||
exports.mkdirSync(destpath); | ||
// First create folder for file | ||
folders.forEach(function(item, index) { | ||
var isCreate = true; | ||
var relative, newpath; | ||
while(length--) { | ||
var newItem = folders[length]; | ||
var isSubdir = newItem.indexOf(item) === 0; | ||
var notSamelevel = newItem.split(path.sep).length != item.split(path.sep).length; | ||
while(index++ < length) { | ||
if (folders[index] === item) { | ||
isCreate = false; | ||
break; | ||
} | ||
} | ||
if (isSubdir && notSamelevel) { | ||
return false; | ||
if (isCreate) { | ||
relative = path.relative(dirpath, item); | ||
if (relative) { | ||
newpath = path.join(destpath, relative); | ||
exports.mkdirSync(newpath); | ||
} | ||
} | ||
return true; | ||
}); | ||
// if dirpath don't exists folder | ||
if (!folders.length) { | ||
exports.mkdirSync(destpath); | ||
} | ||
function copy(oldpath, newpath, options) { | ||
var result; | ||
if (options.process) { | ||
var encoding = { | ||
encoding: options.encoding | ||
}; | ||
result = fs.readFileSync(oldpath, encoding); | ||
result = options.process(result, oldpath); | ||
// first create dir | ||
folders.forEach(function(folder) { | ||
var relative = path.relative(dirpath, folder); | ||
exports.mkdirSync(path.join(destpath, relative)); | ||
}); | ||
var noProcessCb = fileMatch(options.noProcess); | ||
// write file | ||
files.forEach(function(filepath) { | ||
var encoding = options.encoding; | ||
var process = options.process; | ||
var relative = path.relative(dirpath, filepath); | ||
if (!options.process) { | ||
encoding = null; | ||
if (util.isObject(result) && result.filepath) { | ||
fs.writeFileSync(result.filepath, result.contents, encoding); | ||
} else { | ||
fs.writeFileSync(newpath, result, encoding); | ||
} | ||
} else { | ||
result = fs.readFileSync(oldpath); | ||
fs.writeFileSync(newpath, result); | ||
} | ||
} | ||
// Skip not process files | ||
if (noProcessCb(relative)) { | ||
encoding = null; | ||
process = null; | ||
} | ||
// Copy file | ||
files.forEach(function(item) { | ||
var relative = path.relative(dirpath, item); | ||
var newpath = path.join(destpath, relative); | ||
var contents = fs.readFileSync(filepath, { | ||
encoding: encoding | ||
}); | ||
if (process) { | ||
var result = process(contents, filepath); | ||
// change file formate | ||
if (util.isString(result)) { | ||
contents = result; | ||
if (options.process) { | ||
if (noProcessCb(relative)) { | ||
copy(item, newpath, {}); | ||
} else { | ||
contents = result.contents; | ||
relative = path.relative(dirpath, result.filepath); | ||
copy(item, newpath, options); | ||
} | ||
} else { | ||
copy(item, newpath, {}); | ||
} | ||
var newPath = path.join(destpath, relative); | ||
fs.writeFileSync(newPath, contents, { | ||
encoding: encoding | ||
}); | ||
}); | ||
}); | ||
}; | ||
@@ -395,0 +399,0 @@ |
{ | ||
"name": "file-system", | ||
"version": "1.2.9", | ||
"version": "2.0.0", | ||
"description": "Strengthen the ability of file system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -43,3 +43,24 @@ # file-system — Simplified file system | ||
The api equal [file-match](https://github.com/douzi8/file-match) | ||
### file.copyFile | ||
Asynchronously copy a file into newpath | ||
``` | ||
file.copyFile('deom.png', 'dest/demo.png', { | ||
done: function() { | ||
console.log('done'); | ||
} | ||
}); | ||
``` | ||
### file.copyFileSync | ||
Copy a file into newpath | ||
```js | ||
file.copyFileSync('demo.png', 'dest/demo.png'); | ||
file.copyFileSync('demo.css', 'dest/demo.css', { | ||
process: function(contents) { | ||
return contents; | ||
} | ||
}) | ||
``` | ||
### file.recurse | ||
@@ -86,6 +107,4 @@ Recurse into a directory, executing callback for each file and folder. | ||
Recurse into a directory, remove all of the files and folder in this directory. | ||
it also can delete file. | ||
```js | ||
file.rmdirSync('path'); | ||
file.rmdirSync('path/file.txt'); | ||
``` | ||
@@ -121,4 +140,3 @@ | ||
* process | ||
* clear [clear = false] | ||
Should clear empty folder | ||
* noProcess | ||
@@ -125,0 +143,0 @@ ### file.base64 |
@@ -32,31 +32,31 @@ var assert = require("assert"); | ||
it('copySync files with filter', function() { | ||
var dirpath = getPath('var/copy/simple'); | ||
var destpath = getPath('var/copy/simpledest'); | ||
// it('copySync files with filter', function() { | ||
// var dirpath = getPath('var/copy/simple'); | ||
// var destpath = getPath('var/copy/simpledest'); | ||
file.copySync(dirpath, destpath, { | ||
filter: [ | ||
'**/*.js', | ||
'1/**/*.css', | ||
'1/demo.html' | ||
] | ||
}); | ||
// file.copySync(dirpath, destpath, { | ||
// filter: [ | ||
// '**/*.js', | ||
// '1/**/*.css', | ||
// '1/demo.html' | ||
// ] | ||
// }); | ||
var dirDest = [ | ||
getPath('var/copy/simpledest/1/demo.html'), | ||
getPath('var/copy/simpledest/1/demo.css'), | ||
getPath('var/copy/simpledest/1/2/demo.css'), | ||
getPath('var/copy/simpledest/1/demo.js'), | ||
getPath('var/copy/simpledest/demo.js') | ||
]; | ||
var result = []; | ||
// var dirDest = [ | ||
// getPath('var/copy/simpledest/1/demo.html'), | ||
// getPath('var/copy/simpledest/1/demo.css'), | ||
// getPath('var/copy/simpledest/1/2/demo.css'), | ||
// getPath('var/copy/simpledest/1/demo.js'), | ||
// getPath('var/copy/simpledest/demo.js') | ||
// ]; | ||
// var result = []; | ||
file.recurseSync(destpath, function(filepath, filename) { | ||
if (!filename) return; | ||
// file.recurseSync(destpath, function(filepath, filename) { | ||
// if (!filename) return; | ||
result.push(filepath); | ||
}); | ||
// result.push(filepath); | ||
// }); | ||
assert.equal(result.length, dirDest.length); | ||
}); | ||
// assert.equal(result.length, dirDest.length); | ||
// }); | ||
@@ -70,5 +70,7 @@ it('copySync replace filepath', function() { | ||
var basename = path.basename(filepath); | ||
var relative = path.relative(dirpath, filepath); | ||
var newpath = path.join(destpath, relative); | ||
// Replace html to txt | ||
filepath = filepath.replace( | ||
newpath = newpath.replace( | ||
/\.html$/, | ||
@@ -80,4 +82,4 @@ '.txt' | ||
if (/\.css$/.test(basename)) { | ||
var prefix = path.basename(path.dirname(filepath)); | ||
filepath = path.join(destpath, prefix + '-' + basename); | ||
var prefix = path.basename(path.dirname(newpath)); | ||
newpath = path.join(destpath, prefix + '-' + basename); | ||
} | ||
@@ -87,3 +89,3 @@ | ||
contents: contents, | ||
filepath: filepath | ||
filepath: newpath | ||
}; | ||
@@ -137,33 +139,6 @@ } | ||
it('copySync with clear true', function() { | ||
var clearFiles = [ | ||
getPath('var/copy-clear/1/1.html'), | ||
getPath('var/copy-clear/1/111'), | ||
getPath('var/copy-clear/2'), | ||
getPath('var/copy-clear/1/11/11.html'), | ||
getPath('var/copy-clear/1/11/11') | ||
]; | ||
clearFiles.forEach(function(item) { | ||
if (/\.\w+$/.test(item)) { | ||
file.writeFileSync(item); | ||
} else { | ||
file.mkdirSync(item); | ||
} | ||
}); | ||
file.copySync(getPath('var/copy-clear'), getPath('var/copy-clear-dest'), { | ||
clear: true | ||
}); | ||
assert.equal(false, file.existsSync(getPath('var/copy-clear-dest/2'))); | ||
assert.equal(false, file.existsSync(getPath('var/copy-clear-dest/1/11/11'))); | ||
assert.equal(false, file.existsSync(getPath('var/copy-clear-dest/1/111'))); | ||
}); | ||
after(function() { | ||
file.rmdirSync(getPath('var/copy')); | ||
file.rmdirSync(getPath('var/copy-clear')); | ||
file.rmdirSync(getPath('var/copy-clear-dest')); | ||
file.rmdirSync(getPath('var/copy')); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30258
13
858
150
7