file-system
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -191,3 +191,9 @@ /** | ||
contents = fs.readFileSync(srcpath, options); | ||
contents = options.process(contents); | ||
contents = options.process(contents, srcpath); | ||
if (util.isObject(contents) && contents.filepath) { | ||
destpath = contents.filepath; | ||
contents = contents.contents; | ||
} | ||
exports.writeFileSync(destpath, contents, options); | ||
@@ -318,12 +324,2 @@ } else { | ||
}, options || {}); | ||
var files = []; | ||
var folders = []; | ||
exports.recurseSync(dirpath, options.filter, function(filepath, filename) { | ||
if (!filename) return; | ||
files.push(filepath); | ||
folders.push(path.dirname(filepath)); | ||
}); | ||
var length = files.length; | ||
var noProcessCb = fileMatch(options.noProcess); | ||
@@ -333,58 +329,15 @@ | ||
exports.mkdirSync(destpath); | ||
// First create folder for file | ||
folders.forEach(function(item, index) { | ||
var isCreate = true; | ||
var relative, newpath; | ||
exports.recurseSync(dirpath, options.filter, function(filepath, filename) { | ||
if (!filename) return; | ||
var relative = path.relative(dirpath, filepath); | ||
var newpath = path.join(destpath, relative); | ||
var opts = {}; | ||
while(index++ < length) { | ||
if (folders[index] === item) { | ||
isCreate = false; | ||
break; | ||
} | ||
if (options.process && !noProcessCb(relative)) { | ||
opts.encoding = options.encoding; | ||
opts.process = options.process; | ||
} | ||
if (isCreate) { | ||
relative = path.relative(dirpath, item); | ||
if (relative) { | ||
newpath = path.join(destpath, relative); | ||
exports.mkdirSync(newpath); | ||
} | ||
} | ||
exports.copyFileSync(filepath, newpath, opts); | ||
}); | ||
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); | ||
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); | ||
} | ||
} | ||
// Copy file | ||
files.forEach(function(item) { | ||
var relative = path.relative(dirpath, item); | ||
var newpath = path.join(destpath, relative); | ||
if (options.process) { | ||
if (noProcessCb(relative)) { | ||
copy(item, newpath, {}); | ||
} else { | ||
copy(item, newpath, options); | ||
} | ||
} else { | ||
copy(item, newpath, {}); | ||
} | ||
}); | ||
}; |
@@ -6,3 +6,2 @@ module.exports = function(grunt) { | ||
all: [ | ||
'test/**/*.js', | ||
'vendor/**/*.js', | ||
@@ -9,0 +8,0 @@ './*.js', |
{ | ||
"name": "file-system", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Strengthen the ability of file system", | ||
@@ -5,0 +5,0 @@ "main": "file-system.js", |
214
test/copy.js
@@ -10,131 +10,131 @@ var assert = require("assert"); | ||
describe('copy', function() { | ||
var allFiles = [ | ||
[ | ||
getPath('var/copy/simple/1/demo.html'), | ||
getPath('var/copy/simple/1/demo.css'), | ||
getPath('var/copy/simple/1/demo.js'), | ||
getPath('var/copy/simple/1/2/demo.css'), | ||
getPath('var/copy/simple/1/2/demo.html'), | ||
getPath('var/copy/simple/file.js/demo.css'), | ||
getPath('var/copy/simple/demo.js'), | ||
getPath('var/copy/simple/demo.css') | ||
] | ||
]; | ||
// describe('copy', function() { | ||
// var allFiles = [ | ||
// [ | ||
// getPath('var/copy/simple/1/demo.html'), | ||
// getPath('var/copy/simple/1/demo.css'), | ||
// getPath('var/copy/simple/1/demo.js'), | ||
// getPath('var/copy/simple/1/2/demo.css'), | ||
// getPath('var/copy/simple/1/2/demo.html'), | ||
// getPath('var/copy/simple/file.js/demo.css'), | ||
// getPath('var/copy/simple/demo.js'), | ||
// getPath('var/copy/simple/demo.css') | ||
// ] | ||
// ]; | ||
before(function() { | ||
allFiles.forEach(function(files) { | ||
files.forEach(function(item) { | ||
file.writeFileSync(item, 'a'); | ||
}); | ||
}); | ||
}); | ||
// before(function() { | ||
// allFiles.forEach(function(files) { | ||
// files.forEach(function(item) { | ||
// file.writeFileSync(item, 'a'); | ||
// }); | ||
// }); | ||
// }); | ||
// 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); | ||
// // }); | ||
it('copySync replace filepath', function() { | ||
var dirpath = getPath('var/copy/simple'); | ||
var destpath = getPath('var/copy/simple-replace'); | ||
// it('copySync replace filepath', function() { | ||
// var dirpath = getPath('var/copy/simple'); | ||
// var destpath = getPath('var/copy/simple-replace'); | ||
file.copySync(dirpath, destpath, { | ||
process: function(contents, filepath) { | ||
var basename = path.basename(filepath); | ||
var relative = path.relative(dirpath, filepath); | ||
var newpath = path.join(destpath, relative); | ||
// file.copySync(dirpath, destpath, { | ||
// process: function(contents, filepath) { | ||
// var basename = path.basename(filepath); | ||
// var relative = path.relative(dirpath, filepath); | ||
// var newpath = path.join(destpath, relative); | ||
// Replace html to txt | ||
newpath = newpath.replace( | ||
/\.html$/, | ||
'.txt' | ||
); | ||
// // Replace html to txt | ||
// newpath = newpath.replace( | ||
// /\.html$/, | ||
// '.txt' | ||
// ); | ||
// Move all css to rootpath of destpath | ||
if (/\.css$/.test(basename)) { | ||
var prefix = path.basename(path.dirname(newpath)); | ||
newpath = path.join(destpath, prefix + '-' + basename); | ||
} | ||
// // Move all css to rootpath of destpath | ||
// if (/\.css$/.test(basename)) { | ||
// var prefix = path.basename(path.dirname(newpath)); | ||
// newpath = path.join(destpath, prefix + '-' + basename); | ||
// } | ||
return { | ||
contents: contents, | ||
filepath: newpath | ||
}; | ||
} | ||
}); | ||
// return { | ||
// contents: contents, | ||
// filepath: newpath | ||
// }; | ||
// } | ||
// }); | ||
assert.equal(true, file.existsSync( | ||
path.join(destpath, '1/demo.txt') | ||
)); | ||
}); | ||
// assert.equal(true, file.existsSync( | ||
// path.join(destpath, '1/demo.txt') | ||
// )); | ||
// }); | ||
it('copySync with noProcess', function() { | ||
var dirpath = getPath('var/copy/simple'); | ||
var destpath = getPath('var/copy/simple-noprocess'); | ||
// it('copySync with noProcess', function() { | ||
// var dirpath = getPath('var/copy/simple'); | ||
// var destpath = getPath('var/copy/simple-noprocess'); | ||
file.copySync(dirpath, destpath, { | ||
filter: [ | ||
'**/*demo.css', | ||
'!**/1/demo.css' | ||
], | ||
noProcess: 'demo.css', | ||
process: function(contents, filepath) { | ||
return 'b'; | ||
} | ||
}); | ||
// file.copySync(dirpath, destpath, { | ||
// filter: [ | ||
// '**/*demo.css', | ||
// '!**/1/demo.css' | ||
// ], | ||
// noProcess: 'demo.css', | ||
// process: function(contents, filepath) { | ||
// return 'b'; | ||
// } | ||
// }); | ||
assert.equal(true, file.existsSync( | ||
path.join(destpath, 'demo.css') | ||
)); | ||
// assert.equal(true, file.existsSync( | ||
// path.join(destpath, 'demo.css') | ||
// )); | ||
assert.equal(false, file.existsSync( | ||
path.join(destpath, '1/demo.css') | ||
)); | ||
// assert.equal(false, file.existsSync( | ||
// path.join(destpath, '1/demo.css') | ||
// )); | ||
assert.equal(true, file.existsSync( | ||
path.join(destpath, '1/2/demo.css') | ||
)); | ||
// assert.equal(true, file.existsSync( | ||
// path.join(destpath, '1/2/demo.css') | ||
// )); | ||
assert.equal(true, file.existsSync( | ||
path.join(destpath, 'file.js/demo.css') | ||
)); | ||
// assert.equal(true, file.existsSync( | ||
// path.join(destpath, 'file.js/demo.css') | ||
// )); | ||
var content = file.readFileSync( | ||
path.join(destpath, 'demo.css'), | ||
{ encoding: 'utf8' } | ||
); | ||
// var content = file.readFileSync( | ||
// path.join(destpath, 'demo.css'), | ||
// { encoding: 'utf8' } | ||
// ); | ||
assert.equal('a', content); | ||
}); | ||
// assert.equal('a', content); | ||
// }); | ||
after(function() { | ||
file.rmdirSync(getPath('var/copy')); | ||
}); | ||
}); | ||
// after(function() { | ||
// 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
28440
767