使用md5 grunt任务生成md5文件名。现在我想用任务回调中的新文件名重命名HTML文件中的源。我想知道最简单的方法是什么。


当前回答

这可能会帮助到一些人:

这与全局替换略有不同

我们从终端运行 节点replace.js

replace.js:

function processFile(inputFile, repString = "../") {
var fs = require('fs'),
    readline = require('readline'),
    instream = fs.createReadStream(inputFile),
    outstream = new (require('stream'))(),
    rl = readline.createInterface(instream, outstream);
    formatted = '';   

const regex = /<xsl:include href="([^"]*)" \/>$/gm;

rl.on('line', function (line) {
    let url = '';
    let m;
    while ((m = regex.exec(line)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        url = m[1];
    }

    let re = new RegExp('^.* <xsl:include href="(.*?)" \/>.*$', 'gm');

    formatted += line.replace(re, `\t<xsl:include href="${repString}${url}" />`);
    formatted += "\n";
});

rl.on('close', function (line) {
    fs.writeFile(inputFile, formatted, 'utf8', function (err) {
        if (err) return console.log(err);
    });

});
}


// path is relative to where your running the command from
processFile('build/some.xslt');

这就是它的作用。 我们有几个文件有xml:includes

然而,在开发过程中,我们需要向下移动的路径。

从这个

<xsl:include href="common/some.xslt" />

这个

<xsl:include href="../common/some.xslt" />

因此,我们最终运行两个regx模式,一个用于获取href,另一个用于写入 也许有更好的方法,但目前是有效的。

谢谢

其他回答

我会使用双工流代替。就像这里记录的nodejs doc双工流

Transform流是计算输出的双工流 离输入有一段距离。

我在用一串大代码替换一个小占位符时遇到了问题。

我正在做:

var replaced = original.replace('PLACEHOLDER', largeStringVar);

我发现问题出在JavaScript的特殊替换模式上,如下所述。由于我使用的代码作为替换字符串有一些$,它是混乱的输出。

我的解决方案是使用函数替换选项,它不做任何特殊替换:

var replaced = original.replace('PLACEHOLDER', function() {
    return largeStringVar;
});

通常,我使用tiny-replace-files替换文件中的文本。这个pkg更小更轻… https://github.com/Rabbitzzc/tiny-replace-files

import { replaceStringInFilesSync } from 'tiny-replace-files'

const options = {
  files: 'src/targets/index.js',
  from: 'test-plugin',
  to: 'self-name',
}

# await
const result = replaceStringInFilesSync(options)
console.info(result)

<p>请点击下面的{{link}}验证账号</p>


function renderHTML(templatePath: string, object) {
    const template = fileSystem.readFileSync(path.join(Application.staticDirectory, templatePath + '.html'), 'utf8');
    return template.match(/\{{(.*?)\}}/ig).reduce((acc, binding) => {
        const property = binding.substring(2, binding.length - 2);
        return `${acc}${template.replace(/\{{(.*?)\}}/, object[property])}`;
    }, '');
}
renderHTML(templateName, { link: 'SomeLink' })

当然,您可以改进读取模板函数,将其作为流读取,并按行组合字节,使其更有效

这可能会帮助到一些人:

这与全局替换略有不同

我们从终端运行 节点replace.js

replace.js:

function processFile(inputFile, repString = "../") {
var fs = require('fs'),
    readline = require('readline'),
    instream = fs.createReadStream(inputFile),
    outstream = new (require('stream'))(),
    rl = readline.createInterface(instream, outstream);
    formatted = '';   

const regex = /<xsl:include href="([^"]*)" \/>$/gm;

rl.on('line', function (line) {
    let url = '';
    let m;
    while ((m = regex.exec(line)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        url = m[1];
    }

    let re = new RegExp('^.* <xsl:include href="(.*?)" \/>.*$', 'gm');

    formatted += line.replace(re, `\t<xsl:include href="${repString}${url}" />`);
    formatted += "\n";
});

rl.on('close', function (line) {
    fs.writeFile(inputFile, formatted, 'utf8', function (err) {
        if (err) return console.log(err);
    });

});
}


// path is relative to where your running the command from
processFile('build/some.xslt');

这就是它的作用。 我们有几个文件有xml:includes

然而,在开发过程中,我们需要向下移动的路径。

从这个

<xsl:include href="common/some.xslt" />

这个

<xsl:include href="../common/some.xslt" />

因此,我们最终运行两个regx模式,一个用于获取href,另一个用于写入 也许有更好的方法,但目前是有效的。

谢谢