json-to-enum v1.3.0
json-to-enum
CLI tool for TypeScript Enum files generation from JSON files
The Goal
The main goal is to have a convenient tool for working with JSON translation files in TypeScript projects, which are often used by various i18n libraries, for example i18next.
But some other use case may appear, therefore, the tool provides some flexibility in its configuration.
Installation
Local
npm install --save-dev json-to-enumor
yarn add --dev json-to-enumGlobal
yarn global add json-to-enumor
npm install --global json-to-enumUsage
You can use the tool by passing all settings through arguments:
json-to-enum --watch --input locales/**/*.jsonExample
For instance, we have some JSON file with the next content:
{
"nav": {
"home": "Home",
"about": "About",
"contacts": "Contacts"
}
}By calling json-to-enum --input some-file.json we will get the next Enum file:
enum SomeFile {
NavHome = 'nav.home',
NavAbout = 'nav.about',
NavContacts = 'nav.contacts',
}
export default SomeFileYou can make some customization of the output using configuration.
By calling json-to-enum --input some-file.json --enumValue value values of the resulting Enum will be such as in
the source JSON file:
enum SomeFile {
NavHome = 'Home',
NavAbout = 'About',
NavContacts = 'Contacts',
}
export default SomeFileConfiguration file
Also you can create configuration file for more detailed configuration:
json-to-enum initThis command will create configuration file with the next content:
const upperFirst = require('lodash/upperFirst');
const camelCase = require('lodash/camelCase');
module.exports = {
input: 'src/**/*.json',
outputFolderCallback: path => path.dir,
outputFolder: '.', // will be ignored because outputFolderCallback is specified
enumNameCallback: path => upperFirst(camelCase(path.base)),
enumFilenameCase: 'kebab',
enumFilenameEnding: '.ts',
enumValue: 'path',
enumValueQuotes: "'",
enumTabs: false,
enumSpaces: 2,
enumExportDefault: true,
jsonKeySeparator: '.',
jsonFlattenArray: false,
};Watch Mode
If you pass --watch, -w argument, the tool will watch changes of source files.