Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Commit a21d5d3

Browse files
committed
feat: ✨ update sh、ps1 script
1 parent f34ced8 commit a21d5d3

File tree

2 files changed

+261
-27
lines changed

2 files changed

+261
-27
lines changed

scripts/reset.ps1

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,126 @@ if (-not (Test-Path $configDir)) {
1111
Remove-Item -Path "$configDir\Local Storage" -Recurse -Force -ErrorAction SilentlyContinue
1212
Remove-Item -Path "$configDir\Session Storage" -Recurse -Force -ErrorAction SilentlyContinue
1313

14+
# 检查 Cursor 是否已安装
15+
function Check-CursorInstalled {
16+
$cursorPath = Join-Path $env:LOCALAPPDATA "Programs\Cursor\Cursor.exe"
17+
if (-not (Test-Path $cursorPath)) {
18+
Write-Host "❌ 未检测到 Cursor 编辑器,请先安装 Cursor!"
19+
Write-Host "下载地址:https://www.cursor.com/downloads"
20+
exit 1
21+
}
22+
Write-Host "✅ Cursor 编辑器已安装"
23+
}
24+
25+
# 检查 Cursor 是否在运行
26+
function Get-CursorProcess {
27+
$processes = Get-WmiObject -Class Win32_Process | Where-Object {
28+
$_.Name -like "*cursor*" -and
29+
$_.Name -notlike "*cursor-reset*" -and
30+
$_.ProcessId -ne $PID
31+
}
32+
return $processes
33+
}
34+
35+
# 关闭 Cursor 进程
36+
function Stop-CursorProcess {
37+
$processes = Get-CursorProcess
38+
if ($processes) {
39+
$processes | ForEach-Object {
40+
Stop-Process -Id $_.ProcessId -Force
41+
}
42+
Start-Sleep -Seconds 1.5
43+
}
44+
}
45+
46+
# 生成随机设备 ID
47+
function New-DeviceId {
48+
return [guid]::NewGuid().ToString()
49+
}
50+
51+
# 备份配置文件
52+
function Backup-ConfigFile {
53+
param (
54+
[string]$ConfigFile
55+
)
56+
$timestamp = Get-Date -Format "yyyyMMddHHmmssfff"
57+
$backupFile = "${ConfigFile}.${timestamp}.bak"
58+
Copy-Item -Path $ConfigFile -Destination $backupFile -Force
59+
return $backupFile
60+
}
61+
62+
# 主程序
63+
function Main {
64+
Write-Host "🔍 正在检查 Cursor 编辑器..."
65+
Check-CursorInstalled
66+
Write-Host
67+
68+
Write-Host "🔍 检查 Cursor 是否在运行..."
69+
$cursorProcess = Get-CursorProcess
70+
if ($cursorProcess) {
71+
$response = Read-Host "检测到 Cursor 正在运行,是否自动关闭? (y/N)"
72+
if ($response -eq 'y' -or $response -eq 'Y') {
73+
Write-Host "正在关闭 Cursor..."
74+
Stop-CursorProcess
75+
$cursorProcess = Get-CursorProcess
76+
if ($cursorProcess) {
77+
Write-Host "❌ 无法自动关闭 Cursor,请手动关闭后重试!"
78+
exit 1
79+
}
80+
Write-Host "✅ Cursor 已成功关闭"
81+
} else {
82+
Write-Host "❌ 请先关闭 Cursor 编辑器后再运行此工具!"
83+
exit 1
84+
}
85+
} else {
86+
Write-Host "✅ Cursor 编辑器已关闭"
87+
}
88+
Write-Host
89+
90+
$configDir = Join-Path $env:APPDATA "Cursor"
91+
$storageFile = Join-Path $configDir "User\globalStorage\storage.json"
92+
93+
Write-Host "📂 正在准备配置文件..."
94+
New-Item -ItemType Directory -Path (Split-Path $storageFile -Parent) -Force | Out-Null
95+
Write-Host "✅ 配置目录创建成功"
96+
Write-Host
97+
98+
if (Test-Path $storageFile) {
99+
Write-Host "💾 正在备份原配置..."
100+
$backupFile = Backup-ConfigFile -ConfigFile $storageFile
101+
Write-Host "✅ 配置备份完成,备份文件路径:$((Split-Path $backupFile -Leaf))"
102+
Write-Host
103+
}
104+
105+
Write-Host "🎲 正在生成新的设备 ID..."
106+
$machineId = New-DeviceId
107+
$macMachineId = New-DeviceId
108+
$devDeviceId = New-DeviceId
109+
110+
# 创建或更新配置文件
111+
$config = @{
112+
"telemetry.machineId" = $machineId
113+
"telemetry.macMachineId" = $macMachineId
114+
"telemetry.devDeviceId" = $devDeviceId
115+
}
116+
117+
$config | ConvertTo-Json | Set-Content -Path $storageFile -Encoding UTF8
118+
119+
Write-Host "✅ 新设备 ID 生成成功"
120+
Write-Host
121+
Write-Host "💾 正在保存新配置..."
122+
Write-Host "✅ 新配置保存成功"
123+
Write-Host
124+
Write-Host "🎉 设备 ID 重置成功!新的设备 ID 为:"
125+
Write-Host
126+
Get-Content $storageFile
127+
Write-Host
128+
Write-Host "📝 配置文件路径:$storageFile"
129+
Write-Host
130+
Write-Host "✨ 现在可以启动 Cursor 编辑器了"
131+
}
132+
133+
# 运行主程序
134+
Main
14135
Write-Host "✨ Cursor 试用期已重置"
15136
Write-Host "🎉 重启 Cursor 编辑器即可开始新的试用期"

scripts/reset.sh

Lines changed: 140 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,142 @@
11
#!/bin/bash
22

3-
# 获取用户主目录
4-
HOME_DIR="$HOME"
5-
6-
# 定义 Cursor 配置目录
7-
if [[ "$OSTYPE" == "darwin"* ]]; then
8-
# macOS
9-
CONFIG_DIR="$HOME_DIR/Library/Application Support/Cursor"
10-
elif [[ "$OSTYPE" == "linux"* ]]; then
11-
# Linux
12-
CONFIG_DIR="$HOME_DIR/.config/Cursor"
13-
else
14-
echo "不支持的操作系统"
15-
exit 1
16-
fi
17-
18-
# 检查目录是否存在
19-
if [ ! -d "$CONFIG_DIR" ]; then
20-
echo "Cursor 配置目录不存在"
21-
exit 1
22-
fi
23-
24-
# 删除试用相关文件
25-
rm -rf "$CONFIG_DIR/Local Storage"
26-
rm -rf "$CONFIG_DIR/Session Storage"
27-
28-
echo "✨ Cursor 试用期已重置"
29-
echo "🎉 重启 Cursor 编辑器即可开始新的试用期"
3+
# 检查 Cursor 是否已安装
4+
check_cursor_installed() {
5+
if [[ "$OSTYPE" == "darwin"* ]]; then
6+
# macOS
7+
if [ ! -d "/Applications/Cursor.app" ]; then
8+
echo "❌ 未检测到 Cursor 编辑器,请先安装 Cursor!"
9+
echo "下载地址:https://www.cursor.com/downloads"
10+
exit 1
11+
fi
12+
elif [[ "$OSTYPE" == "linux"* ]]; then
13+
# Linux
14+
if [ ! -d "/usr/share/cursor" ] && [ ! -d "/opt/cursor" ] && [ ! -d "$HOME/.local/share/cursor" ]; then
15+
echo "❌ 未检测到 Cursor 编辑器,请先安装 Cursor!"
16+
echo "下载地址:https://www.cursor.com/downloads"
17+
exit 1
18+
fi
19+
fi
20+
echo "✅ Cursor 编辑器已安装"
21+
}
22+
23+
# 检查 Cursor 是否在运行
24+
check_cursor_running() {
25+
if [[ "$OSTYPE" == "darwin"* ]]; then
26+
pgrep -x "Cursor" > /dev/null || pgrep -x "Cursor Helper" > /dev/null
27+
elif [[ "$OSTYPE" == "linux"* ]]; then
28+
pgrep -x "cursor" > /dev/null || pgrep -x "Cursor" > /dev/null
29+
fi
30+
}
31+
32+
# 关闭 Cursor 进程
33+
kill_cursor_process() {
34+
if [[ "$OSTYPE" == "darwin"* ]]; then
35+
pkill -9 "Cursor"
36+
elif [[ "$OSTYPE" == "linux"* ]]; then
37+
pkill -9 "cursor"
38+
fi
39+
sleep 1.5
40+
}
41+
42+
# 获取配置文件路径
43+
get_config_dir() {
44+
if [[ "$OSTYPE" == "darwin"* ]]; then
45+
echo "$HOME/Library/Application Support/Cursor"
46+
elif [[ "$OSTYPE" == "linux"* ]]; then
47+
echo "$HOME/.config/Cursor"
48+
else
49+
echo "❌ 不支持的操作系统"
50+
exit 1
51+
fi
52+
}
53+
54+
# 生成随机设备 ID
55+
generate_device_id() {
56+
if [[ "$OSTYPE" == "darwin"* ]]; then
57+
echo $(uuidgen)
58+
else
59+
echo $(cat /proc/sys/kernel/random/uuid)
60+
fi
61+
}
62+
63+
# 备份配置文件
64+
backup_config() {
65+
local config_file="$1"
66+
local timestamp=$(date +"%Y%m%d%H%M%S%3N")
67+
local backup_file="${config_file}.${timestamp}.bak"
68+
cp "$config_file" "$backup_file"
69+
echo "$backup_file"
70+
}
71+
72+
# 主程序
73+
main() {
74+
echo "🔍 正在检查 Cursor 编辑器..."
75+
check_cursor_installed
76+
echo
77+
78+
echo "🔍 检查 Cursor 是否在运行..."
79+
if check_cursor_running; then
80+
echo "检测到 Cursor 正在运行,是否自动关闭? (y/N): "
81+
read -r response
82+
if [[ "$response" =~ ^[Yy]$ ]]; then
83+
echo "正在关闭 Cursor..."
84+
kill_cursor_process
85+
if check_cursor_running; then
86+
echo "❌ 无法自动关闭 Cursor,请手动关闭后重试!"
87+
exit 1
88+
fi
89+
echo "✅ Cursor 已成功关闭"
90+
else
91+
echo "❌ 请先关闭 Cursor 编辑器后再运行此工具!"
92+
exit 1
93+
fi
94+
else
95+
echo "✅ Cursor 编辑器已关闭"
96+
fi
97+
echo
98+
99+
CONFIG_DIR=$(get_config_dir)
100+
STORAGE_FILE="$CONFIG_DIR/User/globalStorage/storage.json"
101+
102+
echo "📂 正在准备配置文件..."
103+
mkdir -p "$(dirname "$STORAGE_FILE")"
104+
echo "✅ 配置目录创建成功"
105+
echo
106+
107+
if [ -f "$STORAGE_FILE" ]; then
108+
echo "💾 正在备份原配置..."
109+
BACKUP_FILE=$(backup_config "$STORAGE_FILE")
110+
echo "✅ 配置备份完成,备份文件路径:$(basename "$BACKUP_FILE")"
111+
echo
112+
fi
113+
114+
echo "🎲 正在生成新的设备 ID..."
115+
MACHINE_ID=$(generate_device_id)
116+
MAC_MACHINE_ID=$(generate_device_id)
117+
DEV_DEVICE_ID=$(generate_device_id)
118+
119+
# 创建或更新配置文件
120+
cat > "$STORAGE_FILE" << EOF
121+
{
122+
"telemetry.machineId": "${MACHINE_ID}",
123+
"telemetry.macMachineId": "${MAC_MACHINE_ID}",
124+
"telemetry.devDeviceId": "${DEV_DEVICE_ID}"
125+
}
126+
EOF
127+
128+
echo "✅ 新设备 ID 生成成功"
129+
echo
130+
echo "💾 正在保存新配置..."
131+
echo "✅ 新配置保存成功"
132+
echo
133+
echo "🎉 设备 ID 重置成功!新的设备 ID 为:"
134+
echo
135+
cat "$STORAGE_FILE"
136+
echo
137+
echo "📝 配置文件路径:$STORAGE_FILE"
138+
echo
139+
echo "✨ 现在可以启动 Cursor 编辑器了"
140+
}
141+
142+
main

0 commit comments

Comments
 (0)