-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: internalizing
findBinPath
utility (#1679)
- Loading branch information
Showing
13 changed files
with
144 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './cli-utils/findBinPath'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.