MCPcopy Create free account
hub / github.com/Maksandre/w3wallets / buildCacheForSetup

Function buildCacheForSetup

packages/w3wallets/src/cache/buildCache.ts:95–230  ·  view source on GitHub ↗
(
  compiledFilePath: string,
  originalFilePath: string,
  options: BuildOptions = {},
)

Source from the content-addressed store, hash-verified

93 * @param options - Build options
94 */
95export async function buildCacheForSetup(
96 compiledFilePath: string,
97 originalFilePath: string,
98 options: BuildOptions = {},
99): Promise<void> {
100 const hash = hashFilePath(path.resolve(originalFilePath));
101 const cacheDir = path.join(process.cwd(), CACHE_DIR, hash);
102
103 if (!options.force && fs.existsSync(cacheDir)) {
104 console.log(` Cache exists: ${cacheDir} (use --force to rebuild)`);
105 return;
106 }
107
108 // Import the compiled setup file (must use require for dynamic CJS imports)
109 // eslint-disable-next-line @typescript-eslint/no-require-imports
110 const compiled = require(path.resolve(compiledFilePath));
111 const config: CachedWalletConfig = compiled.default ?? compiled;
112
113 if (!isCachedConfig(config)) {
114 throw new Error(
115 `Setup file must export a CachedWalletConfig (use prepareWallet()): ${originalFilePath}`,
116 );
117 }
118
119 // Resolve extension path
120 const extPath = path.join(process.cwd(), W3WALLETS_DIR, config.extensionDir);
121 if (!fs.existsSync(path.join(extPath, "manifest.json"))) {
122 throw new Error(
123 `Extension not found at ${extPath}. Run 'npx w3wallets ${config.name}' first.`,
124 );
125 }
126
127 console.log(` Building cache for "${config.name}"...`);
128
129 // Clean and create cache dir
130 if (fs.existsSync(cacheDir)) {
131 fs.rmSync(cacheDir, { recursive: true });
132 }
133
134 const context = await chromium.launchPersistentContext(cacheDir, {
135 headless: !options.headed,
136 channel: "chromium",
137 args: [
138 `--disable-extensions-except=${extPath}`,
139 `--load-extension=${extPath}`,
140 ],
141 });
142
143 // Derive extension ID from manifest key or path (same algorithm Chrome uses).
144 const extensionId = getExtensionId(extPath);
145
146 // Wait for the extension service worker to register (MV3).
147 if (context.serviceWorkers().length < 1) {
148 await Promise.race([
149 context.waitForEvent("serviceworker", {
150 timeout: SERVICE_WORKER_TIMEOUT,
151 }),
152 (async () => {

Callers 1

buildAllCachesFunction · 0.85

Calls 6

isCachedConfigFunction · 0.90
getExtensionIdFunction · 0.90
sleepFunction · 0.90
hashFilePathFunction · 0.85
waitForStorageStableFunction · 0.85
gotoMethod · 0.45

Tested by

no test coverage detected