Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-subgraph-workspace.md
Original file line number Diff line number Diff line change
@@ -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.
50 changes: 50 additions & 0 deletions packages/cli/src/command-helpers/package-manager.ts
Original file line number Diff line number Diff line change
@@ -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`,
};
}
17 changes: 2 additions & 15 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions packages/cli/tests/package-manager.test.ts
Original file line number Diff line number Diff line change
@@ -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('[email protected]');
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('[email protected]');
expect(getPackageManagerCommands(directory).install).toBe('npm install');
});

it('honors the nearest declaration', () => {
configure('[email protected]');
const project = path.join(directory, 'project');
configure('[email protected]', project);
expect(getPackageManagerCommands(project).install).toBe('yarn');
});

it('does not silently fall back when the declared manager is missing', () => {
configure('[email protected]');
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('[email protected]');
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('[email protected]; echo unsafe');
expect(getPackageManagerCommands(directory).install).toBe('pnpm install');
});
});