@wrote/read-dir-structure
Advanced tools
Comparing version 1.0.3 to 1.1.0
const { lstat, readdir } = require('fs'); | ||
let makePromise = require('makepromise'); if (makePromise && makePromise.__esModule) makePromise = makePromise.default; | ||
const { resolve } = require('path'); | ||
const { join } = require('path'); | ||
@@ -13,3 +13,3 @@ /** | ||
const readFiles = dirContent.map(async (relativePath) => { | ||
const path = resolve(dirPath, relativePath) | ||
const path = join(dirPath, relativePath) | ||
const ls = await makePromise(lstat, path) | ||
@@ -123,2 +123,23 @@ return { | ||
/** | ||
* 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 {string} path The path to the directory. | ||
*/ | ||
const getFiles = (content, path) => { | ||
let files = [] | ||
let dirs = [] | ||
Object.keys(content).forEach((key) => { | ||
const { type } = content[key] | ||
if (type == 'File') files.push(join(path, key)) | ||
else if (type == 'Directory') dirs.push(key) | ||
}) | ||
const dirFiles = dirs.reduce((acc, dir) => { | ||
const { content: c } = content[dir] | ||
const f = getFiles(c, join(path, dir)) | ||
return [...acc, ...f] | ||
}, []) | ||
return [...files, ...dirFiles] | ||
} | ||
/** | ||
* A directory structure representation | ||
@@ -142,2 +163,3 @@ * { dir: subdir: { 'fileA.txt': 'foo', 'fileB.js': 'bar' }, 'fileC.jpg': 'baz' } | ||
module.exports = readDirStructure | ||
module.exports = readDirStructure | ||
module.exports.getFiles = getFiles |
@@ -0,1 +1,7 @@ | ||
## 5 April 2019 | ||
### [1.1.0](https://github.com/wrote/read-dir-structure/compare/v1.0.3...v1.1.0) | ||
- [feature] Implement `getFiles` method. | ||
## 2 April 2019 | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "@wrote/read-dir-structure", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Reads directory structure.", | ||
@@ -51,12 +51,11 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"alamode": "1.9.0", | ||
"documentary": "1.23.2", | ||
"alamode": "^1.9.2", | ||
"documentary": "^1.23.4", | ||
"eslint-config-artdeco": "1.0.1", | ||
"snapshot-context": "2.2.1", | ||
"yarn-s": "1.1.0", | ||
"zoroaster": "3.11.2" | ||
"zoroaster": "^3.11.4" | ||
}, | ||
"dependencies": { | ||
"makepromise": "3.0.3" | ||
"makepromise": "^3.0.3" | ||
} | ||
} |
@@ -17,3 +17,4 @@ # @wrote/read-dir-structure | ||
* [`async readDirStructure(path: string): Structure`](#async-readdirstructurepath-string-structure) | ||
* [Reasons for Errors](#reasons-for-errors) | ||
* [`async getFiles(content: Structure.content, path: string): Array<string>`](#async-getfilescontent-structurecontentpath-string-arraystring) | ||
- [Reasons for Errors](#reasons-for-errors) | ||
- [Copyright](#copyright) | ||
@@ -38,2 +39,4 @@ | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/0.svg?sanitize=true" width="25"></a></p> | ||
### `async readDirStructure(`<br/> `path: string,`<br/>`): Structure` | ||
@@ -43,3 +46,3 @@ | ||
```javascript | ||
```js | ||
/* yarn example/ */ | ||
@@ -87,4 +90,34 @@ import readDirStructure from '@wrote/read-dir-structure' | ||
### Reasons for Errors | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/1.svg?sanitize=true" width="25"></a></p> | ||
### `async getFiles(`<br/> `content: Structure.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). | ||
```js | ||
/* yarn example/ */ | ||
import readDirStructure, { getFiles } from '@wrote/read-dir-structure' | ||
(async () => { | ||
const path = 'example/directory' | ||
const res = await readDirStructure(path) | ||
const files = getFiles(res.content, path) | ||
console.log(JSON.stringify(files, null, 2)) | ||
})() | ||
``` | ||
```json | ||
[ | ||
"example/directory/fileA.txt", | ||
"example/directory/fileB.txt", | ||
"example/directory/test.json", | ||
"example/directory/subdirectory/subdirFileA.txt", | ||
"example/directory/subdirectory/subdirFileB.txt" | ||
] | ||
``` | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/2.svg?sanitize=true"></a></p> | ||
## Reasons for Errors | ||
The following errors can happen and have been [context tested](test/spec/errors.js) against: | ||
@@ -99,2 +132,4 @@ | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/3.svg?sanitize=true"></a></p> | ||
## Copyright | ||
@@ -101,0 +136,0 @@ |
import { lstat, readdir } from 'fs' | ||
import makePromise from 'makepromise' | ||
import { resolve } from 'path' | ||
import { join } from 'path' | ||
@@ -13,3 +13,3 @@ /** | ||
const readFiles = dirContent.map(async (relativePath) => { | ||
const path = resolve(dirPath, relativePath) | ||
const path = join(dirPath, relativePath) | ||
const ls = await makePromise(lstat, path) | ||
@@ -123,2 +123,23 @@ return { | ||
/** | ||
* 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 {string} path The path to the directory. | ||
*/ | ||
export const getFiles = (content, path) => { | ||
let files = [] | ||
let dirs = [] | ||
Object.keys(content).forEach((key) => { | ||
const { type } = content[key] | ||
if (type == 'File') files.push(join(path, key)) | ||
else if (type == 'Directory') dirs.push(key) | ||
}) | ||
const dirFiles = dirs.reduce((acc, dir) => { | ||
const { content: c } = content[dir] | ||
const f = getFiles(c, join(path, dir)) | ||
return [...acc, ...f] | ||
}, []) | ||
return [...files, ...dirFiles] | ||
} | ||
/** | ||
* A directory structure representation | ||
@@ -125,0 +146,0 @@ * { dir: subdir: { 'fileA.txt': 'foo', 'fileB.js': 'bar' }, 'fileC.jpg': 'baz' } |
17465
5
295
155
+ Added@artdeco/clean-stack@1.2.1(transitive)
+ Addederotic@2.1.1(transitive)
+ Addedmakepromise@3.2.0(transitive)
- Removed@artdeco/clean-stack@1.0.1(transitive)
- Removederotic@2.0.3(transitive)
- Removedmakepromise@3.0.3(transitive)
Updatedmakepromise@^3.0.3