Skip to content

Commit

Permalink
fix(cli): fix miscellaneous issue in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 19, 2024
1 parent 5928aec commit 992d3b9
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exports[`Init OIDC Provider project > should generate a project with oidc 1`] =
"project-name/src/services",
"project-name/src/services/Accounts.ts",
"project-name/tsconfig.base.json",
"project-name/tsconfig.json",
"project-name/tsconfig.node.json",
"project-name/views",
"project-name/views/consent.ejs",
Expand Down Expand Up @@ -157,6 +158,7 @@ exports[`Init OIDC Provider project > should generate a project with oidc and sw
"project-name/src/services",
"project-name/src/services/Accounts.ts",
"project-name/tsconfig.base.json",
"project-name/tsconfig.json",
"project-name/tsconfig.node.json",
"project-name/views",
"project-name/views/consent.ejs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe("Init TypeGraphQL project", () => {
"project-name/src/services",
"project-name/src/services/RecipeService.ts",
"project-name/tsconfig.base.json",
"project-name/tsconfig.json",
"project-name/tsconfig.node.json",
]
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe("TypeORM: Init cmd", () => {
"project-name/src/datasources/MysqlDatasource.ts",
"project-name/src/index.ts",
"project-name/tsconfig.base.json",
"project-name/tsconfig.json",
"project-name/tsconfig.node.json",
]
`);
Expand Down Expand Up @@ -151,6 +152,7 @@ describe("TypeORM: Init cmd", () => {
"project-name/src/controllers/rest/HelloWorldController.ts",
"project-name/src/index.ts",
"project-name/tsconfig.base.json",
"project-name/tsconfig.json",
"project-name/tsconfig.node.json",
]
`);
Expand Down
18 changes: 13 additions & 5 deletions packages/cli/src/commands/init/InitCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class InitCmd implements CommandProvider {
},
...getFeaturesPrompt(
runtimes,
packageManagers.filter((o) => o !== "bun"),
packageManagers.filter((o: string) => o !== "bun"),
initialOptions
)
];
Expand All @@ -226,11 +226,11 @@ export class InitCmd implements CommandProvider {

this.runtimes.init(ctx);

this.runtimes.list().forEach((key) => {
this.runtimes.list().forEach((key: string) => {
ctx[key] = ctx.runtime === key;
});

this.packageManagers.list().forEach((key) => {
this.packageManagers.list().forEach((key: string) => {
ctx[key] = ctx.packageManager === key;
});

Expand Down Expand Up @@ -453,23 +453,31 @@ export class InitCmd implements CommandProvider {
ctx = {
...ctx,
node: runtime instanceof NodeRuntime,
bun: runtime instanceof BunRuntime
bun: runtime instanceof BunRuntime,
compiled: runtime instanceof NodeRuntime && runtime.isCompiled()
};

const pm2 = ctx.bun ? "bun" : ctx.compiled ? "node-compiled" : "node-loader";

return this.rootRenderer.renderAll(
[
...runtime.files(),
"/init/.dockerignore.hbs",
"/init/.gitignore.hbs",
"/init/.barrels.json.hbs",
"/init/processes.config.cjs.hbs",
{
path: `/init/pm2/${pm2}/processes.config.cjs.hbs`,
output: `processes.config.cjs`,
replaces: [`pm2/${pm2}`]
},
"/init/docker-compose.yml.hbs",
{
path: `/init/docker/${packageManager.name}/Dockerfile.hbs`,
output: `Dockerfile`,
replaces: [`docker/${packageManager.name}`]
},
"/init/README.md.hbs",
"/init/tsconfig.json.hbs",
"/init/tsconfig.base.json.hbs",
"/init/tsconfig.node.json.hbs",
ctx.testing && "/init/tsconfig.spec.json.hbs",
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/runtimes/supports/BabelRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class BabelRuntime extends NodeRuntime {
readonly name: string = "babel";
readonly order: number = 1;

isCompiled() {
return false;
}

files() {
return ["/init/.babelrc.hbs"];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/runtimes/supports/BaseRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export abstract class BaseRuntime {
return this.packageManagers.get();
}

isCompiled() {
return true;
}

files(): string[] {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/runtimes/supports/NodeRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class NodeRuntime extends BaseRuntime {
}

startDev(main: string) {
return `nodemon --import @swc-node/register/register-esm ${main}`;
return `nodemon ${main}`;
}

startProd(main: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/runtimes/supports/WebpackRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class WebpackRuntime extends BabelRuntime {
readonly name = "webpack";
readonly order: number = 2;

isCompiled() {
return true;
}

files() {
return [...super.files(), "/init/webpack.config.js.hbs"];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/templates/init/.swcrc.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"decorators": true,
"dynamicImport": true
},
"target": "es2022",
"target": "es2023",
"externalHelpers": true,
"keepClassNames": true,
"transform": {
Expand All @@ -16,6 +16,6 @@
}
},
"module": {
"type": "commonjs"
"type": "es6"
}
}
4 changes: 2 additions & 2 deletions packages/cli/templates/init/docker/bun/Dockerfile.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ARG BUN_VERSION=1

FROM oven/bun:${BUN_VERSION} as base
FROM oven/bun:${BUN_VERSION} AS base
WORKDIR /opt

FROM base AS install
Expand Down Expand Up @@ -33,4 +33,4 @@ USER bun

{{> docker-body}}

CMD ["pm2-runtime", "start", "processes.config.js", "--env", "production", "--interpreter", "~/.bun/bin/bun"]
CMD ["pm2-runtime", "start", "processes.config.cjs", "--env", "production", "--interpreter", "~/.bun/bin/bun"]
8 changes: 4 additions & 4 deletions packages/cli/templates/init/docker/npm/Dockerfile.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{> docker-header }}

ARG NODE_VERSION=20.10.0
ARG NODE_VERSION=20.11.0

FROM node:${NODE_VERSION}-alpine as build
FROM node:${NODE_VERSION}-alpine AS build
WORKDIR /opt

COPY package.json package-lock.json tsconfig.json tsconfig.compile.json .barrels.json ./
COPY package.json package-lock.json tsconfig.json tsconfig.base.json tsconfig.node.json tsconfig.spec.json .barrels.json .swcrc ./

RUN npm ci

Expand All @@ -25,4 +25,4 @@ RUN npm ci --omit=dev --ignore-scripts

{{> docker-body}}

CMD ["pm2-runtime", "start", "processes.config.js", "--env", "production"]
CMD ["pm2-runtime", "start", "processes.config.cjs", "--env", "production"]
8 changes: 4 additions & 4 deletions packages/cli/templates/init/docker/pnpm/Dockerfile.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{> docker-header }}

ARG NODE_VERSION=20.10.0
ARG NODE_VERSION=20.11.0

FROM node:${NODE_VERSION}-alpine as build
FROM node:${NODE_VERSION}-alpine AS build
WORKDIR /opt

COPY package.json pnpm-lock.yaml tsconfig.json tsconfig.compile.json .barrels.json ./
COPY package.json pnpm-lock.yaml tsconfig.json tsconfig.base.json tsconfig.node.json tsconfig.spec.json .barrels.json .swcrc ./

RUN pnpm install --frozen-lockfile

Expand All @@ -25,4 +25,4 @@ RUN pnpm install --frozen-lockfile --prod

{{> docker-body}}

CMD ["pm2-runtime", "start", "processes.config.js", "--env", "production"]
CMD ["pm2-runtime", "start", "processes.config.cjs", "--env", "production"]
10 changes: 5 additions & 5 deletions packages/cli/templates/init/docker/yarn/Dockerfile.hbs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{{> docker-header }}

ARG NODE_VERSION=20.10.0
ARG NODE_VERSION=20.11.0

FROM node:${NODE_VERSION}-alpine as build
FROM node:${NODE_VERSION}-alpine AS build
WORKDIR /opt

COPY package.json yarn.lock tsconfig.json tsconfig.compile.json .barrels.json ./
COPY package.json yarn.lock tsconfig.json tsconfig.base.json tsconfig.node.json tsconfig.spec.json .barrels.json .swcrc ./

RUN yarn install --pure-lockfile

COPY ./src ./src

RUN yarn build

FROM node:${NODE_VERSION}-alpine as runtime
FROM node:${NODE_VERSION}-alpine AS runtime
ENV WORKDIR /opt
WORKDIR $WORKDIR

Expand All @@ -25,4 +25,4 @@ RUN yarn install --pure-lockfile --production

{{> docker-body}}

CMD ["pm2-runtime", "start", "processes.config.js", "--env", "production"]
CMD ["pm2-runtime", "start", "processes.config.cjs", "--env", "production"]
10 changes: 5 additions & 5 deletions packages/cli/templates/init/docker/yarn_berry/Dockerfile.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{> docker-header }}

ARG NODE_VERSION=20.10.0
ARG NODE_VERSION=20.11.0

FROM node:${NODE_VERSION}-alpine as build
FROM node:${NODE_VERSION}-alpine AS build
WORKDIR /opt

COPY package.json yarn.lock yarn.lock tsconfig.json tsconfig.compile.json .barrels.json ./
COPY package.json yarn.lock yarn.lock tsconfig.json tsconfig.base.json tsconfig.node.json tsconfig.spec.json .barrels.json .swcrc ./

RUN yarn set version berry
RUN yarn install --immutable
Expand All @@ -14,7 +14,7 @@ COPY ./src ./src

RUN yarn build

FROM node:${NODE_VERSION}-alpine as runtime
FROM node:${NODE_VERSION}-alpine AS runtime
ENV WORKDIR /opt
WORKDIR $WORKDIR

Expand All @@ -28,4 +28,4 @@ RUN yarn install --immutable

{{> docker-body}}

CMD ["pm2-runtime", "start", "processes.config.js", "--env", "production"]
CMD ["pm2-runtime", "start", "processes.config.cjs", "--env", "production"]
23 changes: 23 additions & 0 deletions packages/cli/templates/init/pm2/bun/processes.config.cjs.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const path = require('path')
const defaultLogFile = path.join(__dirname, '/logs/project-server.log')

module.exports = {
'apps': [
{
name: 'api',
interpreter: '~/.bun/bin/bun',
'script': `${process.env.WORKDIR}/dist/index.js`,
'cwd': process.env.WORKDIR,
exec_mode: 'cluster',
instances: process.env.NODE_ENV === 'test' ? 1 : process.env.NB_INSTANCES || 2,
autorestart: true,
max_memory_restart: process.env.MAX_MEMORY_RESTART || '750M',
'out_file': defaultLogFile,
'error_file': defaultLogFile,
'merge_logs': true,
'kill_timeout': 30000,
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ const defaultLogFile = path.join(__dirname, '/logs/project-server.log')
module.exports = {
'apps': [
{
name: "api",
{{#if bun}}interpreter: "~/.bun/bin/bun",{{/if}}
name: 'api',
'script': `${process.env.WORKDIR}/dist/index.js`,
'cwd': process.env.WORKDIR,
exec_mode: "cluster",
instances: process.env.NODE_ENV === "test" ? 1 : process.env.NB_INSTANCES || 2,
instances: process.env.NODE_ENV === 'test' ? 1 : process.env.NB_INSTANCES || 2,
autorestart: true,
max_memory_restart: process.env.MAX_MEMORY_RESTART || "750M",
max_memory_restart: process.env.MAX_MEMORY_RESTART || '750M',
'out_file': defaultLogFile,
'error_file': defaultLogFile,
'merge_logs': true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const path = require('path')
const defaultLogFile = path.join(__dirname, '/logs/project-server.log')

module.exports = {
'apps': [
{
name: 'api',
'interpreter': 'node',
interpreter_args: '--import @swc-node/register/esm-register --enable-source-maps',
'script': `${process.env.WORKDIR}/src/index.ts`,
'cwd': process.env.WORKDIR,
exec_mode: 'cluster',
instances: process.env.NODE_ENV === 'test' ? 1 : process.env.NB_INSTANCES || 2,
autorestart: true,
max_memory_restart: process.env.MAX_MEMORY_RESTART || '750M',
'out_file': defaultLogFile,
'error_file': defaultLogFile,
'merge_logs': true,
'kill_timeout': 30000,
}
]
}
Loading

0 comments on commit 992d3b9

Please sign in to comment.