file-system
Advanced tools
Comparing version 1.1.0 to 1.1.1
73
index.js
@@ -182,3 +182,3 @@ /** | ||
exports.mkdirSync(dirname); | ||
fs.writeFile(filename, data, options); | ||
fs.writeFileSync(filename, data, options); | ||
}; | ||
@@ -306,2 +306,73 @@ | ||
} | ||
}; | ||
/** | ||
* @description | ||
* Copy dirpath to destpath, pass process callback for each file hanlder | ||
* @example | ||
* file.copySync('path', 'dest'); | ||
* file.copySync('src', 'dest/src'); | ||
* file.copySync('path', 'dest', { process: function(contents, filepath) {} }); | ||
*/ | ||
exports.copySync = function(dirpath, destpath, options) { | ||
var defaults = { | ||
encoding: 'utf8', | ||
filter: null, | ||
process: function(contents) { | ||
return contents; | ||
} | ||
}; | ||
options = util.extend(defaults, options || {}); | ||
var folders = []; | ||
var files = []; | ||
exports.recurseSync(dirpath, options.filter, function(filepath, filename) { | ||
if (filename) { | ||
files.push(filepath); | ||
} else { | ||
folders.push(filepath); | ||
} | ||
}); | ||
folders = folders.filter(function(item, index) { | ||
var length = folders.length; | ||
while(length--) { | ||
var isSubdir = new RegExp('^' + item); | ||
if (folders[length] != item && isSubdir.test(folders[length])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
// if dirpath don't exists folder | ||
if (!folders.length) { | ||
fs.mkdirSync(destpath); | ||
} | ||
// first create dir | ||
folders.forEach(function(folder) { | ||
var relative = path.relative(dirpath, folder); | ||
exports.mkdirSync(path.join(destpath, relative)); | ||
}); | ||
// write file | ||
files.forEach(function(filepath) { | ||
var contents = fs.readFileSync(filepath, { | ||
encoding: options.encoding | ||
}); | ||
contents = options.process(contents, filepath); | ||
var relative = path.relative(dirpath, filepath); | ||
var newPath = path.join(destpath, relative); | ||
fs.writeFileSync(newPath, contents, { | ||
encoding: options.encoding | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "file-system", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Strengthen the ability of file system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,2 +65,12 @@ ## file-system | ||
file.rmdirSync('path/file.txt'); | ||
``` | ||
### file.copySync | ||
Recurse into a directory, copy all files into dest. | ||
Pass options filter params to filter files | ||
```js | ||
file.copySync('path', 'dest'); | ||
file.copySync('src', 'dest/src'); | ||
file.copySync('src', 'dest/src', { filter: ['*.js', 'path/**/*.css'] }); | ||
file.copySync('path', 'dest', { process: function(contents, filepath) {} }); | ||
``` |
var assert = require("assert"); | ||
var file = require('../index'); | ||
var fs = require('fs'); | ||
var grunt = require('grunt'); | ||
var path = require('path'); | ||
@@ -26,2 +25,11 @@ | ||
getPath('var/recurse/filter/demo.css') | ||
], | ||
[ | ||
getPath('var/recurse/copy/1/demo.js'), | ||
getPath('var/recurse/copy/1/2/demo.js'), | ||
getPath('var/recurse/copy/1/2/demo.css'), | ||
getPath('var/recurse/copy/1/2/demo.html'), | ||
getPath('var/recurse/copy/demo.html'), | ||
getPath('var/recurse/copy/demo.js'), | ||
getPath('var/recurse/copy/demo.css') | ||
] | ||
@@ -105,2 +113,46 @@ ]; | ||
it('copySync files', function() { | ||
var dest = getPath('var/recurse/dest'); | ||
var destFiles = []; | ||
file.copySync(getPath('var/recurse/copy'), dest); | ||
file.recurseSync(dest, function(filepath, filename) { | ||
if (!filename) return; | ||
destFiles.push(filepath); | ||
}); | ||
assert.equal(destFiles.length, allFiles[2].length); | ||
}); | ||
it('copySync empty folder', function() { | ||
var dest = getPath('var/recurse/copy/emptydest'); | ||
var src = getPath('var/recurse/copy/empty'); | ||
file.mkdirSync(src); | ||
file.copySync(src, dest); | ||
var existsSync = fs.existsSync(dest); | ||
assert.equal(existsSync, true); | ||
}); | ||
it('copySync process content', function() { | ||
var src = getPath('var/recurse/copyprocess/'); | ||
var dest = getPath('var/recurse/copyprocess/dest'); | ||
file.writeFileSync(path.join(src, '1.html'), 'a'); | ||
file.copySync(src, dest, { | ||
process: function(contents, filepath) { | ||
return 'b'; | ||
} | ||
}); | ||
var contents = fs.readFileSync(path.join(dest, '1.html')); | ||
assert.equal('b', contents); | ||
}); | ||
after(function() { | ||
@@ -107,0 +159,0 @@ file.rmdirSync(getPath('var/recurse')); |
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
21132
9
653
75
4