Comparing version 0.0.1 to 0.0.2
169
index.js
@@ -165,3 +165,3 @@ /*jshint esnext: true */ | ||
if (!(entries = ls(path))) { | ||
if (!(entries = list(path))) { | ||
return false; | ||
@@ -232,3 +232,3 @@ } | ||
if (!(entries = ls(path))) { | ||
if (!(entries = list(path))) { | ||
return false; | ||
@@ -329,3 +329,3 @@ } | ||
try { | ||
return fs.readFileSync(path, options); | ||
return fs.readFileSync(path, options).toString(); | ||
} | ||
@@ -469,7 +469,15 @@ | ||
// directory - copy recursively | ||
if (!dstExists && !mkdir(dst)) { | ||
if (dstExists) { | ||
dst = dst + SEP + name; | ||
if (!mkdir(dst)) { | ||
return false; | ||
} | ||
} | ||
else if (!mkdir(dst)) { | ||
return false; | ||
} | ||
if (!(entries = ls(src))) { | ||
if (!(entries = list(src))) { | ||
return false; | ||
@@ -480,3 +488,3 @@ } | ||
for (entry in entries) { | ||
if (!cp(src + SEP + entry, dst + SEP + entry, overwrite)) { | ||
if (!cp(src + SEP + entry, dst, overwrite)) { | ||
return false; | ||
@@ -510,8 +518,6 @@ } | ||
var stat = exists(src); | ||
var dname = p.basename(dst); | ||
var existingSrc = exists(src); | ||
var existingDst = exists(dst); | ||
dst = p.dirname(dst); | ||
if (!stat) { | ||
if (!existingSrc) { | ||
lastError = new Error('Move error: source path "' + src + '" ' + | ||
@@ -522,8 +528,10 @@ 'does not exist!'); | ||
if (stat.isDirectory() && !exists(dst) && !mkdir(dst)) { | ||
if (existingDst && existingDst.isDirectory()) { | ||
dst += SEP + p.basename(src); | ||
} | ||
else if (!mkdir(p.dirname(dst))) { | ||
return false; | ||
} | ||
dst += SEP + dname; | ||
try { | ||
@@ -573,5 +581,4 @@ fs.renameSync(src, dst); | ||
if (!stat) { | ||
lastError = new Error('Remove error: source path "' + path + '" ' + | ||
'does not exist!'); | ||
return false; | ||
// someone did our job already? well done! | ||
return true; | ||
} | ||
@@ -592,3 +599,3 @@ | ||
entries = ls(path); | ||
entries = list(path); | ||
@@ -630,3 +637,3 @@ if (!entries) { | ||
* "stat" method will follow symlinks to stat their targets, "lstat" method will | ||
* stat symlinks themselves, "none" (by default) - will not do stat. | ||
* stat symlinks themselves, "none" (by default) - won't stat anything. | ||
* | ||
@@ -645,3 +652,3 @@ * <pre> | ||
* var fs = require('fs-cli'); | ||
* var entries = fs.ls('.', 'lstat'); | ||
* var entries = fs.list('.', 'lstat'); | ||
* if (!entries) { | ||
@@ -661,5 +668,6 @@ * console.log(fs.error()); | ||
* @param {string} [method] - stat method ('stat'|'lstat'|'none',default:'none') | ||
* @param {boolean} [all] - if true, will not omit listing of '.' and '..' dirs | ||
* @returns {Object|null} | ||
*/ | ||
function ls (path, method) { | ||
function list (path, method, all) { | ||
'use strict'; | ||
@@ -673,2 +681,8 @@ | ||
var mstat = function (itemPath) { | ||
return method != 'none' && fs[method + 'Sync'] ? | ||
fs[method + 'Sync'](itemPath) : | ||
{}; | ||
}; | ||
try { | ||
@@ -678,9 +692,10 @@ var items = {}; | ||
fs.readdirSync(path).forEach(function (item) { | ||
items[item] = ( | ||
method != 'none' && fs[method + 'Sync'] ? | ||
fs[method + 'Sync'](path + SEP + item) : | ||
{} | ||
); | ||
items[item] = mstat(path + SEP + item); | ||
}); | ||
if (all) { | ||
items['.'] = mstat(path + SEP + '.'); | ||
items['..'] = mstat(path + SEP + '..'); | ||
} | ||
return items; | ||
@@ -696,2 +711,78 @@ } | ||
/** | ||
* Alias for list, but will return items as array | ||
* | ||
* @param {string} path | ||
* @returns {Array} | ||
* @access public | ||
* @static | ||
*/ | ||
function ls (path) { | ||
return Object.keys(list(path)); | ||
} | ||
/** | ||
* Same as #ls(), but with '.' and '..' items | ||
* | ||
* @param {string} path | ||
* @returns {Array} | ||
* @access public | ||
* @static | ||
*/ | ||
function lsa (path) { | ||
return ['.', '..'].concat(ls(path)); | ||
} | ||
/** | ||
* Alias for #list(). Treat it as `ls -l` on Unix-like platforms. | ||
* Stat will be provided as lstat, equal to list(path, 'lstat'). For | ||
* stat version use #lsls() | ||
* | ||
* @param {string} path | ||
* @returns {Object|null} | ||
* @access public | ||
* @static | ||
*/ | ||
function lsl (path) { | ||
return list(path, 'lstat'); | ||
} | ||
/** | ||
* Alias for #list(). Treat it as `ls -al` on Unix-like platforms. | ||
* Stat vill be provided as lstat, equal to list(path, 'lstat', true). For | ||
* stat version use #lsals() | ||
* | ||
* @param {string} path | ||
* @returns {Object|null} | ||
* @access public | ||
* @static | ||
*/ | ||
function lsal (path) { | ||
return list(path, 'lstat', true); | ||
} | ||
/** | ||
* Same as #lsl(), but uses stat, instead of lstat. | ||
* | ||
* @param {string} path | ||
* @returns {Object|null} | ||
* @access public | ||
* @static | ||
*/ | ||
function lsls (path) { | ||
return list(path, 'stat'); | ||
} | ||
/** | ||
* Same as lsal, but uses stat instead of lstat. | ||
* | ||
* @param {string} path | ||
* @returns {Object|null} | ||
* @access public | ||
* @static | ||
*/ | ||
function lsals (path) { | ||
return list(path, 'stat', true); | ||
} | ||
/** | ||
* Creates a tarball from a given src directory and save it to given dst path. | ||
@@ -1145,6 +1236,12 @@ * Automatically creates missing paths for a given dst. | ||
module.exports = { | ||
cp: globalize(cp), | ||
mv: globalize(mv), | ||
rm: globalize(rm), | ||
cp: cp, | ||
mv: mv, | ||
rm: rm, | ||
list: list, | ||
ls: ls, | ||
lsa: lsa, | ||
lsl: lsl, | ||
lsal: lsal, | ||
lsls: lsls, | ||
lsals: lsals, | ||
tar: tar, | ||
@@ -1159,6 +1256,6 @@ untar: untar, | ||
close: close, | ||
chown: globalize(chown), | ||
rchown: globalize(rchown), | ||
chmod: globalize(chmod), | ||
rchmod: globalize(rchmod), | ||
chown: chown, | ||
rchown: rchown, | ||
chmod: chmod, | ||
rchmod: rchmod, | ||
read: read, | ||
@@ -1171,4 +1268,10 @@ write: write, | ||
relpath: relpath, | ||
glob: glob.sync, | ||
DIRSEP: SEP, | ||
DIRSEP_REGEX: SEP_RX | ||
}; | ||
['cp', 'mv', 'rm', 'chown', 'rchown', 'chmod', 'rchmod']. | ||
forEach(function (name) { | ||
module.exports[name] = globalize(module.exports[name]); | ||
}); |
{ | ||
"name": "fs-cli", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Easy filesystem functions", | ||
@@ -11,6 +11,6 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"generate-docs": "jsdoc -c .jsdocrc --verbose" | ||
"test": "./node_modules/.bin/mocha ./test", | ||
"generate-docs": "jsdoc -c .jsdocrc --verbose", | ||
"jscheck": "jshint index.js && jscs index.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
@@ -24,4 +24,5 @@ "dependencies": { | ||
"devDependencies": { | ||
"loke-jsdoc-theme": "^2.1.0" | ||
"loke-jsdoc-theme": "^2.1.0", | ||
"mocha": "^2.3.4" | ||
} | ||
} |
# Easy FS Scripting With NodeJS | ||
[![Build Status](https://travis-ci.org/Mikhus/fs-cli.svg?branch=master)](https://travis-ci.org/Mikhus/fs-cli) [![Dependency Status](https://david-dm.org/Mikhus/fs-cli.svg)](https://david-dm.org/Mikhus/fs-cli) [![devDependency Status](https://david-dm.org/Mikhus/fs-cli/dev-status.svg)](https://david-dm.org/Mikhus/fs-cli#info=devDependencies) | ||
It is specially created to get rid of routines when writing CLI tools using | ||
@@ -20,9 +22,9 @@ NodeJS. File system. Recursive. Synchronous... | ||
var fs = require('fs-cli'); | ||
var temp = './doc'; | ||
fs.mv('/home/user/docs/**/*.doc', temp); | ||
fs.tar(temp, './all-docs.tgz'); | ||
fs.rm(temp); | ||
```javascript | ||
var fs = require('fs-cli'); | ||
var temp = './doc'; | ||
fs.mv('/home/user/docs/**/*.doc', temp); | ||
fs.tar(temp, './all-docs.tgz'); | ||
fs.rm(temp); | ||
``` | ||
@@ -34,15 +36,22 @@ Oh, really? - Yeah! | ||
var fs = require('fs-cli'); | ||
var temp = './doc'; | ||
function die() { | ||
console.log(fs.error().stack); | ||
process.exit(); | ||
} | ||
fs.mv('/home/user/docs/**/*.doc', temp) || die(); | ||
fs.tar(temp, './all-docs.tgz') || die(); | ||
fs.rm(temp) || die(); | ||
```javascript | ||
var fs = require('fs-cli'); | ||
var temp = './doc'; | ||
function die() { | ||
console.log(fs.error().stack); | ||
process.exit(); | ||
} | ||
fs.mv('/home/user/docs/**/*.doc', temp) || die(); | ||
fs.tar(temp, './all-docs.tgz') || die(); | ||
fs.rm(temp) || die(); | ||
``` | ||
Want to remove all ```.svn``` folders with its contents from a certain folder? | ||
Here it is: | ||
```javascript | ||
var fs = require('fs-cli'); | ||
fs.rm('./my-project/**/.svn'); | ||
``` | ||
-- Simple? Short? Cross-platform? Synchronous? Recursive? FS? JavaScript?<br> | ||
@@ -65,3 +74,3 @@ -- Yeah! That's why!<br> | ||
- **ls**: directory listing | ||
- **ls/lsa/lsl/lsal/lsls/lsals/list**: directory listing | ||
- **rm**: remove given path with all its contents, globs are allowed | ||
@@ -68,0 +77,0 @@ - **mv**: move source to destination, create missing paths recursive, +globs |
@@ -5,2 +5,7 @@ var assert = require('assert'); | ||
describe('fs.mkdir()', function () { | ||
before(function () { | ||
fs.error(null); | ||
assert.equal(fs.error(), null); | ||
}); | ||
it('should create given path recursively', function () { | ||
@@ -12,12 +17,6 @@ assert.equal(fs.mkdir('./test/some/path/depth'), true); | ||
}); | ||
it('should return false on fail', function () { | ||
assert.equal(fs.mkdir('./test/*/**/***'), false); | ||
}); | ||
it('should report error properly', function () { | ||
assert.equal(fs.error() instanceof Error, true); | ||
}); | ||
after(function() { | ||
fs.rm('./test/some'); | ||
assert.equal(fs.rm('./test/some'), true); | ||
}); | ||
}); |
@@ -14,3 +14,8 @@ var assert = require('assert'); | ||
describe('fs.realpath()', function () { | ||
it('shoud return a path string', function () { | ||
before(function() { | ||
fs.error(null); | ||
assert.equal(fs.error(), null); | ||
}); | ||
it('should return a path string', function () { | ||
assert.equal( | ||
@@ -17,0 +22,0 @@ typeof fs.realpath('./test/some/path/depth') === 'string', |
@@ -12,2 +12,3 @@ var assert = require('assert'); | ||
fs.error(null); | ||
assert.equal(fs.error(), null); | ||
}); | ||
@@ -49,4 +50,4 @@ | ||
after(function() { | ||
fs.rm('./tmp'); | ||
assert.equal(fs.rm('./tmp'), true); | ||
}); | ||
}); |
@@ -12,2 +12,3 @@ var assert = require('assert'); | ||
fs.error(null); | ||
assert.equal(); | ||
}); | ||
@@ -45,4 +46,4 @@ | ||
after(function() { | ||
fs.rm('./tmp'); | ||
assert.equal(fs.rm('./tmp'), true); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
1800457
70
2673
2
112
2
8