以编程方式在JS文件中插入一行

我想写一个程序,将在现有的HTML文件中添加一行到JS。 我以为我会使用node.js,因为它是相同的语言。

我看了一些项目, esprima , 橡子 , rocambole , falafel , escodegen等,但我似乎无法find任何文件如何添加到AST的节点,以创build此新行。 我不知道是否一个parsing工具是最好的东西 – 也许我会更好地使用条纹操纵/正则expression式? 我也想修改一些行以及添加新行。 下面的代码是我目前不得不添加一行到JavaScript的非工作代码。

var newNode = JSON.parse(transparent); // get html string here fs.readFile(path.normalize('C:\myDoc'), function (err, html) { if (err) { throw err; } handler = new htmlparser.DomHandler(function (err, dom) { if (err) { sys.debug("Error: " + err); } else { // console.log(dom); var scripts = select(dom, 'body script'); scripts.forEach(function (script) { if (typeof script.attribs.src === 'undefined') { var ast = rocambole.parse(script.children[0].data); rocambole.recursive(ast, function (rootNode) { if (rootNode.type == "VariableDeclaration") { rocambole.recursive(rootNode, function (node) { if (node.type === "Identifier" && node.name === "params") { // I need to insert a new node after rootNode here console.log(escodegen.generate(rootNode.root)); } }); } }); } }) } }) new htmlparser.Parser(handler).parseComplete(html); });