MCPcopy
hub / github.com/electron/forge / initLink

Function initLink

packages/api/core/src/api/init-scripts/init-link.ts:21–126  ·  view source on GitHub ↗
(
  pm: PMDetails,
  dir: string,
  task?: ForgeListrTask<T>,
)

Source from the content-addressed store, hash-verified

19 * Uses yarn link to create portal: resolutions that point to local workspace paths.
20 */
21export async function initLink<T>(
22 pm: PMDetails,
23 dir: string,
24 task?: ForgeListrTask<T>,
25) {
26 const shouldLink = process.env.LINK_FORGE_DEPENDENCIES_ON_INIT;
27 if (shouldLink) {
28 d('Linking forge dependencies');
29 const packageJson = await readRawPackageJson(dir);
30 const forgeRoot = path.resolve(
31 __dirname,
32 '..',
33 '..',
34 '..',
35 '..',
36 '..',
37 '..',
38 );
39
40 const getWorkspacePath = (packageName: string): string => {
41 const result = spawnSync(
42 'yarn',
43 ['workspace', packageName, 'exec', 'pwd'],
44 {
45 cwd: forgeRoot,
46 encoding: 'utf-8',
47 shell: process.platform === 'win32',
48 },
49 );
50
51 if (result.status !== 0) {
52 d(`Failed to get workspace path for ${packageName}: ${result.stderr}`);
53 throw new Error(
54 `Unable to determine workspace path for ${packageName}`,
55 );
56 }
57
58 const workspacePath = result.stdout.trim();
59 return workspacePath;
60 };
61
62 // Collect all @electron-forge packages and their workspace paths
63 const packagesToLink: Record<string, string> = {};
64 for (const packageName of Object.keys(packageJson.devDependencies)) {
65 if (packageName.startsWith('@electron-forge/')) {
66 const workspacePath = getWorkspacePath(packageName);
67 packagesToLink[packageName] = workspacePath;
68 d(`Found ${packageName}, will link to ${workspacePath}`);
69 }
70 }
71
72 // Use yarn link to create portal: resolutions for local packages
73 if (Object.keys(packagesToLink).length > 0) {
74 // Copy the root .yarnrc.yml to the target directory before linking
75 // This ensures settings like npmMinimalAgeGate are preserved
76 if (pm.executable === 'yarn') {
77 const rootYarnrc = path.join(forgeRoot, '.yarnrc.yml');
78 const targetYarnrc = path.join(dir, '.yarnrc.yml');

Callers 1

init.tsFile · 0.90

Calls 3

readRawPackageJsonFunction · 0.90
spawnPackageManagerFunction · 0.90
getWorkspacePathFunction · 0.85

Tested by

no test coverage detected