Skip to content

Commit

Permalink
fix: bin path for fuels-forc in fuels CLI, add support bun's struct…
Browse files Browse the repository at this point in the history
…ure (#1481)

* fix: bin path for `fuels-forc`, add support bun's structure

* add changeset
  • Loading branch information
Dhaiwat10 authored Dec 6, 2023
1 parent e0efea7 commit 014c27f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-phones-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

fix: bin path for `fuels-forc`, add support bun's structure
9 changes: 3 additions & 6 deletions packages/fuels/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { configureCliOptions as configureTypegenCliOptions } from '@fuel-ts/abi-
import { versions } from '@fuel-ts/versions';
import { runVersions } from '@fuel-ts/versions/cli';
import { Command, Option } from 'commander';
import { join } from 'path';

import { build } from './cli/commands/build';
import { deploy } from './cli/commands/deploy';
Expand All @@ -11,7 +10,7 @@ import { init } from './cli/commands/init';
import { withConfig } from './cli/commands/withConfig';
import { withProgram } from './cli/commands/withProgram';
import { Commands } from './cli/types';
import { findPackageRoot } from './cli/utils/findPackageRoot';
import { findBinPath } from './cli/utils/findBinPath';
import { configureLogging } from './cli/utils/logger';

export const onPreAction = (command: Command) => {
Expand Down Expand Up @@ -100,14 +99,12 @@ export const configureCli = () => {
* Binary wrappers
*/

const binDir = join(findPackageRoot(), 'node_modules', '.bin');

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

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

return program;
Expand Down
6 changes: 2 additions & 4 deletions packages/fuels/src/cli/commands/build/buildSwayPrograms.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { spawn } from 'child_process';
import { join } from 'path';

import type { FuelsConfig } from '../../types';
import { findPackageRoot } from '../../utils/findPackageRoot';
import { findBinPath } from '../../utils/findBinPath';
import { getBinarySource } from '../../utils/getBinarySource';
import { debug, error, log, loggingConfig } from '../../utils/logger';

Expand All @@ -28,8 +27,7 @@ export const buildSwayProgram = async (config: FuelsConfig, path: string) => {
debug('Building Sway program', path);

return new Promise<void>((resolve, reject) => {
const pkgRootDir = findPackageRoot();
const builtInForcPath = join(pkgRootDir, 'node_modules', '.bin', 'fuels-forc');
const builtInForcPath = findBinPath('fuels-forc');

const command = config.useBuiltinForc ? builtInForcPath : 'forc';
const forc = spawn(command, ['build', '-p', path], { stdio: 'pipe' });
Expand Down
7 changes: 2 additions & 5 deletions packages/fuels/src/cli/commands/dev/startFuelCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { getPortPromise } from 'portfinder';
import treeKill from 'tree-kill';

import type { FuelsConfig } from '../../types';
import { findBinPath } from '../../utils/findBinPath';
import { getBinarySource } from '../../utils/getBinarySource';
import { error, log, loggingConfig } from '../../utils/logger';

import { defaultChainConfig, defaultConsensusKey } from './defaultChainConfig';

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

export type FuelCoreNode = {
bindIp: string;
accessIp: string;
Expand Down Expand Up @@ -75,8 +73,7 @@ export const startFuelCore = async (config: FuelsConfig): Promise<FuelCoreNode>
].flat();

return new Promise((resolve, reject) => {
// This line finds the path to the built-in fuels-core binary
const builtInFuelsCorePath = npmWhich.sync('fuels-core');
const builtInFuelsCorePath = findBinPath('fuels-core');

const command = config.useBuiltinFuelCore ? builtInFuelsCorePath : 'fuel-core';
const core = spawn(command, flags, { stdio: 'pipe' });
Expand Down
16 changes: 16 additions & 0 deletions packages/fuels/src/cli/utils/findBinPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { existsSync } from 'fs';
import { join } from 'path';

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

export function 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 = join('node_modules', '.bin', binCommandName);
}

return binPath;
}
9 changes: 0 additions & 9 deletions packages/fuels/src/cli/utils/findPackageRoot.ts

This file was deleted.

0 comments on commit 014c27f

Please sign in to comment.