Skip to content

Commit

Permalink
feat(install): ignore other dependencies (#30)
Browse files Browse the repository at this point in the history
Ignore other `dependencies` and `devDependencies` when running `npm install`. Since the era of package-lock files,`install-local ../foo` would both install `../foo` AND the packages listed in package-lock.json. This behavior was an unfortunate side effect.

BREAKING CHANGE: `dependencies` and `devDependencies` will **no longer be installed**. If you want the old behavior, be sure to run `npm install` before you run `install-local`.
  • Loading branch information
nicojs committed Oct 16, 2020
1 parent eda6880 commit b9faaae
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/LocalInstaller.ts
Expand Up @@ -97,7 +97,7 @@ export class LocalInstaller extends EventEmitter {
};
const { stdout, stderr } = await exec(
'npm',
['i', '--no-save', ...toInstall],
['i', '--no-save', '--no-package-lock', ...toInstall],
options,
);
this.emit(
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
@@ -1,6 +1,6 @@
import { execute, Options } from './index';

export function cli(argv: string[]): Promise<void> {
export async function cli(argv: string[]): Promise<void> {
const l = console.log;
const options = new Options(argv);
if (options.help) {
Expand Down Expand Up @@ -44,8 +44,8 @@ export function cli(argv: string[]): Promise<void> {
l(
' install the packages of 2 sibling directories into the current directory and save them to "localDependencies" in your package.json file.',
);
return Promise.resolve();
} else {
return options.validate().then(() => execute(options));
await options.validate();
await execute(options);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Expand Up @@ -20,6 +20,8 @@ export interface PackageJson {
name: string;
version: string;
localDependencies?: Dependencies;
devDependencies?: Dependencies;
dependencies?: Dependencies;
}

export interface Dependencies {
Expand Down
55 changes: 55 additions & 0 deletions test/integration/cli.it.ts
Expand Up @@ -93,6 +93,53 @@ describe('install-local cli given 3 packages', () => {
cwd: packages.one.directory,
});
});

it('should not install additional (dev) dependencies (https://github.com/nicojs/node-install-local/issues/23)', async () => {
// Arrange
packages.one.packageJson.localDependencies = {
two: '../two',
};
packages.one.packageJson.devDependencies = {
typescript: '4.0.3',
};
packages.one.packageJson.dependencies = {
'typed-inject': '3.0.0',
};
packages.one.packageLock = {
name: 'one',
version: '0.0.0',
lockfileVersion: 1,
requires: true,
dependencies: {
'typed-inject': {
version: '3.0.0',
resolved:
'https://registry.npmjs.org/typed-inject/-/typed-inject-3.0.0.tgz',
integrity:
'sha512-LDuyPsk6mO1R0qpe/rm/4u/6pPgT2Fob5T+u2D/wDlORxqlwtG9oWxruTaFZ6L61kzwWGzSp80soc3UUScHmaQ==',
},
typescript: {
version: '4.0.3',
resolved:
'https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz',
integrity:
'sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==',
dev: true,
},
},
};
await packages.one.writePackage();

// Act
await execa.command(`node ${installLocal}`, {
cwd: packages.one.directory,
});

// Assert
const installed = await packages.one.readdir('node_modules');
expect(installed).not.include('typescript');
expect(installed).not.include('typed-inject');
});
});

const rm = (directory: string) =>
Expand All @@ -109,6 +156,7 @@ const rm = (directory: string) =>
class PackageHelper implements Package {
public directory: string;
public packageJson: PackageJson;
public packageLock: Record<string, unknown> | undefined;
constructor(private name: string) {
this.directory = tmpFolder(name);
this.packageJson = {
Expand All @@ -135,6 +183,13 @@ class PackageHelper implements Package {
'utf8',
),
fs.writeFile(path.resolve(this.directory, this.name), '', 'utf8'),
this.packageLock
? fs.writeFile(
path.resolve(this.directory, 'package-lock.json'),
JSON.stringify(this.packageLock, null, 2),
'utf-8',
)
: Promise.resolve(),
]);
}
}
13 changes: 10 additions & 3 deletions test/unit/LocalInstallerSpec.ts
Expand Up @@ -83,12 +83,18 @@ describe('LocalInstaller install', () => {
await sut.install();
expect(helper.execStub).calledWith(
'npm',
['i', '--no-save', tmp('b-0.0.1.tgz'), tmp('c-0.0.2.tgz')],
[
'i',
'--no-save',
'--no-package-lock',
tmp('b-0.0.1.tgz'),
tmp('c-0.0.2.tgz'),
],
{ cwd: resolve('/a'), env: undefined, maxBuffer: TEN_MEGA_BYTE },
);
expect(helper.execStub).calledWith(
'npm',
['i', '--no-save', tmp('e-0.0.4.tgz')],
['i', '--no-save', '--no-package-lock', tmp('e-0.0.4.tgz')],
{ cwd: resolve('d'), env: undefined, maxBuffer: TEN_MEGA_BYTE },
);
});
Expand Down Expand Up @@ -140,6 +146,7 @@ describe('LocalInstaller install', () => {
expect(helper.execStub).calledWith('npm', [
'i',
'--no-save',
'--no-package-lock',
tmp('s-b-0.0.1.tgz'),
]);
});
Expand All @@ -160,7 +167,7 @@ describe('LocalInstaller install', () => {
await sut.install();
expect(helper.execStub).calledWith(
'npm',
['i', '--no-save', tmp('b-0.0.1.tgz')],
['i', '--no-save', '--no-package-lock', tmp('b-0.0.1.tgz')],
{ env: npmEnv, cwd: resolve('/a'), maxBuffer: TEN_MEGA_BYTE },
);
});
Expand Down

0 comments on commit b9faaae

Please sign in to comment.