How to add a post tsc build task that copy files?

2020-05-31 04:38发布

问题:

How to add another task to VSCode, that copies files from x to y, after the tsc build task?

回答1:

You can use a task runner like gulp to accomplish this...

You can configure vscode to run the build task below, which is dependent upon the compile task.

var gulp = require('gulp'),
  exec = require('child_process').exec;

gulp.task('build', ['compile'], function () {
  return gulp.src('./config/**/*.json')
    .pipe(gulp.dest('./dist'));
});

gulp.task('compile', function (done) {
  exec('tsc -p ./app', function (err, stdOut, stdErr) {
    console.log(stdOut);
    if (err){
      done(err);
    } else {
      done();
    }
  });
});

There is documentation about running gulp tasks via vscode here: https://code.visualstudio.com/Docs/tasks



回答2:

You can use npm scripts for this. For example my package.json:

"scripts": {
  "compile": "tsc -p . ",
  "html": "cp -r ./src/public/ ./bin/public/",
  "views": "cp -r ./src/views/ ./bin/views/",
  "build": "npm run compile && npm run views && npm run html"
}

Here 2 scripts html and views for copying and task build runs them concurrently. In tasks.json I have next:

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "showOutput": "silent",
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "run",
                "build"
            ]
        }
    ]
}

So shift+cmd+B will run npm build script.



回答3:

If you're running in some kind of "developer mode" and need to --watch for file changes to run a task after every compile, I prefer using tsc-watch to accomplish this.

For example, in package.json:

{
  "scripts": {
    "dev": "tsc-watch --onSuccess=\"yarn otherThing\"",
    "otherThing": "echo \"hi\""
  }
}