从nodejs脚本内部解析nodejs(parsing nodejs from inside of a nodejs script)

编程入门 行业动态 更新时间:2024-10-17 22:26:28
从nodejs脚本内部解析nodejs(parsing nodejs from inside of a nodejs script)

是否有可能解析节点js文件并从另一个节点js文件内部运行部分代码?

我有一个node.js文件,我试图从另一个node.js脚本中解析。 我不控制文件的内容,但是它是有效的node.js,它在代码中总是有一个函数调用:getParameterDefinitions

这是我要解析的文件: https : //github.com/jscad/OpenJSCAD.org/blob/master/examples/gear.jscad

特别是我想从我的代码中getParameterDefinitions返回的JSON数组作为变量。

这是我目前的代码:

const fs = require('fs') inputFile = 'gear.jscad' let src = fs.readFileSync(inputFile, 'UTF8') console.log(src) params = Parse(src,'getParameterDefinitions') //how to write this line?

我如何让节点解析javascript并运行函数让结果返回到params变量中。

is it possible to parse a node js file and run part of the code from inside of another node js file?

I have a node.js file that I am trying to parse from inside another node.js script. I don't control the contents of the file, but it is valid node.js and it always has a function inside of the code called: getParameterDefinitions

Here is a file I am trying to parse: https://github.com/jscad/OpenJSCAD.org/blob/master/examples/gear.jscad

Specifically I want the JSON array returned from getParameterDefinitions as a variable in my code.

Here is the code I have currently:

const fs = require('fs') inputFile = 'gear.jscad' let src = fs.readFileSync(inputFile, 'UTF8') console.log(src) params = Parse(src,'getParameterDefinitions') //how to write this line?

How can I have node parse the javascript and run the function to get me back the results into the params variable.

最满意答案

有些人可能会建议使用eval 。 但是有一个更简洁的方法: Function构造函数 - 参见这里 。 然后这样做:

const getGetParameterDefinitions = new Function(src + ";\nreturn getParameterDefinitions;"); const getParameterDefinitions = getGetParameterDefinitions(); const params = getParameterDefinitions();

否则这个:

const getParameterDefinitions = new Function(src + ";\nreturn getParameterDefinitions();"); const params = getParameterDefinitions();

(注意:在这两种情况下, Function的参数略有不同。)

这仍然是一个坏主意 - 除非你相信编写代码的人,否则不要这样做。

另外,我假设函数getParameterDefinitions是在src脚本的最外层范围内声明的。

Some people may suggest using eval. But there's a slightly cleaner way: the Function constructor - see here. Then do this:

const getGetParameterDefinitions = new Function(src + ";\nreturn getParameterDefinitions;"); const getParameterDefinitions = getGetParameterDefinitions(); const params = getParameterDefinitions();

or else this:

const getParameterDefinitions = new Function(src + ";\nreturn getParameterDefinitions();"); const params = getParameterDefinitions();

(Note: the argument to Function is slightly different in these two cases.)

This is still a bad idea though - don't do it unless you trust the people who wrote the code.

Also, I'm assuming that the function getParameterDefinitions is declared in the outermost scope of the src script.

更多推荐

本文发布于:2023-08-07 13:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   nodejs   script   parsing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!