MCPcopy Create free account
hub / github.com/vercel/vercel / runPyprojectScript

Function runPyprojectScript

packages/python/src/utils.ts:149–199  ·  view source on GitHub ↗
(
  workPath: string,
  scriptNames: string | Iterable<string>,
  env?: NodeJS.ProcessEnv,
  useUserVirtualEnv = true
)

Source from the content-addressed store, hash-verified

147}
148
149export async function runPyprojectScript(
150 workPath: string,
151 scriptNames: string | Iterable<string>,
152 env?: NodeJS.ProcessEnv,
153 useUserVirtualEnv = true
154) {
155 const pyprojectPath = join(workPath, 'pyproject.toml');
156 if (!fs.existsSync(pyprojectPath)) return false;
157
158 type Pyproject = {
159 tool?: {
160 vercel?: { scripts?: Record<string, string> };
161 };
162 };
163
164 let pyproject: Pyproject | null = null;
165 try {
166 pyproject = await readConfigFile<Pyproject>(pyprojectPath);
167 } catch {
168 console.error('Failed to parse pyproject.toml');
169 return false;
170 }
171
172 // Read scripts from [tool.vercel.scripts]
173 const scripts: Record<string, string> =
174 pyproject?.tool?.vercel?.scripts || {};
175 const candidates =
176 typeof scriptNames === 'string' ? [scriptNames] : Array.from(scriptNames);
177 const scriptToRun = candidates.find(name => Boolean(scripts[name]));
178 if (!scriptToRun) return false;
179
180 // Use the Python from the virtualenv if present to resolve tooling, else system python
181 const systemPython = process.platform === 'win32' ? 'python' : 'python3';
182 const finalEnv = { ...process.env, ...env };
183 if (useUserVirtualEnv) {
184 useVirtualEnv(workPath, finalEnv, systemPython);
185 }
186
187 const scriptCommand = scripts[scriptToRun];
188 if (typeof scriptCommand === 'string' && scriptCommand.trim()) {
189 console.log(`Executing: ${scriptCommand}`);
190 await execCommand(scriptCommand, {
191 cwd: workPath,
192 env: finalEnv,
193 });
194 return true;
195 }
196
197 // No command string was provided for the found script name
198 return false;
199}

Callers 1

buildFunction · 0.90

Calls 6

execCommandFunction · 0.90
useVirtualEnvFunction · 0.85
errorMethod · 0.80
existsSyncMethod · 0.45
findMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected