有几种方法可以在 launch.json 中引用来自 Visual Studio Code (vscode) 的敏感数据,而无需将数据包含在 launch.json 文件中。
vscode Variables Reference 很好地记录了这些解决方案。
您可以将敏感变量放在环境变量中(可能通过您的 shell 配置文件加载,例如 .bash_profile
)。然后,您可以“通过 ${env:Name}
语法(例如,${env:USERNAME}
)”引用它。
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"cwd": "${workspaceFolder}",
"args": ["${env:USERNAME}"]
}
提示输入
您可以通过定义promptString
输入类型在启动时提示敏感信息,如以下从文档中提取的配置所示。下面的${input:componentName}
用作参数,并在下面的inputs
部分中定义为promptString
,以便在您运行相关任务时提示您输入值。
{
"version": "2.0.0",
"tasks": [
{
"label": "ng g",
"type": "shell",
"command": "ng",
"args": ["g", "${input:componentType}", "${input:componentName}"]
}
],
"inputs": [
/** skipping componentType definition for brevity -- see docs for that */
{
"type": "promptString",
"id": "componentName",
"description": "Name your component.",
"default": "my-new-component"
}
]
}
promptString
的定义如下:
-
描述:显示在快速输入中,为输入提供上下文。
-
default:用户不输入其他内容时将使用的默认值。
-
密码:设置为 true 以使用不会显示键入值的密码提示输入。
为输入运行自定义命令
您还可以运行自定义命令来获取输入。文档使用以下配置作为示例。请注意,与其他输入类型一样,该命令必须在 inputs
部分中使用 type
定义。
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run specific test",
"program": "${workspaceFolder}/${input:pickTest}"
}
],
"inputs": [
{
"id": "pickTest",
"type": "command",
"command": "extension.mochaSupport.testPicker",
"args": {
"testFolder": "/out/tests"
}
}
]
}
command
类型的其他选项是:
-
command:在变量插值上运行的命令。
-
args:传递给命令实现的可选选项包。