Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
generate a js file dynamically using fs node

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda