MCPcopy Create free account
hub / github.com/LuanRT/YouTube.js / buildScript

Method buildScript

src/utils/javascript/JsExtractor.ts:329–513  ·  view source on GitHub ↗

* Processes extracted matches from the analyzer, handles dependencies, predeclares * variables as needed, and generates an IIFE-wrapped output string containing the * code snippets and exported variables. * @param config - Configuration options for the emitter.

(config: EmitterOptions)

Source from the content-addressed store, hash-verified

327 * @param config - Configuration options for the emitter.
328 */
329 public buildScript(config: EmitterOptions): BuildScriptResult {
330 const { maxDepth = Infinity, forceVarPredeclaration = false, exportRawValues = false, rawValueOnly: skipEmitFor = [] } = config;
331
332 const extractions = this.analyzer.getExtractedMatches();
333 const seen = new Set<string>(extractions.map((e) => e.metadata?.name || ''));
334
335 const snippets: string[] = [];
336 const predeclaredVarSet = new Set<string>();
337 const exported = new Map<string, ESTree.Node>();
338 const exportedRawValues: Record<string, any> = {};
339
340 function registerPredeclaredVar(name?: string) {
341 if (!name || name.includes('.')) return;
342 predeclaredVarSet.add(name);
343 }
344
345 const visit = (metadata?: VariableMetadata, depth: number = 0, whitelistedDep?: string) => {
346 if (!metadata || depth > maxDepth) return;
347
348 for (const dependency of metadata.dependencies) {
349 if (whitelistedDep && whitelistedDep !== dependency) {
350 // If we haven't yet encountered the whitelisted dependency, skip this one.
351 // And if we have, delete the whitelist var so that all subsequent dependencies are included.
352 if (!seen.has(whitelistedDep))
353 continue;
354 whitelistedDep = undefined;
355 }
356
357 if (seen.has(dependency))
358 continue;
359
360 seen.add(dependency);
361
362 const dependencyMetadata = this.analyzer.declaredVariables.get(dependency);
363
364 if (!dependencyMetadata)
365 continue;
366
367 const shouldPredeclare = forceVarPredeclaration || dependencyMetadata.predeclared;
368 if (shouldPredeclare) {
369 registerPredeclaredVar(dependency);
370 }
371
372 visit(dependencyMetadata, depth + 1, whitelistedDep);
373
374 snippets.push(this.renderNode(dependencyMetadata.node, shouldPredeclare, config));
375
376 if (dependencyMetadata.prototypeAliases.size > 0) {
377 for (const [ , aliasMembers ] of dependencyMetadata.prototypeAliases) {
378 for (const member of aliasMembers) {
379 // This is deeper than the first visit, so no need to pass the whitelist, we want all deps of the member to be included.
380 visit(member, depth);
381 snippets.push(this.renderNode(member.node, shouldPredeclare, config));
382 }
383 }
384 }
385 }
386 };

Callers 1

createMethod · 0.95

Calls 8

renderNodeMethod · 0.95
memberToStringFunction · 0.85
extractNodeSourceFunction · 0.85
createWrapperFunctionFunction · 0.85
getExtractedMatchesMethod · 0.80
getSourceMethod · 0.80
setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected