Skip to content

Commit

Permalink
chore: 合并docker入口到主工程
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 26, 2024
1 parent c0e493b commit ca62483
Show file tree
Hide file tree
Showing 18 changed files with 975 additions and 1,281 deletions.
18 changes: 17 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
.wrangler
adapter
dist
doc
node_modules
adapter/local/node_modules
plugins
.gitattributes
.gitignore
config.json
docker-compose.yaml
Dockerfile
eslint.config.js
LICENSE
README.md
README_CN.md
wrangler.toml
wrangler-example.toml
yarn.lock
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM node:20
COPY . /app
WORKDIR /app
COPY adapter/local /app/adapter/local/
COPY src /app/src
WORKDIR /app/adapter/local
RUN npm install
RUN pwd && ls -la && npm install && npm run build:local
EXPOSE 8787
CMD ["npm", "run", "run:local"]
CMD ["npm", "run", "start:local"]
7 changes: 0 additions & 7 deletions adapter/local/.dockerignore

This file was deleted.

3 changes: 0 additions & 3 deletions adapter/local/.gitignore

This file was deleted.

20 changes: 0 additions & 20 deletions adapter/local/package.json

This file was deleted.

1,167 changes: 0 additions & 1,167 deletions adapter/local/yarn.lock

This file was deleted.

7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"database": {
"type": "local",
"path": "./data.json"
},
"mode": "polling"
}
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ services:
ports:
- "8787:8787"
volumes:
- ./adapter/local/config.json:/app/config.json:ro # change `./adapter/local/config.json` to your local path
- ./config.json:/app/config.json:ro # change `./config.json` to your local path
- ./wrangler.toml:/app/config.toml:ro # change `./wrangler.toml` to your local path
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
"scripts": {
"lint": "eslint --fix *.js *.ts *.json src adapter",
"build": "vite build",
"debug": "wrangler dev --local",
"wrangler": "wrangler",
"build:local": "BUILD_MODE=local vite build",
"debug:workers": "wrangler dev --local",
"debug:local": "CONFIG_PATH=./config.json TOML_PATH=./wrangler.toml tsx index.ts",
"deploy:dist": "wrangler deploy",
"deploy:build": "npm run build && wrangler deploy",
"test:plugin": "node src/plugins/template_test.ts"
"start:local": "node dist/index.js"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.2.3"
},
"dependencies": {},
"devDependencies": {
"@antfu/eslint-config": "^2.25.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^22.5.0",
"eslint": "^9.8.0",
"eslint-plugin-format": "^0.1.2",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-node-externals": "^7.1.3",
"telegram-bot-api-types": "^7.9.7",
"typescript": "^5.5.4",
"vite": "^5.2.10",
Expand Down
7 changes: 5 additions & 2 deletions adapter/local/index.ts → src/entry/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { createCache, defaultRequestBuilder, initEnv, startServerV2 } from 'clou
// @ts-expect-error
import { installFetchProxy } from 'cloudflare-worker-adapter/fetchProxy';
import type { GetUpdatesResponse } from 'telegram-bot-api-types';
import { ENV, createRouter, createTelegramBotAPI, handleUpdate } from '../../src';
import type { TelegramBotAPI } from '../../src/telegram/api';
import { ENV } from '../config/env';
import type { TelegramBotAPI } from '../telegram/api';
import { createTelegramBotAPI } from '../telegram/api';
import { handleUpdate } from '../telegram/handler';
import { createRouter } from '../route';

const {
CONFIG_PATH = '/app/config.json',
Expand Down
10 changes: 0 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createRouter } from './route';
import { createTelegramBotAPI } from './telegram/api';
import { handleUpdate } from './telegram/handler';
import { ENV } from './config/env';

export default {
Expand All @@ -17,11 +15,3 @@ export default {
}
},
};

// 暴露给adapter使用的函数和变量
export {
ENV,
createRouter,
createTelegramBotAPI,
handleUpdate,
};
86 changes: 47 additions & 39 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
import { execSync } from 'node:child_process';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import cleanup from 'rollup-plugin-cleanup';
import checker from 'vite-plugin-checker';
import dts from 'vite-plugin-dts';
import { nodeExternals } from 'rollup-plugin-node-externals';

const TIMESTAMP_FILE = './dist/timestamp';
const BUILD_INFO_JSON = './dist/buildinfo.json';
const { BUILD_MODE } = process.env;

const COMMIT_HASH = execSync('git rev-parse --short HEAD').toString().trim();
const TIMESTAMP = Math.floor(Date.now() / 1000);
const plugins: Plugin[] = [
nodeResolve({
preferBuiltins: true,
}),
cleanup({
comments: 'none',
extensions: ['js', 'ts'],
}),
dts({
rollupTypes: true,
}),
checker({
typescript: true,
}),
nodeExternals(),
];
const define: Record<string, string> = {};
const entry = path.resolve(__dirname, BUILD_MODE === 'local' ? 'src/entry/local.ts' : 'src/index.ts');

export default defineConfig({
plugins: [
nodeResolve({
preferBuiltins: true,
}),
cleanup({
comments: 'none',
extensions: ['js', 'ts'],
}),
dts({
rollupTypes: true,
}),
checker({
typescript: true,
}),
{
name: 'buildInfo',
async closeBundle() {
await fs.writeFile(TIMESTAMP_FILE, TIMESTAMP.toString());
await fs.writeFile(BUILD_INFO_JSON, JSON.stringify({
sha: COMMIT_HASH,
timestamp: TIMESTAMP,
}));
},
if (BUILD_MODE !== 'local') {
const TIMESTAMP_FILE = './dist/timestamp';
const BUILD_INFO_JSON = './dist/buildinfo.json';
const COMMIT_HASH = execSync('git rev-parse --short HEAD').toString().trim();
const TIMESTAMP = Math.floor(Date.now() / 1000);
plugins.push({
name: 'buildInfo',
async closeBundle() {
await fs.writeFile(TIMESTAMP_FILE, TIMESTAMP.toString());
await fs.writeFile(BUILD_INFO_JSON, JSON.stringify({
sha: COMMIT_HASH,
timestamp: TIMESTAMP,
}));
},
],
});
define.__BUILD_VERSION__ = JSON.stringify(COMMIT_HASH);
define.__BUILD_TIMESTAMP__ = TIMESTAMP.toString();
} else {
define.__BUILD_VERSION__ = JSON.stringify('local');
define.__BUILD_TIMESTAMP__ = '0';
}

export default defineConfig({
plugins,
build: {
target: 'esnext',
lib: {
entry: 'src/index.ts',
entry,
fileName: 'index',
formats: ['es'],
},
minify: false,
rollupOptions: {
external: ['node:buffer'],
plugins: [
],
},
},
define: {
__BUILD_VERSION__: JSON.stringify(COMMIT_HASH),
__BUILD_TIMESTAMP__: TIMESTAMP.toString(),
},
define,
});
2 changes: 1 addition & 1 deletion wrangler-example.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 这里的 name 改成你自己的workers 的名字
name = 'chatgpt-telegram-workers'
compatibility_date = '2023-10-07'
main = './dist/index.ts'
main = './dist/index.js'
workers_dev = true
compatibility_flags = [ "nodejs_compat" ]

Expand Down
Loading

0 comments on commit ca62483

Please sign in to comment.