Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
npm install modify-json-file
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1 Stars
46 Commits
1 Watching
2 Branches
2 Contributors
Updated on 31 Aug 2023
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
33.3%
16
Compared to previous day
Last week
62.9%
114
Compared to previous week
Last month
7.4%
435
Compared to previous month
Last year
134.8%
4,649
Compared to previous year
Simplest way to modify JSON files
Becaues I got tired of writing read
/write
functions for JSON files, especially when I need to change dozens of files.
Let's start with package.json file:
1// 📁package.json 2{ 3 "name": "package", 4 "main": "index.js", 5 "author": "eldar", 6 "files": [ "build" ], 7 "dependencies": { 8 "type-fest": "*", 9 "fdir": ">=2" 10 } 11}
And this code:
1import { modifyJsonFile } from "modify-json-file"; 2 3// modify package.json in the same dir 4await modifyJsonFile( 5 path.join(__dirname, "package.json"), 6 { 7 name: s => `super ${s}`, 8 main: "build/electron.js", 9 files: undefined, // removing the property 10 dependencies: { 11 "type-fest": "^1.0.0" 12 } 13 } 14)
After running the code, package.json will be:
1{ 2 "name": "super package", 3 "main": "build/electron.js", 4 "author": "eldar", 5 "dependencies": { 6 "type-fest": "^1.0.0" 7 } 8}
As you can see above, modifyJsonFile
only merges only on top level properties. Currently, there is no support for nested property merging.
Note that to simplify docs I won't use path
module here, but you should use it everywhere.
In example above,
modifyPackageJson
should be used to provider typing for you
As always, for more usage examples you can look into test-d/index.test-d.ts
or tests
files.
Remember, that at root level value can be any valid JSON value:
string
,number
,boolean
,null
,object
orarray
.
Be aware of modifying non object JSON files (where root type is not an object). For example:
Our code:
1import { modifyJsonFile } from "modify-json-file"; 2 3// telling that root type is number (in this case it's obligatory) 4await modifyJsonFile<number>("package.json", n => n + 1);
Expected JSON:
1// 📁someNumber.json 25
Actual JSON:
1// 📁someNumber.json 2{ 3 "retries": 5 4}
After running the code above, without any warnings you will get this:
1// 📁someNumber.json 2"[object Object]1"
That's because callback n => n + 1
has transformed n
(object) into string.
Here, despite of the TS type (number), n
is object in runtime, so n + 1
just stringified n
and returned [object Object]1
.
Then this module just stringified the string to store output as valid JSON string in file.
Remember, this module doesn't do any type checking in runtime, you need to use typeof
in callback for checking root types or schema validators (like ajv) for objects.
By default, it will preserve tab size (thanks to detect-indent), but you can control this by overriding tabSize
option. For example, we can use it to just format the file:
1import { modifyJsonFile } from "modify-json-file"; 2 3// this will format file to use \t (hard tabs) 4await modifyJsonFile("someFile.json", {}, { tabSize: "hard" });
Pass { removeJsonc: true }
option to enable processing jsonc
files.
WARNING, this option will remove comments and trailing commas forever!
This is temporary limitation and the option will be renamed to jsonc
once limitation is removed.
modifyTsConfigJsonFile
has this option enabled by default
Docs:
Custom parser / stringifer (look at quicktype issue) to save property order
Helper exports section
Extend package.json typings
Examples with immer
Fix auto generated docs
Runtypes?
Describe all possible usage cases
Give a hint, that it doesn't perform schema checking again actual file contents when type is passed into generic function modifyJsonFile
Strip bom option
1await fs.promises.readFile(./package.json, "utf8");
Into this:
1await fs.promises.readFile(path.join(__dirname, "package.json"), "utf8");
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More