节点应用程序在全局安装时处理路径中的转义字符处理方式不同

时间:2022-04-10 15:39:01

I am very new to this Node stuff so there is probably a really simple answer to this but anyways...

我对这个节点的东西很新,所以可能有一个非常简单的答案,但无论如何......

I am building an app that prompts for a file path. This path is supplied by dropping a file into the Terminal window which gives a path with spaces escaped like this:

我正在构建一个提示输入文件路径的应用程序。通过将文件放入终端窗口来提供此路径,该窗口提供了一个空格转义的路径,如下所示:

Users/[username]/Desktop/test\ file.txt

I then use jsonfile to add this path to an array and write out a JSON file. In this file the path now has the escape escaped and looks like this:

然后我使用jsonfile将此路径添加到数组并写出JSON文件。在此文件中,路径现在具有转义转义,如下所示:

Users/[username]/Desktop/test\\ file.txt

Now I want to grab that path from the JSON and reveal it in Finder and this is where I get lost. I am using mac-open to reveal the path by passing the -R option and this works fine while I am testing and running my code using node bin/my-code.js but as soon I install with npm install -g . it breaks. The install works and I can run the app (there are other functions that work fine) but now if I try to reveal the path I get the error:

现在我想从JSON中获取该路径并在Finder中显示它,这就是我迷失的地方。我使用mac-open通过传递-R选项来显示路径,这在我使用node bin / my-code.js测试和运行我的代码时工作正常,但是我很快就用npm install -g安装了。它打破了。安装工作,我可以运行应用程序(还有其他功能正常工作)但现在如果我试图揭示路径我得到错误:

{ [Error: Command failed: /bin/sh -c open -a "Finder" -R "/Users/[username]/Desktop/test\\\ file.txt"
The file /Users/[username]/Desktop/test\\ file.txt does not exist.
]
  killed: false,
  code: 1,
  signal: null,
  cmd: '/bin/sh -c open -a "Finder" -R "/Users/[username]/Desktop/test\\\\\\ file.txt"' }

I have noticed the varying amounts of escapes in the different path references in the error so my questions are:

我注意到错误中不同路径引用中的转义量不同,所以我的问题是:

  1. Why is this different when I test using node bin/my-code.js and when I install globally and run that way?
  2. 当我使用node bin / my-code.js进行测试时以及当我全局安装并以这种方式运行时,为什么会有所不同?
  3. Is there a way that I can make the two behave the same as I don't really want to be installing every time I want to test my code?
  4. 是否有一种方法可以让我的两个行为相同,因为每次我想测试我的代码时我都不想安装它?
  5. Is there a more robust way of storing and/or escaping file paths?
  6. 是否有更强大的存储和/或转义文件路径的方法?

Sorry this has got a bit rambling. Any advice welcome :)

对不起,这有点散漫。欢迎任何建议:)

1 个解决方案

#1


0  

So I haven't exactly answered all the questions I posed but I have fixed the issue and learned what I consider a really valuable lesson in Node development.

所以我还没有完全回答我提出的所有问题,但我已经解决了这个问题并且学到了我认为在Node开发中非常有价值的一课。

Trying to find the problem I actually open the source mac-open and in doing so realised that I didn't actually need to be using the additional module at all. Searching the source led me to look into reading about child_process and eventually sacking off the whole mac-open module for a simple:

试图找到问题我实际上打开源mac-open并且这样做意识到我实际上根本不需要使用附加模块。搜索源代码让我看看有关child_process的阅读,并最终解除了整个mac-open模块的简单问题:

var exec = require("child_process").exec;

exec("open " +assetsPath +" -R", function(err){
    if(err)
        console.log(err);
});

The esson here is not to use 3rd party modules just because they are there. In this situation I didn't need the all-situation covereage that it was trying to provide and the extra complexity actually caused the problem.

这里的esson不是因为它们在那里而使用第三方模块。在这种情况下,我不需要它试图提供的全部情况,并且额外的复杂性实际上导致了问题。

#1


0  

So I haven't exactly answered all the questions I posed but I have fixed the issue and learned what I consider a really valuable lesson in Node development.

所以我还没有完全回答我提出的所有问题,但我已经解决了这个问题并且学到了我认为在Node开发中非常有价值的一课。

Trying to find the problem I actually open the source mac-open and in doing so realised that I didn't actually need to be using the additional module at all. Searching the source led me to look into reading about child_process and eventually sacking off the whole mac-open module for a simple:

试图找到问题我实际上打开源mac-open并且这样做意识到我实际上根本不需要使用附加模块。搜索源代码让我看看有关child_process的阅读,并最终解除了整个mac-open模块的简单问题:

var exec = require("child_process").exec;

exec("open " +assetsPath +" -R", function(err){
    if(err)
        console.log(err);
});

The esson here is not to use 3rd party modules just because they are there. In this situation I didn't need the all-situation covereage that it was trying to provide and the extra complexity actually caused the problem.

这里的esson不是因为它们在那里而使用第三方模块。在这种情况下,我不需要它试图提供的全部情况,并且额外的复杂性实际上导致了问题。