Javascript 从测试项目中的源json文件创建新json

Javascript 从测试项目中的源json文件创建新json,javascript,codeceptjs,Javascript,Codeceptjs,更新 我一直在努力解决这个问题,并有以下代码-st async generatejobs() { const fs = require("fs"); const path = require("path"); const directoryPath = path.join(__dirname, "../data/pjo/in"); fs.readdir(directoryPath, function

更新 我一直在努力解决这个问题,并有以下代码-st

  async generatejobs() {
    const fs = require("fs");
    const path = require("path");
    const directoryPath = path.join(__dirname, "../data/pjo/in");

    fs.readdir(directoryPath, function (err, files) {
      if (err) {
        console.log("Error getting directory information." + err);
      } else {
        files.forEach(function (file) {
          console.log(file);
          fs.readFile(file, (err, data) => {
            console.log(file); // this works, if I stop here
            // if (err) throw err;
            // let newJson = fs.readFileSync(data);
            // console.log(newJson);
          })
          // let data = fs.readFileSync(file);
          // let obj = JSON.parse(data);
          // let isoUtc = new Date();
          // let isoLocal = toISOLocal(isoUtc);
          // obj.printingStart = isoLocal;
          // obj.printingEnd = isoLocal;
          // let updatedFile = JSON.stringify(obj);
          // let write = fs.createWriteStream(
          //   path.join(__dirname, "../data/pjo/out", updatedFile)
          // );
          // read.pipe(write);
        });
      }
    });
只要我尝试取消注释下面显示的行,它就会失败

let newJson = fs.readFileSync(data);
我得到的错误是这个

Uncaught ENOENT: no such file or directory, open 'C:\projects\codeceptJs\ipt\80-012345.json'
这是一条正确的语句,因为路径应该如下所示

'C:\projects\codeceptJs\ipt\src\data\pjo\in\80-012345.json'
我不明白它为什么要在这里查找文件,因为在代码的前面,路径已经设置好,并且似乎可以通过该路径正确地查找文件

const directoryPath = path.join(__dirname, "../data/pjo/in");
当前注释掉的代码的其余部分是我尝试执行以下操作的地方

  • 从源目录抓取每个文件
  • 放入json对象
  • 更新json对象以更改两个日期条目
  • 保存到项目中的新json文件/新位置
原创帖子 我有一个codeceptjs测试项目,希望在我的项目中包括一组现有的json文件(src/data/jsondata/in),然后更新每个文件中的date属性,并将它们写入项目中的输出位置(src/data/jsondata/out)。我需要更改日期,然后将其返回到一个非常特定的字符串格式,我已经这样做了,然后将其插入正在创建的新json中。我大约80%的时间都是这样,然后在尝试将文件从项目中的一个文件夹移到另一个文件夹时遇到了问题

我把它分成两部分

  • 函数获取日期并将其转换为所需的日期字符串

  • 函数获取源json,更新日期,并在新文件夹位置创建新json

  • 1号正在正常工作。2号不是

    如果有更好的方法来实现这一点,我对此持开放态度

    下面是我试图更新json的代码。这里的主要问题是我没有正确理解和/或处理连接路径的内容

    generatePressJobs() {
        //requiring path and fs modules
        const path = require('path');
        const fs = require('fs');
        //joining path of directory
        const directoryPath = path.join(__dirname, '../', 'data/pjo/in/');
        //passsing directoryPath and callback function
        fs.readdir(directoryPath, function (err, files) {
          //handling error
          if (err) {
            I.say('unable to scan directory: ' + err);
            return console.log('Unable to scan directory: ' + err);
          }
          //listing all files using forEach
          files.forEach(function (file) {
            // Update each file with new print dates
            let data = fs.readFileSync(file);
            let obj = JSON.parse(data);
            let isoUtc = new Date();
            let isoLocal = toISOLocal(isoUtc);
            obj.printingStart = isoLocal;
            obj.printingEnd = isoLocal;
            let updatedFile = JSON.stringify(obj);
            fs.writeFile(`C:\\projects\\csPptr\\ipt\\src\\data\\pjo\\out\\${file}`, updatedFile, (err) => {
              if (err) {
                throw err;
              }
            });
          });
        });
      },
    
    收到错误

    Uncaught ENOENT: no such file or directory, open '80-003599.json'
          at Object.openSync (fs.js:462:3)
          at Object.readFileSync (fs.js:364:35)
          at C:\projects\codeceptJs\ipt\src\pages\Base.js:86:23
          at Array.forEach (<anonymous>)
          at C:\projects\codeceptJs\ipt\src\pages\Base.js:84:13
          at FSReqCallback.oncomplete (fs.js:156:23)
    
    Uncaught enoint:没有这样的文件或目录,打开'80-003599.json'
    在Object.openSync(fs.js:462:3)
    在Object.readFileSync(fs.js:364:35)
    在C:\projects\codeceptJs\ipt\src\pages\Base.js:86:23
    在Array.forEach()处
    在C:\projects\codeceptJs\ipt\src\pages\Base.js:84:13
    在FSReqCallback.oncomplete(fs.js:156:23)
    
    生成json的函数位于src/pages/basePage.js中

    我为json文件构建的文件夹结构位于 原始源文件的src/data/jsondata/in-->格式 src/data/jsondata/out-->用于更改后生成的json

    如有任何见解或建议,将不胜感激

    谢谢,,
    鲍勃

    我的方法/解决方案 传递我在事件中采取的最终方法,这对其他人都有帮助。中间的数据是特定于我的要求,但留下来显示我做我需要做的过程。

    async generatePressjobs(count) {
        const fs = require("fs");
        const path = require("path");
        const sourceDirectoryPath = path.join(__dirname, "../data/pjo/in/");
        const destDirectoryPath = path.join(__dirname, "../data/pjo/out/");
    
        for (i = 0; i < count; i++) {
          // read file and make object
          let content = JSON.parse(
            fs.readFileSync(sourceDirectoryPath + "source.json")
          );
    
          // Get current date and convert to required format for json file
          let isoUtc = new Date();
          let isoLocal = await this.toISOLocal(isoUtc);
          let fileNameTimeStamp = await this.getFileNameDate(isoUtc);
    
          // Get current hour and minute for DPI time stamp
          let dpiDate = new Date;
          let hour = dpiDate.getHours();
          let minute = dpiDate.getMinutes();
          dpiStamp = hour + '' + minute;
    
          // update attributes in the json obj
          content.batchid = `80-0000${i}`;
          content.id = `80-0000${i}-10035-tcard-${dpiStamp}-0101010000_.pdf`
          content.name = `80-0000${i}-8.5x11CALJEF-CalBody-${dpiStamp}-01010100${i}_.pdf`;
          content.printingStart = isoLocal;
          content.printingEnd = isoLocal;
    
          // write the file
          fs.writeFileSync(
            destDirectoryPath + `80-0000${i}-SOME-JOB-NAME-${dpiStamp}.pdf_Press Job printing end_${fileNameTimeStamp}.json`,
            JSON.stringify(content)
          );
        }
      },
    
    异步generatePressjobs(计数){
    常数fs=要求(“fs”);
    常量路径=要求(“路径”);
    const sourceDirectoryPath=path.join(uu dirname,“../data/pjo/in/”);
    const destDirectoryPath=path.join(uu dirname,“../data/pjo/out/”);
    对于(i=0;i
    现在发生了什么?有什么错误吗?