0

I am trying to read a JSON file and then change 2 of the 3 values within it so that the last one does not change at all, my code is below, apparently I must add some more details so I am just going to try to fill this up a little bit with some text so you can probably stop reading at this point and start to evaluate my code, dont go too hard on me though I dont know that much Javascript and have been using it as my coding language for around 3 weeks mainly using documentation to aid me.

const fs = require('fs-extra');
const config_json  = __dirname + "\\config.json"
const args = require('minimist')(process.argv.slice(2))


var exp_clean = args["clean"]
var exp_asked = args["asked"]
var remove_arg = args["remove"]
if (remove_arg == "") {
    fs.readFile(config_json, 'utf8', (err, data) => {
        if (err) {console.log("error", err)}
        var obj = JSON.stringify(data, null, 4)
        // how do i change values within the read json data?
        // {
        //     "experimental-cleaning": "false",
        //     "experimental-asked": "false",
        //     "to-remove": ""
        // }
        // that is whats in the json file being read
    })
    var obj = {
        "experimental-cleaning": exp_clean,
        "experimental-asked": exp_asked,
        "to-remove": to_remove
    }
    var stringified = JSON.stringify(obj, null, 4)
    fs.writeFile(config_json, stringified, (err) => {
        if (err) {console.log("error", err)}
    })
}
else {var remove_arg}
var obj = {
    "experimental-cleaning": exp_clean,
    "experimental-asked": exp_asked,
    "to-remove": to_remove
}
var stringified = JSON.stringify(obj, null, 4)
fs.writeFile(config_json, stringified, (err) => {
    if (err) {console.log("error", err)}
})

Here is the fixed code

const fs = require('fs-extra');
const config = require('.\\config.json')
const args = require('minimist')(process.argv.slice(2))


const exp_clean = `${args["clean"]}`
const exp_asked = `${args["asked"]}`
const remove = `${args["remove"]}`
if (remove == 'undefined') {
    config['experimental-cleaning'] = exp_clean
    config['experimental-asked'] = exp_asked
    config_string = JSON.stringify(config, null, 4)
    fs.writeFile(__dirname + "\\config.json", config_string, (err) => {
        if (err) {console.log("error"), err}
    })
    console.log("config updated")
}
else {
config['experimental-cleaning'] = exp_clean
config['experimental-asked'] = exp_asked
config['to-remove'] = remove
config_string = JSON.stringify(config, null, 4)
fs.writeFile(__dirname + "\\config.json", config_string, (err) => {
    if (err) {console.log("error"), err}
})
console.log("config updated")
}
Fisheiyy
  • 65
  • 6

1 Answers1

1

You can include a json file with require. No need use fs.readFile()

const fs = require('fs-extra');
const config_json  = require('./config_json.json')
const args = require('minimist')(process.argv.slice(2))


const exp_clean = args["clean"]
const exp_asked = args["asked"]
const remove_arg = args["remove"]
if (remove_arg == "") {

    config_json["experimental-cleaning"] = exp_clean;
    config_json["experimental-asked"] = exp_asked;
    config_json["to-remove"] = to_remove;

    fs.writeFile("config_json.js", config_json, (err) => {
        if (err) {console.log("error", err)}
    });
}
else 
    const remove_arg

config_json["experimental-cleaning"] = exp_clean;
config_json["experimental-asked"] = exp_asked;
config_json["to-remove"] = to_remove;

fs.writeFile("config_json.js", config_json, (err) => {
    if (err) 
        console.log("error", err)
});

Edit: I think this is what you need:

const exp_clean = `${args["clean"]}`
const exp_asked = `${args["asked"]}`
const remove = `${args["remove"]}`
Murat Colyaran
  • 2,075
  • 2
  • 8
  • 27
  • you have solved my issue, something so simple, now i just need to put my args in quotes like '' but im not sure how – Fisheiyy Jul 02 '21 at 12:52