【问题标题】:Configure VSCode to execute different task配置 VSCode 以执行不同的任务
【发布时间】:2015-11-05 13:31:18
【问题描述】:

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

{
  "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”运行/调试时,它会通过命令行运行命令?

谢谢。

【问题讨论】:

    标签: configuration typescript visual-studio-code


    【解决方案1】:

    我相信这已通过后续功能解决,即预启动任务。你可以让它在启动 node/Chrome 之前运行一个任务。

    http://tstringer.github.io/javascript/vscode/nodejs/gulpjs/2015/10/14/vscode-prelaunchtask.html

    【讨论】:

      【解决方案2】:

      如果你不想使用 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>"
          }
      

      }

      【讨论】:

        【解决方案3】:

        TASK RUNNERS VS DEBUGGING PLUS 实时预览

        任务运行器

        目前,对于 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 and CTRL+SHFT+T 相关。所以这两个任务可以作为键盘快捷键使用。此外,如果您添加其他任务,它们将被枚举并通过CTRL+SHFT+P then type "RUN" then select "TASK: Run Task". 访问您的每个任务都将被枚举、列出和选择。

        以下代码只是演示了 eash 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 或按'gear icon'。您可以将调试器设置为 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
            }
          ]
        }
        

        注意RUN and DEBUG 设置的'stopOnEntry' 属性。这是您可以使用调试器运行或调试应用程序的方式。从那里您只需使用调试'PLAY' 按钮和调试菜单来选择适当的配置。

        实时预览

        实时预览当前未在 VSCode 中实现。到目前为止,我最喜欢的两个是 BrowserSyncLive.JS

        使用 NODEMON 完成任务

        以下是一些代码,它们可能有助于指明配置 Gulp 以运行 node.js 服务器的方法。请记住,Gulp 任务可能需要先运行其他任务。在上面的代码中,Gulp 任务"serve-build" 需要另一个任务"optimize" 才能首先运行。 "optimize" can require other tasks to run and so forth.您可以链接这些任务,以便您的顶级任务运行所有子级任务。以下是从 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 任务运行器

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

        {
        "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。也许这可以取代您启动运行节点应用程序的终端/命令行窗口的需要。您还可以根据需要将此属性设置为neversilent。您可以在 VSCode documentation 中找到有关这些属性的更多信息。

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

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

        【讨论】:

        • 所以看来我现在唯一的选择是创建 2 个 gulp 任务,其中一个任务来编译我的 TypeScript 代码,另一个任务是通过命令提示符窗口运行命令,正确?基本上我想按 F5 来运行一个在控制台中启动应用程序的命令。
        • 我认为您要完成的是编译您的打字稿并使用“F5”运行调试器键运行节点服务器。这将不起作用,因为调试器与任务运行器是分开的。但是,您可以将任务运行器配置为编译并随后使用单个任务快捷方式 CTRL+SHFT+B 或 CTRL+SHFT+T 运行节点服务器。可以将任务配置为仅在其他任务运行后才运行,就 Gulp 而言,您可以使用许多插件,例如 nodemon.ionpmjs.com/package/gulp-nodemon 。我试图在上面的答案中添加一些代码。
        • 不,我不是要启动节点服务器。我想将 F5 配置为通过命令行运行执行程序,该程序会将我的项目部署到不同的设备。我在配置任务运行器时看到的唯一选项是“node”和“mono”,这就是为什么我要问如何更改它以执行批处理文件或我选择的其他命令。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 2020-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多