From ba96dc4ca1bb6bfee7f5e49f574ed51abf6a4c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 24 Aug 2017 18:27:17 +0100 Subject: [PATCH] Better active directory selection (#4246) * 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 --- __tests__/index.js | 4 ++-- __tests__/integration.js | 16 ++++++++++++++++ src/cli/index.js | 21 ++++++++++++++++++++- src/constants.js | 1 + 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/__tests__/index.js b/__tests__/index.js index 4d2a46eb4b..0e3b49a60a 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -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); }); diff --git a/__tests__/integration.js b/__tests__/integration.js index dc7cf953ad..683bf6200a 100644 --- a/__tests__/integration.js +++ b/__tests__/integration.js @@ -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(); diff --git a/src/cli/index.js b/src/cli/index.js index 1f893f34e7..24fd9d5217 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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, @@ -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, @@ -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, }) diff --git a/src/constants.js b/src/constants.js index 73f422d9d3..5a08a01b79 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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');