@wrote/read-dir-structure
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -8,4 +8,4 @@ const { lstat, readdir } = require('fs'); | ||
* @param {string} dirPath Path to the root directory | ||
* @param {string[]} dirContent | ||
* @returns {File[]} An array with file objects. | ||
* @param {!Array<string>} dirContent | ||
* @returns {Promise<Array<_readDirStructure.File>>} An array with file objects. | ||
*/ | ||
@@ -28,3 +28,6 @@ async function lstatFiles(dirPath, dirContent) { | ||
* Check if lstat result is a directory | ||
* @param {LstatRes} lstatRes Result of lib.lstatFiles | ||
* @param {_readDirStructure.File} lstatRes | ||
* @param {!fs.Stats} lstatRes.lstat The stats of the item. | ||
* @param {string} lstatRes.path The full path of the item. | ||
* @param {string} lstatRes.relativePath The name of the item. | ||
* @returns {boolean} true if is a directory | ||
@@ -35,3 +38,6 @@ */ | ||
* Check if lstat result is not a directory | ||
* @param {LstatRes} lstatRes Result of lib.lstatFiles | ||
* @param {_readDirStructure.File} lstatRes | ||
* @param {!fs.Stats} lstatRes.lstat The stats of the item. | ||
* @param {string} lstatRes.path The full path of the item. | ||
* @param {string} lstatRes.relativePath The name of the item. | ||
* @returns {boolean} true if is not a directory | ||
@@ -56,24 +62,24 @@ */ | ||
* @param {string} dirPath Path to the directory. | ||
* @returns {Promise.<DirectoryStructure>} An object reflecting the directory structure. | ||
* @returns {Promise<_readDirStructure.DirectoryStructure>} An object reflecting the directory structure. | ||
* @example | ||
* | ||
* const res = await readDirStructure('dir') | ||
* | ||
* { | ||
* type: 'Directory', | ||
* content: { | ||
* 'data.txt': { | ||
* type: 'File' | ||
* }, | ||
* subdir: { | ||
* type: 'Directory', | ||
* content: { | ||
* 'data-ln.txt': { | ||
* type: 'SymbolicLink' | ||
* }, | ||
* } | ||
* } | ||
* } | ||
* } | ||
```js | ||
const res = await readDirStructure('dir') | ||
// result: | ||
{ | ||
type: 'Directory', | ||
content: { | ||
'data.txt': { | ||
type: 'File' | ||
}, | ||
subdir: { | ||
type: 'Directory', | ||
content: { | ||
'data-ln.txt': { | ||
type: 'SymbolicLink' | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
*/ | ||
@@ -90,3 +96,3 @@ async function readDirStructure(dirPath) { | ||
} | ||
const dir = await makePromise(readdir, dirPath) | ||
const dir = /** @type {!Array<string>} */ (await makePromise(readdir, dirPath)) | ||
const lsr = await lstatFiles(dirPath, dir) | ||
@@ -128,3 +134,3 @@ | ||
* After running the `readDirStructure`, this function can be used to flatten the `content` output and return the list of all files (not including symlinks). | ||
* @param {Object<string, DirectoryStructure>} content The computed content. | ||
* @param {!_readDirStructure.Content} content The recursive content of the directory. | ||
* @param {string} path The path to the directory. | ||
@@ -141,3 +147,4 @@ */ | ||
const dirFiles = dirs.reduce((acc, dir) => { | ||
const { content: c } = content[dir] | ||
const { content: c } = | ||
/** @type {!_readDirStructure.Content} */ (content[dir]) | ||
const f = getFiles(c, join(path, dir)) | ||
@@ -149,19 +156,36 @@ return [...acc, ...f] | ||
/* typal types/index.xml */ | ||
/** | ||
* A directory structure representation | ||
* { dir: subdir: { 'fileA.txt': 'foo', 'fileB.js': 'bar' }, 'fileC.jpg': 'baz' } | ||
* @typedef {Object} LstatRes | ||
* @property {fs.Stats} lstat | ||
* @property {string} relativePath | ||
* | ||
* A directory structure representation | ||
* @typedef {Object} DirectoryStructure | ||
* @property {string} type File type, e.g., Directory, File, Symlink | ||
* @property {Object.<string, DirectoryStructureA>} [content] Content if directory. | ||
* | ||
* | ||
* @typedef {Object} DirectoryStructureA | ||
* @property {string} type File type, e.g., Directory, File, Symlink | ||
* @property {'etc'} [content] Content if directory. | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.File} File | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object} _readDirStructure.File | ||
* @prop {!fs.Stats} lstat The stats of the item. | ||
* @prop {string} path The full path of the item. | ||
* @prop {string} relativePath The name of the item. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.Content} Content The recursive content of the directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object<string, !_readDirStructure.DirectoryStructure>} _readDirStructure.Content The recursive content of the directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.DirectoryStructure} DirectoryStructure A directory structure representation. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object} _readDirStructure.DirectoryStructure A directory structure representation. | ||
* @prop {string} [type] The type of the item. | ||
* @prop {!_readDirStructure.Content} [content] The recursive content if the item is a directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {import('fs').Stats} fs.Stats | ||
*/ | ||
@@ -168,0 +192,0 @@ |
@@ -0,1 +1,9 @@ | ||
## 10 April 2019 | ||
### [1.2.0](https://github.com/wrote/read-dir-structure/compare/v1.1.0...v1.2.0) | ||
- [externs] Provide externs for _Google Closure Compiler_ via _Depack_. | ||
- [doc] Document types recursively. | ||
- [doc] Update `makepromise`. | ||
## 5 April 2019 | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "@wrote/read-dir-structure", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Reads directory structure.", | ||
@@ -15,2 +15,4 @@ "main": "build/index.js", | ||
"doc": "NODE_DEBUG=doc doc documentary -o README.md", | ||
"d": "typal src/index.js -c", | ||
"externs": "typal externs.js -e", | ||
"e": "alanode", | ||
@@ -23,4 +25,6 @@ "example/": "yarn e example/example", | ||
"build", | ||
"src" | ||
"src", | ||
"externs.js" | ||
], | ||
"externs": "externs.js", | ||
"repository": { | ||
@@ -60,4 +64,4 @@ "type": "git", | ||
"dependencies": { | ||
"makepromise": "^3.0.3" | ||
"makepromise": "^3.1.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # @wrote/read-dir-structure | ||
`@wrote/read-dir-structure` is Node.js package to a read directory structure. | ||
`@wrote/read-dir-structure` is Node.JS package to a read directory structure. | ||
@@ -16,5 +16,5 @@ ```sh | ||
- [API](#api) | ||
* [`Structure` Type](#structure-type) | ||
* [`async readDirStructure(path: string): Structure`](#async-readdirstructurepath-string-structure) | ||
* [`async getFiles(content: Structure.content, path: string): Array<string>`](#async-getfilescontent-structurecontentpath-string-arraystring) | ||
- [Types](#types) | ||
- [`async readDirStructure(path: string): DirectoryStructure`](#async-readdirstructurepath-string-directorystructure) | ||
- [`async getFiles(content: Content, path: string): Array<string>`](#async-getfilescontent-contentpath-string-arraystring) | ||
- [Reasons for Errors](#reasons-for-errors) | ||
@@ -31,15 +31,21 @@ - [Copyright](#copyright) | ||
### `Structure` Type | ||
The types and [externs](externs.js) for _Google Closure Compiler_ via [**_Depack_**](https://github.com/dpck/depack) are defined in the `_readDirStructure` namespace. | ||
The return type of the function is a directory `Structure`. It is an associative array which contains the next properties: | ||
## Types | ||
| 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. | | ||
The return type of the function is a _DirectoryStructure_. It is a recursive object, where items have either `File`, `Directory` or `SymLink` types specified in the `type` field, and if the item is a directory, it has the `content` property which is another _DirectoryStructure_. | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/0.svg?sanitize=true" width="25"></a></p> | ||
`Object<string, !DirectoryStructure>` __<a name="type-content">`Content`</a>__: The recursive content of the directory. | ||
### `async readDirStructure(`<br/> `path: string,`<br/>`): Structure` | ||
__<a name="type-directorystructure">`DirectoryStructure`</a>__: A directory structure representation. | ||
| Name | Type | Description | | ||
| ------- | --------------------------- | ------------------------------------------------- | | ||
| type | _string_ | The type of the item. | | ||
| content | _[!Content](#type-content)_ | The recursive content if the item is a directory. | | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/0.svg?sanitize=true"></a></p> | ||
## `async readDirStructure(`<br/> `path: string,`<br/>`): DirectoryStructure` | ||
Reads the structure of the directory. | ||
@@ -90,6 +96,7 @@ | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/1.svg?sanitize=true" width="25"></a></p> | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/1.svg?sanitize=true"></a></p> | ||
### `async getFiles(`<br/> `content: Structure.content,`<br/> `path: string,`<br/>`): Array<string>` | ||
## `async getFiles(`<br/> `content: Content,`<br/> `path: string,`<br/>`): Array<string>` | ||
After running the `readDirStructure`, this function can be used to flatten the `content` output and return the list of all files (not including symlinks). | ||
@@ -108,3 +115,2 @@ | ||
``` | ||
```json | ||
@@ -111,0 +117,0 @@ [ |
110
src/index.js
@@ -8,4 +8,4 @@ import { lstat, readdir } from 'fs' | ||
* @param {string} dirPath Path to the root directory | ||
* @param {string[]} dirContent | ||
* @returns {File[]} An array with file objects. | ||
* @param {!Array<string>} dirContent | ||
* @returns {Promise<Array<_readDirStructure.File>>} An array with file objects. | ||
*/ | ||
@@ -28,3 +28,6 @@ async function lstatFiles(dirPath, dirContent) { | ||
* Check if lstat result is a directory | ||
* @param {LstatRes} lstatRes Result of lib.lstatFiles | ||
* @param {_readDirStructure.File} lstatRes | ||
* @param {!fs.Stats} lstatRes.lstat The stats of the item. | ||
* @param {string} lstatRes.path The full path of the item. | ||
* @param {string} lstatRes.relativePath The name of the item. | ||
* @returns {boolean} true if is a directory | ||
@@ -35,3 +38,6 @@ */ | ||
* Check if lstat result is not a directory | ||
* @param {LstatRes} lstatRes Result of lib.lstatFiles | ||
* @param {_readDirStructure.File} lstatRes | ||
* @param {!fs.Stats} lstatRes.lstat The stats of the item. | ||
* @param {string} lstatRes.path The full path of the item. | ||
* @param {string} lstatRes.relativePath The name of the item. | ||
* @returns {boolean} true if is not a directory | ||
@@ -56,24 +62,24 @@ */ | ||
* @param {string} dirPath Path to the directory. | ||
* @returns {Promise.<DirectoryStructure>} An object reflecting the directory structure. | ||
* @returns {Promise<_readDirStructure.DirectoryStructure>} An object reflecting the directory structure. | ||
* @example | ||
* | ||
* const res = await readDirStructure('dir') | ||
* | ||
* { | ||
* type: 'Directory', | ||
* content: { | ||
* 'data.txt': { | ||
* type: 'File' | ||
* }, | ||
* subdir: { | ||
* type: 'Directory', | ||
* content: { | ||
* 'data-ln.txt': { | ||
* type: 'SymbolicLink' | ||
* }, | ||
* } | ||
* } | ||
* } | ||
* } | ||
```js | ||
const res = await readDirStructure('dir') | ||
// result: | ||
{ | ||
type: 'Directory', | ||
content: { | ||
'data.txt': { | ||
type: 'File' | ||
}, | ||
subdir: { | ||
type: 'Directory', | ||
content: { | ||
'data-ln.txt': { | ||
type: 'SymbolicLink' | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
*/ | ||
@@ -90,3 +96,3 @@ export default async function readDirStructure(dirPath) { | ||
} | ||
const dir = await makePromise(readdir, dirPath) | ||
const dir = /** @type {!Array<string>} */ (await makePromise(readdir, dirPath)) | ||
const lsr = await lstatFiles(dirPath, dir) | ||
@@ -128,3 +134,3 @@ | ||
* After running the `readDirStructure`, this function can be used to flatten the `content` output and return the list of all files (not including symlinks). | ||
* @param {Object<string, DirectoryStructure>} content The computed content. | ||
* @param {!_readDirStructure.Content} content The recursive content of the directory. | ||
* @param {string} path The path to the directory. | ||
@@ -141,3 +147,4 @@ */ | ||
const dirFiles = dirs.reduce((acc, dir) => { | ||
const { content: c } = content[dir] | ||
const { content: c } = | ||
/** @type {!_readDirStructure.Content} */ (content[dir]) | ||
const f = getFiles(c, join(path, dir)) | ||
@@ -149,18 +156,35 @@ return [...acc, ...f] | ||
/* typal types/index.xml */ | ||
/** | ||
* A directory structure representation | ||
* { dir: subdir: { 'fileA.txt': 'foo', 'fileB.js': 'bar' }, 'fileC.jpg': 'baz' } | ||
* @typedef {Object} LstatRes | ||
* @property {fs.Stats} lstat | ||
* @property {string} relativePath | ||
* | ||
* A directory structure representation | ||
* @typedef {Object} DirectoryStructure | ||
* @property {string} type File type, e.g., Directory, File, Symlink | ||
* @property {Object.<string, DirectoryStructureA>} [content] Content if directory. | ||
* | ||
* | ||
* @typedef {Object} DirectoryStructureA | ||
* @property {string} type File type, e.g., Directory, File, Symlink | ||
* @property {'etc'} [content] Content if directory. | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.File} File | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object} _readDirStructure.File | ||
* @prop {!fs.Stats} lstat The stats of the item. | ||
* @prop {string} path The full path of the item. | ||
* @prop {string} relativePath The name of the item. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.Content} Content The recursive content of the directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object<string, !_readDirStructure.DirectoryStructure>} _readDirStructure.Content The recursive content of the directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {_readDirStructure.DirectoryStructure} DirectoryStructure A directory structure representation. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {Object} _readDirStructure.DirectoryStructure A directory structure representation. | ||
* @prop {string} [type] The type of the item. | ||
* @prop {!_readDirStructure.Content} [content] The recursive content if the item is a directory. | ||
*/ | ||
/** | ||
* @suppress {nonStandardJsDocs} | ||
* @typedef {import('fs').Stats} fs.Stats | ||
*/ |
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
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
20380
7
360
161
Updatedmakepromise@^3.1.0