Skip to content

Commit

Permalink
Init project.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 26, 2018
0 parents commit 73dc316
Show file tree
Hide file tree
Showing 7 changed files with 507 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added .npmignore
Empty file.
135 changes: 135 additions & 0 deletions README.md
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
})
```
47 changes: 47 additions & 0 deletions index.js
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);
}));
})
}
Loading

0 comments on commit 73dc316

Please sign in to comment.