|
14 | 14 | // See the License for the specific language governing permissions and |
15 | 15 | // limitations under the License. |
16 | 16 |
|
17 | | -const { spawn, execSync } = require('child_process'); |
| 17 | +const { spawn } = require('child_process'); |
18 | 18 | const path = require('path'); |
19 | 19 | const fs = require('fs'); |
| 20 | +const os = require('os'); |
20 | 21 |
|
21 | | -const toolName = "execute_sql"; |
22 | | -const configArgs = ["--prebuilt", "cloud-sql-postgres"]; |
| 22 | +/** |
| 23 | + * Configuration Constants |
| 24 | + */ |
| 25 | +const TOOL_NAME = "execute_sql"; |
| 26 | +const CONFIG_ARGS = ["--prebuilt", "cloud-sql-postgres"]; |
| 27 | +const OPTIONAL_VARS_TO_OMIT_IF_EMPTY = [ |
| 28 | + 'CLOUD_SQL_POSTGRES_USER', |
| 29 | + 'CLOUD_SQL_POSTGRES_PASSWORD', |
| 30 | + 'CLOUD_SQL_POSTGRES_IP_TYPE' |
| 31 | +]; |
| 32 | + |
| 33 | +/** |
| 34 | + * Merges external variables into the environment based on the current context. |
| 35 | + * For GEMINI_CLI, it loads from a .env file. |
| 36 | + * For CLAUDE_CODE, it transforms CLAUDE_PLUGIN_ prefixed variables. |
| 37 | + * @param {Object} env The environment object to populate. |
| 38 | + */ |
| 39 | +function mergeContextualVariables(env) { |
| 40 | + const additionalVars = {}; |
23 | 41 |
|
24 | | -function getToolboxPath() { |
25 | 42 | if (process.env.GEMINI_CLI === '1') { |
26 | | - const localPath = path.resolve(__dirname, '../../../toolbox'); |
27 | | - if (fs.existsSync(localPath)) { |
28 | | - return localPath; |
| 43 | + const envPath = path.resolve(__dirname, '../../../.env'); |
| 44 | + if (fs.existsSync(envPath)) { |
| 45 | + fs.readFileSync(envPath, 'utf-8').split('\n').forEach(line => { |
| 46 | + const trimmed = line.trim(); |
| 47 | + if (!trimmed || trimmed.startsWith('#')) return; |
| 48 | + const splitIdx = trimmed.indexOf('='); |
| 49 | + if (splitIdx === -1) return; |
| 50 | + const key = trimmed.slice(0, splitIdx).trim(); |
| 51 | + const value = trimmed.slice(splitIdx + 1).trim().replace(/(^['"]|['"]$)/g, ''); |
| 52 | + additionalVars[key] = value; |
| 53 | + }); |
29 | 54 | } |
30 | | - } |
31 | | - try { |
32 | | - const checkCommand = process.platform === 'win32' ? 'where toolbox' : 'which toolbox'; |
33 | | - const globalPath = execSync(checkCommand, { stdio: 'pipe', encoding: 'utf-8' }).trim(); |
34 | | - if (globalPath) { |
35 | | - return globalPath.split('\n')[0].trim(); |
| 55 | + } else if (process.env.CLAUDE_CODE === 'true') { |
| 56 | + const prefix = 'CLAUDE_PLUGIN_'; |
| 57 | + for (const key in process.env) { |
| 58 | + if (key.startsWith(prefix)) { |
| 59 | + additionalVars[key.substring(prefix.length)] = process.env[key]; |
| 60 | + } |
36 | 61 | } |
37 | | - throw new Error("Toolbox binary not found"); |
38 | | - } catch (e) { |
39 | | - throw new Error("Toolbox binary not found"); |
40 | 62 | } |
41 | | -} |
42 | 63 |
|
43 | | -let toolboxBinary; |
44 | | -try { |
45 | | - toolboxBinary = getToolboxPath(); |
46 | | -} catch (err) { |
47 | | - console.error("Error:", err.message); |
48 | | - process.exit(1); |
| 64 | + for (const [key, value] of Object.entries(additionalVars)) { |
| 65 | + if (env[key] === undefined) { |
| 66 | + env[key] = value; |
| 67 | + } |
| 68 | + } |
49 | 69 | } |
50 | 70 |
|
51 | | -function getEnv() { |
52 | | - const envPath = path.resolve(__dirname, '../../../.env'); |
| 71 | +/** |
| 72 | + * Prepares the environment and determines the user agent. |
| 73 | + * @returns {{ env: Object, userAgent: string }} |
| 74 | + */ |
| 75 | +function prepareEnvironment() { |
53 | 76 | const env = { ...process.env }; |
54 | | - if (fs.existsSync(envPath)) { |
55 | | - const envContent = fs.readFileSync(envPath, 'utf-8'); |
56 | | - envContent.split('\n').forEach(line => { |
57 | | - const trimmed = line.trim(); |
58 | | - if (trimmed && !trimmed.startsWith('#')) { |
59 | | - const splitIdx = trimmed.indexOf('='); |
60 | | - if (splitIdx !== -1) { |
61 | | - const key = trimmed.slice(0, splitIdx).trim(); |
62 | | - let value = trimmed.slice(splitIdx + 1).trim(); |
63 | | - value = value.replace(/(^['"]|['"]$)/g, ''); |
64 | | - if (env[key] === undefined) { |
65 | | - env[key] = value; |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - }); |
| 77 | + let userAgent = "skills"; |
| 78 | + |
| 79 | + if (process.env.GEMINI_CLI === '1') { |
| 80 | + userAgent = "skills-geminicli"; |
| 81 | + } else if (process.env.CLAUDE_CODE === 'true') { |
| 82 | + userAgent = "skills-claudecode"; |
70 | 83 | } |
71 | | - return env; |
72 | | -} |
73 | 84 |
|
74 | | -let env = process.env; |
75 | | -let userAgent = "skills"; |
76 | | -if (process.env.GEMINI_CLI === '1') { |
77 | | - env = getEnv(); |
78 | | - userAgent = "skills-geminicli"; |
| 85 | + mergeContextualVariables(env); |
| 86 | + |
| 87 | + // Omit optional variables if they are empty strings to avoid misconfiguration |
| 88 | + OPTIONAL_VARS_TO_OMIT_IF_EMPTY.forEach(key => { |
| 89 | + if (env[key] === '') { |
| 90 | + delete env[key]; |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + return { env, userAgent }; |
79 | 95 | } |
80 | 96 |
|
81 | | -const args = process.argv.slice(2); |
| 97 | +/** |
| 98 | + * Main execution function. |
| 99 | + */ |
| 100 | +function main() { |
| 101 | + const { env, userAgent } = prepareEnvironment(); |
| 102 | + const args = process.argv.slice(2); |
| 103 | + const npxArgs = ["--yes", "@toolbox-sdk/server", "--log-level", "error", ...CONFIG_ARGS, "invoke", TOOL_NAME, "--user-agent-metadata", userAgent, ...args]; |
82 | 104 |
|
83 | | -const toolboxArgs = ["--log-level", "error", ...configArgs, "invoke", toolName, "--user-agent-metadata", userAgent, ...args]; |
| 105 | + let command = 'npx'; |
| 106 | + let spawnArgs = npxArgs; |
84 | 107 |
|
85 | | -const child = spawn(toolboxBinary, toolboxArgs, { stdio: 'inherit', env }); |
| 108 | + // The Windows Dependency-Free Bypass |
| 109 | + if (os.platform() === 'win32') { |
| 110 | + const nodeDir = path.dirname(process.execPath); |
| 111 | + const npxCliJs = path.join(nodeDir, 'node_modules', 'npm', 'bin', 'npx-cli.js'); |
86 | 112 |
|
87 | | -child.on('close', (code) => { |
88 | | - process.exit(code); |
89 | | -}); |
| 113 | + if (fs.existsSync(npxCliJs)) { |
| 114 | + command = process.execPath; |
| 115 | + spawnArgs = [npxCliJs, ...npxArgs]; |
| 116 | + } else { |
| 117 | + console.error("Error: Could not find the npx executable to launch."); |
| 118 | + process.exit(1); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const child = spawn(command, spawnArgs, { stdio: 'inherit', env }); |
| 123 | + |
| 124 | + child.on('close', (code) => { |
| 125 | + process.exit(code); |
| 126 | + }); |
| 127 | + |
| 128 | + child.on('error', (err) => { |
| 129 | + console.error("Error executing toolbox:", err); |
| 130 | + process.exit(1); |
| 131 | + }); |
| 132 | +} |
90 | 133 |
|
91 | | -child.on('error', (err) => { |
92 | | - console.error("Error executing toolbox:", err); |
93 | | - process.exit(1); |
94 | | -}); |
| 134 | +// Start the script |
| 135 | +main(); |
0 commit comments