diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..d8bc5ea --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,158 @@ +# DevBox 服务器部署 + +一条命令搞定,跟宝塔面板的安装方式一样:SSH 连上服务器,粘一行,回车。 + +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash +``` + +跑完会打印访问地址,默认 `http://服务器IP:8090/`。 + +> 想换端口、换目录,在后面加参数(见下方「参数」)。 +> 没装 sudo 的机器用 `su -` 切到 root 再跑,或者把 `sudo bash` 换成 `bash`。 + +## 它做了什么 + +1. 检查 root,缺 `git` / `curl` / `python3` 就用 apt / dnf / yum / apk 自动装 +2. 把仓库浅克隆到 `/opt/devbox`(已存在就 `fetch + reset --hard` 更新) +3. 写 `systemd` 服务 `devbox.service`,用 Python 自带的静态服务器跑,`Restart=always` +4. `curl` 健康检查,确认页面能打开 +5. 可选:写一份 Nginx 站点配置并 reload + +装完这些命令都能用: + +```bash +systemctl status devbox # 看状态 +journalctl -u devbox -f # 看日志 +systemctl restart devbox # 重启 +``` + +## 参数 + +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- [参数] +``` + +| 参数 | 说明 | 默认 | +| --- | --- | --- | +| `--port 8080` | HTTP 端口 | `8090` | +| `--dir /srv/devbox` | 安装目录 | `/opt/devbox` | +| `--nginx box.example.com` | 同时写 Nginx 站点并 reload(`_` 表示默认站点) | 不写 | +| `--no-service` | 只拉代码,不装 systemd(容器、没 systemd 的机器) | 关 | +| `--update` | 更新到最新代码并重启 | — | +| `--uninstall` | 停服务、删 unit 和 Nginx 配置(保留代码目录) | — | +| `-h` | 帮助 | — | + +例子: + +```bash +# 换端口 +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --port 8080 + +# 装到 /www/wwwroot/devbox,并挂一个域名 +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- \ + --dir /www/wwwroot/devbox --port 8080 --nginx box.example.com + +# 更新 +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --update + +# 卸载 +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --uninstall +``` + +## 宝塔面板 + +两种都行: + +**A. 用面板自带的终端(推荐)** + +面板 → 终端(或 SSH 工具),直接粘上面那条一键命令。脚本和宝塔不冲突,走的是 `systemd`,不占面板的站点。 + +**B. 用面板建个纯静态站点** + +1. 面板 → 网站 → 添加站点,域名填你的域名,PHP 版本选「纯静态」 +2. 把仓库代码放进站点根目录(面板 → 文件 → 上传,或终端执行): + ```bash + cd /www/wwwroot/你的域名 && git clone --depth=1 https://github.com/syc65535/devbox.git . + ``` +3. 面板 → 网站 → 设置 → 网站目录,确认根目录对上,保存 +4. 需要 HTTPS 就在面板 → SSL 里申请 Let's Encrypt 证书,一键部署 + +纯静态站点不需要 Node、不需要数据库,也不吃 CPU。 + +## Nginx + HTTPS(不用宝塔时) + +脚本带 `--nginx 域名` 会自动写好 `/etc/nginx/conf.d/devbox.conf`。之后: + +```bash +sudo apt install -y certbot python3-certbot-nginx # Debian/Ubuntu +sudo certbot --nginx -d box.example.com --redirect +``` + +不用 Nginx 也能跑:脚本起的 8090 端口本身就是 HTTP 服务,直接访问 IP 即可。 + +## 手动部署(不用脚本) + +DevBox 是纯静态页面,任何静态托管都能放: + +```bash +git clone --depth=1 https://github.com/syc65535/devbox.git +cd devbox +python3 -m http.server 8090 +# 打开 http://127.0.0.1:8090 +``` + +也可以丢给 GitHub Pages、Cloudflare Pages、Vercel,或者公司内网的 Nginx / Caddy,指向目录即可,不用构建。 + +## 常见问题 + +**打不开 / 一直在转** + +- 云服务器要在「安全组」里放行端口,本机防火墙也要放行:`ufw allow 8090/tcp` +- 看服务活着没:`systemctl status devbox`;看报错:`journalctl -u devbox -n 50 --no-pager` +- 端口被占用:换 `--port 8081` 再装一遍,脚本是幂等的 + +**想用 80 端口** + +加 `--nginx 域名`,用 Nginx 反代/静态托管到 80/443,脚本自己的 8090 只监听本机也行(把 unit 里的 `0.0.0.0` 改成 `127.0.0.1`)。 + +**服务器是 Docker / 没 systemd** + +用 `--no-service` 只拉代码,然后自己起: + +```bash +docker run -d --name devbox -p 8090:80 \ + -v /opt/devbox:/usr/share/nginx/html:ro nginx:alpine +``` + +**怎么更新到最新版** + +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --update +``` + +**私有仓库** + +把 `REPO_URL` 换掉再跑: + +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | \ + sudo DEVBOX_REPO=https://@github.com/你/仓库.git bash +``` + +**会不会收集数据** + +不会。页面全在浏览器里算,服务端只是个静态文件服务器。 + +## 目录 + +装完之后是这些文件,升级只会覆盖代码,不动配置: + +``` +/opt/devbox/ +├── index.html +├── css/ js/ preview/ +└── README.md +/etc/systemd/system/devbox.service # 服务 +/etc/nginx/conf.d/devbox.conf # 只有加了 --nginx 才有 +``` diff --git a/README.md b/README.md index a8adbc3..861f50a 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,29 @@ # DevBox · 开发者工具箱 -一个纯前端的开发者工具箱:图片转格式、哈希与 AES、JWT、时间戳、Cron、JSON、正则、Diff、颜色、CIDR、chmod,还有**一键生成服务器部署脚本**。没有后端、没有依赖、没有追踪,打开就能用。 +一个纯前端的开发者工具箱:图片转格式与加水印、哈希与 AES、JWT、时间戳、Cron、JSON、正则、Diff、颜色、CIDR、chmod、cURL 转代码……没有后端、没有依赖、没有追踪,打开就能用。 ![首页](preview/home.png) +## 装到服务器 + +一条命令,跟宝塔面板的装法一样(SSH 里粘进去回车): + +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash +``` + +跑完打印地址,默认 `http://服务器IP:8090/`。脚本会自动装 git / python3、拉到 `/opt/devbox`、注册 `systemd` 服务、做健康检查;要换端口就加 `--port 8080`,要 Nginx 站点就加 `--nginx 你的域名`。 + +更新、卸载、宝塔面板用法、HTTPS、Docker 部署都在 **[INSTALL.md](INSTALL.md)**。 + ## 特点 -- **26 个工具,6 类**:图片 / 编码 / 加密 / 文本 / 时间 / Web 与系统 +- **25 个工具,6 类**:图片 / 编码 / 加密 / 文本 / 时间 / Web 与系统 - **全部本地算**:哈希、AES、图片转码都走浏览器自带的 Web Crypto 和 Canvas,文件不会离开这台设备 - **零依赖、无构建**:原生 HTML + CSS + JS,没有 npm install,没有打包器;双击 `index.html` 也能跑(`file://` 支持) - **手机能用**:侧栏自动收成抽屉,卡片和表单按竖屏重排 - **键盘党**:`⌘K` / `Ctrl+K` 呼出命令面板,`/` 聚焦搜索,`Esc` 关弹层,`↑↓` 选工具 -- **复制即用**:结果区、颜色写法、部署脚本都是一键复制;部署脚本还能直接下载成 `deploy.sh` +- **复制即用**:结果区、颜色写法、取色板、生成的代码都是一键复制 ## 工具清单 @@ -42,20 +54,18 @@ | Web | 文件权限 chmod | `rwx` 勾选 ↔ 八进制互转,附常见权限解释 | | Web | IPv4 / CIDR 计算 | 网络地址、掩码、通配符、广播、可用范围、主机数、是否私有地址 | | Web | cURL 转代码 | 粘贴浏览器复制的 cURL,转成 fetch / axios / Python requests,方法、请求头、body 都能带 | -| Web | **一键部署脚本** | 填仓库、目录、构建命令,生成服务器上直接能跑的 `deploy.sh` + systemd 单元 + Nginx 配置 | -## 一键部署脚本 +## 部署 -选这个工具,填四个东西(仓库地址、部署目录、依赖安装方式、重启方式),右边就会生成: +DevBox 是纯静态页面,丢到任何静态托管都能用;想在自己的服务器上装成服务,用根目录的 `install.sh`: -1. **一行命令**:`ssh root@你的服务器 'bash -s' < deploy.sh` -2. **deploy.sh**:首次 `git clone --depth=1`,之后 `fetch + reset --hard`;自动识别 `package-lock.json` / `pnpm-lock.yaml` / `yarn.lock` / `requirements.txt` / `composer.json` 装依赖;跑构建;按 systemd / pm2 / docker compose 重启;最后 `curl` 健康检查,20 秒起不来就报错并提示看日志 -3. **systemd 单元**:`WorkingDirectory`、`PORT`、`Restart=always` 都填好,拷到 `/etc/systemd/system/` 就能 `enable --now` -4. **Nginx 反代**:转发到 `127.0.0.1:端口`,带 WebSocket 头和 `X-Forwarded-*`,后面的 HTTPS 用 `certbot --nginx` 一条命令 +```bash +curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash +``` -![部署脚本](preview/deploy.png) - -脚本默认 `set -euo pipefail`,`clean -fdx` 时保留 `node_modules`、`.env`、`data`,不会把你服务器上的环境和数据删掉。 +- 默认装到 `/opt/devbox`,端口 `8090`,注册 `systemd` 服务 `devbox` +- `--port` / `--dir` / `--nginx 域名` / `--no-service` / `--update` / `--uninstall` 都能调 +- 宝塔面板:面板自带终端里跑同一条命令,或者建个「纯静态」站点把目录指过去——两种做法都写在 [INSTALL.md](INSTALL.md) ## 跑起来 @@ -107,8 +117,9 @@ devbox/ │ ├── tools-time.js # 时间戳 / 日期 / Cron │ ├── tools-image.js # 图片转换 / Base64 / 取色 │ ├── tools-web.js # 颜色 / HTTP / chmod / CIDR -│ ├── tools-deploy.js # 一键部署脚本 │ ├── tools-extra.js # JSON→TS / 图片水印 / cURL 转代码 +├── install.sh # 服务器一键安装脚本 +├── INSTALL.md # 部署文档(宝塔 / Nginx / Docker) │ └── app.js # 路由、搜索、命令面板 ├── preview/ # README 截图 └── README.md @@ -128,7 +139,6 @@ devbox/ - 超大图片(>50MB)在手机上可能转码失败,建议先缩尺寸。 - `crypto.subtle` 只在安全上下文(https 或 localhost)可用;用 `file://` 打开时加密工具会提示不可用,其他工具正常。 - Cron 只解析标准 5/6 段,不支持 `@reboot` 这类宏;`L`、`W`、`#` 也不支持。 -- 部署脚本面向 systemd / pm2 / docker compose 三种常见方式;Windows 服务器、宝塔面板请按脚本里的命令手动改。 ## 加一个工具 diff --git a/index.html b/index.html index f66c388..0a9498e 100644 --- a/index.html +++ b/index.html @@ -105,7 +105,6 @@ - diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..ad23852 --- /dev/null +++ b/install.sh @@ -0,0 +1,212 @@ +#!/usr/bin/env bash +# DevBox 一键安装 / 更新 / 卸载 +# +# 装: +# curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash +# 换端口: +# curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --port 8080 +# 更新 / 卸载: +# curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --update +# curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --uninstall +set -euo pipefail + +REPO_URL="${DEVBOX_REPO:-https://github.com/syc65535/devbox.git}" +APP_DIR="${DEVBOX_DIR:-/opt/devbox}" +PORT="${DEVBOX_PORT:-8090}" +SERVICE="devbox" +NGINX_DOMAIN="" +MODE="install" +NO_SERVICE=0 + +c_reset=$'\033[0m'; c_dim=$'\033[2m'; c_cyan=$'\033[36m'; c_green=$'\033[32m'; c_red=$'\033[31m'; c_bold=$'\033[1m' +log() { printf '%s▸%s %s\n' "$c_cyan" "$c_reset" "$*"; } +ok() { printf '%s✓%s %s\n' "$c_green" "$c_reset" "$*"; } +warn() { printf '%s!%s %s\n' "$c_red" "$c_reset" "$*" >&2; } +die() { warn "$*"; exit 1; } + +usage() { + cat <<'EOF' +DevBox 一键安装脚本 + + --dir <路径> 安装目录,默认 /opt/devbox + --port <端口> HTTP 端口,默认 8090 + --nginx <域名> 额外写一份 Nginx 站点配置并 reload(域名可写 _ 表示默认站点) + --no-service 只拉代码,不装 systemd 服务(容器 / 没 systemd 时用) + --update 更新到最新代码并重启服务 + --uninstall 停服务、删 unit(不删代码目录) + -h, --help 显示这份帮助 +EOF +} + +while [ $# -gt 0 ]; do + case "$1" in + --dir) APP_DIR="${2:?缺少目录}"; shift 2 ;; + --port) PORT="${2:?缺少端口}"; shift 2 ;; + --nginx) NGINX_DOMAIN="${2:-_}"; shift 2 ;; + --no-service) NO_SERVICE=1; shift ;; + --update) MODE="update"; shift ;; + --uninstall) MODE="uninstall"; shift ;; + -h|--help) usage; exit 0 ;; + *) warn "未知参数:$1"; usage; exit 1 ;; + esac +done + +case "$PORT" in ''|*[!0-9]*) die "端口要填数字:$PORT" ;; esac +[ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ] || die "端口范围 1-65535:$PORT" + +if [ "$(id -u)" -ne 0 ]; then + die "需要 root:curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash" +fi + +pm_install() { + if command -v apt-get >/dev/null 2>&1; then + DEBIAN_FRONTEND=noninteractive apt-get update -qq >/dev/null 2>&1 || true + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null + elif command -v dnf >/dev/null 2>&1; then dnf install -y "$@" >/dev/null + elif command -v yum >/dev/null 2>&1; then yum install -y "$@" >/dev/null + elif command -v apk >/dev/null 2>&1; then apk add --no-cache "$@" >/dev/null + else die "没认出你的发行版,请手动装:$*" + fi +} + +need() { + command -v "$1" >/dev/null 2>&1 && return 0 + log "安装 $1" + pm_install "$2" + command -v "$1" >/dev/null 2>&1 || die "$1 装完还是找不到,请手动安装后重跑" +} + +unit_path="/etc/systemd/system/${SERVICE}.service" +nginx_conf="/etc/nginx/conf.d/${SERVICE}.conf" + +stop_service() { + if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE}.service"; then + systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true + fi +} + +write_unit() { + cat >"$unit_path" </dev/null 2>&1 || true + systemctl restart "$SERVICE" +} + +write_nginx() { + need nginx nginx + mkdir -p /etc/nginx/conf.d + cat >"$nginx_conf" </dev/null 2>&1; then + systemctl reload nginx >/dev/null 2>&1 || service nginx reload >/dev/null 2>&1 || true + ok "Nginx 站点已写入 $nginx_conf(server_name ${NGINX_DOMAIN})" + else + warn "Nginx 配置检查没过,已保留 $nginx_conf,手动执行 nginx -t 看看" + fi +} + +uninstall() { + log "卸载 $SERVICE" + stop_service + rm -f "$unit_path" + rm -f "$nginx_conf" + command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload || true + command -v nginx >/dev/null 2>&1 && nginx -t >/dev/null 2>&1 && (systemctl reload nginx >/dev/null 2>&1 || true) || true + ok "服务已移除;代码目录保留在 ${APP_DIR},要彻底删就 rm -rf ${APP_DIR}" +} + +pull_code() { + need git git + need curl curl + if [ -d "${APP_DIR}/.git" ]; then + log "更新 ${APP_DIR}" + git -C "$APP_DIR" fetch --depth=1 origin main + git -C "$APP_DIR" reset --hard FETCH_HEAD + else + log "克隆仓库到 ${APP_DIR}" + mkdir -p "$(dirname "$APP_DIR")" + rm -rf "$APP_DIR" + git clone --depth=1 "$REPO_URL" "$APP_DIR" + fi + [ -f "${APP_DIR}/index.html" ] || die "拉下来的目录里没有 index.html,仓库地址对吗?" +} + +health() { + command -v curl >/dev/null 2>&1 || return 0 + for _ in $(seq 1 15); do + if curl -fsS "http://127.0.0.1:${PORT}/" 2>/dev/null | grep -q 'DevBox'; then + ok "服务已就绪" + return 0 + fi + sleep 1 + done + warn "15 秒内没起来,看看日志:journalctl -u ${SERVICE} -n 50 --no-pager" + return 1 +} + +summary() { + local ip + ip="$(hostname -I 2>/dev/null | awk '{print $1}')" + echo + printf '%sDevBox 已就位%s\n' "$c_bold" "$c_reset" + printf ' 地址 http://%s:%s/\n' "${ip:-服务器IP}" "$PORT" + printf ' 目录 %s\n' "$APP_DIR" + printf ' 命令 systemctl status %s / journalctl -u %s -f\n' "$SERVICE" "$SERVICE" + printf ' 更新 curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --update\n' + printf ' 卸载 curl -fsSL https://raw.githubusercontent.com/syc65535/devbox/main/install.sh | sudo bash -s -- --uninstall\n' + echo + printf '%s如果外面访问不到,记得放行端口:ufw allow %s/tcp(云服务器还要在安全组里放行)%s\n' "$c_dim" "$PORT" "$c_reset" + [ -n "$NGINX_DOMAIN" ] && printf '%sHTTPS:certbot --nginx -d %s%s\n' "$c_dim" "$NGINX_DOMAIN" "$c_reset" +} + +case "$MODE" in + uninstall) uninstall; exit 0 ;; + update) pull_code; [ "$NO_SERVICE" = 1 ] || write_unit; health || true; summary; exit 0 ;; + install) pull_code ;; +esac + +if [ "$NO_SERVICE" = 1 ]; then + ok "代码已就位:${APP_DIR}" + echo " 手动启动:python3 -m http.server ${PORT} --directory ${APP_DIR} --bind 0.0.0.0" + exit 0 +fi + +command -v systemctl >/dev/null 2>&1 || die "这台机器没有 systemd,请加 --no-service 只拉代码" + +need python3 python3 +log "写入 systemd 服务 /etc/systemd/system/${SERVICE}.service" +write_unit +[ -n "$NGINX_DOMAIN" ] && write_nginx +health || true +summary diff --git a/js/tools-deploy.js b/js/tools-deploy.js deleted file mode 100644 index cd49ee6..0000000 --- a/js/tools-deploy.js +++ /dev/null @@ -1,306 +0,0 @@ -/* DevBox · 部署:一键拉仓库 + 构建 + 重启 + 健康检查 */ -(function () { - 'use strict' - const { el, panelTitle, copy, output, downloadText, debounce } = DB - const pane = (title, right) => { const p = el('div', { class: 'panel' }); if (title) p.append(panelTitle(title, right)); return p } - const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn }) - const field = (label, control) => el('label', { class: 'field' }, el('span', { text: label }), control) - - const INSTALLS = { - auto: '自动检测(推荐)', - npm: 'npm ci(干净安装 lock 里的版本)', - pnpm: 'pnpm install --frozen-lockfile', - yarn: 'yarn install --immutable', - pip: 'pip install -r requirements.txt', - composer: 'composer install --no-dev', - none: '不需要(纯静态 / 手动)', - } - const RUNS = { - systemd: 'systemd 服务(推荐,开机自启)', - pm2: 'pm2(Node 生态,进程管理)', - docker: 'docker compose(容器)', - none: '只拉代码,不重启', - } - - const shq = (s) => '"' + String(s).replace(/"/g, '\\"') + '"' - - DB.tool({ - id: 'deploy', - name: '一键部署脚本', - group: 'web', - icon: 'terminal', - desc: '填仓库和目录,生成服务器上能直接跑的拉取 + 构建 + 重启脚本,附带 systemd 与 Nginx 配置', - keywords: ['deploy', 'deployment', 'git', 'pull', 'ci', 'systemd', 'nginx', 'pm2', '部署', '上线', '发布', '服务器'], - mount(root) { - const cfg = { - repo: 'https://github.com/syc65535/devbox.git', - ref: 'main', - dir: '/srv/devbox', - name: 'devbox', - install: 'auto', - build: 'npm run build', - run: 'systemd', - port: '3000', - user: 'www-data', - start: 'npm start', - health: '/', - nginx: true, - domain: 'box.example.com', - ssh: 'root@你的服务器IP', - } - - const inputs = {} - const add = (key, node) => { inputs[key] = node; return node } - const text = (key, ph) => add(key, el('input', { type: 'text', placeholder: ph || '', value: cfg[key] })) - const select = (key, options) => { - const s = el('select') - for (const [v, label] of Object.entries(options)) { - s.append(el('option', { value: v, text: label, selected: v === cfg[key] })) - } - return add(key, s) - } - - const repoIn = text('repo', 'https://github.com/owner/repo.git') - const refIn = text('ref', 'main') - const dirIn = text('dir', '/srv/app') - const nameIn = text('name', 'my-app') - const installIn = select('install', INSTALLS) - const buildIn = text('build', '留空表示不需要构建') - const runIn = select('run', RUNS) - const portIn = text('port', '3000') - const userIn = text('user', 'www-data') - const startIn = text('start', 'npm start') - const healthIn = text('health', '/') - const domainIn = text('domain', 'example.com') - const sshIn = text('ssh', 'root@1.2.3.4') - const nginxBox = el('input', { type: 'checkbox', checked: true }) - - const outScript = output() - const outUnit = output() - const outNginx = output() - const oneLine = el('pre', { class: 'out flush', style: { minHeight: '0' } }) - const hintBox = el('div', { class: 'hint' }) - - const installBlock = (mode, repo) => { - const lines = [] - if (mode === 'none') return '# 依赖:跳过(按需自己装)' - if (mode === 'npm') lines.push('npm ci --no-audit --no-fund') - else if (mode === 'pnpm') lines.push('corepack enable >/dev/null 2>&1 || true', 'pnpm install --frozen-lockfile') - else if (mode === 'yarn') lines.push('corepack enable >/dev/null 2>&1 || true', 'yarn install --immutable') - else if (mode === 'pip') lines.push('python3 -m pip install --upgrade pip', 'python3 -m pip install -r requirements.txt') - else if (mode === 'composer') lines.push('composer install --no-dev --optimize-autoloader') - else { - lines.push('if [ -f pnpm-lock.yaml ]; then') - lines.push(' corepack enable >/dev/null 2>&1 || true; pnpm install --frozen-lockfile') - lines.push('elif [ -f yarn.lock ]; then') - lines.push(' corepack enable >/dev/null 2>&1 || true; yarn install --immutable') - lines.push('elif [ -f package-lock.json ]; then') - lines.push(' npm ci --no-audit --no-fund') - lines.push('elif [ -f package.json ]; then') - lines.push(' npm install --no-audit --no-fund') - lines.push('elif [ -f requirements.txt ]; then') - lines.push(' python3 -m pip install -r requirements.txt') - lines.push('elif [ -f composer.json ]; then') - lines.push(' composer install --no-dev --optimize-autoloader') - lines.push('else') - lines.push(' echo " (没有识别到依赖清单,跳过)"') - lines.push('fi') - } - return lines.join('\n') - } - - const restartBlock = (run, name) => { - if (run === 'none') return '# 重启:跳过(只更新了代码)' - if (run === 'systemd') return [ - `if systemctl list-unit-files | grep -q '^${name}.service'; then`, - ` sudo systemctl restart ${name}`, - 'else', - ` echo " 还没装 ${name}.service,把下面的 unit 装到 /etc/systemd/system/ 再跑一次"`, - 'fi', - ].join('\n') - if (run === 'pm2') return [ - `if command -v pm2 >/dev/null 2>&1; then`, - ` pm2 restart ${name} --update-env 2>/dev/null || pm2 start npm --name ${name} -- start`, - ' pm2 save >/dev/null 2>&1 || true', - 'else', - ' echo " 没装 pm2:npm i -g pm2"', - 'fi', - ].join('\n') - return [ - 'if [ -f docker-compose.yml ] || [ -f compose.yaml ]; then', - ' docker compose up -d --build', - 'else', - ' echo " 目录里没有 compose 文件,跳过容器重启"', - 'fi', - ].join('\n') - } - - const buildScript = () => { - const dir = cfg.dir.replace(/\/+$/, '') - const healthPath = cfg.health.startsWith('/') ? cfg.health : '/' + cfg.health - const port = String(cfg.port).replace(/\D/g, '') || '3000' - const lines = [] - lines.push('#!/usr/bin/env bash') - lines.push('# 由 DevBox 生成 · ' + new Date().toISOString().slice(0, 10)) - lines.push(`# 用法:ssh ${cfg.ssh} 'bash -s' < deploy.sh`) - lines.push('set -euo pipefail') - lines.push('') - lines.push(`REPO=${shq(cfg.repo)}`) - lines.push(`REF=${shq(cfg.ref || 'main')}`) - lines.push(`APP_DIR=${shq(dir)}`) - lines.push(`APP_NAME=${shq(cfg.name || 'app')}`) - lines.push(`PORT=${shq(port)}`) - lines.push('') - lines.push(`log() { printf '\\033[36m▸ %s\\033[0m\\n' "$*"; }`) - lines.push(`die() { printf '\\033[31m✗ %s\\033[0m\\n' "$*" >&2; exit 1; }`) - lines.push('') - lines.push('command -v git >/dev/null 2>&1 || die "没装 git:apt-get update && apt-get install -y git"') - lines.push('') - lines.push('log "目标 $APP_NAME → $APP_DIR(分支 $REF)"') - lines.push('if [ -d "$APP_DIR/.git" ]; then') - lines.push(' log "已存在,拉取最新代码"') - lines.push(' git -C "$APP_DIR" fetch --depth=1 origin "$REF"') - lines.push(' git -C "$APP_DIR" reset --hard FETCH_HEAD') - lines.push(' git -C "$APP_DIR" clean -fdx -e node_modules -e .env -e data') - lines.push('else') - lines.push(' log "首次部署,克隆仓库"') - lines.push(' mkdir -p "$(dirname "$APP_DIR")"') - lines.push(' git clone --depth=1 --branch "$REF" "$REPO" "$APP_DIR"') - lines.push('fi') - lines.push('') - lines.push('cd "$APP_DIR"') - lines.push('') - lines.push('log "安装依赖"') - lines.push(installBlock(cfg.install, cfg.repo)) - if (cfg.build.trim()) { - lines.push('') - lines.push('log "构建"') - lines.push(cfg.build.trim()) - } - lines.push('') - lines.push('log "重启服务"') - lines.push(restartBlock(cfg.run, cfg.name || 'app')) - if (cfg.run !== 'none') { - lines.push('') - lines.push('log "健康检查 http://127.0.0.1:$PORT' + healthPath + '"') - lines.push('for i in $(seq 1 20); do') - lines.push(' if curl -fsS "http://127.0.0.1:$PORT' + healthPath + '" >/dev/null 2>&1; then') - lines.push(' log "服务已就绪"; break') - lines.push(' fi') - lines.push(' if [ "$i" = "20" ]; then') - lines.push(' die "20 秒没起来,看看日志:journalctl -u $APP_NAME -n 50 --no-pager"') - lines.push(' fi') - lines.push(' sleep 1') - lines.push('done') - } - lines.push('') - lines.push('log "完成:$APP_NAME 现在跑的是 $(git -C "$APP_DIR" rev-parse --short HEAD)"') - return lines.join('\n') - } - - const buildUnit = () => `[Unit] -Description=${cfg.name} -After=network.target - -[Service] -Type=simple -User=${cfg.user || 'www-data'} -WorkingDirectory=${cfg.dir} -Environment=NODE_ENV=production -Environment=PORT=${cfg.port} -ExecStart=/usr/bin/env ${cfg.start || 'npm start'} -Restart=always -RestartSec=3 -StandardOutput=journal -StandardError=journal - -[Install] -WantedBy=multi-user.target` - - const buildNginx = () => `server { - listen 80; - listen [::]:80; - server_name ${cfg.domain || 'example.com'}; - client_max_body_size 50m; - - location / { - proxy_pass http://127.0.0.1:${cfg.port}; - proxy_http_version 1.1; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; - proxy_read_timeout 120s; - } -}` - - let p5, p6 - const render = () => { - for (const k of Object.keys(inputs)) cfg[k] = inputs[k].value - cfg.nginx = nginxBox.checked - const script = buildScript() - outScript.set(script) - oneLine.textContent = `ssh ${cfg.ssh || 'root@服务器'} 'bash -s' < deploy.sh` - oneLine.dataset.raw = oneLine.textContent - outUnit.set(buildUnit()) - outNginx.set(buildNginx()) - p5.hidden = cfg.run !== 'systemd' - p6.hidden = !cfg.nginx - hintBox.textContent = cfg.nginx - ? `Nginx 转发到 127.0.0.1:${cfg.port},域名 ${cfg.domain};HTTPS 用 certbot --nginx 一条命令签发。` - : `没有开 Nginx:直接访问 http://服务器IP:${cfg.port},记得放行防火墙端口。` - } - - const lazy = debounce(render, 140) - root.addEventListener('input', lazy) - root.addEventListener('change', lazy) - - const p1 = pane('项目') - p1.append(el('div', { class: 'grid2' }, - field('仓库地址(https 最省事)', repoIn), - field('分支 / tag', refIn), - field('服务器上的部署目录', dirIn), - field('应用名(服务名 / pm2 名)', nameIn))) - - const p2 = pane('构建与运行') - p2.append(el('div', { class: 'grid2' }, - field('依赖安装', installIn), - field('构建命令', buildIn), - field('重启方式', runIn), - field('端口', portIn), - field('运行用户', userIn), - field('启动命令', startIn), - field('健康检查路径', healthIn), - field('SSH 目标(只影响那行一键命令)', sshIn))) - p2.append(el('div', { class: 'row', style: { marginTop: '12px', alignItems: 'flex-end' } }, - el('label', { class: 'check', style: { paddingBottom: '11px', whiteSpace: 'nowrap' } }, nginxBox, el('span', { text: '附带 Nginx 配置' })), - el('label', { class: 'field', style: { flex: '1' } }, el('span', { text: '域名(Nginx server_name)' }), domainIn))) - hintBox.style.marginTop = '10px' - p2.append(hintBox) - - const p3 = pane('① 一键执行(本地跑,自动在服务器上执行)') - p3.append( - el('div', { class: 'hint', text: '先在本地存成 deploy.sh(点右上「下载」),再执行下面这行;首次部署会遇到域名解析、权限等提示,按提示装 git / systemd 即可。' }), - el('div', { class: 'row', style: { marginTop: '10px' } }, btn('复制这一行', () => copy(oneLine.dataset.raw))), - oneLine) - - const p4 = pane('② 部署脚本 deploy.sh', btn('下载 deploy.sh', () => downloadText('deploy.sh', outScript.text, 'text/x-sh'))) - p4.append(outScript.node) - - p5 = pane('③ systemd 单元(首次部署时装一次)') - p5.append( - el('div', { class: 'hint', text: '存成 /etc/systemd/system/<应用名>.service,然后执行:sudo systemctl daemon-reload && sudo systemctl enable --now <应用名>' }), - outUnit.node) - - p6 = pane('④ Nginx 反向代理(可选)') - p6.append( - el('div', { class: 'hint', text: '存成 /etc/nginx/sites-available/<应用名>.conf,软链到 sites-enabled,nginx -t 通过后 reload;再 certbot --nginx -d 域名 上 HTTPS。' }), - outNginx.node) - - root.append(p1, p2, p3, p4, p5, p6) - render() - }, - }) -})() diff --git a/preview/deploy.png b/preview/deploy.png deleted file mode 100644 index 975a9aa..0000000 Binary files a/preview/deploy.png and /dev/null differ diff --git a/preview/deploy2.png b/preview/deploy2.png deleted file mode 100644 index 7dde6a1..0000000 Binary files a/preview/deploy2.png and /dev/null differ diff --git a/preview/home.png b/preview/home.png index 87ba134..69a33fd 100644 Binary files a/preview/home.png and b/preview/home.png differ