Skip to content

Commit

Permalink
chore: 分离自定义vite plugins,添加快速构建docker image 脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 27, 2024
1 parent 21523d1 commit f9a01dd
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 427 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ out
.wrangler
package-lock.json
/plugins/interpolate.js
dist/index.d.ts
config.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN npm install && npm run build:local
FROM node:alpine as PROD

WORKDIR /app
COPY --from=DEV /app/dist /app/dist
COPY --from=DEV /app/dist/index.js /app/dist/index.js
COPY --from=DEV /app/package.json /app/
RUN npm install --only=production --omit=dev
RUN apk add --no-cache sqlite
Expand Down
10 changes: 8 additions & 2 deletions doc/en/LOCAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ Go to the root directory of the project
### 1. Build image

```bash
docker build -t chatgpt-telegram-bot:latest .
docker build -t chatgpt-telegram-workers:latest .
```

or

```shell
npm run build:docker # more faster
```

### 2. Run container

```bash
docker run -d -p 8787:8787 -v $(pwd)/config.json:/app/config.json:ro -v $(pwd)/wrangler.toml:/app/config.toml:ro chatgpt-telegram-worker:latest
docker run -d -p 8787:8787 -v $(pwd)/config.json:/app/config.json:ro -v $(pwd)/wrangler.toml:/app/config.toml:ro chatgpt-telegram-workers:latest
```


Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chatgpt-telegram-worker
name: chatgpt-telegram-workers
services:
chatgpt-telegram-worker:
chatgpt-telegram-workers:
build: .
ports:
- "8787:8787"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"lint": "eslint --fix *.js *.ts *.json src adapter",
"build": "vite build",
"build:local": "BUILD_MODE=local vite build",
"build:docker": "npm run build:local && cd dist && docker build -t chatgpt-telegram-workers:latest .",
"deploy:dist": "wrangler deploy",
"deploy:build": "npm run build && wrangler deploy",
"start:dist": "node dist/index.js",
Expand All @@ -38,9 +39,8 @@
"telegram-bot-api-types": "^7.9.7",
"tsx": "^4.18.0",
"typescript": "^5.5.4",
"vite": "^5.2.10",
"vite": "^5.4.2",
"vite-plugin-checker": "^0.7.2",
"vite-plugin-dts": "^4.0.3",
"wrangler": "^3.69.1"
}
}
42 changes: 42 additions & 0 deletions src/adapter/docker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'node:fs/promises';
import path from "node:path";

const dockerfile = `
FROM node:alpine as PROD
WORKDIR /app
COPY index.js package.json /app/
RUN npm install --only=production --omit=dev
RUN apk add --no-cache sqlite
EXPOSE 8787
CMD ["npm", "run", "start"]
`

const packageJson = `
{
"name": "chatgpt-telegram-workers",
"type": "module",
"version": "1.8.0",
"author": "TBXark",
"license": "MIT",
"module": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.2.3"
},
"devDependencies": {}
}
`

export const createDockerPlugin = (targetDir: string) => {
return {
name: 'docker',
async closeBundle() {
await fs.writeFile(path.resolve(targetDir, 'Dockerfile'), dockerfile.trim());
await fs.writeFile(path.resolve(targetDir, 'package.json'), packageJson.trim());
},
}
}

31 changes: 31 additions & 0 deletions src/adapter/version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {execSync} from "node:child_process";
import * as fs from 'node:fs/promises';
import path from "node:path";


let COMMIT_HASH = 'unknown'
let TIMESTAMP = Math.floor(Date.now() / 1000);

try {
COMMIT_HASH = execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
console.warn(e)
}

export const createVersionPlugin = (targetDir: string) => {
return {
name: 'buildInfo',
async closeBundle() {
await fs.writeFile(path.resolve(targetDir, 'timestamp'), TIMESTAMP.toString());
await fs.writeFile(path.resolve(targetDir, 'buildinfo.json'), JSON.stringify({
sha: COMMIT_HASH,
timestamp: TIMESTAMP,
}));
},
};
}

export const versionDefine = {
__BUILD_VERSION__: JSON.stringify(COMMIT_HASH),
__BUILD_TIMESTAMP__: TIMESTAMP.toString(),
};
33 changes: 8 additions & 25 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ 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';
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore
import nodeExternals from 'rollup-plugin-node-externals';
import { createVersionPlugin, versionDefine } from "./src/adapter/version";
import { createDockerPlugin } from "./src/adapter/docker";

const { BUILD_MODE } = process.env;

Expand All @@ -21,37 +22,17 @@ const plugins: Plugin[] = [
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/adapter/local.ts' : 'src/index.ts');

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();
if (BUILD_MODE === 'local') {
plugins.push(createDockerPlugin('dist'));
} else {
define.__BUILD_VERSION__ = JSON.stringify('local');
define.__BUILD_TIMESTAMP__ = '0';
plugins.push(createVersionPlugin('dist'))
}

export default defineConfig({
Expand All @@ -65,5 +46,7 @@ export default defineConfig({
},
minify: false,
},
define,
define: {
...versionDefine
},
});
Loading

0 comments on commit f9a01dd

Please sign in to comment.