Skip to content

Commit

Permalink
Better active directory selection (#4246)
Browse files Browse the repository at this point in the history
* Automatically backtrack to the closest package.json directory

* Add a test

* Prettier

* Fixes linting

* Executes a test in a temp directory to not get affected by the change

* Removes now useless await

* Feedbacks

* Update index.js

* Update index.js
  • Loading branch information
arcanis committed Aug 24, 2017
1 parent 6bab5cc commit ba96dc4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions __tests__/index.js
Expand Up @@ -243,8 +243,8 @@ if (process.platform !== 'win32') {
}

test.concurrent('should run bin command', async () => {
const stdout = await execCommand('bin', [], '', false);
expect(stdout[0]).toEqual(path.join(fixturesLoc, 'node_modules', '.bin'));
const stdout = await execCommand('bin', [], '', true);
expect(stdout[0]).toMatch(/[\\\/]node_modules[\\\/]\.bin\n?$/);
expect(stdout.length).toEqual(1);
});

Expand Down
16 changes: 16 additions & 0 deletions __tests__/integration.js
Expand Up @@ -82,6 +82,22 @@ test('--mutex network', async () => {
]);
});

test('--cwd option', async () => {
const cwd = await makeTemp();
const cacheFolder = path.join(cwd, '.cache');

const subdir = path.join(cwd, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
await fs.mkdirp(subdir);

const packageJsonPath = path.join(cwd, 'package.json');
await fs.writeFile(packageJsonPath, JSON.stringify({}));

await runYarn(['add', 'left-pad'], {cwd: subdir});

const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
expect(packageJson.dependencies['left-pad']).toBeDefined();
});

test('yarnrc binary path (js)', async () => {
const cwd = await makeTemp();

Expand Down
21 changes: 20 additions & 1 deletion src/cli/index.js
Expand Up @@ -21,6 +21,22 @@ const net = require('net');
const onDeath = require('death');
const path = require('path');

function findProjectRoot(base: string): string {
let prev = null;
let dir = base;

do {
if (fs.existsSync(path.join(dir, constants.NODE_PACKAGE_JSON))) {
return dir;
}

prev = dir;
dir = path.dirname(dir);
} while (dir !== prev);

return base;
}

export function main({
startArgs,
args,
Expand Down Expand Up @@ -338,8 +354,12 @@ export function main({
return reporter.close();
};

const cwd = findProjectRoot(commander.cwd);

config
.init({
cwd,

binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
linkFolder: commander.linkFolder,
Expand All @@ -360,7 +380,6 @@ export function main({
networkTimeout: commander.networkTimeout,
nonInteractive: commander.nonInteractive,
scriptsPrependNodePath: commander.scriptsPrependNodePath,
cwd: commander.cwd,

commandName: commandName === 'run' ? commander.args[0] : commandName,
})
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Expand Up @@ -67,6 +67,7 @@ export const LINK_REGISTRY_DIRECTORY = path.join(CONFIG_DIRECTORY, 'link');
export const GLOBAL_MODULE_DIRECTORY = path.join(CONFIG_DIRECTORY, 'global');

export const NODE_MODULES_FOLDER = 'node_modules';
export const NODE_PACKAGE_JSON = 'package.json';

export const POSIX_GLOBAL_PREFIX = '/usr/local';
export const FALLBACK_GLOBAL_PREFIX = path.join(userHome, '.yarn');
Expand Down

0 comments on commit ba96dc4

Please sign in to comment.