Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: internalizing findBinPath utility #1679

Merged
merged 18 commits into from
Jan 24, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/gorgeous-tips-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-fuels": patch
"fuels": patch
"@fuel-ts/utils": patch
---

Fixing and internalizing `findBinPath` utility
1 change: 0 additions & 1 deletion packages/fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"handlebars": "^4.7.7",
"joycon": "^3.1.1",
"lodash.camelcase": "^4.3.0",
"npm-which": "^3.0.1",
"portfinder": "^1.0.32",
"rimraf": "^3.0.2",
"toml": "^3.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/fuels/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { configureCliOptions as configureTypegenCliOptions } from '@fuel-ts/abi-typegen/cli';
import { findBinPath } from '@fuel-ts/utils/cli-utils';
import { versions } from '@fuel-ts/versions';
import { runVersions } from '@fuel-ts/versions/cli';
import { findBinPath } from '@fuel-ts/wallet/test-utils';
import { Command, Option } from 'commander';

import { build } from './cli/commands/build';
Expand Down Expand Up @@ -100,11 +100,11 @@ export const configureCli = () => {
*/

program.command('core', 'Wrapper around Fuel Core binary', {
executableFile: findBinPath('fuels-core'),
executableFile: findBinPath('fuels-core', __dirname),
});

program.command('forc', 'Wrapper around Forc binary', {
executableFile: findBinPath('fuels-forc'),
executableFile: findBinPath('fuels-forc', __dirname),
});

return program;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as findBinPathMod from '@fuel-ts/wallet/test-utils';
import * as findBinPathMod from '@fuel-ts/utils/cli-utils';
import * as childProcessMod from 'child_process';

import { fuelsConfig } from '../../../../test/fixtures/fuels.config';
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels/src/cli/commands/build/buildSwayProgram.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findBinPath } from '@fuel-ts/wallet/test-utils';
import { findBinPath } from '@fuel-ts/utils/cli-utils';
import { spawn } from 'child_process';

import type { FuelsConfig } from '../../types';
Expand All @@ -10,7 +10,7 @@ export const buildSwayProgram = async (config: FuelsConfig, path: string) => {
debug('Building Sway program', path);

return new Promise<void>((resolve, reject) => {
const builtInForcPath = findBinPath('fuels-forc');
const builtInForcPath = findBinPath('fuels-forc', __dirname);

const command = config.useBuiltinForc ? builtInForcPath : 'forc';
const forc = spawn(command, ['build', '-p', path], { stdio: 'pipe' });
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
"require": "./dist/test-utils.js",
"import": "./dist/test-utils.mjs",
"types": "./dist/test-utils.d.ts"
},
"./cli-utils": {
"require": "./dist/cli-utils.js",
"import": "./dist/cli-utils.mjs",
"types": "./dist/cli-utils.d.ts"
}
},
"typesVersions": {
"*": {
"test-utils": [
"./dist/test-utils.d.ts"
],
"cli-utils": [
"./dist/cli-utils.d.ts"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/cli-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cli-utils/findBinPath';
57 changes: 57 additions & 0 deletions packages/utils/src/cli-utils/findBinPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { safeExec } from '@fuel-ts/errors/test-utils';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { join } from 'path';

import { findBinPath } from './findBinPath';

/**
* @group node
*/
describe('findBinPath', () => {
const bootstrap = (dir: string) => {
const cmdName = 'my-cmd';
const mods = join(dir, 'node_modules');
const bin = join(mods, '.bin');
const cmdPath = join(bin, cmdName);

const resetDisk = () => rmSync(mods, { recursive: true });

mkdirSync(bin, { recursive: true });
writeFileSync(cmdPath, '');

return { resetDisk, mods, cmdName, cmdPath };
};

it('should find bin path in current dir', () => {
const base = __dirname; // current dir
const { cmdName, cmdPath, resetDisk } = bootstrap(base);
const binPath = findBinPath(cmdName, base);

resetDisk();
expect(binPath).toEqual(cmdPath);
});

it('should find bin path one dir up', () => {
const base = join(__dirname, '..'); // one dir up
const { cmdName, cmdPath, resetDisk } = bootstrap(base);
const binPath = findBinPath(cmdName, base);

resetDisk();
expect(binPath).toEqual(cmdPath);
});

it('should find bin path two dir up', () => {
const base = join(__dirname, '..', '..'); // two dirs up
const { cmdName, cmdPath, resetDisk } = bootstrap(base);
const binPath = findBinPath(cmdName, base);

resetDisk();
expect(binPath).toEqual(cmdPath);
});

it('should throw for bin path not found', async () => {
const cmdName = 'non-existent';
const { error } = await safeExec(() => findBinPath(cmdName, __dirname));
expect(error?.message).toEqual(`Command not found: ${cmdName}`);
});
});
17 changes: 17 additions & 0 deletions packages/utils/src/cli-utils/findBinPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { existsSync } from 'fs';
import { join } from 'path';

export const findBinPath = (binCommandName: string, startingDir: string): string => {
const cmdPath = join(startingDir, 'node_modules', '.bin', binCommandName);
const parentDir = join(startingDir, '..');

if (existsSync(cmdPath)) {
return cmdPath;
}

if (parentDir === startingDir) {
throw new Error(`Command not found: ${binCommandName}`);
}

return findBinPath(binCommandName, parentDir);
};
1 change: 1 addition & 0 deletions packages/utils/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const configs: Options = {
entry: {
index: 'src/index.ts',
'test-utils': 'src/test-utils.ts',
'cli-utils': 'src/cli-utils.ts',
},
};

Expand Down
1 change: 0 additions & 1 deletion packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@fuel-ts/utils": "workspace:*",
"@fuels/vm-asm": "0.42.1",
"ethers": "^6.7.1",
"npm-which": "^3.0.1",
"portfinder": "^1.0.32",
"tree-kill": "^1.2.2",
"uuid": "^9.0.0"
Expand Down
17 changes: 2 additions & 15 deletions packages/wallet/src/test-utils/launchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { toHex } from '@fuel-ts/math';
import { Provider } from '@fuel-ts/providers';
import { Signer } from '@fuel-ts/signer';
import { defaultChainConfig, defaultConsensusKey } from '@fuel-ts/utils';
import { findBinPath } from '@fuel-ts/utils/cli-utils';
import type { ChildProcessWithoutNullStreams } from 'child_process';
import { spawn } from 'child_process';
import { randomUUID } from 'crypto';
Expand All @@ -17,9 +18,6 @@ import type { WalletUnlocked } from '../wallets';

import { generateTestWallet } from './generateTestWallet';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const npmWhich = require('npm-which')(__dirname);

const getFlagValueFromArgs = (args: string[], flag: string) => {
const flagIndex = args.indexOf(flag);
if (flagIndex === -1) {
Expand Down Expand Up @@ -65,17 +63,6 @@ export type KillNodeParams = {
};
};

export const findBinPath = (binCommandName: string) => {
let binPath = npmWhich.sync(binCommandName);

if (!existsSync(binPath)) {
// The user might be using bun, which has a different structure for binaries inside node_modules
binPath = path.join('node_modules', '.bin', binCommandName);
}

return binPath;
};

export const killNode = (params: KillNodeParams) => {
const { child, configPath, state, killFn } = params;
if (!state.isDead) {
Expand Down Expand Up @@ -138,7 +125,7 @@ export const launchNode = async ({
// This string is logged by the client when the node has successfully started. We use it to know when to resolve.
const graphQLStartSubstring = 'Binding GraphQL provider to';

const binPath = await findBinPath('fuels-core');
const binPath = findBinPath('fuels-core', __dirname);

const command = useSystemFuelCore ? 'fuel-core' : binPath;

Expand Down
71 changes: 45 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading