|
1 | 1 | import {IInitCmdContext} from "@tsed/cli"; |
2 | | -import {Inject, Injectable, OnExec, ProjectPackageJson, Renderer} from "@tsed/cli-core"; |
| 2 | +import { |
| 3 | + Inject, |
| 4 | + Injectable, |
| 5 | + OnExec, |
| 6 | + ProjectPackageJson, |
| 7 | + RootRendererService, |
| 8 | + ScriptsRendererService, |
| 9 | + SrcRendererService |
| 10 | +} from "@tsed/cli-core"; |
| 11 | +import {join} from "path"; |
| 12 | +import {TEMPLATE_DIR} from "../utils/templateDir"; |
3 | 13 |
|
4 | 14 | @Injectable() |
5 | 15 | export class JestInitHook { |
6 | 16 | @Inject() |
7 | 17 | protected packageJson: ProjectPackageJson; |
8 | 18 |
|
| 19 | + @Inject() |
| 20 | + protected srcRenderer: SrcRendererService; |
| 21 | + |
| 22 | + @Inject() |
| 23 | + protected rootRenderer: RootRendererService; |
| 24 | + |
| 25 | + @Inject() |
| 26 | + protected scriptsRenderer: ScriptsRendererService; |
| 27 | + |
9 | 28 | @OnExec("init") |
10 | | - onInitExec(ctx: IInitCmdContext) {} |
| 29 | + onInitExec(ctx: IInitCmdContext) { |
| 30 | + this.addScripts(ctx); |
| 31 | + this.addDependencies(ctx); |
| 32 | + this.addDevDependencies(ctx); |
| 33 | + |
| 34 | + return [ |
| 35 | + { |
| 36 | + title: "Generate files for mocha", |
| 37 | + task: (ctx: any) => { |
| 38 | + return this.rootRenderer.renderAll([ |
| 39 | + "init/jest.config.js.hbs" |
| 40 | + ], ctx, { |
| 41 | + templateDir: TEMPLATE_DIR |
| 42 | + }); |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + title: "Generate scripts files for mocha", |
| 47 | + task: (ctx: any) => { |
| 48 | + return this.scriptsRenderer.renderAll( |
| 49 | + [ |
| 50 | + "init/setup.jest.js.hbs" |
| 51 | + ], ctx, { |
| 52 | + templateDir: TEMPLATE_DIR, |
| 53 | + rootDir: join(this.scriptsRenderer.rootDir, "jest") |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + ]; |
| 58 | + } |
11 | 59 |
|
12 | 60 | addScripts(ctx: IInitCmdContext) { |
13 | | - this.packageJson.addScripts({}); |
| 61 | + this.packageJson.addScripts({ |
| 62 | + test: "yarn test:lint && yarn test:coverage", |
| 63 | + "test:unit": "cross-env NODE_ENV=test jest", |
| 64 | + "test:coverage": "yarn test:unit" |
| 65 | + }); |
14 | 66 | } |
15 | 67 |
|
16 | 68 | addDependencies(ctx: IInitCmdContext) { |
17 | 69 | this.packageJson.addDependencies({}, ctx); |
18 | 70 | } |
19 | 71 |
|
20 | 72 | addDevDependencies(ctx: IInitCmdContext) { |
21 | | - this.packageJson.addDevDependencies({}, ctx); |
| 73 | + this.packageJson.addDevDependencies({ |
| 74 | + "@types/jest": "latest", |
| 75 | + "jest": "latest", |
| 76 | + "ts-jest": "latest" |
| 77 | + }, ctx); |
22 | 78 | } |
23 | 79 | } |
0 commit comments