|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Copyright 2026 Google LLC |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +const { spawn, execSync } = require('child_process'); |
| 18 | +const path = require('path'); |
| 19 | +const fs = require('fs'); |
| 20 | +const os = require('os'); |
| 21 | + |
| 22 | +const toolName = "analyze_contribution"; |
| 23 | +const configArgs = ["--prebuilt", "bigquery"]; |
| 24 | + |
| 25 | +const OPTIONAL_VARS_TO_OMIT_IF_EMPTY = [ |
| 26 | + 'BIGQUERY_LOCATION', |
| 27 | + 'BIGQUERY_USE_CLIENT_OAUTH', |
| 28 | + 'BIGQUERY_SCOPES', |
| 29 | + 'BIGQUERY_MAX_QUERY_RESULT_ROWS', |
| 30 | + 'BIGQUERY_IMPERSONATE_SERVICE_ACCOUNT', |
| 31 | +]; |
| 32 | + |
| 33 | + |
| 34 | +function mergeEnvVars(env) { |
| 35 | + if (process.env.GEMINI_CLI === '1') { |
| 36 | + const envPath = path.resolve(__dirname, '../../../.env'); |
| 37 | + if (fs.existsSync(envPath)) { |
| 38 | + const envContent = fs.readFileSync(envPath, 'utf-8'); |
| 39 | + envContent.split('\n').forEach(line => { |
| 40 | + const trimmed = line.trim(); |
| 41 | + if (trimmed && !trimmed.startsWith('#')) { |
| 42 | + const splitIdx = trimmed.indexOf('='); |
| 43 | + if (splitIdx !== -1) { |
| 44 | + const key = trimmed.slice(0, splitIdx).trim(); |
| 45 | + let value = trimmed.slice(splitIdx + 1).trim(); |
| 46 | + value = value.replace(/(^['"]|['"]$)/g, ''); |
| 47 | + if (env[key] === undefined) { |
| 48 | + env[key] = value; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + } else if (process.env.CLAUDECODE === '1') { |
| 55 | + const prefix = 'CLAUDE_PLUGIN_OPTION_'; |
| 56 | + for (const key in process.env) { |
| 57 | + if (key.startsWith(prefix)) { |
| 58 | + env[key.substring(prefix.length)] = process.env[key]; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function prepareEnvironment() { |
| 65 | + let env = { ...process.env }; |
| 66 | + let userAgent = "skills"; |
| 67 | + if (process.env.GEMINI_CLI === '1') { |
| 68 | + userAgent = "skills-geminicli"; |
| 69 | + } else if (process.env.CLAUDECODE === '1') { |
| 70 | + userAgent = "skills-claudecode"; |
| 71 | + } else if (process.env.CODEX_CI === '1') { |
| 72 | + userAgent = "skills-codex"; |
| 73 | + } |
| 74 | + mergeEnvVars(env); |
| 75 | + |
| 76 | + OPTIONAL_VARS_TO_OMIT_IF_EMPTY.forEach(varName => { |
| 77 | + if (env[varName] === '') { |
| 78 | + delete env[varName]; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + |
| 83 | + return { env, userAgent }; |
| 84 | +} |
| 85 | + |
| 86 | +function main() { |
| 87 | + const { env, userAgent } = prepareEnvironment(); |
| 88 | + const args = process.argv.slice(2); |
| 89 | + |
| 90 | + const command = os.platform() === 'win32' ? 'npx.cmd' : 'npx'; |
| 91 | + const processedArgs = os.platform() === 'win32' ? args.map(arg => arg.includes('"') ? '"' + arg.replace(/"/g, '""') + '"' : arg) : args; |
| 92 | + const npxArgs = ["--yes", "@toolbox-sdk/server@1.1.0", "--log-level", "error", ...configArgs, "invoke", toolName, "--user-agent-metadata", userAgent, ...processedArgs]; |
| 93 | + |
| 94 | + const child = spawn(command, npxArgs, { shell: os.platform() === 'win32', stdio: 'inherit', env }); |
| 95 | + |
| 96 | + |
| 97 | + child.on('close', (code) => { |
| 98 | + process.exit(code); |
| 99 | + }); |
| 100 | + |
| 101 | + child.on('error', (err) => { |
| 102 | + console.error("Error executing toolbox:", err); |
| 103 | + process.exit(1); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +main(); |
0 commit comments