From 1638d06f5c4ac94e980b3d044fbc67e50a7b6543 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 26 Apr 2017 16:12:26 +0100 Subject: [PATCH] Add .yarnrc env config option (#3218) * add .yarnrc env config option * inherit existing env config --- .../lifecycle-scripts/yarnrc-env/.yarnrc | 3 +++ .../yarnrc-env/log-command.js | 1 + .../yarnrc-env/nested/.yarnrc | 2 ++ .../yarnrc-env/nested/log-command.js | 2 ++ .../yarnrc-env/nested/package.json | 9 +++++++ .../lifecycle-scripts/yarnrc-env/package.json | 9 +++++++ __tests__/lifecycle-scripts.js | 26 ++++++++++++++++++- src/registries/yarn-registry.js | 9 +++++++ src/util/execute-lifecycle-script.js | 6 +++++ 9 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/.yarnrc create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/log-command.js create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/.yarnrc create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/log-command.js create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/package.json create mode 100644 __tests__/fixtures/lifecycle-scripts/yarnrc-env/package.json diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/.yarnrc b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/.yarnrc new file mode 100644 index 0000000000..e26e73960c --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/.yarnrc @@ -0,0 +1,3 @@ +env: + FOO "BAR" + BAR "FOO" diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/log-command.js b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/log-command.js new file mode 100644 index 0000000000..d375f0d17d --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/log-command.js @@ -0,0 +1 @@ +console.log(process.env.FOO); diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/.yarnrc b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/.yarnrc new file mode 100644 index 0000000000..77e3fd7a55 --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/.yarnrc @@ -0,0 +1,2 @@ +env: + FOO "RAB" diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/log-command.js b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/log-command.js new file mode 100644 index 0000000000..c4d405967e --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/log-command.js @@ -0,0 +1,2 @@ +console.log(process.env.FOO); +console.log(process.env.BAR); diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/package.json b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/package.json new file mode 100644 index 0000000000..eee7725406 --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/nested/package.json @@ -0,0 +1,9 @@ +{ + "name": "yarn-config-env", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "preinstall" : "node log-command.js" + } +} diff --git a/__tests__/fixtures/lifecycle-scripts/yarnrc-env/package.json b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/package.json new file mode 100644 index 0000000000..eee7725406 --- /dev/null +++ b/__tests__/fixtures/lifecycle-scripts/yarnrc-env/package.json @@ -0,0 +1,9 @@ +{ + "name": "yarn-config-env", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "preinstall" : "node log-command.js" + } +} diff --git a/__tests__/lifecycle-scripts.js b/__tests__/lifecycle-scripts.js index d7e900b903..ab5fafe5c9 100644 --- a/__tests__/lifecycle-scripts.js +++ b/__tests__/lifecycle-scripts.js @@ -19,7 +19,6 @@ async function execCommand(cmd: string, packageName: string, env = process.env): await fs.copy(srcPackageDir, packageDir, new NoopReporter()); return new Promise((resolve, reject) => { - exec(`node "${yarnBin}" ${cmd}`, {cwd:packageDir, env}, (err, stdout) => { if (err) { reject(err); @@ -112,3 +111,28 @@ test('should run both prepublish and prepare when installing, but not prepublish expect(stdout).not.toMatch(/^running the prepublishOnly hook$/m); }); + +test('should allow setting environment variables via yarnrc', async () => { + const stdout = await execCommand('install', 'yarnrc-env'); + expect(stdout).toMatch(/^BAR$/m); +}); + +test('should inherit existing environment variables when setting via yarnrc', async () => { + const srcPackageDir = path.join(fixturesLoc, 'yarnrc-env'); + const packageDir = await makeTemp('yarnrc-env-nested'); + + await fs.copy(srcPackageDir, packageDir, new NoopReporter()); + + const stdout = await new Promise((resolve, reject) => { + exec(`node "${yarnBin}" install`, {cwd:path.join(packageDir, 'nested')}, (err, stdout) => { + if (err) { + reject(err); + } else { + resolve(stdout.toString()); + } + }); + }); + + expect(stdout).toMatch(/^RAB$/m); + expect(stdout).toMatch(/^FOO$/m); +}); diff --git a/src/registries/yarn-registry.js b/src/registries/yarn-registry.js index d000c0ead6..7510bb0438 100644 --- a/src/registries/yarn-registry.js +++ b/src/registries/yarn-registry.js @@ -92,6 +92,15 @@ export default class YarnRegistry extends NpmRegistry { await fs.mkdirp(mirrorLoc); } + // merge with any existing environment variables + const env = config.env; + if (env) { + const existingEnv = this.config.env; + if (existingEnv) { + this.config.env = Object.assign({}, env, existingEnv); + } + } + this.config = Object.assign({}, config, this.config); } diff --git a/src/util/execute-lifecycle-script.js b/src/util/execute-lifecycle-script.js index 124e92e4b4..e3e7caf4da 100644 --- a/src/util/execute-lifecycle-script.js +++ b/src/util/execute-lifecycle-script.js @@ -33,6 +33,12 @@ async function makeEnv(stage: string, cwd: string, config: Config): { } { const env = Object.assign({}, process.env); + // Merge in the `env` object specified in .yarnrc + const customEnv = config.getOption('env'); + if (customEnv && typeof customEnv === 'object') { + Object.assign(env, customEnv); + } + env.npm_lifecycle_event = stage; env.npm_node_execpath = env.NODE || process.execPath; env.npm_execpath = env.npm_execpath || process.mainModule.filename;