@wrote/read-dir-structure
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,16 +0,5 @@ | ||
"use strict"; | ||
const { lstat, readdir } = require('fs'); | ||
let makePromise = require('makepromise'); if (makePromise && makePromise.__esModule) makePromise = makePromise.default; | ||
const { resolve } = require('path'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = readDirStructure; | ||
var _fs = require("fs"); | ||
var _makepromise = _interopRequireDefault(require("makepromise")); | ||
var _path = require("path"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -23,14 +12,15 @@ * Update information about directory's content with lstat. | ||
async function lstatFiles(dirPath, dirContent) { | ||
const readFiles = dirContent.map(async relativePath => { | ||
const path = (0, _path.resolve)(dirPath, relativePath); | ||
const ls = await (0, _makepromise.default)(_fs.lstat, path); | ||
const readFiles = dirContent.map(async (relativePath) => { | ||
const path = resolve(dirPath, relativePath) | ||
const ls = await makePromise(lstat, path) | ||
return { | ||
lstat: ls, | ||
path, | ||
relativePath | ||
}; | ||
}); | ||
const res = await Promise.all(readFiles); | ||
return res; | ||
relativePath, | ||
} | ||
}) | ||
const res = await Promise.all(readFiles) | ||
return res | ||
} | ||
/** | ||
@@ -41,5 +31,3 @@ * Check if lstat result is a directory | ||
*/ | ||
const isDirectory = lstatRes => lstatRes.lstat.isDirectory(); | ||
const isDirectory = lstatRes => lstatRes.lstat.isDirectory() | ||
/** | ||
@@ -50,19 +38,16 @@ * Check if lstat result is not a directory | ||
*/ | ||
const isNotDirectory = lstatRes => !lstatRes.lstat.isDirectory() | ||
const isNotDirectory = lstatRes => !lstatRes.lstat.isDirectory(); | ||
const getType = lstatRes => { | ||
const getType = (lstatRes) => { | ||
if (lstatRes.lstat.isDirectory()) { | ||
return 'Directory'; | ||
return 'Directory' | ||
} | ||
if (lstatRes.lstat.isFile()) { | ||
return 'File'; | ||
return 'File' | ||
} | ||
if (lstatRes.lstat.isSymbolicLink()) { | ||
return 'SymbolicLink'; | ||
return 'SymbolicLink' | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -94,48 +79,47 @@ * Read a directory, and return its structure as an object. Only `Files`, `Directories` and `Symlinks` are included! | ||
*/ | ||
async function readDirStructure(dirPath) { | ||
async function readDirStructure(dirPath) { | ||
if (!dirPath) { | ||
throw new Error('Please specify a path to the directory'); | ||
throw new Error('Please specify a path to the directory') | ||
} | ||
const ls = await (0, _makepromise.default)(_fs.lstat, dirPath); | ||
const ls = await makePromise(lstat, dirPath) | ||
if (!ls.isDirectory()) { | ||
const err = new Error('Path is not a directory'); | ||
err.code = 'ENOTDIR'; | ||
throw err; | ||
const err = new Error('Path is not a directory') | ||
err.code = 'ENOTDIR' | ||
throw err | ||
} | ||
const dir = await makePromise(readdir, dirPath) | ||
const lsr = await lstatFiles(dirPath, dir) | ||
const dir = await (0, _makepromise.default)(_fs.readdir, dirPath); | ||
const lsr = await lstatFiles(dirPath, dir); | ||
const directories = lsr.filter(isDirectory); // reduce at once | ||
const directories = lsr.filter(isDirectory) // reduce at once | ||
const notDirectories = lsr.filter(isNotDirectory) | ||
const notDirectories = lsr.filter(isNotDirectory); | ||
const files = notDirectories.reduce((acc, current) => { | ||
const type = getType(current); | ||
return { ...acc, | ||
const type = getType(current) | ||
return { | ||
...acc, | ||
[current.relativePath]: { | ||
type | ||
} | ||
}; | ||
}, {}); | ||
const dirs = await directories.reduce(async (acc, { | ||
path, | ||
relativePath | ||
}) => { | ||
const res = await acc; | ||
const structure = await readDirStructure(path); | ||
return { ...res, | ||
[relativePath]: structure | ||
}; | ||
}, {}); | ||
const content = { ...files, | ||
...dirs | ||
}; | ||
type, | ||
}, | ||
} | ||
}, {}) | ||
const dirs = await directories.reduce(async (acc, { path, relativePath }) => { | ||
const res = await acc | ||
const structure = await readDirStructure(path) | ||
return { | ||
...res, | ||
[relativePath]: structure, | ||
} | ||
}, {}) | ||
const content = { | ||
...files, | ||
...dirs, | ||
} | ||
return { | ||
content, | ||
type: 'Directory' | ||
}; | ||
type: 'Directory', | ||
} | ||
} | ||
/** | ||
@@ -158,2 +142,4 @@ * A directory structure representation | ||
*/ | ||
//# sourceMappingURL=index.js.map | ||
module.exports = readDirStructure |
@@ -0,1 +1,8 @@ | ||
## 15 January 2019 | ||
### 1.0.2 | ||
- [package] Add the "module" field. | ||
- [package] Remove source maps, migrate to [ÀLaMode](https://alamode.cc). | ||
## 4 July 2018 | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@wrote/read-dir-structure", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Reads directory structure.", | ||
"main": "build", | ||
"module": "src/index.js", | ||
"scripts": { | ||
"t": "zoroaster -b", | ||
"test": "zoroaster -b test/spec", | ||
"test-build": "BABEL_ENV=test-build yarn test", | ||
"t": "zoroaster -a", | ||
"test": "zoroaster -a test/spec", | ||
"test-build": "ALAMODE_ENV=test-build yarn test", | ||
"test-all": "yarn-s test test-build", | ||
"test-watch": "yarn test -w", | ||
"lint": "eslint .", | ||
"doc": "NODE_DEBUG=doc doc README-source.md -o README.md", | ||
"doc": "NODE_DEBUG=doc doc documentary -o README.md", | ||
"e": "node example", | ||
"example/": "yarn e example/example.js", | ||
"b": "b --source-maps", | ||
"b": "alamode src -o build -s", | ||
"build": "yarn-s doc b" | ||
}, | ||
"files": [ | ||
"build" | ||
"build", | ||
"src" | ||
], | ||
@@ -49,11 +51,12 @@ "repository": { | ||
"devDependencies": { | ||
"documentary": "1.8.2", | ||
"eslint-config-artdeco": "1.0.0", | ||
"snapshot-context": "2.0.1", | ||
"alamode": "1.6.1", | ||
"documentary": "1.20.1", | ||
"eslint-config-artdeco": "1.0.1", | ||
"snapshot-context": "2.0.4", | ||
"yarn-s": "1.1.0", | ||
"zoroaster": "2.1.0" | ||
"zoroaster": "3.6.6" | ||
}, | ||
"dependencies": { | ||
"makepromise": "2.0.0" | ||
"makepromise": "3.0.2" | ||
} | ||
} |
@@ -18,2 +18,3 @@ # @wrote/read-dir-structure | ||
* [Reasons for Errors](#reasons-for-errors) | ||
- [Copyright](#copyright) | ||
@@ -32,8 +33,7 @@ ## API | ||
| Property | Type | Description | | ||
| -------- | ---- | ----------- | | ||
| type | `string` | The result of the _lstat_ and one of the following: `Directory`, `File`, `SymbolicLink`. | | ||
| content | `Structure` | If the type is `Directory`, the object will also have a `content` which also is a `Structure`. Therefore, the whole nested structure will be read. See below for an example. | | ||
| Property | Type | Description | | ||
| -------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| type | `string` | The result of the _lstat_ and one of the following: `Directory`, `File`, `SymbolicLink`. | | ||
| content | `Structure` | If the type is `Directory`, the object will also have a `content` which also is a `Structure`. Therefore, the whole nested structure will be read. See below for an example. | | ||
### `async readDirStructure(`<br/> `path: string,`<br/>`): Structure` | ||
@@ -90,13 +90,15 @@ | ||
| Happens when... | code | Message | | ||
| --------------- | ---- | ------- | | ||
| not passing any path | `-` | Please specify a path to the directory | | ||
| passing a path to a symbolic link | `ENOTDIR` | Path is not a directory | | ||
| passing a path to a file | `ENOTDIR` | Path is not a directory | | ||
| directory does not exist | `ENOENT` | ENOENT: no such file or directory, lstat '%DIRECTORY%' | | ||
| Happens when... | code | Message | | ||
| --------------------------------- | --------- | ------------------------------------------------------ | | ||
| not passing any path | `-` | Please specify a path to the directory | | ||
| passing a path to a symbolic link | `ENOTDIR` | Path is not a directory | | ||
| passing a path to a file | `ENOTDIR` | Path is not a directory | | ||
| directory does not exist | `ENOENT` | ENOENT: no such file or directory, lstat '%DIRECTORY%' | | ||
--- | ||
## Copyright | ||
(c) [Art Deco][1] 2018 | ||
(c) [Wrote][1] 2019 | ||
[1]: https://artdeco.bz | ||
[1]: https://wrote.cc | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/-1.svg?sanitize=true"></a></p> |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
254
101
13531
6
6
2
1
+ Added@artdeco/clean-stack@1.0.1(transitive)
+ Addederotic@2.0.2(transitive)
+ Addedmakepromise@3.0.2(transitive)
- Removedclean-stack@1.3.0(transitive)
- Removederotic@1.0.1(transitive)
- Removedmakepromise@2.0.0(transitive)
Updatedmakepromise@3.0.2