如何在 vss 扩展中处理 Node_Modules(用于 DevOps)

问题描述 投票:0回答:1

我正在构建一个可以在构建管道中安装和使用的 vss 扩展。我遵循了 Microsoft 的此示例。我的扩展任务是一个nodejs脚本。

我的扩展程序具有以下文件夹结构:

  • [node_modules](未捆绑)
  • [扫描任务]
    • 图标.png
    • run_sensor.js
    • 任务.json
  • my-hub.html
  • package.json
  • vss-extension.json

通过 Visual Studio 代码在本地运行 npm install 并执行 run_sensor.js 效果非常好。

我有一个自动化管道,当签入扩展时,它会捆绑 vsix 并使用以下命令发布到我的“发布者”:

npm install -g tfx-cli
npx tfx-cli extension create  --manifest-globs vss-extension.json --root $(Build.SourcesDirectory) --output-path $(Build.SourcesDirectory)
npx tfx extension publish --vsix $vsixFilePath --token $patToken --publisher $organizationOrPublisher --rev-version

到目前为止一切顺利!按预期发布。我已将此(私有)扩展安装到我的组织中,并在我的管道中运行它:

- task: scanTask@3
  inputs:
    repoDirectory: '$(buildDirectory)'

运行正常,但失败了,因为我的

run_sensor.js
需要“解压”。样品:

console.log(`Running script...`);

const fs = require('fs');
const https = require('https');
const os = require('os');
const path = require('path');
const unzipper = require('unzipper'); /*ERROR*/

// Get the current working directory
const currentDirectory = process.cwd();

// Print the command-line arguments and current directory
console.log(`Node Path: ${process.argv[0]}`);
console.log(`Script Path: ${process.argv[1]}`);
console.log(`Argument 3: ${process.argv[2]}`);
console.log(`The current working directory is: ${currentDirectory}`);
...

此行导致了问题:

const unzipper = require('unzipper');
并给出此错误:

2024-04-13T10:17:34.2926703Z ##[section]Starting: scanTask
2024-04-13T10:17:34.3048885Z ==============================================================================
2024-04-13T10:17:34.3049056Z Task         : Scan sample
2024-04-13T10:17:34.3049172Z Description  : Example task
2024-04-13T10:17:34.3049294Z Version      : 3.14.0
2024-04-13T10:17:34.3049371Z Author       : Me!
2024-04-13T10:17:34.3049480Z Help         : Example help
2024-04-13T10:17:34.3049585Z ==============================================================================
2024-04-13T10:17:35.0945760Z Running script...
2024-04-13T10:17:37.2196175Z module.js:478
2024-04-13T10:17:37.2199321Z     throw err;
2024-04-13T10:17:37.2199629Z     ^
2024-04-13T10:17:37.2199753Z 
2024-04-13T10:17:37.2200496Z Error: Cannot find module 'unzipper'
2024-04-13T10:17:37.2200888Z     at Function.Module._resolveFilename (module.js:476:15)
2024-04-13T10:17:37.2201241Z     at Function.Module._load (module.js:424:25)
2024-04-13T10:17:37.2201551Z     at Module.require (module.js:504:17)
2024-04-13T10:17:37.2201862Z     at require (internal/module.js:20:19)
2024-04-13T10:17:37.2202567Z     at Object.<anonymous> (C:\Users\myaccount\agent1\_work\_tasks\scanTask_a12345\3.13.0\run_sensor.js:39:18)
2024-04-13T10:17:37.2203032Z     at Module._compile (module.js:577:32)
2024-04-13T10:17:37.2203365Z     at Object.Module._extensions..js (module.js:586:10)
2024-04-13T10:17:37.2203691Z     at Module.load (module.js:494:32)
2024-04-13T10:17:37.2203984Z     at tryModuleLoad (module.js:453:12)
2024-04-13T10:17:37.2213242Z     at Function.Module._load (module.js:445:3)
2024-04-13T10:17:37.2722743Z ##[error]Exit code 1 returned from process: file name 'C:\Users\myaccount\agent1\externals\node\bin\node.exe', arguments '"C:\Users\myaccount\agent1\_work\_tasks\scanTask_a12345\3.13.0\run_sensor.js"'.
2024-04-13T10:17:37.2742884Z ##[section]Finishing: scanTask

发生这种情况是因为

node_modules
文件夹未部署在运行任务的构建代理上。已安装任务的示例输出:

我尝试通过添加“预启动”步骤来运行 npm install 来更新

package.json
文件,如下所示:

{
  "name": "my-ado-extension",
  "version": "3.14.0",
  "description": "Example task",
  "main": "run_sensor.js",
  "scripts": {
    "prestart": "npm install",
    "start": "node run_sensor.js"
  },
  "repository": {
  ...

它没有解决问题。

我还尝试将 unzipper 添加到 package.json 的依赖项中,但这也不起作用:

...
"dependencies": {
    "azure-devops-extension-sdk": "^4.0.2",
    "unzipper": "^0.10.14"
  }

这也不起作用。

如何成功部署带有扩展的解压器node_module?

Task.json 是:

{ 
  "id": "a4234567-b49c-22d3-f456-526614176030",
  "name": "scanTask",
  "friendlyName": "Scan code",
  "description": "my description",
  "author": "Me!",
  "helpMarkDown": "Help mark down",
  "category": "DevOps",
  "visibility": [
    "Build",
    "Release"
  ],
  "demands": [],
  "version": {
    "Major": 1,
    "Minor": 1,
    "Patch": 0
  },
  "instanceNameFormat": "Sample Task $(message)",
  "inputs": [
  ],
  "execution": {
    "Node": {
      "target": "run_sensor.js",
      "argumentFormat": "$(buildDirectory)"
    }
  }
}
node.js npm azure-devops azure-pipelines
1个回答
0
投票

根据您的描述,您需要在创建 Azure DevOps Extension 任务时包含 node_modules 文件夹。

扩展包将基于文件 vss-extension.json 创建。

我们可以在 vss-extension.json 中设置字段 files 以包含 node_modules 文件夹。请参阅此文档:扩展文件

您可以参考以下方法:

方法1:在vss-extension.json -> files 字段中添加node_modules文件夹 .

例如:

....
    "files": [
        {
            "path": "scanTask",
            "addressable": true
        },
        {
            "path": "node_modules",
            "packagePath": "node_modules",
            "addressable": true
        }
    ],
....

方法2:您可以手动/使用命令将node_modules复制到scanTask文件夹。然后就可以直接创建扩展包了。

例如:

[node_modules] 
[scanTask]
  --node_modules
  --icon.png
  --run_sensor.js
  --task.json
my-hub.html
package.json
vss-extension.json

结果:

© www.soinside.com 2019 - 2024. All rights reserved.