diff --git a/docs/File.html b/docs/File.html index 6f4926b..3b106b4 100644 --- a/docs/File.html +++ b/docs/File.html @@ -24,7 +24,7 @@
@@ -102,7 +102,7 @@

new FileSource:
@@ -202,7 +202,7 @@

(static) creat
Source:
@@ -250,6 +250,206 @@

Example
+

getAbsolutePath()

+ + + + + +
+

Returns the absolutePath

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

String

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('myFile');
+console.log(file.getAbsolutePath());
+ + + + + + + + +

getCanonicalPath()

+ + + + + +
+

Returns the canonical path

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

String

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('myFile');
+console.log(file.getCanonicalPath());
+ + + + + + + +

getDepthSync()

@@ -301,7 +501,7 @@

getDepthS
Source:
@@ -462,7 +662,7 @@

Parameters:
Source:
@@ -506,7 +706,7 @@
Example
const file = File.create('./myDirectory'); file.getFiles('*.json') .then((jsonFiles) => { - console.log(files.map(file => file.lastModifiedSync())); + console.log(jsonFiles.map(file => file.lastModifiedSync())); }); @@ -628,7 +828,7 @@
Parameters:
Source:
@@ -733,7 +933,7 @@

getListSyn
Source:
@@ -834,7 +1034,7 @@

getNameSource:
@@ -883,14 +1083,14 @@
Example
-

isDirectory()

+

getPathExtension()

-

Determine if pathname is a file

+

Returns the file extension.

@@ -934,7 +1134,7 @@

isDirector
Source:
@@ -961,8 +1161,7 @@

Returns:
-

If the Promise fulfils, the fulfilment value is -a boolean indicating if the pathname is a file

+

String

@@ -974,10 +1173,8 @@
Example
import File from 'file-js';
 
-const file = File.create('myDirectory');
-file.isFile((isFile) => {
-  console.log(isFile);
-});
+const file = File.create('./tmp.sh'); +console.log(file.getPathExtension()); // sh @@ -1037,7 +1234,7 @@

isDirector
Source:
@@ -1140,7 +1337,7 @@

isDirector
Source:
@@ -1192,14 +1389,14 @@

Example
-

isDirectorySync()

+

isDirectory()

-

Synchronously determine if pathname is a directory

+

Determine if pathname is a file

@@ -1243,7 +1440,7 @@

isDire
Source:
@@ -1270,7 +1467,8 @@

Returns:
-

boolean

+

If the Promise fulfils, the fulfilment value is +a boolean indicating if the pathname is a file

@@ -1283,9 +1481,9 @@
Example
import File from 'file-js';
 
 const file = File.create('myDirectory');
-if (file.isDirectorySync()) {
-   console.log('processing directory');
-}
+file.isFile((isFile) => { + console.log(isFile); +}); @@ -1294,14 +1492,14 @@
Example
-

isFileSync()

+

isDirectorySync()

-

Synchronously determine if pathname is a file

+

Synchronously determine if pathname is a directory

@@ -1345,7 +1543,7 @@

isFileSync<
Source:
@@ -1385,8 +1583,8 @@

Example
import File from 'file-js';
 
 const file = File.create('myDirectory');
-if (file.isFileSync()) {
-   console.log('processing file');
+if (file.isDirectorySync()) {
+   console.log('processing directory');
 }
@@ -1396,14 +1594,14 @@
Example
-

isHiddenSync()

+

isFileSync()

-

Synchronously determine if pathname is a hidden file

+

Synchronously determine if pathname is a file

@@ -1447,7 +1645,7 @@

isHiddenS
Source:
@@ -1486,9 +1684,9 @@

Example
import File from 'file-js';
 
-const file = File.create('./myHiddenFile');
-if (file.isHiddenSync()) {
-   console.log('processing hidden file');
+const file = File.create('myDirectory');
+if (file.isFileSync()) {
+   console.log('processing file');
 }
@@ -1498,14 +1696,14 @@
Example
-

rename(pathname)

+

isHiddenSync()

-

Renames the abstract pathname

+

Synchronously determine if pathname is a hidden file

@@ -1516,31 +1714,338 @@

renameParameters:

- - - - - - - - - - +
-
- - + - - - + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + +
Returns:
+ + +
+

boolean

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('./myHiddenFile');
+if (file.isHiddenSync()) {
+   console.log('processing hidden file');
+}
+ + + + + + + + +

isSocket()

+ + + + + +
+

Determine if pathname is a Socket

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

If the Promise fulfils, the fulfilment value is +a boolean indicating if the pathname is a Socket

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('mySocket');
+file.isSocket((isSocket) => {
+  console.log(isSocket);
+});
+ + + + + + + + +

issocketSync()

+ + + + + +
+

Synchronously determine if pathname is a socket

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

boolean

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('mysocket');
+if (file.issocketSync()) {
+   console.log('processing socket');
+}
+ + + + + + + + +

rename(pathname)

+ + + + + +
+

Renames the abstract pathname

+
+ + + + + + + + + +
Parameters:
+ + +
NameTypeDescription
+ + + + + + + + + + + + + + + + + + + + + @@ -1601,7 +2106,7 @@
Parameters:
Source:
@@ -1651,6 +2156,110 @@
Example
+ + + + +

withLock()

+ + + + + +
+

Locks the pathname

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

returning value of function

+
+ + + + + + +
Example
+ +
import File from 'file-js';
+
+const file = File.create('myFile');
+file.with(() => {
+  if (file.isFileSync()) {
+    file.delete();
+  } 
+});
+ + + + @@ -1668,7 +2277,7 @@
Example

- Documentation generated by JSDoc 3.4.3 on Sat Feb 25 2017 11:23:08 GMT+0000 (GMT) using the Minami theme. + Documentation generated by JSDoc 3.4.3 on Fri Mar 10 2017 12:51:17 GMT+0000 (GMT) using the Minami theme.
diff --git a/docs/file.js.html b/docs/file.js.html index d3a6f9d..5eba3a6 100644 --- a/docs/file.js.html +++ b/docs/file.js.html @@ -24,7 +24,7 @@
@@ -43,10 +43,11 @@

file.js

import path from 'path'; import fileGlob from 'minimatch'; -import fs from './fs'; +import fsp from './fs'; +import fs from 'fs'; import filelock from './lock'; -const fsp = Promise.promisifyAll(fs); +// const fsp = Promise.promisifyAll(fs); function joinWith(dir) { return (file) => { @@ -64,7 +65,7 @@

file.js

} _getStatsSync() { - return fs.statSync(this._pathname); + return fsp.statSync(this._pathname); } _getStats() { @@ -115,6 +116,26 @@

file.js

return this._getStatsSync().isDirectory(); } + /** + * Synchronously determine if pathname is a socket + * + * @instance + * @memberOf File + * @method + * issocketSync + * @return boolean + * @example + * import File from 'file-js'; + * + * const file = File.create('mysocket'); + * if (file.issocketSync()) { + * console.log('processing socket'); + * } + */ + isSocketSync() { + return this._getStatsSync().isSocket(); + } + /** * Synchronously determine if pathname is a file * @@ -157,6 +178,28 @@

file.js

return this._checkAsyncStats('isDirectory'); } + /** + * Determine if pathname is a Socket + * + * @instance + * @memberOf File + * @method + * isSocket + * @return If the Promise fulfils, the fulfilment value is + * a boolean indicating if the pathname is a Socket + * @example + * import File from 'file-js'; + * + * const file = File.create('mySocket'); + * file.isSocket((isSocket) => { + * console.log(isSocket); + * }); + * + */ + isSocket() { + return this._checkAsyncStats('isSocket'); + } + /** * Determine if pathname is a file * @@ -328,7 +371,7 @@

file.js

* const file = File.create('./myDirectory'); * file.getFiles('*.json') * .then((jsonFiles) => { - * console.log(files.map(file => file.lastModifiedSync())); + * console.log(jsonFiles.map(file => file.lastModifiedSync())); * }); */ getFiles(glob) { @@ -387,6 +430,20 @@

file.js

return this._pathname; } + /** + * Returns the absolutePath + * + * @instance + * @memberOf File + * @method + * getAbsolutePath + * @return String + * @example + * import File from 'file-js'; + * + * const file = File.create('myFile'); + * console.log(file.getAbsolutePath()); + */ getAbsolutePath() { if (path.isAbsolute(this._pathname)) { return this._pathname; @@ -394,10 +451,38 @@

file.js

return [this._dir, this._pathname].join(path.sep); } + /** + * Returns the canonical path + * + * @instance + * @memberOf File + * @method + * getCanonicalPath + * @return String + * @example + * import File from 'file-js'; + * + * const file = File.create('myFile'); + * console.log(file.getCanonicalPath()); + */ getCanonicalPath() { return path.normalize(this.getAbsolutePath()); } + /** + * Returns the file extension. + * + * @instance + * @memberOf File + * @method + * getPathExtension + * @return String + * @example + * import File from 'file-js'; + * + * const file = File.create('./tmp.sh'); + * console.log(file.getPathExtension()); // sh + */ getPathExtension() { return path.extname(this._pathname).substring(1); } @@ -441,6 +526,24 @@

file.js

return fsp.unlinkAsync(this._pathname); } + /** + * Locks the pathname + * + * @instance + * @memberOf File + * @method + * withLock + * @return returning value of function + * @example + * import File from 'file-js'; + * + * const file = File.create('myFile'); + * file.with(() => { + * if (file.isFileSync()) { + * file.delete(); + * } + * }); + */ withLock(fn) { return filelock.lockAsync(this._pathname) .then(() => { @@ -469,8 +572,7 @@

file.js

} } -module.exports.create = File.create; - +module.exports.create = File.create; @@ -482,7 +584,7 @@

file.js


- Documentation generated by JSDoc 3.4.3 on Sat Feb 25 2017 11:23:08 GMT+0000 (GMT) using the Minami theme. + Documentation generated by JSDoc 3.4.3 on Fri Mar 10 2017 12:51:16 GMT+0000 (GMT) using the Minami theme.
diff --git a/docs/index.html b/docs/index.html index 9527b5d..e3c28a3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -24,7 +24,7 @@
@@ -121,7 +121,7 @@

Installation

npm install --save fi
 
- Documentation generated by JSDoc 3.4.3 on Sat Feb 25 2017 11:23:08 GMT+0000 (GMT) using the Minami theme. + Documentation generated by JSDoc 3.4.3 on Fri Mar 10 2017 12:51:16 GMT+0000 (GMT) using the Minami theme.
diff --git a/src/file.js b/src/file.js index 0ee94e4..5260eb3 100644 --- a/src/file.js +++ b/src/file.js @@ -2,10 +2,11 @@ import Promise from 'bluebird'; import path from 'path'; import fileGlob from 'minimatch'; -import fs from './fs'; +import fsp from './fs'; +import fs from 'fs'; import filelock from './lock'; -const fsp = Promise.promisifyAll(fs); +// const fsp = Promise.promisifyAll(fs); function joinWith(dir) { return (file) => { @@ -23,7 +24,7 @@ class File { } _getStatsSync() { - return fs.statSync(this._pathname); + return fsp.statSync(this._pathname); } _getStats() { diff --git a/src/fs.js b/src/fsp.js similarity index 100% rename from src/fs.js rename to src/fsp.js diff --git a/test/file.js b/test/file.js index 9e973ae..e1659e6 100644 --- a/test/file.js +++ b/test/file.js @@ -5,7 +5,8 @@ import File from '../lib/file'; import moment from 'moment'; import sinon from 'sinon'; import Promise from 'bluebird'; -import fs from '../lib/fs'; +// import fsp from '../lib/fs'; +import fsp from '../lib/fsp'; import filelock from '../lib/lock'; const sandbox = sinon.sandbox.create(); @@ -32,13 +33,13 @@ function formatDate(date) { function createFile(fname, opts) { const time = new Date(moment().subtract(opts.duration, opts.modifier)); - const fd = fs.openSync(fname, 'w+'); - fs.futimesSync(fd, time, time); - fs.closeSync(fd); + const fd = fsp.openSync(fname, 'w+'); + fsp.futimesSync(fd, time, time); + fsp.closeSync(fd); } function deleteFile(fname) { - return fs.unlinkSync(fname); + return fsp.unlinkSync(fname); } function deleteFileIfExists(fname) { @@ -50,6 +51,10 @@ function deleteFileIfExists(fname) { } describe('File', () => { + afterEach(() => { + sandbox.restore(); + }); + describe('.isDirectorySync', () => { it('returns true when a pathname is a directory', () => { const file = File.create(getFixturePath('/justFiles')); @@ -82,19 +87,33 @@ describe('File', () => { describe('.isSocketSync', () => { it('returns true when a pathname is a socket', () => { - const file = File.create(getFixturePath('/types/socketfile1')); + const statsSync = { + isSocket: () => { + return true; + } + }; + sandbox.stub(fsp, 'statSync').returns(statsSync); + + const file = File.create(getFixturePath('/types/mySocketfile')); assert(file.isSocketSync()); }); it('returns false when a pathname is not a socket', () => { - const file = File.create(getFixturePath('/types/file.json')); + const file = File.create(getFixturePath('/justFiles/a.json')); assert(!file.isSocketSync()); }); }); describe('.isSocket', () => { it('returns true when a pathname is a Socket', () => { - const file = File.create(getFixturePath('/types/socketfile1')); + const stats = { + isSocket: () => { + return true; + } + }; + sandbox.stub(fsp, 'statAsync').returns(Promise.resolve(stats)); + + const file = File.create(getFixturePath('/types/mySocketfile')); return file.isSocket() .then((isSocket) => { return assert(isSocket); @@ -102,7 +121,7 @@ describe('File', () => { }); it('returns false when a pathname is not a Socket', () => { - const file = File.create(getFixturePath('/types/file.json')); + const file = File.create(getFixturePath('/justFiles/a.json')); return file.isSocket() .then((isSocket) => { return assert(!isSocket); @@ -122,7 +141,7 @@ describe('File', () => { }); afterEach(() => { - fs.readdir(getFixturePath('rename'), (err, files) => { + fsp.readdir(getFixturePath('rename'), (err, files) => { files.forEach((file) => { if (!_.startsWith(file, '.keep')) { deleteFileIfExists(getFixturePath(`/rename/${file}`)); @@ -139,8 +158,8 @@ describe('File', () => { return original .rename(renameTo) .then(() => { - assert.strictEqual(fs.existsSync(oldname), false); - assert.strictEqual(fs.existsSync(newname), true); + assert.strictEqual(fsp.existsSync(oldname), false); + assert.strictEqual(fsp.existsSync(newname), true); assert.equal(original.getName(), newname); return; }); @@ -153,8 +172,8 @@ describe('File', () => { return original .rename(newname) .then(() => { - assert.strictEqual(fs.existsSync(oldname), false); - assert.strictEqual(fs.existsSync(newname), true); + assert.strictEqual(fsp.existsSync(oldname), false); + assert.strictEqual(fsp.existsSync(newname), true); assert.equal(original.getName(), newname); return; }); @@ -380,11 +399,11 @@ describe('File', () => { describe('.lastModified', () => { before(() => { - fs.mkdirSync(getFixturePath('dates')); + fsp.mkdirSync(getFixturePath('dates')); }); after(() => { - fs.rmdirSync(getFixturePath('dates')); + fsp.rmdirSync(getFixturePath('dates')); }); const files = [ @@ -437,11 +456,11 @@ describe('File', () => { describe('.lastAccessed', () => { before(() => { - fs.mkdirSync(getFixturePath('dates')); + fsp.mkdirSync(getFixturePath('dates')); }); after(() => { - fs.rmdirSync(getFixturePath('dates')); + fsp.rmdirSync(getFixturePath('dates')); }); const files = [ @@ -496,9 +515,9 @@ describe('File', () => { let statSync; before(() => { - fs.mkdirSync(getFixturePath('dates')); + fsp.mkdirSync(getFixturePath('dates')); - statSync = sandbox.stub(fs, 'statSync'); + statSync = sandbox.stub(fsp, 'statSync'); statSync.returns({ isDirectory: function () { return true; @@ -507,7 +526,7 @@ describe('File', () => { }); after(() => { - fs.rmdirSync(getFixturePath('dates')); + fsp.rmdirSync(getFixturePath('dates')); sandbox.restore(); }); @@ -628,11 +647,11 @@ describe('File', () => { describe('.delete', () => { before(() => { - fs.mkdirSync(getFixturePath('delete')); + fsp.mkdirSync(getFixturePath('delete')); }); after(() => { - fs.rmdirSync(getFixturePath('delete')); + fsp.rmdirSync(getFixturePath('delete')); }); const fileToDelete = getFixturePath('delete/a.txt'); @@ -649,7 +668,7 @@ describe('File', () => { return file.delete() .then(() => { assert.throws(() => { - fs.statSync(getFixturePath('delete/a.txt')); + fsp.statSync(getFixturePath('delete/a.txt')); }, /ENOENT/); }); }); diff --git a/test/fixtures/types/file.json b/test/fixtures/types/file.json deleted file mode 100644 index e69de29..0000000
NameTypeDescription
pathname