-
-
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.
Merge pull request #2 from SecJS/feat/len-load-sync
feat(sntl): implement loadsync method on Sntl class
- Loading branch information
Showing
4 changed files
with
82 additions
and
1 deletion.
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
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
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,46 @@ | ||
import { resolve, basename } from 'path' | ||
import { readdirSync, readFileSync } from 'fs' | ||
import { DirectoryContract } from '@secjs/contracts' | ||
|
||
/** | ||
* Return all folders of a directory and files inside | ||
* | ||
* @param dir The directory | ||
* @param withFiles If need to get files inside folders by default false | ||
* @param buffer Return the buffer of the file by default false | ||
* @param fullPath Return the full path of folder or archive by default false | ||
* @return The directory root with sub folders and files when withFiles true | ||
*/ | ||
export function getFoldersSync( | ||
dir: string, | ||
withFiles = false, | ||
buffer = false, | ||
fullPath = false, | ||
): DirectoryContract { | ||
const dirents = readdirSync(dir, { withFileTypes: true }) | ||
|
||
const directory = { | ||
path: fullPath ? resolve(dir) : basename(resolve(dir)), | ||
files: [], | ||
folders: [], | ||
} | ||
|
||
for (const dirent of dirents) { | ||
const res = resolve(dir, dirent.name) | ||
|
||
if (dirent.isDirectory()) { | ||
directory.folders.push(getFoldersSync(res, withFiles, buffer, fullPath)) | ||
|
||
continue | ||
} | ||
|
||
if (dirent.isFile() && withFiles) { | ||
directory.files.push({ | ||
name: fullPath ? res : basename(res), | ||
value: buffer ? Buffer.from(res) : readFileSync(res, 'utf-8'), | ||
}) | ||
} | ||
} | ||
|
||
return directory | ||
} |
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