json-to-properties
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "json-to-properties", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A standalone utility to transform language files in JSON format into .properties files, and languages files in .properties format into JSON format.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -88,4 +88,69 @@ # json-to-properties | ||
### -m, --merge | ||
Running the util with the -m, --merge option bundles the generated properties files into one file of a given name, or bundle.properties if none is specified. The content of the bundled file are prefixed with the name of the original file. | ||
##### Example | ||
`json-to-properties -m` bundles the content in a file named bundle.properties | ||
`json-to-properties -m mynewbundle.properties` bundles the content in a file named mynewbundle.properties | ||
Having two files `en.json` and `it.json` both containing the json content below | ||
``` | ||
{ | ||
"KEY1" : "Hello Sir", | ||
"KEY2" : "How is your day?" | ||
} | ||
``` | ||
will result in a bundle file `bundle.properties` having the below content. | ||
``` | ||
EN.KEY1=Hello Sir | ||
EN.KEY2=How is your day? | ||
IT.KEY1=Hello Sir | ||
IT.KEY2=How is your day? | ||
``` | ||
_Note that the standard behavior is preserved, thus two properties files `en.properties` and `it.properties` are also created._ | ||
#### Use of merge in conjunction with -r, --reverse option | ||
The merge process can also be combined with the -r, -reverse flag, where the specified file (or bundle.properties if none is specified) is expanded into separate json files whose name is equivalent to the first part of a key. | ||
##### Example | ||
`json-to-properties -m` or `json-to-properties -rm` | ||
Having a bundle file `bundle.properties` with the below content | ||
``` | ||
EN.KEY1=Hello Sir | ||
EN.KEY2=How is your day? | ||
IT.KEY1=Salve signore | ||
IT.KEY2=Com'è la tua giornata? | ||
``` | ||
will result in two files, `en.json` and `it.json`, having the below content respectively. | ||
``` | ||
{ | ||
"KEY1": "Hello Sir", | ||
"KEY2": "How is your day?" | ||
} | ||
``` | ||
and | ||
``` | ||
{ | ||
"KEY1": "Salve signore", | ||
"KEY2": "Com'è la tua giornata?" | ||
} | ||
``` | ||
## Try It | ||
The `sample` folder contains both .json and .properties fileS to download and test on. |
@@ -10,2 +10,3 @@ #!/usr/bin/env node | ||
.option('-r, --reverse', 'Perform the reverse process, creating a json structure from a .properties file') | ||
.option('-m, --merge [file name]', 'Bundles the content of all the generated properties file into one file, prefixed by the name of the file. When used with -r --reverse option, the specified file is unmerged in separate json files represented by the initial key of each group.') | ||
.option('-s, --spaces <spaces>', 'The amount of spaces per line during the properties to json conversion. Defaults to 4.') | ||
@@ -24,2 +25,13 @@ .option('-c, --config <config>', 'A file in json format having a src and dist attribute, pointing to the source directory where the input files are located, and a destination directory where the output files are written.') | ||
if (program.merge) { | ||
// If no file name was provided, program.merge has value 'true' | ||
if (typeof program.merge === 'boolean') { | ||
var fileName = 'bundle.properties'; | ||
console.log('No file name was provided when using the -m, --merge option. Defaulting to %s...', fileName); | ||
options.merge = fileName; | ||
} else { | ||
options.merge = program.merge; | ||
} | ||
} | ||
if (program.config) { | ||
@@ -26,0 +38,0 @@ // Read the content of the provided config file as string value |
@@ -27,2 +27,23 @@ var fs = require('fs'), | ||
/** | ||
* The removes the .json or .properties extension from the given file name and returns the result. | ||
* | ||
* @param fileName The file name to remove the extension from | ||
* @returns A string representing the file name without an extension | ||
*/ | ||
stripExtension = function (fileName) { | ||
// If the fileName is not provided, something's wrong. Report a bug!! | ||
if (!fileName) { | ||
throw new Error('No file name was specified.'); | ||
} | ||
if (fileName.indexOf('.json') !== -1) { | ||
return fileName.substr(0, fileName.length - 5); // Omit the .json extension from the file name | ||
} else if (fileName.indexOf('.properties') !== -1) { | ||
return fileName.substr(0, fileName.length - 11); //Omit the .properties extension from the file name | ||
} | ||
return fileName; | ||
}; | ||
/** | ||
* Returns an array of file names ending in .json found in the specified directory. | ||
@@ -112,3 +133,3 @@ * | ||
var fileName = file.substr(0, file.length - 5); // Omit the .json extension from the file name | ||
var fileName = stripExtension(file); // Omit the file extension | ||
var writeStream = fs.createWriteStream(dir.concat('/').concat(fileName.concat('.properties')), { | ||
@@ -138,3 +159,3 @@ autoClose: false | ||
var fileName = file.substr(0, file.length - 11); //Omit the .properties extension from the file name | ||
var fileName = stripExtension(file); // Omit the file extension | ||
var writeStream = fs.createWriteStream(dir.concat('/').concat(fileName.concat('.json')), { | ||
@@ -141,0 +162,0 @@ autoClose: false |
var files = require('./files'); | ||
var parser = require('./parser'); | ||
var merger = require('./merger'); | ||
@@ -19,2 +20,4 @@ /** | ||
var config = options.config; | ||
// Create a merger object to register items to | ||
var _merger = new merger.Merger(); | ||
@@ -28,4 +31,12 @@ if (!options.reverse) { | ||
files.writeAsProperties(config.dist, file, entries); // Writes the parsed result to the dist directory | ||
// Register the file and its deflated entries to the merger. | ||
_merger.addCollection(file, entries); | ||
}); | ||
} | ||
// If the option to merge is specified, proceed with the merging process. | ||
if (options.merge) { | ||
_merger.merge(config.dist, options.merge); | ||
} | ||
} else { | ||
@@ -35,2 +46,7 @@ var propertiesFiles = files.getPropertiesFiles(config.src); | ||
propertiesFiles.forEach(function (file) { | ||
// Do not convert the source merge file to json | ||
if (options.merge === file) { | ||
return; | ||
} | ||
var promise = files.getFileDataAsLines(config.src, file); | ||
@@ -43,2 +59,7 @@ promise.then(function (lines) { | ||
} | ||
// Reverse any merged files in their own json files | ||
if (options.merge) { | ||
_merger.reverse(config.src, config.dist, options.merge, options.spaces); | ||
} | ||
} | ||
@@ -66,3 +87,4 @@ }; | ||
reverse: false, | ||
spaces: 4 | ||
spaces: 4, | ||
merge: '' // Defaults to no merge | ||
}; | ||
@@ -69,0 +91,0 @@ |
21936
10
396
155
3