Skip to content

Commit b55124e

Browse files
committed
fix: 修复codesandbox启动时间过长的问题
1 parent 055e0b8 commit b55124e

File tree

7 files changed

+97
-21
lines changed

7 files changed

+97
-21
lines changed

.codesandbox/tasks.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
{
22
// These tasks will run in order when initializing your CodeSandbox project.
3-
"setupTasks": [
4-
{
5-
"name": "Install Dependencies",
6-
"command": "npm install"
7-
}
8-
],
3+
"setupTasks": [],
94

105
// These tasks can be run from CodeSandbox. Running one will open a log in the app.
116
"tasks": {
127
"dev": {
138
"name": "Start Server",
14-
"command": "npm install && npm run build && npm start",
9+
"command": "npm start",
1510
"runAtStart": true,
1611
"preview": {
1712
"port": 3000
1813
},
1914
"restartOn": {
20-
"files": ["./package-lock.json"]
15+
"files": [
16+
"./package.json"
17+
],
18+
"branch": false,
19+
"resume": false
2120
}
2221
},
2322
"build": {
2423
"name": "Build",
25-
"command": "npm run build",
26-
"runAtStart": false
24+
"command": "npm run build"
2725
},
2826
"start": {
2927
"name": "Start Server",
30-
"command": "npm run start",
31-
"runAtStart": false
28+
"command": "npm start"
3229
},
3330
"lint": {
3431
"name": "Lint",
35-
"command": "npm run lint",
36-
"runAtStart": false
32+
"command": "npm run lint"
3733
},
3834
"install": {
3935
"name": "Install Dependencies",
4036
"command": "npm install",
4137
"restartOn": {
42-
"files": ["./package.json"]
38+
"files": [
39+
"./package.json"
40+
],
41+
"branch": false,
42+
"resume": false
4343
}
4444
}
4545
}

.devcontainer/Dockerfile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM weaigc/bingo as build
2+
3+
FROM mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye
4+
5+
ARG DEBIAN_FRONTEND=noninteractive
6+
7+
# 如果没有特别需要不要配置
8+
ENV BING_HEADER ""
9+
10+
# Set home to the user's home directory
11+
ENV HOME=/home/user \
12+
PATH=/home/user/.local/bin:$PATH
13+
14+
# Set up a new user named "user" with user ID 1000
15+
RUN useradd -o -u 1000 user && mkdir -p $HOME/app && chown -R user $HOME
16+
17+
# Switch to the "user" user
18+
USER user
19+
20+
# Set the working directory to the user's home directory
21+
WORKDIR $HOME/app
22+
23+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
24+
COPY --chown=user . $HOME/app/
25+
26+
COPY --from=build /home/user/app $HOME/
27+
28+
ENV PORT 7860
29+
30+
EXPOSE 7860
31+
32+
# CMD npm start

.github/pull.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "1"
2+
rules:
3+
- base: main
4+
upstream: weaigc:main # change `weaigc` to the owner of upstream repo
5+
mergeMethod: rebase

Dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
FROM node:18
1+
FROM node:20
22

33
ARG DEBIAN_FRONTEND=noninteractive
44

5+
# 如果没有特别需要不要配置
56
ENV BING_HEADER ""
67

78
# Set home to the user's home directory
89
ENV HOME=/home/user \
9-
PATH=/home/user/.local/bin:$PATH
10+
PATH=/home/user/.local/bin:$PATH
1011

1112
# Set up a new user named "user" with user ID 1000
1213
RUN useradd -o -u 1000 user && mkdir -p $HOME/app && chown -R user $HOME
@@ -22,7 +23,7 @@ COPY --chown=user . $HOME/app/
2223

2324
RUN if [ ! -f ".next/routes-manifest.json" ]; then \
2425
npm install && npm run build; \
25-
fi
26+
fi
2627

2728
RUN rm -rf src
2829

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dev": "cross-env DEBUG=bingo* NODE_ENV=development node ./server.js",
88
"build": "next build",
99
"proxy": "node ./cloudflare/cli.js",
10-
"start": "cross-env NODE_ENV=production node ./server.js",
10+
"start": "node ./scripts/pre-check.js && cross-env NODE_ENV=production node ./server.js",
1111
"lint": "next lint"
1212
},
1313
"dependencies": {

scripts/pre-check.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { existsSync } = require('fs');
2+
const { spawn } = require('child_process');
3+
4+
function executeCommand(command) {
5+
return new Promise((resolve, reject) => {
6+
const child = spawn(command, { shell: true });
7+
8+
child.stdout.on('data', (data) => {
9+
console.log(data.toString());
10+
});
11+
12+
child.stderr.on('data', (data) => {
13+
console.error(data.toString());
14+
});
15+
16+
child.on('exit', (code) => {
17+
console.log(`Child process exited with code ${code}`);
18+
resolve();
19+
});
20+
21+
child.on('error', (err) => {
22+
console.error(err);
23+
reject(err);
24+
});
25+
});
26+
}
27+
28+
async function start() {
29+
let exists = false;
30+
try {
31+
exists = existsSync('.next/BUILD_ID');
32+
} catch (e) {}
33+
if (!exists) {
34+
await executeCommand('npm install');
35+
await executeCommand('npm run build');
36+
}
37+
}
38+
start();

0 commit comments

Comments
 (0)