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

Better active directory selection #4246

Merged
merged 9 commits into from Aug 24, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -337,8 +353,12 @@ export function main({
return reporter.close();
};

const cwd = findProjectRoot(commander.cwd);

config
.init({
cwd,

binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
globalFolder: commander.globalFolder,
Expand All @@ -358,7 +378,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