replace-json-property
Advanced tools
Comparing version 1.7.1 to 1.8.0
@@ -18,2 +18,6 @@ #!/usr/bin/env node | ||
.option('--silent [boolean]', 'Silent mode. Executes without log messages') | ||
.option( | ||
'--limit [number]', | ||
'Limit the maximum number of replacements, for example (1, 4)' | ||
) | ||
.action(function(path, property, value) { | ||
@@ -23,5 +27,6 @@ replaceJsonProperty.replace(path, property, value, { | ||
EOL: commander.eol, | ||
silent: commander.silent | ||
silent: commander.silent, | ||
limit: commander.limit | ||
}); | ||
}) | ||
.parse(process.argv); |
{ | ||
"name": "replace-json-property", | ||
"version": "1.7.1", | ||
"version": "1.8.0", | ||
"description": "CLI tool to replace a property in a JSON file", | ||
@@ -5,0 +5,0 @@ "main": "src/replace-json-property.js", |
@@ -76,4 +76,2 @@ | ||
## Usage from code | ||
@@ -87,3 +85,3 @@ | ||
import {replace} from 'replace-json-property'; | ||
replace('./environment/test.json', 'foo', 'new value'); | ||
@@ -96,8 +94,6 @@ ``` | ||
const replaceJSONProperty = require('replace-json-property'); | ||
replaceJSONProperty.replace('./environment/test.json', 'foo', 'new value'); | ||
``` | ||
## Options | ||
@@ -112,5 +108,4 @@ | ||
| --silent | Silent mode. Executes without log messages | False | | ||
| --limit | Limit the number of replacements | 0 (unlimited) | | ||
## Help command | ||
@@ -117,0 +112,0 @@ |
@@ -7,2 +7,3 @@ const jsonfile = require('jsonfile'); | ||
EOL: '\n', | ||
LIMIT: 0, | ||
SILENT: false | ||
@@ -17,10 +18,11 @@ }; | ||
EOL: (options && options.EOL) || DEFAULT_OPTIONS.EOL, | ||
silent: (options && options.silent) || DEFAULT_OPTIONS.SILENT | ||
silent: (options && options.silent) || DEFAULT_OPTIONS.SILENT, | ||
limit: parseInt((options && options.limit) || DEFAULT_OPTIONS.LIMIT, 10) | ||
}; | ||
try { | ||
const file = readFile(path, property, newValue); | ||
const file = readFile(path, property, newValue, options.limit); | ||
jsonfile.writeFileSync(path, file, options); | ||
if (!options.silent) { | ||
log.success( | ||
`Property: "${property}" in file: ${path} successfully overwritten with "${newValue}"` | ||
`Properties: "${property}" in file: ${path} successfully overwritten with "${newValue}"` | ||
); | ||
@@ -33,6 +35,11 @@ } | ||
const readFile = (path, property, newValue) => { | ||
const readFile = (path, property, newValue, limit) => { | ||
let limitCounter = 0; | ||
return jsonfile.readFileSync(path, { | ||
reviver: (key, value) => { | ||
if (key === property) { | ||
if (limit > 0 && limitCounter >= limit) { | ||
return value; | ||
} | ||
++limitCounter; | ||
return newValue; | ||
@@ -39,0 +46,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8537
83
119