Skip to content

Commit

Permalink
Merge pull request #2 from SecJS/feat/len-load-sync
Browse files Browse the repository at this point in the history
feat(sntl): implement loadsync method on Sntl class
  • Loading branch information
jlenon7 authored Nov 19, 2021
2 parents aab5867 + afd228a commit c2dce61
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/intl",
"version": "1.0.3",
"version": "1.0.4",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand Down
27 changes: 27 additions & 0 deletions src/Sntl.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { parse } from 'path'
import { getFolders } from '@secjs/utils'
import { getFoldersSync } from './utils/getFoldersSync'
import { NotImplementedException } from '@secjs/exceptions'

export class Sntl {
private static _tempLocale: string
private static _defaultLocale: string
private static locales: Map<string, Map<string, any>> = new Map()

constructor() {
Sntl.locales.clear()
Sntl._tempLocale = null
Sntl._defaultLocale = null
}

async load() {
const path = `${process.cwd()}/resources/locales`

Expand All @@ -27,6 +34,26 @@ export class Sntl {
return this
}

loadSync() {
const path = `${process.cwd()}/resources/locales`

const { folders } = getFoldersSync(path, true)

folders.forEach(folder => {
const filesMap = new Map()

folder.files.forEach(file => {
if (typeof file.value !== 'string') return

filesMap.set(parse(file.name).name, JSON.parse(file.value))
})

Sntl.locales.set(folder.path, filesMap)
})

return this
}

setDefaultLocale(locale: string) {
Sntl._defaultLocale = locale

Expand Down
46 changes: 46 additions & 0 deletions src/utils/getFoldersSync.ts
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
}
8 changes: 8 additions & 0 deletions tests/sntl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ describe('\n Sntl', () => {
test: 'Hello my name is {{name}}!',
})
})

it('should be able to reload Sntl locales and load it sync', async () => {
new Sntl().setDefaultLocale('en-us').loadSync()

const message = Sntl.formatMessage('stub.test', { name: 'João' })

expect(message).toBe('Hello my name is João!')
})
})

0 comments on commit c2dce61

Please sign in to comment.