上传测试文件时如何修复tsc-watch重启?

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

我想在我的NestJS项目中本地测试文件上传API。我关注了this doc。但是,当我上传文件时,由于tsc-watch文件夹已更改,因此upload重新启动。

这里我正在使用NestJS版本6,并且从nest-cli安装了所有组件。我也正在使用Google Cloud Storage存储文件。我通过运行yarn start:dev在本地测试项目。

我已经尝试在uploadexcludetsconfig.json选项中添加tsconfig.build.json文件夹,但是它不起作用。

我也尝试过使用nodemon。不幸的是,当我更改代码时,nodemon无法重新启动。

以下是一些代码(repo here):

控制器

    @Patch(':id/file')
    @UseInterceptors(
        FileInterceptor('file', { dest: join(__dirname, '../../upload') }),
    )
    async uploadProblem(
        @UploadedFile() file: FileDto,
        @Param('id') id: string,
    ) {
        if (file) {
            await this.service.uploadProblem(id, file);
        } else {
            throw new BadRequestException('No files uploaded');
        }
    }

服务

    async uploadProblem(id: string, file: FileDto) {
        const problem = await this.findById(id);
        await this.fileService.uploadFile(`${problem.code}/problem`, file);
    }

fileService

    async uploadFile(name: string, file: FileDto) {
        await this.bucket.upload(file.path, {
            destination: name,
            contentType: file.mimetype,
            resumable: false,
        });
        removeSync(file.path);
    }

package.json

{
  ...
  "scripts": {
    ...
    "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
    ...
  },
  "dependencies": {
    "@google-cloud/storage": "^3.3.1",
    "@nestjs/common": "^6.0.0",
    "@nestjs/core": "^6.0.0",
    "@nestjs/mongoose": "^6.1.2",
    "@nestjs/platform-express": "^6.0.0",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.10.1",
    "fs-extra": "^8.1.0",
    "mongoose": "^5.7.3",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.0",
    "@types/express": "4.16.1",
    "@types/fs-extra": "^8.0.0",
    "@types/jest": "24.0.11",
    "@types/mongoose": "^5.5.19",
    "@types/node": "11.13.4",
    "@types/supertest": "2.0.7",
    "jest": "24.7.1",
    "prettier": "1.17.0",
    "supertest": "4.0.2",
    "ts-jest": "24.0.2",
    "ts-node": "8.1.0",
    "tsc-watch": "2.2.1",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typescript": "3.4.3"
  },
  ...
}

我希望以后再上传和删除文件,但是tsc-watch会继续重新启动,并且永远不会上传或删除文件。

无论何时更改tsc-watch目录时,是否都可以阻止upload重新启动,或者我还应该使用其他工具吗?

file nestjs tsc
2个回答
0
投票

您可以在package.json中设置忽略监视

"watch": {
    "ignore": "upload"
  }

0
投票

使用ts-lint的第一个解决方案是将upload文件夹移到src文件夹之外,然后忽略该文件夹。

另一种解决方案与您建议的相同,将nodemonts-node一起使用,但是nodemon本身存在观看docker内部文件并将启动命令更改为以下内容的问题。

"start": "nodemon -L --watch 'src/**/*.ts' --exec ts-node src/main.ts",
© www.soinside.com 2019 - 2024. All rights reserved.