Skip to content

Commit

Permalink
fix(cli): fix template generation issue
Browse files Browse the repository at this point in the history
Closes: #365
  • Loading branch information
Romakita committed Mar 2, 2024
1 parent 1d996f7 commit b3dc1d9
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ import { {{symbolName}} } from "./{{symbolPathBasename}}";
import { Server } from "{{relativeSrcPath}}/Server";

describe("{{symbolName}}", () => {
let request: SuperTest.Agent;

beforeEach(PlatformTest.bootstrap(Server, {
mount: {
"/": [{{symbolName}}]
}
}));
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});

afterEach(PlatformTest.reset);

it("should call GET {{route}}", async () => {
const request = SuperTest(PlatformTest.callback());
const response = await request.get("{{route}}").expect(200);

expect(response.text).toEqual("hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import SuperTest from "supertest";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
let request: SuperTest.SuperTest<SuperTest.Test>;

beforeEach(PlatformTest.bootstrap({{symbolName}}));
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});

afterEach(PlatformTest.reset);

it("should call GET {{route}}", async () => {
const request = SuperTest(PlatformTest.callback());
const response = await request.get("{{route}}").expect(404);

expect(response.body).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ import { {{symbolName}} } from "./{{symbolPathBasename}}";
import { Server } from "{{relativeSrcPath}}/Server";

describe("{{symbolName}}", () => {
let request: SuperTest.Agent;

beforeEach(PlatformTest.bootstrap(Server, {
mount: {
"/": {{symbolName}}
}
}));
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});

afterEach(PlatformTest.reset);

it("should call GET {{route}}", async () => {
const request = SuperTest(PlatformTest.callback());
const response = await request.get("{{route}}").expect(200);

expect(response.text).to.eq("hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import {expect} from "chai";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
let request: SuperTest.SuperTest<SuperTest.Test>;

beforeEach(PlatformTest.bootstrap({{symbolName}}));
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});

afterEach(PlatformTest.reset);

it("should call GET {{route}}", async () => {
const request = SuperTest(PlatformTest.callback());
const response = await request.get("{{route}}").expect(404);

expect(response.body).to.deep.equal({
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-plugin-typeorm/src/hooks/TypeORMGenerateHook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FeaturesMap, GenerateCmdContext, ProvidersInfoService} from "@tsed/cli";
import {CliDockerComposeYaml, Inject, OnExec, OnPrompt, ProjectPackageJson, SrcRendererService, Tasks} from "@tsed/cli-core";
import {Injectable} from "@tsed/di";
import {constantCase} from "change-case";
import {camelCase} from "change-case";
import {TEMPLATE_DIR} from "../utils/templateDir";

export interface TypeORMGenerateOptions extends GenerateCmdContext {
Expand Down Expand Up @@ -103,7 +103,7 @@ export class TypeORMGenerateHook {
name,
database,
symbolName,
tokenName: constantCase(symbolName)
instanceName: camelCase(symbolName)
},
{
templateDir: TEMPLATE_DIR,
Expand Down
12 changes: 7 additions & 5 deletions packages/cli-plugin-typeorm/templates/datasource.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {registerProvider} from "@tsed/di";
import {DataSource} from "typeorm";
import {Logger} from "@tsed/logger";

export const {{tokenName}} = Symbol.for("{{symbolName}}");
export const {{symbolName}} = new DataSource({
export const {{symbolName}} = Symbol.for("{{symbolName}}");
export type {{symbolName}} = DataSource;
export const {{instanceName}} = new DataSource({
type: "{{database}}",
entities: [],
{{#switch database}}
Expand Down Expand Up @@ -60,16 +61,17 @@ export const {{symbolName}} = new DataSource({
{{/switch}}
});


registerProvider<DataSource>({
provide: {{tokenName}},
provide: {{symbolName}},
type: "typeorm:datasource",
deps: [Logger],
async useAsyncFactory(logger: Logger) {
await {{symbolName}}.initialize();
await {{instanceName}}.initialize();

logger.info("Connected with typeorm to database: {{name}}");

return {{symbolName}};
return {{instanceName}};
},
hooks: {
$onDestroy(dataSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ exports[`Generate DataSource should generate the template with the right options
import {DataSource} from \\"typeorm\\";
import {Logger} from \\"@tsed/logger\\";
export const TEST_DATASOURCE = Symbol.for(\\"TestDatasource\\");
export const TestDatasource = new DataSource({
export const TestDatasource = Symbol.for(\\"TestDatasource\\");
export type TestDatasource = DataSource;
export const testDatasource = new DataSource({
type: \\"mysql\\",
entities: [],
host: \\"localhost\\",
Expand All @@ -16,16 +17,17 @@ export const TestDatasource = new DataSource({
database: \\"test\\"
});
registerProvider<DataSource>({
provide: TEST_DATASOURCE,
provide: TestDatasource,
type: \\"typeorm:datasource\\",
deps: [Logger],
async useAsyncFactory(logger: Logger) {
await TestDatasource.initialize();
await testDatasource.initialize();
logger.info(\\"Connected with typeorm to database: Test\\");
return TestDatasource;
return testDatasource;
},
hooks: {
$onDestroy(dataSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ exports[`TypeORM: Init cmd should generate a project with the right options 3`]
import {DataSource} from \\"typeorm\\";
import {Logger} from \\"@tsed/logger\\";
export const MYSQL_DATASOURCE = Symbol.for(\\"MysqlDatasource\\");
export const MysqlDatasource = new DataSource({
export const MysqlDatasource = Symbol.for(\\"MysqlDatasource\\");
export type MysqlDatasource = DataSource;
export const mysqlDatasource = new DataSource({
type: \\"mysql\\",
entities: [],
host: \\"localhost\\",
Expand All @@ -67,16 +68,17 @@ export const MysqlDatasource = new DataSource({
database: \\"test\\"
});
registerProvider<DataSource>({
provide: MYSQL_DATASOURCE,
provide: MysqlDatasource,
type: \\"typeorm:datasource\\",
deps: [Logger],
async useAsyncFactory(logger: Logger) {
await MysqlDatasource.initialize();
await mysqlDatasource.initialize();
logger.info(\\"Connected with typeorm to database: Mysql\\");
return MysqlDatasource;
return mysqlDatasource;
},
hooks: {
$onDestroy(dataSource) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = {

coverageThreshold: {
global: {
statements: 93.01,
statements: 92.99,
branches: 80.73,
functions: 83.47,
lines: 93.01
lines: 92.99
}
}
};
1 change: 1 addition & 0 deletions packages/cli/src/runtimes/supports/BabelRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class BabelRuntime extends NodeRuntime {
"@babel/plugin-proposal-decorators": "latest",
"@babel/preset-env": "latest",
"@babel/preset-typescript": "latest",
"@babel/plugin-proposal-object-rest-spread": "latest",
"babel-plugin-transform-typescript-metadata": "latest",
"babel-watch": "latest"
};
Expand Down
10 changes: 1 addition & 9 deletions packages/cli/src/runtimes/supports/WebpackRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ export class WebpackRuntime extends BabelRuntime {

devDependencies() {
return {
"@babel/cli": "latest",
"@babel/core": "latest",
"@babel/node": "latest",
"@babel/plugin-proposal-class-properties": "latest",
"@babel/plugin-proposal-decorators": "latest",
"@babel/preset-env": "latest",
"@babel/preset-typescript": "latest",
"babel-plugin-transform-typescript-metadata": "latest",
"babel-watch": "latest",
...super.devDependencies(),
"babel-loader": "latest",
webpack: "latest",
"webpack-cli": "latest"
Expand Down
1 change: 1 addition & 0 deletions packages/cli/templates/init/src/config/envs/index.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ process.env.NODE_ENV = process.env.NODE_ENV || "development";

export const config = dotenv.config();
export const isProduction = process.env.NODE_ENV === "production";
export const envs = process.env

0 comments on commit b3dc1d9

Please sign in to comment.