Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
66 changes: 35 additions & 31 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,56 @@ on:
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: 22.x
- name: Install dependencies
run: yarn install --immutable
- name: Run lint
run: yarn lint

test:
runs-on: ${{ matrix.os }}
test-cli-core:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
- name: Install dependencies
run: yarn install --immutable
- name: Run tests for cli-core
run: yarn test --project=@tsed/cli-core

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [20.x]
exclude:
# - os: macos-latest
# node-version: 12.x
# - os: macos-latest
# node-version: 15.x
# - os: windows-latest
# node-version: 12.x
- os: windows-latest
node-version: 20.x
test-cli:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
- name: Install dependencies
run: yarn install --immutable
- name: Run tests for cli
run: yarn test run --project=@tsed/cli

test-plugins:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: 22.x
- name: Install dependencies
run: yarn install --immutable
- name: Run test
run: yarn test
- name: Run tests for other packages
run: yarn test --project="@tsed/cli-plugin-*"

build:
runs-on: ubuntu-latest
Expand All @@ -77,19 +85,15 @@ jobs:

deploy-packages:
runs-on: ubuntu-latest
needs: [lint, test, build]
needs: [lint, test-cli-core, test-cli, test-plugins, build]
if: ${{ github.event_name != 'pull_request' && (contains(github.ref, 'master') || contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc')) }}

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: 22.x
- name: Install dependencies
run: yarn install --frozen-lockfile --network-timeout 500000
- name: Release packages
Expand Down
7 changes: 6 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export default {extends: ["@commitlint/config-conventional"]};
export default {
extends: ["@commitlint/config-conventional"],
rules: {
"header-max-length": [2, "always", 200]
}
};
3 changes: 0 additions & 3 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ frameworks:
- title: AWS
href: https://tsed.dev/tutorials/aws.html
src: /aws.png
- title: Handlebars
href: https://handlebarsjs.com/
src: https://handlebarsjs.com/images/handlebars_logo.png
---

::: slot hero-brand
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,22 @@
},
"homepage": "https://github.com/tsedio/tsed-cli",
"dependencies": {
"@tsed/core": ">=8.3.1",
"@tsed/di": ">=8.3.1",
"@tsed/core": ">=8.16.0-rc.1",
"@tsed/di": ">=8.16.0-rc.1",
"@tsed/logger": ">=8.0.3",
"@tsed/logger-std": ">=8.0.3",
"@tsed/normalize-path": ">=8.3.1",
"@tsed/openspec": ">=8.3.1",
"@tsed/schema": ">=8.3.1",
"@tsed/normalize-path": ">=8.16.0-rc.1",
"@tsed/openspec": ">=8.16.0-rc.1",
"@tsed/schema": ">=8.16.0-rc.1",
"axios": "^1.7.7",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"consolidate": "^1.0.4",
"execa": "8.0.1",
"figures": "^6.1.0",
"fs-extra": "^11.2.0",
"handlebars": "^4.7.8",
"handlebars-helpers": "^0.10.0",
"inquirer": "^9.3.7",
"inquirer-autocomplete-prompt": "^3.0.1",
"inquirer-autocomplete-prompt": "3.0.1",
"js-yaml": "^4.1.0",
"lerna": "^8.1.8",
"listr2": "^8.2.4",
Expand All @@ -82,6 +80,7 @@
"@tsed/markdown-it-symbols": "3.20.8",
"@tsed/monorepo-utils": "2.3.12",
"@tsed/ts-doc": "^4.1.0",
"@types/inquirer-autocomplete-prompt": "3.0.3",
"@types/node": "22.7.4",
"@typescript-eslint/eslint-plugin": "8.7.0",
"@typescript-eslint/parser": "8.7.0",
Expand Down
109 changes: 5 additions & 104 deletions packages/cli-core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ npm install @tsed/core @tsed/di @tsed/cli-core

## Getting started

Create CLI require some steps like create a package.json with the right information and create a structure directory
Create CLI requires some steps like create a package.json with the right information and create a structure directory
aligned with TypeScript to be compiled correctly for a npm deployment.

Here a structure directory example:
Here is a structure directory example:

```
.
Expand All @@ -57,93 +57,6 @@ Here a structure directory example:
└── tsconfig.compile.json
```

## Create package.json and tsconfig

The first step is to create the package.json with the following lines:

```jsonc
{
"name": "{{name}}",
"version": "1.0.0",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"bin": {
"tsed": "lib/bin/{{name}}.js"
},
"files": ["lib/bin/{{name}}.js", "lib/bin", "lib", "templates"],
"description": "An awesome CLI build on top of @tsed/cli-core",
"dependencies": {
"@tsed/cli-core": "1.3.1",
"tslib": "1.11.1"
},
"devDependencies": {
"@tsed/cli-testing": "1.3.1",
"ts-node": "latest",
"typescript": "latest"
},
"scripts": {
"build": "tsc --build tsconfig.compile.json",
"start:cmd:add": "cross-env NODE_ENV=development ts-node -r src/bin/{{name}}.ts add -r ./.tmp"
},
"engines": {
"node": ">=8.9"
},
"peerDependencies": {}
}
```

Then create tsconfig files one for the IDE (`tsconfig.json`):

```json
{
"compilerOptions": {
"module": "commonjs",
"target": "es2016",
"sourceMap": true,
"declaration": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"isolatedModules": false,
"suppressImplicitAnyIndexErrors": false,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"allowSyntheticDefaultImports": true,
"importHelpers": true,
"newLine": "LF",
"noEmit": true,
"lib": ["es7", "dom", "esnext.asynciterable"],
"typeRoots": ["./node_modules/@types"]
},
"linterOptions": {
"exclude": []
},
"exclude": []
}
```

And another one to compile source (`tsconfig.compile.json`):

```json
{
"extends": "./tsconfig.compile.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"moduleResolution": "node",
"declaration": true,
"noResolve": false,
"preserveConstEnums": true,
"sourceMap": true,
"noEmit": false,
"inlineSources": true
},
"exclude": ["node_modules", "test", "lib", "**/*.spec.ts"]
}
```

## Create the bin file

The bin file is used by npm to create your node.js executable program when you install the node_module globally.
Expand All @@ -155,19 +68,13 @@ Create a new file according to your project name (example: `name.ts`) and add th
import {AddCmd, CliCore} from "@tsed/cli-core";
import {resolve} from "node:path";

const pkg = require("../../package.json");
const TEMPLATE_DIR = resolve(__dirname, "..", "..", "templates");

CliCore.bootstrap({
commands: [
AddCmd // CommandProvider to install a plugin
// then add you commands
],

// optionals
name: "name", // replace by the cli name. This property will be used by Plugins command
pkg,
templateDir: TEMPLATE_DIR
name: "name" // replace by the cli name. This property will be used by Plugins command
}).catch(console.error);
```

Expand Down Expand Up @@ -265,12 +172,6 @@ export class GenerateCmd implements CommandProvider {
}
```

Finally, create a handlebars template in templates directory:

```hbs
import {Injectable} from "@tsed/di"; @Injectable() export class {{symbolName}} { }
```

## Run command in dev mode

In your package.json add the following line in scripts property:
Expand All @@ -281,13 +182,13 @@ In your package.json add the following line in scripts property:
}
```

> Note: replace {{name}} by the name of you bin file located in src/bin.
> Note: replace {{name}} by the name of your bin file located in src/bin.

> Note 2: The option `-r ./.tmp` create a temporary directory to generate files with your command.

## More examples

Here other commands examples:
Here are other command examples:

- Init a project command: https://github.com/tsedio/tsed-cli/tree/master/packages/cli/src/commands/init/InitCmd
- Generate command: https://github.com/tsedio/tsed-cli/tree/master/packages/cli/src/commands/generate/GenerateCmd
Expand Down
15 changes: 7 additions & 8 deletions packages/cli-core/src/CliCore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "@tsed/logger-std";

// import "@tsed/logger/layouts/ColoredLayout";
import {join, resolve} from "node:path";

import {Type} from "@tsed/core";
import {inject, InjectorService, Module} from "@tsed/di";
import {inject, injectable, InjectorService} from "@tsed/di";
import {$asyncEmit} from "@tsed/hooks";
import chalk from "chalk";
import {Command} from "commander";
import semver from "semver";
Expand All @@ -23,9 +23,6 @@ function isHelpManual(argv: string[]) {
return argv.includes("-h") || argv.includes("--help");
}

@Module({
imports: [CliPackageJson, ProjectPackageJson, CliService, CliConfiguration]
})
export class CliCore {
readonly injector = inject(InjectorService);
readonly cliService = inject(CliService);
Expand Down Expand Up @@ -58,9 +55,9 @@ export class CliCore {

await this.loadInjector(injector, module);

await injector.emit("$onReady");
await $asyncEmit("$onReady");

return injector.get<Cli>(CliCore)!;
return inject<Cli>(CliCore as any)!;
}

static async bootstrap(settings: Partial<TsED.Configuration>, module: Type = CliCore) {
Expand All @@ -78,7 +75,7 @@ export class CliCore {

await injector.load();
await injector.invoke(module);
await injector.emit("$afterInit");
await $asyncEmit("$afterInit");

injector.settings.set("loaded", true);
}
Expand Down Expand Up @@ -126,3 +123,5 @@ export class CliCore {
return this;
}
}

injectable(CliCore).imports([CliPackageJson, ProjectPackageJson, CliService, CliConfiguration]);
9 changes: 4 additions & 5 deletions packages/cli-core/src/decorators/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {StoreSet, useDecorators} from "@tsed/core";
import {Injectable} from "@tsed/di";

import {CommandStoreKeys} from "../domains/CommandStoreKeys.js";
import {command} from "../fn/command.js";
import type {CommandParameters} from "../interfaces/CommandParameters.js";

export function Command(options: CommandParameters): ClassDecorator {
return useDecorators(Injectable({type: "command"}), StoreSet(CommandStoreKeys.COMMAND, options)) as any;
return (token) => {
command(token, options);
};
}
8 changes: 8 additions & 0 deletions packages/cli-core/src/fn/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {injectable, type TokenProvider} from "@tsed/di";

import {CommandStoreKeys} from "../domains/CommandStoreKeys.js";
import type {CommandParameters} from "../interfaces/CommandParameters.js";

export function command(token: TokenProvider, options: CommandParameters): ReturnType<typeof injectable> {
return injectable(token).type("command").set(CommandStoreKeys.COMMAND, options);
}
1 change: 1 addition & 0 deletions packages/cli-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Inquirer from "inquirer";

export * from "./CliCore.js";
export * from "./decorators/index.js";
export * from "./fn/command.js";
export * from "./interfaces/index.js";
export * from "./packageManagers/index.js";
export * from "./services/index.js";
Expand Down
Loading
Loading