-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 73dc316
Showing
7 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
reader-stat | ||
=== | ||
|
||
Reads the directory files and adds the stat info. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install reader-stat --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const { readdir } = require('reader-stat'); | ||
|
||
readdir('/').then((files) => { | ||
files.map((Stats) => { | ||
console.log('Stats', Stats.isDirectory()); | ||
// output => true | ||
console.log('Stats:', Stats); | ||
// output => | ||
// Stats { | ||
// dev: 16777220, | ||
// mode: 16877, | ||
// nlink: 26, | ||
// uid: 0, | ||
// gid: 0, | ||
// rdev: 0, | ||
// blksize: 4194304, | ||
// ino: 18208078, | ||
// size: 832, | ||
// blocks: 0, | ||
// atimeMs: 1529987103353.0234, | ||
// mtimeMs: 1506403970873.939, | ||
// ctimeMs: 1506403970873.939, | ||
// birthtimeMs: 1506402494000, | ||
// atime: 2018-06-26T04:25:03.353Z, | ||
// mtime: 2017-09-26T05:32:50.874Z, | ||
// ctime: 2017-09-26T05:32:50.874Z, | ||
// birthtime: 2017-09-26T05:08:14.000Z, | ||
// uidToName: 'root', | ||
// path: '/var', | ||
// basename: 'var', | ||
// extname: '' | ||
// } | ||
}); | ||
}) | ||
``` | ||
|
||
Read a single directory or file the stat info. | ||
|
||
```js | ||
const { getStat } = require('reader-stat'); | ||
|
||
getStat('/var').then((Stats) => { | ||
console.log('Stats', Stats.isDirectory()); | ||
// output => true | ||
console.log('Stats', Stats); | ||
// output => | ||
// Stats { | ||
// dev: 16777220, | ||
// mode: 16877, | ||
// nlink: 26, | ||
// uid: 0, | ||
// gid: 0, | ||
// rdev: 0, | ||
// blksize: 4194304, | ||
// ino: 18208078, | ||
// size: 832, | ||
// blocks: 0, | ||
// atimeMs: 1529987103353.0234, | ||
// mtimeMs: 1506403970873.939, | ||
// ctimeMs: 1506403970873.939, | ||
// birthtimeMs: 1506402494000, | ||
// atime: 2018-06-26T04:25:03.353Z, | ||
// mtime: 2017-09-26T05:32:50.874Z, | ||
// ctime: 2017-09-26T05:32:50.874Z, | ||
// birthtime: 2017-09-26T05:08:14.000Z, | ||
// uidToName: 'root', | ||
// path: '/var', | ||
// basename: 'var', | ||
// extname: '' | ||
// } | ||
}) | ||
``` | ||
|
||
Get all the file names in the directory. | ||
|
||
```js | ||
const { readdirAsync } = require('reader-stat'); | ||
|
||
readdirAsync('/var').then((Stats) => { | ||
console.log('Stats', Stats); | ||
// output => | ||
// [ | ||
// 'agentx', | ||
// 'at', | ||
// 'audit', | ||
// 'backups', | ||
// 'db', | ||
// 'empty', | ||
// 'folders', | ||
// 'install', | ||
// 'jabberd', | ||
// ... | ||
// ] | ||
}) | ||
``` | ||
|
||
```js | ||
const { uidToName } = require('reader-stat'); | ||
|
||
uidToName().then((Users) => { | ||
console.log('Users:', Users); | ||
// output => | ||
// { | ||
// '0': 'root', | ||
// '1': 'daemon', | ||
// '4': '_uucp', | ||
// '13': '_taskgated', | ||
// '24': '_networkd', | ||
// '25': '_installassistant', | ||
// '26': '_lp', | ||
// '27': '_postfix', | ||
// .... | ||
// } | ||
}) | ||
|
||
|
||
uidToName(0).then((User) => { | ||
console.log('User:', User); | ||
// output => root | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const nicki = require('nicki'); | ||
|
||
exports.getStat = async (currentPath, names) => { | ||
if (!names) names = await this.uidToName(); | ||
return new Promise((resolve, reject) => { | ||
fs.stat(currentPath, (err, data) => { | ||
if (err) reject(err); | ||
else { | ||
if (names && names[data.uid]) { | ||
data.uidToName = names[data.uid]; | ||
} | ||
data.path = currentPath; | ||
data.basename = path.basename(currentPath); | ||
data.extname = path.extname(data.basename); | ||
resolve(data); | ||
}; | ||
}) | ||
}); | ||
} | ||
exports.readdirAsync = (dirPath) => { | ||
return new Promise((resolve, reject) => { | ||
fs.readdir(dirPath, (err, filenames) => { | ||
if (err) reject(err); | ||
else resolve(filenames); | ||
}); | ||
}); | ||
} | ||
exports.uidToName = (id) => { | ||
return new Promise((resolve, reject) => { | ||
nicki((err, names) => { | ||
if (err) reject(err); | ||
else if (id || id === 0) { | ||
resolve(names[id]); | ||
} else resolve(names); | ||
}); | ||
}); | ||
} | ||
exports.readdir = (dirPath) => { | ||
return this.readdirAsync(dirPath).then(async (filenames) => { | ||
const names = await this.uidToName(); | ||
return Promise.all(filenames.map((filename) => { | ||
return this.getStat(path.join(dirPath, filename), names); | ||
})); | ||
}) | ||
} |
Oops, something went wrong.