在zeit / pkg npm中找不到包含的.ps1文件来运行node-powershell命令

时间:2019-03-10 20:45:28

标签: windows zeit-pkg

我正在努力找到使用node-powershell调用powershell脚本的正确语法,并使用zeit / pkg语法将所有内容编译到单个.exe文件中。如何使用https://github.com/zeit/pkg中的信息指定正确的PS1文件的编译路径,并使其正确启动?我的文件夹结构和package.json在下面。我认为我正确地遵循了指示,但是我花了几个小时尝试尝试,但没有成功。因此,请其他人注意,以防我遗漏明显的东西。

我在项目根目录中的文件夹结构具有一个资产目录(请参见下文)。在此,我有一个PS1文件,然后还有三个其他目录,这些目录具有包含PS1模块,信息文件等各种文件的子目录,我的PS1脚本用于运行和锁定IE。如果使用Electron之类的东西来打包所有内容并公开源代码,则所有脚本都可以正常工作。但是,我们需要锁定源代码,因此Electron不能作为选择。

assets
  IE-Start.ps1 <-- this is a file
  InternetExplorer <-- this is a folder with files I read in with my script
  modules <-- this is a folder with files I read in with my script
  Windows <-- this is a folder with files I read in with my script
index.js
package-lock.json
package.json
README.md
node_modules

我从根目录编译.exe。我知道目录中的文件在那里,因为添加它们后,.exe上的文件大小增加了很多。所以它确实增加了一些东西。我只是似乎无法正确引用内部路径。

pkg . -t node8-win

我正在我的index.js中运行以下代码,但未能显示找不到ps1文件

let scriptPath = path.join(__dirname, 'assets/IE-Start.ps1')
ps.addCommand(scriptPath);

我得到的错误是这样的:

Cannot find path 'C:\snapshot\ielockdown\assets\InternetExplorer\' 
because it does not exist.

我的package.json内容如下:

{
  "name": "ielockdown",
  "version": "1.0.0",
  "description": "IE Lockdown Tool compiled for Win10",
  "main": "index.js",
  "scripts": {
    "start": "npm start"
  },
  "author": "Dale Bingham",
  "license": "ISC",
  "dependencies": {
    "node-powershell": "^4.0.0",
    "path": "^0.12.7"
  },
  "bin": "index.js",
  "pkg": {
    "scripts": "*.js",
    "assets": [
      "assets/*.ps1",
      "assets/**/*"
    ],
    "targets" : [
      "node8"
    ]
  }
}

我在下面包括的--debug标志的某些输出,以确保所看到的是正在添加文件。我只是没有正确地引用它们?任何帮助表示赞赏。

> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/IE-Start.ps1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/Excel.psm1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/GitHub.psm1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/GroupPolicy.psm1

1 个答案:

答案 0 :(得分:0)

我只是想在这里发布我的答案,因为在尝试找到答案时,我偶然发现了这个问题(并且只有这个问题)。 我在node-powershell的Github上发布了类似的问题,最后我自己回答了这个问题:https://github.com/rannn505/node-powershell/issues/106#issuecomment-577249713

这是我的解释:

基本上,据我了解,您只能使用fs引用打包到exe中的文件。在这种情况下,仅告诉Powershell打开路径的任何其他方法均无效,因为只有Node的fs可以理解快照文件系统。这意味着您必须首先经过fs才能获得脚本的字符串版本,然后使用script block对其进行调用。您还需要添加最后一个换行符,因为否则该命令将无法执行并且外壳会挂起。我也向该示例添加了一个参数。

...
var arg1 = 14;
var startStr = fs.readFileSync(path.join(__dirname, 'start.ps1')).toString();
ps.addCommand('& {' + startStr + '} ' + arg1 + '\n');
...