Skip to content

Commit

Permalink
Add .yarnrc env config option (yarnpkg#3218)
Browse files Browse the repository at this point in the history
* add .yarnrc env config option

* inherit existing env config
  • Loading branch information
Sebastian McKenzie authored and bestander committed Apr 26, 2017
1 parent 08e1e6b commit 1638d06
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions __tests__/fixtures/lifecycle-scripts/yarnrc-env/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
env:
FOO "BAR"
BAR "FOO"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(process.env.FOO);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env:
FOO "RAB"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(process.env.FOO);
console.log(process.env.BAR);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "yarn-config-env",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"preinstall" : "node log-command.js"
}
}
9 changes: 9 additions & 0 deletions __tests__/fixtures/lifecycle-scripts/yarnrc-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "yarn-config-env",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"preinstall" : "node log-command.js"
}
}
26 changes: 25 additions & 1 deletion __tests__/lifecycle-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
9 changes: 9 additions & 0 deletions src/registries/yarn-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions src/util/execute-lifecycle-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1638d06

Please sign in to comment.