2

i am trying to generate a .js file by running a node script. here's my code.

const dynamicCode = [
    {
        "ClassCode" : "9620",
        "Rate" : 0.99,
        "IsActive" : "TRUE",
        "StartDate" : "2021-10-19T17:00:00.000-07:00",
        "EndDate" : "9999-12-30T16:00:00.000-08:00"
    },
    {
        "ClassCode" : "0038",
        "Rate" : 12.19,
        "IsActive" : true,
        "StartDate" : "2021-05-31T17:00:00.000-07:00",
        "EndDate" : "9999-12-30T16:00:00.000-08:00"
    }
]

const bdCode = module.exports = {
    async up(db, client) {
      await db.collection('Configuration_Lookup').deleteMany();
      await db.collection('Configuration_Lookup').insertMany(dynamicCode)
    },
  
    async down(db, client) {
      // TODO write the statements to rollback your migration (if possible)
      // Example:
      // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
    }
  };

fs.writeFileSync("bds.js", JSON.stringify(bdCode, null, 2), 'utf-8');

while this worked for .txt files containing simple texts, or non-executable codes like json objects. it is executing before running the writeFileSync function. so the data inserting into the js file is

{}

how can we generate a js file with working code inside it

3
  • Does this question help? stackoverflow.com/q/53501756/14032355 Commented Jan 19, 2022 at 10:43
  • Why do you want to do that don't you think it will not be secure as anyone can write the code and try to execute it on the server? What do you want to achieve? Commented Jan 19, 2022 at 11:08
  • Hi Apoorva, this snippet is just to automate a daily process, this is not going to be hosted on server, from xlsx file i need to create a mongo migration script to update data in db. Commented Jan 19, 2022 at 16:50

2 Answers 2

2

i solved the issue by following code.

const dynamicCode = [
    {
        "ClassCode" : "9620",
        "Rate" : 0.99,
        "IsActive" : "TRUE",
        "StartDate" : {"$date": "2021-10-19T00:00:00Z"},
        "EndDate" : {"$date": "9999-12-30T23:59:59Z"}
    },
    {
        "ClassCode" : "0038",
        "Rate" : 12.19,
        "IsActive" : true,
        "StartDate" : {"$date": "2021-10-19T00:00:00Z"},
        "EndDate" : {"$date": "9999-12-30T23:59:59Z"}
    }
]

const bdCode = `module.exports = {
    async up(db, client) {
      await db.collection('Configuration_Lookup').deleteMany();
      await db.collection('Configuration_Lookup').insertMany(${JSON.stringify(dynamicCode, null, 2)})
    },
  
    async down(db, client) {
      // TODO write the statements to rollback your migration (if possible)
      // Example:
      // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
    }
  };`

fs.writeFileSync("bds.js", bdCode);
Sign up to request clarification or add additional context in comments.

Comments

1

You can run the code as a string by creating a function from it using the Function constructor.

const code = "let x = 100; console.log(x + 1)";  
const run = new Function(code);  
run();

Write this string to the JS file and run the js file using child process.

3 Comments

Woah, that's interesting. I had no idea you could do that with the Function constructor lol
Yes, this way you can actually run the code. You can use eval(string), but it has security flaws.
Yeah I would always use eval when I needed to do something like this; However, I always tried to avoid it for the aforementioned security flaws. How does the function constructor avoid those flaws?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.