Skip to content

Commit

Permalink
feat: 实现修改 telemetry.macMachineId 功能
Browse files Browse the repository at this point in the history
- 添加修改 storage.json 文件中 telemetry.macMachineId 的功能
- 实现跨平台支持(Windows、macOS、Linux)
- 增加用户自定义路径和 machineId 配置选项
- 优化错误处理和用户提示
  • Loading branch information
bestK committed Nov 28, 2024
1 parent 422238e commit 1c0d601
Show file tree
Hide file tree
Showing 6 changed files with 1,227 additions and 884 deletions.
11 changes: 1 addition & 10 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
.vscode/**
.vscode-test/**
test/**
.gitignore
.yarnrc
vsc-extension-quickstart.md
**/jsconfig.json
**/*.map
**/eslint.config.mjs
**/.vscode-test.*
'node_modules/**/*'
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MIT
101 changes: 100 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,106 @@
const vscode = require('vscode');
const os = require('os');
const path = require('path');
const fs = require('fs');

function getStoragePath() {
// 首先检查用户是否在设置中指定了路径
const config = vscode.workspace.getConfiguration('cursorFakeMachine');
const customPath = config.get('storagePath');

if (customPath && fs.existsSync(customPath)) {
return customPath;
}

// 如果没有指定或路径无效,使用默认路径
const platform = os.platform();
let basePath;

switch (platform) {
case 'win32':
basePath = path.join(os.homedir(), 'AppData', 'Roaming', 'Cursor', 'User', 'globalStorage');
break;
case 'darwin':
basePath = path.join(os.homedir(), 'Library', 'Application Support', 'Cursor', 'User', 'globalStorage');
break;
case 'linux':
basePath = path.join(os.homedir(), '.config', 'Cursor', 'User', 'globalStorage');
break;
default:
throw new Error('不支持的操作系统');
}

return path.join(basePath, 'storage.json');
}

function modifyMacMachineId() {
try {
const storagePath = getStoragePath();

// 检查文件是否存在
if (!fs.existsSync(storagePath)) {
throw new Error(`文件不存在: ${storagePath}`);
}

// 读取文件
let data = JSON.parse(fs.readFileSync(storagePath, 'utf8'));

// 获取用户配置的 machineId 或生成随机 ID
const config = vscode.workspace.getConfiguration('cursorFakeMachine');
const customMachineId = config.get('customMachineId');
const newMachineId = customMachineId || generateRandomMachineId();

// 修改 telemetry.macMachineId
if (!data.telemetry) {
data.telemetry = {};
}
data['telemetry.macMachineId'] = newMachineId;

// 写回文件
fs.writeFileSync(storagePath, JSON.stringify(data, null, 2), 'utf8');

return {
success: true,
message: '已成功修改 telemetry.macMachineId',
newId: newMachineId,
path: storagePath,
};
} catch (error) {
throw new Error(`修改失败: ${error.message}`);
}
}

function generateRandomMachineId() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

function activate(context) {
vscode.window.showInformationMessage('Cursor Fake Machine 已启动!');
let disposable = vscode.commands.registerCommand('cursor-fake-machine.cursor-fake-machine', async function () {
try {
const result = modifyMacMachineId();
vscode.window.showInformationMessage(`修改成功!\n路径: ${result.path}\n新的 machineId: ${result.newId}`);
} catch (error) {
vscode.window.showErrorMessage(`修改失败: ${error.message}`);

// 如果是路径不存在的错误,提示用户设置路径
if (error.message.includes('不存在')) {
const answer = await vscode.window.showQuestionMessage(
'是否要打开设置页面指定 storage.json 的路径?',
'是',
'否',
);
if (answer === '是') {
vscode.commands.executeCommand('workbench.action.openSettings', 'cursorFakeMachine.storagePath');
}
}
}
});

context.subscriptions.push(disposable);
}

function deactivate() {}
Expand Down
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 36 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
{
"name": "cursor-fake-machine",
"displayName": "cursor-fake-machine",
"description": "",
"displayName": "Fake Cursor: Fake Machine",
"publisher": "fake-cursor",
"description": "https://github.com/fake-cursor/cursor-fake-machine",
"repository": "https://github.com/fake-cursor/cursor-fake-machine",
"version": "0.0.1",
"engines": {
"vscode": "^1.95.0"
"vscode": "^1.93.0"
},
"categories": [
"Other"
],
"activationEvents": [],
"main": "./extension.js",
"contributes": {
"configuration": {
"title": "Cursor Fake Machine",
"properties": {
"cursorFakeMachine.storagePath": {
"type": "string",
"default": "",
"description": "自定义 storage.json 文件的路径。留空则使用默认路径。"
},
"cursorFakeMachine.customMachineId": {
"type": "string",
"default": "",
"description": "自定义 machineId。留空则随机生成。"
}
}
},
"commands": [
{
"command": "cursor-fake-machine.cursor-fake-machine",
Expand All @@ -22,14 +39,27 @@
"scripts": {
"lint": "eslint .",
"pretest": "pnpm run lint",
"test": "vscode-test"
"test": "vscode-test",
"prepare": "echo 'node_modules/**/*' > .vscodeignore"
},
"devDependencies": {
"@types/vscode": "^1.95.0",
"@types/vscode": "^1.93.0",
"@types/mocha": "^10.0.9",
"@types/node": "20.x",
"eslint": "^9.13.0",
"@vscode/test-cli": "^0.0.10",
"@vscode/test-electron": "^2.4.1"
}
},
"peerDependenciesMeta": {
"@babel/core": {
"optional": true
},
"ava": {
"optional": true
},
"benchmark": {
"optional": true
}
},
"icon": "icon.png"
}
Loading

0 comments on commit 1c0d601

Please sign in to comment.