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

feat(local-install): Add npmEnv option to LocalInstaller #7

Merged
merged 1 commit into from
Mar 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,14 @@ the "dependant" directory located next to the current working directory.
Construct the `LocalInstall` by using an object. The properties of this object are the relative package locations to install into. The array values are the packages to be installed. Use the `install()` method to install, returns a promise.

If you want the progress reporting like the CLI has: use `progress(localInstaller)`;

##### Passing npm env variables

In some cases it might be useful to pass some custom env variables object to npm. For example when you want npm to rebuild native node modules against Electron headers. You can do it by passing `options` to `LocalInstaller`'s constructor.

```javascript
const localInstaller = new LocalInstaller(
{ '.': ['../sibling'] },
{ npmEnv: { envVar: 'envValue' } }
);
```
24 changes: 21 additions & 3 deletions src/LocalInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import * as _ from 'lodash';
import { exec } from 'mz/child_process';
import { exec, ExecOptions } from 'mz/child_process';
import * as fs from 'mz/fs';
import * as os from 'os';
import * as path from 'path';
Expand All @@ -11,17 +11,31 @@ interface PackageByDirectory {
[directory: string]: PackageJson;
}

export interface Env {
[name: string]: string;
}

export interface Options {
npmEnv?: Env;
}

export interface ListByPackage {
[key: string]: string[];
}

export class LocalInstaller extends EventEmitter {

private sourcesByTarget: ListByPackage;
private options: Options;

constructor(sourcesByTarget: ListByPackage) {
constructor(sourcesByTarget: ListByPackage, options?: Options) {
super();
this.sourcesByTarget = resolve(sourcesByTarget);
if (options) {
Copy link
Owner

Choose a reason for hiding this comment

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

Could we use Object.assign here? options = Object.assign({}, options);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, good point

Copy link
Owner

Choose a reason for hiding this comment

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

Object.assign can handle an undefined value. So you can even remove this entire if:

 constructor(sourcesByTarget: ListByPackage, options?: Options) {
         super();
         this.sourcesByTarget = resolve(sourcesByTarget);
         this.options = Object.assign({}, options);
     }

this.options = Object.assign({}, options);
} else {
this.options = {};
}
}

public on(event: 'install_targets_identified', listener: (installTargets: InstallTarget[]) => void): void;
Expand Down Expand Up @@ -61,7 +75,11 @@ export class LocalInstaller extends EventEmitter {

private installOne(target: InstallTarget): Promise<void> {
const toInstall = target.sources.map(source => resolvePackFile(source.packageJson)).join(' ');
return exec(`npm i --no-save ${toInstall}`, { cwd: target.directory }).then(([stdout, stderr]) =>
const options: ExecOptions = { cwd: target.directory };
if (this.options.npmEnv) {
options.env = this.options.npmEnv;
}
return exec(`npm i --no-save ${toInstall}`, options).then(([stdout, stderr]) =>
void this.emit('installed', target.packageJson.name, stdout.toString(), stderr.toString()));
}

Expand Down
15 changes: 15 additions & 0 deletions test/unit/LocalInstallerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ describe('LocalInstaller install', () => {
});
});

describe('with npmEnv', () => {
const npmEnv = { test: 'test', dummy: 'dummy' };
beforeEach(() => {
sut = new LocalInstaller({'/a': ['b']}, { npmEnv });
stubPackageJson({'/a': 'a', 'b': 'b'});
execStub.resolves(['stdout', 'stderr']);
unlinkStub.resolves();
});

it('should call npm with correct env vars', async () => {
await sut.install();
expect(execStub).calledWith(`npm i --no-save ${tmp('b-0.0.1.tgz')}`, { env: npmEnv, cwd: resolve('/a') });
});
});

describe('when readFile errors', () => {
it('should propagate the error', () => {
readFileStub.rejects(new Error('file error'));
Expand Down