Skip to content

Commit

Permalink
Merge pull request #20 from rawand-faraidun/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rawand-faraidun authored Dec 9, 2023
2 parents 3c3a94c + 3dc59ea commit 24f65ce
Show file tree
Hide file tree
Showing 15 changed files with 1,114 additions and 1,314 deletions.
9 changes: 9 additions & 0 deletions .changeset/clean-ligers-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'linguify': minor
---

output JSON indentation

* allow to indent output json files
* `jsonIndentation` optional config option
* `useSingleFile` is now optional
8 changes: 7 additions & 1 deletion assets/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
"description": "determines to use single file translations or not",
"default": false,
"$comment": "determines to use single file translations or not"
},
"jsonIndentation": {
"type": "number",
"description": "determines indentation of output translations files",
"default": 0,
"$comment": "determines indentation of output translations files"
}
},
"required": ["localesPath", "locales", "defaultLocale", "useSingleFile"]
"required": ["localesPath", "locales", "defaultLocale"]
}
3 changes: 2 additions & 1 deletion lib/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const defaultConfig: DefaultConfig = {
localesPath: './public/locales',
locales: ['en'],
defaultLocale: 'en',
useSingleFile: false
useSingleFile: false,
jsonIndentation: 0
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, readdirSync, readFileSync } from 'fs'
import { extname, resolve } from 'path'
import chalk from 'chalk'
import { defaultConfig } from './defaults'
import type { Config } from './types'
import { config, configPath, rootPath } from './utils'

Expand Down
15 changes: 11 additions & 4 deletions lib/linguifyValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ export const linguifyValidation = () => {
}

// checking useSingleFile
if (typeof config.useSingleFile == 'undefined') {
throw new Error(chalk.yellow(`Linguify config file misses 'useSingleFile' key, please add it before starting`))
}
if (typeof config.useSingleFile != 'boolean') {
// can be undefined, but must be boolean
if (typeof config.useSingleFile != 'undefined' && typeof config.useSingleFile != 'boolean') {
throw new Error(chalk.yellow(`Provided 'useSingleFile' is not boolean, please change it before starting`))
}

// checking jsonIndentation
// can be undefined, but must be number
if (typeof config.jsonIndentation != 'undefined' && typeof config.jsonIndentation != 'number') {
throw new Error(chalk.yellow(`Provided 'jsonIndentation' is not number, please change it before starting`))
}
if (config.jsonIndentation < 0) {
throw new Error(chalk.yellow(`Provided 'jsonIndentation' is invalid, please change it before starting`))
}
} catch (error: any) {
console.error(chalk.red(error.message))
process.exit(0)
Expand Down
14 changes: 7 additions & 7 deletions lib/syncNamespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const syncNamespaces = () => {
let json: DynamicObject = {}
try {
json = clear(JSON.parse(file), { skipFirstDepth: true })
writeFileSync(path, JSON.stringify(json))
writeFileSync(path, JSON.stringify(json, null, config.jsonIndentation))
} catch {
writeFileSync(path, '{}')
}
Expand All @@ -60,7 +60,7 @@ export const syncNamespaces = () => {
let json: DynamicObject = {}
try {
json = clear(JSON.parse(file))
writeFileSync(path, JSON.stringify(json))
writeFileSync(path, JSON.stringify(json, null, config.jsonIndentation))
} catch {
writeFileSync(path, '{}')
}
Expand All @@ -74,21 +74,21 @@ export const syncNamespaces = () => {
const path = getPath(`${locale}.json`)
try {
const json = clear(getFileJson(`${locale}.json`), { skipFirstDepth: true })
writeFileSync(path, JSON.stringify(_.defaultsDeep(json, nsKeys)))
writeFileSync(path, JSON.stringify(_.defaultsDeep(json, nsKeys), null, config.jsonIndentation))
} catch {
writeFileSync(path, JSON.stringify(nsKeys))
writeFileSync(path, JSON.stringify(nsKeys, null, config.jsonIndentation))
}
} else {
Object.keys(nsKeys).forEach(ns => {
const path = getPath(locale, ns)
if (!existsSync(path)) {
return writeFileSync(path, JSON.stringify({ ...nsKeys[ns] }))
return writeFileSync(path, JSON.stringify({ ...nsKeys[ns] }, null, config.jsonIndentation))
}
try {
const json = clear(getNamespaceJson(locale, ns))
writeFileSync(path, JSON.stringify(_.defaultsDeep(json, { ...nsKeys[ns] })))
writeFileSync(path, JSON.stringify(_.defaultsDeep(json, { ...nsKeys[ns] }), null, config.jsonIndentation))
} catch {
writeFileSync(path, JSON.stringify({ ...nsKeys[ns] }))
writeFileSync(path, JSON.stringify({ ...nsKeys[ns] }, null, config.jsonIndentation))
}
})
}
Expand Down
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ export type Config = {
* @default false
*/
useSingleFile: boolean

/**
* determines indentation of output translations files
*
* determines to beautify output json files or not
*
* @default 0
*/
jsonIndentation: number
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
"prompts": "^2.4.2"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@changesets/cli": "^2.27.1",
"@ianvs/prettier-plugin-sort-imports": "^4.1.1",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express-serve-static-core": "^4.17.41",
"@types/findup-sync": "^4.0.4",
"@types/lodash": "^4.14.202",
"@types/node": "^20.10.0",
"@types/node": "^20.10.4",
"@types/prompts": "^2.4.9",
"concurrently": "^8.2.2",
"ncp": "^2.0.0",
"nodemon": "^3.0.1",
"nodemon": "^3.0.2",
"prettier": "^3.1.0",
"tsup": "^8.0.1",
"typescript": "^5.3.2"
"typescript": "^5.3.3"
},
"files": [
"dist/**/*",
Expand Down
Loading

0 comments on commit 24f65ce

Please sign in to comment.