配置VSCode执行不同的任务

4

我有一个 TypeScript 项目,在 Visual Studio Code 中有以下任务:

{
  "version": "0.1.0",

  // The command is tsc.
  "command": "tsc",

  // Show the output window only if unrecognized errors occur. 
  "showOutput": "silent",

  // Under windows use tsc.exe. This ensures we don't need a shell.
  "windows": {
    "command": "tsc"
  },

  "isShellCommand": true,

  // args is the program to compile.
  "args": [],

  // use the standard tsc problem matcher to find compile problems in the output.
  "problemMatcher": "$tsc"
}

当我们按下 "Ctrl + Shift + B" 来构建时,它可以很好地工作。

当我们按下 "F5" 以运行/调试时,是否有可能通过命令行运行一个命令?

谢谢。

3个回答

10

任务运行器与调试加实时预览

任务运行器

目前,对于VSCode 0.5.0版本,您可以使用任务运行器,在task.json中标识出来,使用同一运行器来运行多个任务。但此时,配置不同的任务运行器是不可能的。例如,如果您正在使用Gulp作为任务运行器,您可能会有以下内容:

{
"version": "0.1.0",
"command": "gulp",
"isShellCommand": true,
"args": [
    "--no-color"
],
"tasks": [
    {
        "taskName": "serve-dev",
        "isBuildCommand": false,
        "isTestCommand": true,
        "showOutput": "always",
        "args": []
    },
    {
        "taskName": "serve-build",
        "isBuildCommand": false,
        "isTestCommand": true,
        "showOutput": "always",
        "args": []
    }

现在Gulp任务将使用Gulp定义和编码,但需要注意的重要事项是isBuildCommandisTestCommand,因为它们分别与CTRL+SHFT+B和CTRL+SHFT+T相关联。 因此,这两个任务将作为键盘快捷方式可用。 另外,如果您添加其他任务,则每个任务都会被枚举并可通过CTRL+SHFT+P,然后输入“RUN”,然后选择“TASK: Run Task”访问。 您的每个任务都将被列举、列出并可选择。

以下代码仅演示了如何将每个VSCode任务与任务运行器任务关联:

//automate build node server start and restart on changes
gulp.task('serve-build', ['optimize'], function () {

serve(false /* is Build */);

});

//automate dev node server start and restart on changes
gulp.task('serve-dev', ['inject'], function () {

serve(true /* is Dev */);

});

调试

现在,在使用Node.js或Mono进行调试时,您有类似的选项。您需要配置 launch.json 或按下 '齿轮图标'。您可以将调试器设置为 debugrun 您的应用程序,并使用 VSCode 的 'F5'PLAY 按钮或菜单来启动/停止/重新启动您的应用程序。从那里开始,您只需使用您喜欢的浏览器访问您的应用程序的服务器。您也可以使用外部调试器'附加'到您的应用程序。以下是一个示例 launch.json:

{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
    {
        // Name of configuration; appears in the launch configuration drop down menu.
        "name": "Debug src/server/app.js",
        // Type of configuration. Possible values: "node", "mono".
        "type": "node",
        // Workspace relative or absolute path to the program.
        "program": "src/server/app.js",
        // Automatically stop program after launch.
        "stopOnEntry": true,
        // Command line arguments passed to the program.
        "args": [],
        // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
        "cwd": ".",
        // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
        "runtimeExecutable": null,
        // Optional arguments passed to the runtime executable.
        "runtimeArgs": [],
        // Environment variables passed to the program.
        "env": { },
        // Use JavaScript source maps (if they exist).
        "sourceMaps": false,
        // If JavaScript source maps are enabled, the generated code is expected in this directory.
        "outDir": null
    },
    {
        // Name of configuration; appears in the launch configuration drop down menu.
        "name": "Run src/server/app.js",
        // Type of configuration. Possible values: "node", "mono".
        "type": "node",
        // Workspace relative or absolute path to the program.
        "program": "src/server/app.js",
        // Automatically stop program after launch.
        "stopOnEntry": false,
        // Command line arguments passed to the program.
        "args": [],
        // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
        "cwd": ".",
        // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
        "runtimeExecutable": null,
        // Optional arguments passed to the runtime executable.
        "runtimeArgs": [],
        // Environment variables passed to the program.
        "env": { },
        // Use JavaScript source maps (if they exist).
        "sourceMaps": false,
        // If JavaScript source maps are enabled, the generated code is expected in this directory.
        "outDir": null
    },
    {
        "name": "Attach",
        "type": "node",
        // TCP/IP address. Default is "localhost".
        "address": "localhost",
        // Port to attach to.
        "port": 5858,
        "sourceMaps": false
    }
  ]
}

注意 'stopOnEntry' 属性适用于运行和调试配置。这是您可以使用调试器运行或调试应用程序的方法。从那里,您只需使用调试 'PLAY' 按钮结合调试菜单选择适当的配置。

实时预览

目前在 VSCode 中还没有实现实时预览。到目前为止,我喜欢的有 BrowserSyncLive.JS

使用 NODEMON 的 GULP 任务

以下是一些代码,可以帮助指导如何配置 Gulp 来运行 node.js 服务器。请记住,Gulp 任务可以要求先运行其他任务。在上面的代码中,Gulp 任务 "serve-build" 需要先运行另一个任务 "optimize" "优化" 可以要求运行其他任务,等等。 您可以链接这些任务,以便您的顶级任务运行所有子级任务。以下是从 gulpfile.js 设置中的 Gulp 任务执行的函数:

function serve(isDev) {
log('Start pre processes and node server...');
var nodeOptions = {
    script: config.nodeServer,
    delayTime: 3,
    env: {
        'PORT': port,
        'NODE_ENV': isDev ? 'dev' : 'build'
    },
    watch: [config.server]
};

return $.nodemon(nodeOptions)
    .on('restart', ['vet'], function (ev) {
        log('*** nodemon restarted');
        log('files changes on restart:\n' + ev);
        setTimeout(function () {
            browserSync.notify('reloading now ...');
            browserSync.reload({ stream: false });
        }, config.browserReloadDelay);
    })
    .on('start', function () {
        log('*** nodemon started');
        startBrowserSync('isDev');
    })
    .on('crash', function () {
        log('*** nodemon crashed: script crashed for some reason');
    })
    .on('exit', function () {
        log('*** nodemon exited cleanly');
    });

}
因此,以下Gulp任务实际上只运行此函数,该函数通过Gulp nodemon插件运行nodemon以使用参数变量制作“production /“ build””或“test /“ dev””构建:
//automate build node server start and restart on changes
gulp.task('serve-build', ['optimize'], function () {

serve(false /* is Build */);

});

//automate dev node server start and restart on changes
gulp.task('serve-dev', ['inject'], function () {

serve(true /* is Dev */);

});

将 Gulp 任务映射到 VSCODE 任务运行器

最后,您可以通过向 VSCode tasks.json 添加条目,并使用 isBuildCommandisTestCommand 映射到相应的 CTRL+SHFT+B CTRL+SHFT-T,来映射顶级 Gulp 任务,例如 "serve-dev""serve-build"

{
"version": "0.1.0",
"command": "gulp",
"isShellCommand": true,
"args": [
    "--no-color"
],
"tasks": [
    {
        "taskName": "serve-dev",
        "isBuildCommand": false,
        "isTestCommand": true,
        "showOutput": "always",
        "args": []
    },
    {
        "taskName": "serve-build",
        "isBuildCommand": false,
        "isTestCommand": true,
        "showOutput": "always",
        "args": []
    }

VSCode 输出

VSCode 还有一个 task.json 属性,可以在 VSCode 中显示运行任务的输出。这将打开 VSCode 的 OUTPUT 窗口,就像使用 SHFT+CTRL+H 或选择菜单 VIEW 然后选择 SHOW OUTPUT 一样。目前输出窗口不会显示颜色。

只需将 "showOutput" 设置为 always 即可。也许这可以替换您启动运行 node 应用程序的终端/命令行窗口的需要。您还可以根据需要将此属性设置为 neversilent。您可以在 VSCode 文档 中找到更多有关这些属性的信息。

您还可以使用 CTRL-SHFT-BCTRL-SHFT-T 或在启动任务后使用菜单来 STOP 运行中的任务。

最后,如果必须在终端中编译代码并运行应用程序,我认为您需要在 task.json 配置中使用一个脚本/批处理文件来运行您的任务运行器,然后启动您的 node 服务器。


所以目前看来,我的唯一选择就是创建两个gulp任务,其中一个任务编译我的TypeScript代码,另一个任务通过命令提示窗口运行命令,对吗?基本上,我想按F5运行一个命令,该命令将在控制台中启动应用程序。 - n0n4m3
我认为你想要实现的是编译你的TypeScript并使用“F5”运行调试器键来运行一个Node服务器。这不会起作用,因为调试器与任务运行器是分开的。但是,你可以配置你的任务运行器以便使用单个任务快捷键CTRL+SHFT+B或CTRL+SHFT+T来编译和随后运行你的Node服务器。任务可以配置为在其他任务运行后才运行,就Gulp而言,有许多插件可供使用,如http://nodemon.io/和https://www.npmjs.com/package/gulp-nodemon。我已经尝试添加了一些代码到上面的答案中。 - GJSmith3rd
不,我不是在尝试启动node服务器。我想配置F5通过命令行运行一个程序,以便将我的项目部署到另一台设备上。当配置任务运行器时,我只看到了“node”和“mono”这两个选项,所以我想知道如何更改它以执行批处理文件或其他我选择的命令。谢谢! - n0n4m3

0

0

如果您不想使用gulp,只需进行typescript编译,则可以通过转到终端并运行tsc -w <filename.ts>来简单地完成。无需使用tasks.json。

它会监视文件更改并将它们转换为js文件。

然后,每当您按下“F5”时,它都应该运行在launch.json中指定的更新的js文件。

如果您想让tsc转换多个ts文件,还可以在应用程序根目录中添加tsconfig.json,其中包含“rootdir”,然后只需运行tsc -w和F5以执行应用程序。

示例tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "ES5",
        "outDir": "<js dir>",
        "rootDir": "<point to all ts dir>"
    }

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接