Skip to content

Commit

Permalink
feat(bin): Implement an optional parameter support to yarn bin (#5739)
Browse files Browse the repository at this point in the history
* feat(bin): Implement an optional parameter support to `yarn bin`

This allows to get the path to a binary without making assumptions regarding the layout of the
`node_modules` folder.

BREAKING CHANGE: n/a

n/a

* Adds `yarn bin` tests

* Update en.js

* Update bin.js

* Update bin.js

* Fixes the tests
  • Loading branch information
arcanis authored Apr 26, 2018
1 parent f1aba79 commit 12d8f7f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
26 changes: 26 additions & 0 deletions __tests__/commands/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

import {run as buildRun, runInstall} from './_helpers.js';
import {BufferReporter} from '../../src/reporters/index.js';
import {run} from '../../src/cli/commands/bin.js';

const path = require('path');

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'bin');
const runBin = buildRun.bind(null, BufferReporter, fixturesLoc, (args, flags, config, reporter): Promise<void> => {
return run(config, reporter, flags, args);
});

test('running bin without arguments should return the folder where the binaries are stored', (): Promise<void> => {
return runBin([], {}, '../install/install-production-bin', (config, reporter): ?Promise<void> => {
expect(reporter.getBufferText()).toMatch(/[\\\/]node_modules[\\\/]\.bin[\\\/]?$/);
});
});

test('running bin with a binary name as the argument should return its full path', (): Promise<void> => {
return runInstall({binLinks: true}, 'install-production-bin', async (config): ?Promise<void> => {
const reporter = new BufferReporter();
await run(config, reporter, {}, ['rimraf']);
expect(reporter.getBufferText()).toMatch(/[\\\/]node_modules[\\\/]\.bin[\\\/]rimraf$/);
});
});
13 changes: 12 additions & 1 deletion src/cli/commands/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import RegistryYarn from '../../resolvers/registries/yarn-resolver.js';

const fs = require('fs');
const path = require('path');

export function hasWrapper(commander: Object): boolean {
Expand All @@ -16,6 +17,16 @@ export function setFlags(commander: Object) {

export function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const binFolder = path.join(config.cwd, config.registries[RegistryYarn.registry].folder, '.bin');
reporter.log(binFolder, {force: true});
if (args.length === 0) {
reporter.log(binFolder, {force: true});
} else {
const binName = args[0];
const finalPath = path.normalize(`${binFolder}/${binName}`);
if (fs.existsSync(finalPath)) {
reporter.log(finalPath, {force: true});
} else {
reporter.error(reporter.lang('packageBinaryNotFound', binName));
}
}
return Promise.resolve();
}
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ const messages = {
packageInstalledWithBinaries: 'Installed $0 with binaries:',
packageHasBinaries: '$0 has binaries:',
packageHasNoBinaries: '$0 has no binaries',
packageBinaryNotFound: "Couldn't find a binary named $0",

couldBeDeduped: '$0 could be deduped from $1 to $2',
lockfileNotContainPattern: 'Lockfile does not contain pattern: $0',
Expand Down

0 comments on commit 12d8f7f

Please sign in to comment.