Skip to content

Commit c9ce2ab

Browse files
committed
feat: add tianji cli which should more easy to manage worker
1 parent 072e27c commit c9ce2ab

19 files changed

Lines changed: 1088 additions & 0 deletions

File tree

packages/cli/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
lib
3+
*.log
4+
.DS_Store

packages/cli/README.md

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# Tianji CLI
2+
3+
Command-line tool for developing and deploying Tianji Workers.
4+
5+
## Installation
6+
7+
### From Source (Development)
8+
9+
```bash
10+
# In the Tianji monorepo root
11+
pnpm install
12+
cd packages/cli
13+
pnpm build
14+
15+
# Link globally for development
16+
pnpm link --global
17+
```
18+
19+
### From NPM (When Published)
20+
21+
```bash
22+
npm install -g tianji-cli
23+
# or
24+
pnpm add -g tianji-cli
25+
```
26+
27+
## Usage
28+
29+
### Login to Tianji
30+
31+
Before deploying workers, you need to login to your Tianji instance:
32+
33+
```bash
34+
tianji login
35+
```
36+
37+
You will be prompted for:
38+
- **Server URL**: Your Tianji server URL (e.g., `https://tianji.example.com`)
39+
- **Workspace ID**: Your workspace ID (found in Tianji dashboard)
40+
- **API Key**: Your API key (generate from Tianji settings)
41+
42+
The credentials are saved to `~/.config/tianji/config.json`.
43+
44+
### Create a New Worker Project
45+
46+
```bash
47+
# Create in a new directory
48+
tianji worker init my-worker
49+
50+
# Or initialize in current directory
51+
mkdir my-worker && cd my-worker
52+
tianji worker init
53+
```
54+
55+
This creates a new project with:
56+
- `src/index.ts`: Worker handler function
57+
- `vite.config.ts`: Build configuration
58+
- `package.json`: Dependencies
59+
- `.tianjirc`: Project configuration
60+
61+
### Develop Your Worker
62+
63+
Edit `src/index.ts` to implement your worker logic:
64+
65+
```typescript
66+
export default async function handler(payload: any, context: any) {
67+
// Your worker logic here
68+
return {
69+
success: true,
70+
message: 'Hello from Tianji Worker!',
71+
data: payload,
72+
};
73+
}
74+
```
75+
76+
### Build and Deploy
77+
78+
```bash
79+
# Login first (if not already logged in)
80+
tianji login
81+
82+
# Install dependencies (first time only)
83+
npm install
84+
85+
# Build the worker
86+
npm run build
87+
88+
# Deploy to Tianji
89+
tianji worker deploy
90+
```
91+
92+
The worker will be built using Vite and deployed to your Tianji instance. After deployment, you'll receive a URL to access your worker.
93+
94+
## Project Structure
95+
96+
```
97+
my-worker/
98+
├── src/
99+
│ └── index.ts # Worker entry point
100+
├── dist/ # Built output (generated)
101+
├── package.json # Project dependencies
102+
├── vite.config.ts # Vite build configuration
103+
├── tsconfig.json # TypeScript configuration
104+
├── .tianjirc # Tianji project config
105+
└── README.md # Project documentation
106+
```
107+
108+
## Worker API
109+
110+
Your worker receives two parameters:
111+
112+
### `payload`
113+
114+
The request payload containing query parameters and request body merged together.
115+
116+
### `context`
117+
118+
The request context with the following structure:
119+
120+
```typescript
121+
interface RequestContext {
122+
type: 'http' | 'cron';
123+
request?: {
124+
method: string;
125+
url: string;
126+
headers: Record<string, string>;
127+
};
128+
}
129+
```
130+
131+
- For HTTP requests: `context.type === 'http'` and `context.request` contains request details
132+
- For cron-triggered workers: `context.type === 'cron'`
133+
134+
## Configuration Files
135+
136+
### `.tianjirc`
137+
138+
Project-level configuration stored in your worker project:
139+
140+
```json
141+
{
142+
"name": "my-worker",
143+
"workerId": "clxxx..." // Auto-generated after first deployment
144+
}
145+
```
146+
147+
### `~/.config/tianji/config.json`
148+
149+
Global configuration for CLI authentication:
150+
151+
```json
152+
{
153+
"serverUrl": "https://tianji.example.com",
154+
"workspaceId": "clxxx...",
155+
"apiKey": "your-api-key"
156+
}
157+
```
158+
159+
## Commands
160+
161+
### `tianji login`
162+
163+
Login to Tianji and save credentials.
164+
165+
### `tianji worker init [project-name]`
166+
167+
Create a new Tianji Worker project.
168+
169+
**Options:**
170+
- `project-name`: Optional. Name of the project directory to create.
171+
172+
### `tianji worker deploy`
173+
174+
Build and deploy the current worker project to Tianji.
175+
176+
Must be run from a worker project directory (containing `.tianjirc`).
177+
178+
## Examples
179+
180+
### Simple Echo Worker
181+
182+
```typescript
183+
export default async function handler(payload: any) {
184+
return {
185+
echo: payload,
186+
timestamp: new Date().toISOString(),
187+
};
188+
}
189+
```
190+
191+
### HTTP Method-Based Worker
192+
193+
```typescript
194+
export default async function handler(payload: any, context: any) {
195+
if (context.type !== 'http') {
196+
return { error: 'Only HTTP requests supported' };
197+
}
198+
199+
const method = context.request?.method;
200+
201+
switch (method) {
202+
case 'GET':
203+
return { message: 'Hello GET' };
204+
case 'POST':
205+
return { message: 'Data received', data: payload };
206+
default:
207+
return { error: 'Method not supported' };
208+
}
209+
}
210+
```
211+
212+
### Cron Worker
213+
214+
```typescript
215+
export default async function handler(payload: any, context: any) {
216+
if (context.type === 'cron') {
217+
// Perform scheduled task
218+
console.log('Cron job executed at:', new Date().toISOString());
219+
return { success: true };
220+
}
221+
222+
return { error: 'This worker only runs on schedule' };
223+
}
224+
```
225+
226+
## Troubleshooting
227+
228+
### "Not logged in" Error
229+
230+
Run `tianji login` to authenticate.
231+
232+
### ".tianjirc not found" Error
233+
234+
Make sure you're in a worker project directory. Run `tianji worker init` to create a new project.
235+
236+
### "Build failed" Error
237+
238+
Ensure all dependencies are installed:
239+
```bash
240+
npm install
241+
```
242+
243+
Check that your `src/index.ts` has a default export function.
244+
245+
### "Deployment failed" Error
246+
247+
- Verify your credentials are correct (try logging in again)
248+
- Check that your API key has the necessary permissions
249+
- Ensure your server URL is accessible
250+
251+
## Learn More
252+
253+
- [Tianji Documentation](https://tianji.msgbyte.com/docs)
254+
- [Worker Documentation](https://tianji.msgbyte.com/docs/dev)
255+
256+
## License
257+
258+
MIT

packages/cli/bin/tianji

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/index.js');

packages/cli/package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "tianji-cli",
3+
"version": "0.1.0",
4+
"description": "CLI tool for Tianji Worker development and deployment",
5+
"main": "lib/index.js",
6+
"bin": {
7+
"tianji": "./bin/tianji"
8+
},
9+
"scripts": {
10+
"build": "tsc",
11+
"prepare": "tsc",
12+
"dev": "tsc --watch"
13+
},
14+
"keywords": [
15+
"tianji",
16+
"cli",
17+
"worker"
18+
],
19+
"author": "moonrailgun <moonrailgun@gmail.com>",
20+
"license": "MIT",
21+
"dependencies": {
22+
"tianji-client-sdk": "workspace:*",
23+
"yargs": "^17.7.2",
24+
"prompts": "^2.4.2",
25+
"chalk": "^4.1.2",
26+
"fs-extra": "^11.2.0",
27+
"ora": "^5.4.1"
28+
},
29+
"devDependencies": {
30+
"@types/fs-extra": "^11.0.4",
31+
"@types/node": "^20.10.0",
32+
"@types/prompts": "^2.4.9",
33+
"@types/yargs": "^17.0.32",
34+
"typescript": "^5.4.5"
35+
}
36+
}

0 commit comments

Comments
 (0)