Skip to content

zardoy/modify-json-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Modify JSON

Simplest way to modify JSON files

API

Usage

  • Only async use

Let's say we package.json file:

// 📁package.json
{
    "name": "package",
    "main": "index.js",
    "author": "eldar",
    "dependencies": {
        "type-fest": "*",
        "fdir": ">=2"
    }
}

And this code:

import { modifyJsonFile } from "@zardoy/modify-json";

// of course, you should use path module here
await modifyJsonFile("package.json", {
    name: s => `super ${s}`,
    main: "build/electron.js",
    dependencies: {
        "type-fest": "^1.0.0"
    }
})

After running this code, we'll get this package.json:

{
    "name": "super package",
    "main": "build/electron.js",
    "author": "eldar",
    "dependencies": {
        "type-fest": "^1.0.0"
    }
}

As you can see above, modifyJsonFile only merges fields on 1 level depth. Currently, we don't support merging in nested fields.

We're using detect-indent to preserve the tab size in .json files.

TODO

  • Fix. Currently, to preserve the tab size, json file readed twice
  • tests. should be easy but I'm extremely lazy
  • transformer for paths (most likely babel plugin):
await fs.promises.readFile(./package.json, "utf8");

Into this:

await fs.promises.readFile(path.join(__dirname, "package.json"), "utf8");
  • find a way to use FS in builder-way (like fdir does) and deprecate this module

Related

  • jsonfile: simple reading / writing for json files
  • immer ?

Other cool modules for working with JSON

About

Simple module for modifying fields in JSON files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •