【问题标题】:Copy file and replace variable with NodeJS复制文件并用 NodeJS 替换变量
【发布时间】:2020-04-28 06:33:05
【问题描述】:

我尝试复制和替换多个文件中的一些变量。为此,我使用esj 模块替换我的变量。

但我不知道 ejs 模块是否适合我的情况。我只想复制“模板”初始文件并替换文件中的变量。

我使用 NodeJS 的例子:

    const symfonyPluginPath = path.join(
      __dirname,
      '../plugins/symfony/template'
    );
    const testPath = path.join(__dirname, '../plugins/test');
    shell.rm('-rf', testPath);
    shell.mkdir(testPath);
    shell.cp('-r', `${symfonyPluginPath}/*`, testPath);
    shell.cp('-r', `${symfonyPluginPath}/.*`, testPath);
    shell.cd(testPath);

    // @ts-ignore
    fs.readdir(testPath, (error, files) => {
      files.forEach((file) => {
        const compiled = ejs.compile(
          fs.readFileSync(`${testPath}/${file}`, 'utf8')
        );
        const test = compiled({ appName: 'test' });

        console.log(test);
      });
    });

此代码仅适用于 1 个文件,但在 forEach 我有一个错误 EISDIR: illegal operation on a directory, read

我不知道我的方法是否良好,以及 ejs 是否是正确的模块。

谁能帮帮我?

感谢社区!

【问题讨论】:

    标签: node.js command-line-interface ejs


    【解决方案1】:

    EISDIR 代表“错误,是目录”。这意味着 NPM 正在尝试对文件执行某些操作,但它是一个目录。 试试这种格式--- path.join('x/y/z', '/plugins/test')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2013-04-24
      • 1970-01-01
      • 2017-07-30
      • 2014-02-16
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多