Skip to content

Commit

Permalink
fix(cli): fix swc and bun runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 28, 2023
1 parent c7097f1 commit b37c2ce
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/cli-plugin-jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module.exports = {
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
statements: 79.45,
statements: 80,
branches: 37.5,
functions: 60,
lines: 79.45
lines: 80
}
}
};
8 changes: 4 additions & 4 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.13,
branches: 80.66,
functions: 82.75,
lines: 93.13
statements: 93.09,
branches: 80.73,
functions: 83.47,
lines: 93.09
}
}
};
21 changes: 15 additions & 6 deletions packages/cli/src/commands/init/InitCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,31 @@ export class InitCmd implements CommandProvider {
return paramCase(input);
}
},
...getFeaturesPrompt(runtimes, packageManagers, initialOptions)
...getFeaturesPrompt(
runtimes,
packageManagers.filter((o) => o === "bun"),
initialOptions
)
];
}

$mapContext(ctx: any): InitCmdContext {
this.resolveRootDir(ctx);
ctx = mapToContext(ctx);

this.runtimes.init(ctx);

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

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

return fillImports({
...ctx,
entryServer: ctx.convention !== ProjectConvention.ANGULAR ? "Server" : "server",
yarn: PackageManager.YARN === ctx.packageManager || !ctx.packageManager,
yarn_berry: ctx.packageManager === PackageManager.YARN_BERRY || !ctx.packageManager,
npm: ctx.packageManager == PackageManager.NPM,
pnpm: ctx.packageManager == PackageManager.PNPM,
cliVersion: ctx.cliVersion || this.cliPackageJson.version,
srcDir: this.configuration.project?.srcDir,
platformSymbol: ctx.platform && pascalCase(`Platform ${ctx.platform}`)
Expand Down Expand Up @@ -254,7 +264,6 @@ export class InitCmd implements CommandProvider {
{
title: "Initialize package.json",
task: async () => {
this.runtimes.init(ctx);
await this.packageManagers.init(ctx);

this.addScripts(ctx);
Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/commands/init/config/FeaturesPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export enum FeatureType {
ESLINT = "eslint",
LINT_STAGED = "lintstaged",
PRETTIER = "prettier"

// BUNDLER
// BUNDLER = "bundler",
// BABEL = "babel",
// WEBPACK = "babel:webpack"
}

export const FeaturesMap: Record<string, Feature> = {
Expand Down
8 changes: 2 additions & 6 deletions packages/cli/src/commands/init/utils/hasFeature.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {getValue} from "@tsed/core";

export function hasValue(expression: string, value: any) {
return (ctx: any) => getValue(expression, ctx) === value;
}

export function hasValues(expression: string, values: string[]) {
return (ctx: any) => values.includes(getValue(expression, ctx)!);
export function hasValue(expression: string, value: string | string[]) {
return (ctx: any) => [].concat(value as any).includes(getValue(expression, ctx)!);
}

export function hasFeature(feature: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/runtimes/RuntimesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class RuntimesModule {
}

list(): string[] {
return this.runtimes.map((manager) => manager.name);
return this.runtimes.sort((a, b) => a.order - b.order).map((manager) => manager.name);
}

get(name?: string) {
Expand Down
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 @@ -7,6 +7,7 @@ import {dirname} from "path";
})
export class BabelRuntime extends NodeRuntime {
readonly name: string = "babel";
readonly order: number = 1;

files() {
return ["/init/.babelrc.hbs"];
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/runtimes/supports/BaseRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {CliExeca, PackageManagersModule} from "@tsed/cli-core";
export abstract class BaseRuntime {
abstract readonly name: string;
abstract readonly cmd: string;
readonly order: number = 10;

@Inject(PackageManagersModule)
protected packageManagers: PackageManagersModule;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/runtimes/supports/BunRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Injectable} from "@tsed/di";
export class BunRuntime extends BaseRuntime {
readonly name = "bun";
readonly cmd = "bun";
readonly order: number = 4;

compile(src: string, out: string) {
return `${this.cmd} build --target=bun ${src} --outfile=${out}`;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/runtimes/supports/NodeRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Injectable} from "@tsed/di";
export class NodeRuntime extends BaseRuntime {
readonly name: string = "node";
readonly cmd: string = "node";
readonly order: number = 0;

devDependencies(): Record<string, any> {
return {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/runtimes/supports/SWCRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {NodeRuntime} from "./NodeRuntime";
})
export class SWCRuntime extends NodeRuntime {
readonly name = "swc";
readonly order: number = 3;

files() {
return [...super.files(), "/init/.swcrc.hbs"];
return [...super.files(), "/init/.swcrc.hbs", "/init/.node-dev.json.hbs"];
}

startDev(main: string) {
return `swc ${main} -w -s `;
return `node-dev ${main}`;
}

compile(src: string, out: string) {
Expand All @@ -23,7 +24,8 @@ export class SWCRuntime extends NodeRuntime {
return {
"@swc/core": "latest",
"@swc/cli": "latest",
"@swc/helpers": "latest"
"@swc/helpers": "latest",
"node-dev": "latest"
};
}
}
1 change: 1 addition & 0 deletions packages/cli/src/runtimes/supports/WebpackRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {BabelRuntime} from "./BabelRuntime";
})
export class WebpackRuntime extends BabelRuntime {
readonly name = "webpack";
readonly order: number = 2;

files() {
return [...super.files(), "/init/webpack.config.js.hbs"];
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/templates/init/.node-dev.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extensions": {
"ts": "@swc/register"
}
}
3 changes: 2 additions & 1 deletion packages/cli/test/integrations/init/init.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ describe("Init cmd", () => {
"project-name/.barrelsby.json",
"project-name/.dockerignore",
"project-name/.gitignore",
"project-name/.node-dev.json",
"project-name/.swcrc",
"project-name/Dockerfile",
"project-name/README.md",
Expand Down Expand Up @@ -567,7 +568,7 @@ describe("Init cmd", () => {
"scripts": Object {
"barrels": "barrelsby --config .barrelsby.json",
"build": "yarn run barrels && swc src/index.ts -o dist/index.js -s",
"start": "yarn run barrels && swc src/index.ts -w -s ",
"start": "yarn run barrels && node-dev src/index.ts",
"start:prod": "cross-env NODE_ENV=production node dist/index.js",
},
"tsed": Object {
Expand Down

0 comments on commit b37c2ce

Please sign in to comment.