From 8665921bc12804b7b216947815a85a056dcf0c57 Mon Sep 17 00:00:00 2001 From: yinbowen-dc <65101280+yinbowen-dc@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:15:49 +0800 Subject: [PATCH] fix(cli): honor workspace package manager during init --- .changeset/fresh-subgraph-workspace.md | 5 ++ .../src/command-helpers/package-manager.ts | 50 +++++++++++ packages/cli/src/commands/init.ts | 17 +--- packages/cli/tests/package-manager.test.ts | 84 +++++++++++++++++++ 4 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 .changeset/fresh-subgraph-workspace.md create mode 100644 packages/cli/src/command-helpers/package-manager.ts create mode 100644 packages/cli/tests/package-manager.test.ts diff --git a/.changeset/fresh-subgraph-workspace.md b/.changeset/fresh-subgraph-workspace.md new file mode 100644 index 000000000..a79a10457 --- /dev/null +++ b/.changeset/fresh-subgraph-workspace.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Honor a project's or parent workspace's packageManager declaration when choosing install, codegen, and deploy commands during graph init, including pnpm workspaces. diff --git a/packages/cli/src/command-helpers/package-manager.ts b/packages/cli/src/command-helpers/package-manager.ts new file mode 100644 index 000000000..bca97f2f7 --- /dev/null +++ b/packages/cli/src/command-helpers/package-manager.ts @@ -0,0 +1,50 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { system } from 'gluegun'; + +type PackageManager = 'yarn' | 'npm' | 'pnpm'; + +function findConfiguredPackageManager(directory: string): PackageManager | undefined { + let current = path.resolve(directory); + + while (true) { + const manifest = path.join(current, 'package.json'); + if (fs.existsSync(manifest)) { + const { packageManager } = JSON.parse(fs.readFileSync(manifest, 'utf8')); + if (packageManager) { + return /^(yarn|npm|pnpm)@/.exec(packageManager)?.[1] as PackageManager | undefined; + } + } + + const parent = path.dirname(current); + if (parent === current) { + return; + } + current = parent; + } +} + +export function getPackageManagerCommands(directory: string) { + let manager = findConfiguredPackageManager(directory); + if (manager) { + if (!system.which(manager)) { + throw new Error( + `${manager} was not found on your system. Please install it so it is in $PATH.`, + ); + } + } else { + manager = (['yarn', 'npm', 'pnpm'] as const).find(candidate => system.which(candidate)); + if (!manager) { + throw new Error( + 'Neither Yarn, NPM nor pnpm were found on your system. Please install one of them.', + ); + } + } + + return { + link: `${manager} link ${manager === 'pnpm' ? '--global ' : ''}@graphprotocol/graph-cli`, + install: manager === 'yarn' ? 'yarn' : `${manager} install`, + codegen: manager === 'yarn' ? 'yarn codegen' : `${manager} run codegen`, + deploy: manager === 'yarn' ? 'yarn deploy' : `${manager} run deploy`, + }; +} diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 8a5e23ab0..5d5c830e4 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -10,6 +10,7 @@ import { resolveFile } from '../command-helpers/file-resolver.js'; import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs.js'; import { initNetworksConfig } from '../command-helpers/network.js'; import { chooseNodeUrl } from '../command-helpers/node.js'; +import { getPackageManagerCommands } from '../command-helpers/package-manager.js'; import { PromptManager } from '../command-helpers/prompt-manager.js'; import { loadRegistry } from '../command-helpers/registry.js'; import { retryWithPrompt } from '../command-helpers/retry.js'; @@ -184,21 +185,7 @@ export default class InitCommand extends Command { }); } - // Detect Yarn and/or NPM - const yarn = system.which('yarn'); - const npm = system.which('npm'); - if (!yarn && !npm) { - this.error(`Neither Yarn nor NPM were found on your system. Please install one of them.`, { - exit: 1, - }); - } - - const commands = { - link: yarn ? 'yarn link @graphprotocol/graph-cli' : 'npm link @graphprotocol/graph-cli', - install: yarn ? 'yarn' : 'npm install', - codegen: yarn ? 'yarn codegen' : 'npm run codegen', - deploy: yarn ? 'yarn deploy' : 'npm run deploy', - }; + const commands = getPackageManagerCommands(path.resolve(directory || '.')); // If all parameters are provided from the command-line, // go straight to creating the subgraph from the example diff --git a/packages/cli/tests/package-manager.test.ts b/packages/cli/tests/package-manager.test.ts new file mode 100644 index 000000000..66f9fec33 --- /dev/null +++ b/packages/cli/tests/package-manager.test.ts @@ -0,0 +1,84 @@ +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { system } from 'gluegun'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { getPackageManagerCommands } from '../src/command-helpers/package-manager.js'; + +describe('package manager selection', () => { + let directory: string; + + beforeEach(() => { + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'graph-package-manager-')); + vi.spyOn(system, 'which').mockImplementation(command => `/bin/${command}`); + }); + + afterEach(() => { + vi.restoreAllMocks(); + fs.rmSync(directory, { recursive: true, force: true }); + }); + + function configure(packageManager: string, root = directory) { + fs.mkdirSync(root, { recursive: true }); + fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ packageManager })); + } + + it('uses pnpm from a parent workspace instead of an available yarn', () => { + configure('pnpm@9.15.0'); + const project = path.join(directory, 'packages', 'subgraph'); + configure('', path.dirname(project)); + + expect(getPackageManagerCommands(project)).toEqual({ + link: 'pnpm link --global @graphprotocol/graph-cli', + install: 'pnpm install', + codegen: 'pnpm run codegen', + deploy: 'pnpm run deploy', + }); + }); + + it('honors an npm declaration even when yarn is installed', () => { + configure('npm@10.0.0'); + expect(getPackageManagerCommands(directory).install).toBe('npm install'); + }); + + it('honors the nearest declaration', () => { + configure('pnpm@9.15.0'); + const project = path.join(directory, 'project'); + configure('yarn@4.0.0', project); + expect(getPackageManagerCommands(project).install).toBe('yarn'); + }); + + it('does not silently fall back when the declared manager is missing', () => { + configure('pnpm@9.15.0'); + vi.mocked(system.which).mockImplementation(command => + command === 'pnpm' ? null : `/bin/${command}`, + ); + expect(() => getPackageManagerCommands(directory)).toThrow(/pnpm.*not found/i); + }); + + it('keeps the existing yarn preference without a declaration', () => { + expect(getPackageManagerCommands(directory).install).toBe('yarn'); + }); + + it('keeps the existing fallback for other package managers', () => { + configure('bun@1.0.0'); + expect(getPackageManagerCommands(directory).install).toBe('yarn'); + }); + + it('falls back to npm without yarn', () => { + vi.mocked(system.which).mockImplementation(command => (command === 'npm' ? '/bin/npm' : null)); + expect(getPackageManagerCommands(directory).install).toBe('npm install'); + }); + + it('supports a pnpm-only installation', () => { + vi.mocked(system.which).mockImplementation(command => + command === 'pnpm' ? '/bin/pnpm' : null, + ); + expect(getPackageManagerCommands(directory).install).toBe('pnpm install'); + }); + + it('does not interpret packageManager content as a shell command', () => { + configure('pnpm@9.15.0; echo unsafe'); + expect(getPackageManagerCommands(directory).install).toBe('pnpm install'); + }); +});