refactor: devcontainer
This commit is contained in:
@@ -10,12 +10,10 @@
|
||||
"ANTHROPIC_AUTH_TOKEN": "${localEnv:ANTHROPIC_AUTH_TOKEN}",
|
||||
"ANTHROPIC_BASE_URL": "${localEnv:ANTHROPIC_BASE_URL}",
|
||||
"GEMINI_API_KEY": "${localEnv:GEMINI_API_KEY}",
|
||||
"GOOGLE_GEMINI_BASE_URL": "${localEnv:GOOGLE_GEMINI_BASE_URL}",
|
||||
"PATH": "${containerEnv:PATH}:${containerWorkspaceFolder}/node_modules/.bin:${containerEnv:HOME}/.bun/bin:${containerEnv:HOME}/.bun/bin/bun-node-fallback-bin"
|
||||
"GOOGLE_GEMINI_BASE_URL": "${localEnv:GOOGLE_GEMINI_BASE_URL}"
|
||||
},
|
||||
"containerEnv": {
|
||||
"HOME": "/home/usr_vscode",
|
||||
// "NODE_OPTIONS": "--max-old-space-size=4096",
|
||||
"NODE_OPTIONS": "--max-old-space-size=4096",
|
||||
"TZ": "${localEnv:TZ:Asia/Shanghai}"
|
||||
},
|
||||
"customizations": {
|
||||
@@ -55,88 +53,23 @@
|
||||
// * 尽管使用了“/.*/”,但有些还是会失败,因为有几个错误的默认值:
|
||||
// * https://github.com/microsoft/vscode/issues/266651#issuecomment-3292581459
|
||||
"chat.tools.terminal.ignoreDefaultAutoApproveRules": true,
|
||||
"tasks": {
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "dev",
|
||||
"label": "🚀 Dev: Run on Folder Open",
|
||||
"runOptions": {
|
||||
"runOn": "folderOpen"
|
||||
},
|
||||
"isBackground": true,
|
||||
"presentation": {
|
||||
"panel": "dedicated"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
// https://stackoverflow.com/questions/75708866/vscode-dev-container-fails-to-load-ms-vscode-js-debug-extension-correctly
|
||||
// https://davidwesst.com/blog/missing-bootloader-in-vscode-devcontainer/
|
||||
/**
|
||||
* autoAttachFilter 存在问题:会在 NODE_OPTIONS 中注入 bootloader.js 的 --require 参数,
|
||||
* 并错误地重复拼接 --max-old-space-size,形成类似:
|
||||
* NODE_OPTIONS= --require /home/.../bootloader.js --max-old-space-size=4096--max-old-space-size=4096
|
||||
* 导致启动时报错:
|
||||
* Error: illegal value for flag --max-old-space-size=4096--max-old-space-size=4096 of type size_t
|
||||
* 将其设置为 "disabled" 以规避该问题。
|
||||
*/
|
||||
// "debug.javascript.autoAttachFilter": "disabled",
|
||||
"terminal.integrated.defaultProfile.linux": "💲zsh",
|
||||
"terminal.integrated.profiles.linux": {
|
||||
"💲zsh": {
|
||||
"path": "zsh",
|
||||
"icon": "terminal-cmd",
|
||||
"overrideName": true,
|
||||
"color": "terminal.ansiGreen"
|
||||
}
|
||||
}
|
||||
"tasks": { "version": "2.0.0", "tasks": [] },
|
||||
"terminal.integrated.defaultProfile.linux": "zsh"
|
||||
}
|
||||
}
|
||||
},
|
||||
"mounts": [
|
||||
{
|
||||
// 不挂载可能会遇到:`Cannot run macOS (Mach-O) executable in Docker: Exec format error`
|
||||
"type": "volume",
|
||||
"source": "${localWorkspaceFolderBasename}-node_modules",
|
||||
"target": "${containerWorkspaceFolder}/node_modules"
|
||||
},
|
||||
{
|
||||
"type": "volume",
|
||||
"source": "devcontainer-pnpm-store",
|
||||
"target": "/home/usr_vscode/.pnpm-store"
|
||||
},
|
||||
{
|
||||
"type": "volume",
|
||||
"source": "devcontainer-bun-install-cache",
|
||||
"target": "/home/usr_vscode/.bun/install/cache"
|
||||
// rm -rf ~/.bun/install/cache/*
|
||||
},
|
||||
{
|
||||
"type": "bind",
|
||||
"source": "${localWorkspaceFolder}/.devcontainer/onCreateCommand.d",
|
||||
"target": "/usr/local/etc/onCreateCommand.d"
|
||||
}
|
||||
// 不挂载还可能会遇到:`Cannot run macOS (Mach-O) executable in Docker: Exec format error`
|
||||
"source=${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume",
|
||||
"source=${localWorkspaceFolder}/.devcontainer/lifecycle-scripts.d,target=/usr/local/etc/lifecycle-scripts.d,type=bind,consistency=delegated"
|
||||
],
|
||||
/**
|
||||
* 执行顺序总结
|
||||
1. initializeCommand (本地主机,容器创建前)
|
||||
2. 容器创建
|
||||
3. onCreateCommand (容器首次创建时执行一次)
|
||||
4. updateContentCommand ()
|
||||
5. postCreateCommand (每次容器启动时执行)
|
||||
6. 容器启动
|
||||
7. postStartCommand (容器内,每次启动)
|
||||
8. VS Code 附加到容器
|
||||
9. postAttachCommand (容器内,每次附加)
|
||||
*/
|
||||
"initializeCommand": "echo '↘️ 准备创建容器...'; docker pull ghcr.io/yanhao98/h-devcontainer:main;",
|
||||
"onCreateCommand": "echo '↘️ 容器首次创建!'; /usr/local/bin/onCreateCommand.sh",
|
||||
"updateContentCommand": "echo '↘️ 容器内容已更新!'; zsh -c 'time pnpm install;'",
|
||||
"postCreateCommand": "echo '↘️ 容器已创建!';",
|
||||
"postStartCommand": "echo '↘️ 容器启动了!'",
|
||||
"postAttachCommand": "echo '↘️ VS Code 已附加到容器!'; zsh -c 'source ~/.zshrc; echo \"$(date +%Y-%m-%dT%H:%M:%S%z) - Welcome to your Dev Container!\"';",
|
||||
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/${localWorkspaceFolderBasename},type=bind,consistency=delegated",
|
||||
"initializeCommand": "docker pull ghcr.io/yanhao98/h-devcontainer:main;",
|
||||
"onCreateCommand": "/usr/local/bin/run-lifecycle-scripts.sh onCreateCommand",
|
||||
"updateContentCommand": "/usr/local/bin/run-lifecycle-scripts.sh updateContentCommand",
|
||||
"postCreateCommand": "/usr/local/bin/run-lifecycle-scripts.sh postCreateCommand",
|
||||
"postStartCommand": "/usr/local/bin/run-lifecycle-scripts.sh postStartCommand",
|
||||
"postAttachCommand": "/usr/local/bin/run-lifecycle-scripts.sh postAttachCommand",
|
||||
"waitFor": "updateContentCommand",
|
||||
"remoteUser": "usr_vscode"
|
||||
}
|
||||
|
||||
14
.devcontainer/lifecycle-scripts.d/01-onCreateCommand.d/00-setup-container
Executable file
14
.devcontainer/lifecycle-scripts.d/01-onCreateCommand.d/00-setup-container
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/bin/zsh -eu
|
||||
# 打印带有颜色的欢迎信息
|
||||
echo -e "\033[1;32m↘️ 容器首次创建!\033[0m"
|
||||
|
||||
# 修复权限问题(比如 node_modules 目录)
|
||||
sudo chown -R $(whoami):$(whoami) /workspaces || true
|
||||
|
||||
# 调用内置命令 (这些命令在 _build-context/builtin-scripts 目录中的脚本中定义)
|
||||
h-setup-locale
|
||||
h-setup-zsh
|
||||
|
||||
h-setup-bun-bin
|
||||
h-setup-ai-tools
|
||||
h-setup-pnpm-bin
|
||||
13
.devcontainer/lifecycle-scripts.d/02-updateContentCommand.d/00-pnpm-install
Executable file
13
.devcontainer/lifecycle-scripts.d/02-updateContentCommand.d/00-pnpm-install
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash -eu
|
||||
# 容器内容更新时的消息和依赖安装
|
||||
|
||||
echo '↘️ 容器内容已更新!'
|
||||
|
||||
# 安装依赖
|
||||
if command -v pnpm >/dev/null 2>&1; then
|
||||
# 跳过: The modules directory at "/workspaces/h-devcontainers/node_modules" will be removed and reinstalled from scratch. Proceed? (Y/n) ·
|
||||
time pnpm install --config.confirmModulesPurge=false
|
||||
else
|
||||
echo '❌错误: pnpm 未安装'
|
||||
exit 0
|
||||
fi
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/bin/zsh -eu
|
||||
|
||||
# 修复 node_modules 权限问题:
|
||||
sudo chown -R $(whoami):$(whoami) node_modules || true
|
||||
|
||||
h-setup-locale
|
||||
h-setup-zsh
|
||||
h-setup-bun
|
||||
Reference in New Issue
Block a user