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 6 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
2 changes: 1 addition & 1 deletion __tests__/index.js
Expand Up @@ -243,7 +243,7 @@ if (process.platform !== 'win32') {
}

test.concurrent('should run bin command', async () => {
const stdout = await execCommand('bin', [], '', false);
const stdout = await execCommand('bin', [], '', true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not run in a temp directory, the new behavior makes us go to the repository root. Other tests don't seem to break.

expect(stdout[0]).toEqual(path.join(fixturesLoc, 'node_modules', '.bin'));
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using path.join I'd recommend writing this as path.join(cwd, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i') for cross-platform compatibility. You can even do path.join(cwd, ...Array.from('abcdefghi')) if you wanna look cool :P

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 findActiveDirectory(base): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename this to findProjectRoot

let prev = null;
let dir = base;

do {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified as:

while (prevDir !== currentDir && !fs.existsSync(path.join(currentDir, constants.NODE_PACKAGE_JSON))) {
    prevDir = currentDir;
    currentDir= path.dirname(currentDir);
}

return currentDir;

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 = findActiveDirectory(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