fs-jetpack
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -27,2 +27,12 @@ "use strict"; | ||
function normalizeContent(content) { | ||
if (typeof content === 'string' || Buffer.isBuffer(content)) { | ||
return content; | ||
} | ||
if (content !== null && typeof content === 'object') { | ||
return JSON.stringify(content, null, 2); | ||
} | ||
return content; | ||
} | ||
//--------------------------------------------------------- | ||
@@ -71,3 +81,3 @@ // Sync | ||
if (criteria.empty !== true && criteria.content !== undefined) { | ||
fs.writeFileSync(path, criteria.content); | ||
fs.writeFileSync(path, normalizeContent(criteria.content)); | ||
} | ||
@@ -87,3 +97,3 @@ | ||
} | ||
fs.writeFileSync(path, criteria.content, { mode: criteria.mode }); | ||
fs.writeFileSync(path, normalizeContent(criteria.content), { mode: criteria.mode }); | ||
@@ -119,3 +129,3 @@ } | ||
} | ||
qWriteFile(path, criteria.content, { mode: criteria.mode }) | ||
qWriteFile(path, normalizeContent(criteria.content), { mode: criteria.mode }) | ||
.then(qd.resolve, qd.reject); | ||
@@ -140,3 +150,3 @@ } | ||
if (criteria.content !== undefined) { | ||
qWriteFile(path, criteria.content) | ||
qWriteFile(path, normalizeContent(criteria.content)) | ||
.then(qd.resolve, qd.reject); | ||
@@ -143,0 +153,0 @@ } else { |
@@ -9,2 +9,3 @@ "use strict"; | ||
var copy = require('./copy'); | ||
var read = require('./read'); | ||
var remove = require('./remove'); | ||
@@ -34,2 +35,10 @@ var list = require('./list'); | ||
function path() { | ||
var path = getCwdPath(); | ||
for (var i = 0; i < arguments.length; i++){ | ||
path = pathUtil.resolve(path, arguments[i]); | ||
} | ||
return path; | ||
} | ||
// API | ||
@@ -39,2 +48,3 @@ | ||
cwd: cwd, | ||
path: path, | ||
@@ -96,2 +106,18 @@ copy: function (from, to, options) { | ||
read: function (path, mode) { | ||
var normalizedPath = pathUtil.resolve(getCwdPath(), path); | ||
return read.sync(normalizedPath, mode); | ||
}, | ||
readAsync: function (path, mode) { | ||
var normalizedPath = pathUtil.resolve(getCwdPath(), path); | ||
return read.async(normalizedPath, mode); | ||
}, | ||
write: function (path, content) { | ||
return this.file(path, { content: content }); | ||
}, | ||
writeAsync: function (path, content) { | ||
return this.fileAsync(path, { content: content }); | ||
}, | ||
remove: function (path, options) { | ||
@@ -98,0 +124,0 @@ var normalizedPath = pathUtil.resolve(getCwdPath(), path); |
{ | ||
"name": "fs-jetpack", | ||
"description": "Jetpack for 'fs' library", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Jakub Szwacz <jakub@szwacz.com>", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
#fs-jetpack | ||
This is an attempt to make comprehensive, higher level API for node's [fs library](http://nodejs.org/api/fs.html). | ||
Attempt to make comprehensive, higher level API for node's [fs library](http://nodejs.org/api/fs.html). | ||
@@ -15,6 +15,3 @@ ###Installation | ||
###Backwards compatibility policy | ||
This library is all about fun to use, clean API, so if better way to do some task will emerge **breaking changes will be introduced ruthlessly** (ofcourse until we reach version 1.0.0). Just want to make this clear. | ||
#API | ||
@@ -36,15 +33,20 @@ | ||
* <a href="#listasyncpath-options">listAsync(path, [options])</a> | ||
* <a href="#pathparts">path([parts...])</a> | ||
* <a href="#readpath-mode">read(path, [mode])</a> | ||
* <a href="#readasyncpath-mode">readAsync(path, [mode])</a> | ||
* <a href="#removepath-options">remove(path, [options])</a> | ||
* <a href="#removeasyncpath-options">removeAsync(path, [options])</a> | ||
* <a href="#writepath-content">write(path, content)</a> | ||
* <a href="#writeasyncpath-content">writeAsync(path, content)</a> | ||
###cwd([path]) | ||
Returns Current Working Directory (CWD) path, or creates new CWD context. | ||
Returns Current Working Directory (CWD) path, or creates new jetpack object with different CWD. | ||
**parameters:** | ||
`path` (optional) path for new CWD context. Could be absolute, or relative. If relative path given new CWD will be resolved basing on current CWD context. | ||
`path` (optional) path to become new CWD. Could be absolute, or relative. If relative path given new CWD will be resolved basing on current CWD. | ||
**returns:** | ||
If `path` not specified, returns CWD path. For main instance of fs-jetpack it is always `process.cwd()`. | ||
If `path` specified, returns new CWD context, which is totally the same thing as jetpack library, but resolves paths according to its inner CWD path, not the global one (`process.cwd()`). See code below for more. | ||
If `path` not specified, returns CWD path of this jetpack object. For main instance of fs-jetpack it is always `process.cwd()`. | ||
If `path` specified, returns new jetpack object (totally the same thing as main jetpack). The new object resolves paths according to its inner CWD, not the global one (`process.cwd()`). | ||
@@ -59,10 +61,10 @@ **examples:** | ||
// now let's create new CWD context... | ||
var jetpackContext = jetpack.cwd('..'); | ||
console.log(jetpackContext.cwd()); // '/one/two' | ||
var jetParent = jetpack.cwd('..'); | ||
console.log(jetParent.cwd()); // '/one/two' | ||
// ...and use this new context | ||
jetpackContext.dir('four'); // we just created directory '/one/two/four' | ||
jetParent.dir('four'); // we just created directory '/one/two/four' | ||
// one CWD context can be used to create next CWD context | ||
var jetpackContext2 = jetpackContext.cwd('..'); | ||
console.log(jetpackContext2.cwd()); // '/one' | ||
var jetParentParent = jetpackContext.cwd('..'); | ||
console.log(jetParentParent.cwd()); // '/one' | ||
``` | ||
@@ -80,5 +82,3 @@ | ||
* `'no'` don't allow to replace any file or directory in destination location. | ||
* `'yes'` replace every file already existing. | ||
* `'merge'` *(TODO, not implemented yet)* | ||
* `'ifNewer'` *(TODO, not implemented yet)* | ||
* `'yes'` replace every file already existing. | ||
* `only` (`array` of masks) will copy **only** items matching any of specified masks. Mask is `string` with .gitignore-like notation (see section *"Matching paths .gitignore style"*). | ||
@@ -111,3 +111,3 @@ * `allBut` (`array` of masks) will copy **everything except** items matching any of specified masks. If `only` is specified this field is ignored. | ||
**parameters:** | ||
`path` path to directory to examine. | ||
`path` path to directory to examine. | ||
`criteria` (optional) criteria to be met by the directory. Is an `object` with possible fields: | ||
@@ -156,4 +156,4 @@ * `exists` (default: `true`) whether directory should exist or not. If set to `true` and `path` contains many nested, nonexistent directories all of them will be created. | ||
* `empty` (default: `false`) whether file should be forced to be empty. If `exists = false` this field is ignored. | ||
* `content` (`string`, `buffer`, `object` ot `array`) sets file content. If `object` or `array` given to this parameter the output will be JSON. If `exists = false`, or `empty = true` this field is ignored. | ||
* `mode` ensures file has specified mode. If not set and file already exists, current mode will be intact. Value could be number (eg. `0700`) or string (eg. `'700'`). | ||
* `content` (`string` or `buffer`) ensures that file has precisely this content. If `exists = false`, or `empty = true` this field is ignored. | ||
@@ -222,2 +222,38 @@ **returns:** | ||
###path([parts...]) | ||
Returns path resolved to current CWD. | ||
**parameters:** | ||
`parts` (optional) strings to join and resolve as path (as many as you like). | ||
**returns:** | ||
Resolved path as String. | ||
**examples:** | ||
```javascript | ||
jetpack.cwd(); // if it returns '/one/two' | ||
jetpack.path(); // this will return the same '/one/two' | ||
jetpack.path('three'); // this will return '/one/two/three' | ||
jetpack.path('..', 'four'); // this will return '/one/four' | ||
``` | ||
###read(path, [mode]) | ||
Reads content of file. | ||
**parameters:** | ||
`path` path to file. | ||
`mode` (optional) how the content of file should be returned. Is a String with possible values: | ||
* `'utf8'` (default) content will be returned as UTF-8 String. | ||
* `'buf'` content will be returned as Buffer. | ||
* `'json'` content will be returned as parsed JSON object. | ||
**returns:** | ||
File content in specified format. | ||
###readAsync(path, [mode]) | ||
Asynchronous equivalent of `read()` method. The only difference is that it returns promise. | ||
###remove(path, [options]) | ||
@@ -256,2 +292,17 @@ Deletes given path, no matter what it is (file or directory). | ||
###write(path, content) | ||
Writes content to file. | ||
**parameters:** | ||
`path` path to file. | ||
`content` data to be written. This could be `string`, `buffer`, `object` or `array` (if last two used, the data will be outputed into file as JSON). | ||
**returns:** | ||
Recently used CWD context. | ||
###writeAsync(path, content) | ||
Asynchronous equivalent of `write()` method. The only difference is that it returns promise. | ||
#Matching paths .gitignore style | ||
@@ -314,2 +365,2 @@ | ||
}); | ||
``` | ||
``` |
54127
14
1064
358
6