Skip to content

Commit

Permalink
fix: 修复codesandbox启动时间过长的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Nov 28, 2023
1 parent 055e0b8 commit b55124e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 21 deletions.
30 changes: 15 additions & 15 deletions .codesandbox/tasks.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
{
// These tasks will run in order when initializing your CodeSandbox project.
"setupTasks": [
{
"name": "Install Dependencies",
"command": "npm install"
}
],
"setupTasks": [],

// These tasks can be run from CodeSandbox. Running one will open a log in the app.
"tasks": {
"dev": {
"name": "Start Server",
"command": "npm install && npm run build && npm start",
"command": "npm start",
"runAtStart": true,
"preview": {
"port": 3000
},
"restartOn": {
"files": ["./package-lock.json"]
"files": [
"./package.json"
],
"branch": false,
"resume": false
}
},
"build": {
"name": "Build",
"command": "npm run build",
"runAtStart": false
"command": "npm run build"
},
"start": {
"name": "Start Server",
"command": "npm run start",
"runAtStart": false
"command": "npm start"
},
"lint": {
"name": "Lint",
"command": "npm run lint",
"runAtStart": false
"command": "npm run lint"
},
"install": {
"name": "Install Dependencies",
"command": "npm install",
"restartOn": {
"files": ["./package.json"]
"files": [
"./package.json"
],
"branch": false,
"resume": false
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM weaigc/bingo as build

FROM mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye

ARG DEBIAN_FRONTEND=noninteractive

# 如果没有特别需要不要配置
ENV BING_HEADER ""

# Set home to the user's home directory
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH

# Set up a new user named "user" with user ID 1000
RUN useradd -o -u 1000 user && mkdir -p $HOME/app && chown -R user $HOME

# Switch to the "user" user
USER user

# Set the working directory to the user's home directory
WORKDIR $HOME/app

# Copy the current directory contents into the container at $HOME/app setting the owner to the user
COPY --chown=user . $HOME/app/

COPY --from=build /home/user/app $HOME/

ENV PORT 7860

EXPOSE 7860

# CMD npm start
5 changes: 5 additions & 0 deletions .github/pull.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "1"
rules:
- base: main
upstream: weaigc:main # change `weaigc` to the owner of upstream repo
mergeMethod: rebase
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM node:18
FROM node:20

ARG DEBIAN_FRONTEND=noninteractive

# 如果没有特别需要不要配置
ENV BING_HEADER ""

# Set home to the user's home directory
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
PATH=/home/user/.local/bin:$PATH

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

RUN if [ ! -f ".next/routes-manifest.json" ]; then \
npm install && npm run build; \
fi
fi

RUN rm -rf src

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "cross-env DEBUG=bingo* NODE_ENV=development node ./server.js",
"build": "next build",
"proxy": "node ./cloudflare/cli.js",
"start": "cross-env NODE_ENV=production node ./server.js",
"start": "node ./scripts/pre-check.js && cross-env NODE_ENV=production node ./server.js",
"lint": "next lint"
},
"dependencies": {
Expand Down
38 changes: 38 additions & 0 deletions scripts/pre-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { existsSync } = require('fs');
const { spawn } = require('child_process');

function executeCommand(command) {
return new Promise((resolve, reject) => {
const child = spawn(command, { shell: true });

child.stdout.on('data', (data) => {
console.log(data.toString());
});

child.stderr.on('data', (data) => {
console.error(data.toString());
});

child.on('exit', (code) => {
console.log(`Child process exited with code ${code}`);
resolve();
});

child.on('error', (err) => {
console.error(err);
reject(err);
});
});
}

async function start() {
let exists = false;
try {
exists = existsSync('.next/BUILD_ID');
} catch (e) {}
if (!exists) {
await executeCommand('npm install');
await executeCommand('npm run build');
}
}
start();

0 comments on commit b55124e

Please sign in to comment.