-
-
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/len load sync
- Loading branch information
Showing
8 changed files
with
131 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { Config } from '../Config' | ||
import { Config as ConfigInstance } from '../Config' | ||
|
||
export {} | ||
|
||
declare global { | ||
class Config { | ||
static get<T>(key: string, defaultValue?: any): T | ||
load(): Promise<void> | ||
loadSync(): void | ||
} | ||
} | ||
|
||
new ConfigInstance().loadSync() | ||
|
||
const _global = global as any | ||
_global.Config = Config | ||
_global.Config = ConfigInstance |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import '../src/utils/global' | ||
import { Config } from '../src/Config' | ||
|
||
describe('\n Config', () => { | ||
beforeAll(() => (process.env.DB_NAME = 'testing')) | ||
|
||
it('should be able to use global Config instance', async () => { | ||
await new Config().load() | ||
expect(Config.get('app.hello')).toBe('world') | ||
}) | ||
|
||
it('should be able to reload environment variables from Config class but never the config files', async () => { | ||
process.env.DB_NAME = 'testing' | ||
|
||
new Config().loadSync() | ||
|
||
expect(Config.get('DB_NAME')).toBe('testing') | ||
expect(Config.get('database.dbName')).toBe('testing') | ||
expect(Config.get('database.dbName')).toBe(undefined) | ||
}) | ||
}) |