Skip to content

Commit 7213a30

Browse files
authored
Merge pull request #60 from saucelabs/devx-2388-npm-install
feat: Install npm packages without package.json
2 parents aff2139 + 183d788 commit 7213a30

File tree

6 files changed

+15
-40
lines changed

6 files changed

+15
-40
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Sauce Runner Utils
22

3-
A utilities library used by other Sauce Runners (e.g.: https://github.com/saucelabs/sauce-cypress-runner/) to share commonly used code between them
3+
A utilities library that is shared among Sauce Runners (e.g., https://github.com/saucelabs/sauce-cypress-runner/) to provide commonly used code across different runner implementations.
44

55
## Usage
66

77
```javascript
8-
const sauceRunnerUtils = require('sauce-runner-utils');
9-
sauceRunnerUtils.
8+
const sauceRunnerUtils = require('sauce-testrunner-utils');
9+
10+
sauceRunnerUtils.prepareNpmEnv();
1011
```
1112

1213
## Publishing

src/npm.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawn } from 'child_process';
2-
import { lstat, rename, rm, writeFile } from 'fs/promises';
2+
import { lstat, rename } from 'fs/promises';
33
import { NodeContext } from './types';
44

55
const temporarilyMovedFiles: {[key: string]: string} = {
@@ -34,13 +34,9 @@ export default class NPM {
3434
}
3535
}
3636

37-
public static async install (nodeCtx: NodeContext, pkg: {[key: string]: string}) {
37+
public static async install (nodeCtx: NodeContext, pkgs: string[]) {
3838
await this.renamePackageJson();
39-
await writeFile('package.json', JSON.stringify({
40-
dependencies: pkg,
41-
}));
42-
43-
const p = spawn(nodeCtx.nodePath, [nodeCtx.npmPath, 'install']);
39+
const p = spawn(nodeCtx.nodePath, [nodeCtx.npmPath, 'install', ...pkgs]);
4440
p.stdout.pipe(process.stdout);
4541
p.stderr.pipe(process.stderr);
4642

@@ -52,7 +48,6 @@ export default class NPM {
5248

5349
const exitCode = await exitPromise;
5450

55-
await rm('package.json', { force: true });
5651
await this.restorePackageJson();
5752

5853
return exitCode;

src/types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
export type Suite = {
42
name: string;
53
env?: { [key: string]: string };

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function setUpNpmConfig (nodeCtx: NodeContext, userConfig: NpmConfi
6060
export async function installNpmDependencies (nodeCtx: NodeContext, packageList: {[key:string]: string}) {
6161
const packages = Object.entries(packageList).map(([k, v]) => (`${k}@${v}`));
6262
console.log(`\nInstalling packages: ${packages.join(' ')}`);
63-
await npm.install(nodeCtx, packageList);
63+
await npm.install(nodeCtx, packages);
6464
}
6565

6666
export async function rebuildNpmDependencies (nodeCtx: NodeContext, path: string) {

tests/unit/src/__snapshots__/utils.spec.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ Array [
6363
"nodePath": "node-bin",
6464
"npmPath": "npm-bin",
6565
},
66-
Object {
67-
"mypackage": "1.2.3",
68-
},
66+
Array [
67+
"mypackage@1.2.3",
68+
],
6969
]
7070
`;
7171

tests/unit/src/npm.spec.ts

+4-23
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,10 @@ describe('NPM', function () {
4040
it('.install must invoke npm install', async function () {
4141
const interceptor = spawk.spawn(nodeCtx.nodePath).stdout('npm runned').exit(0);
4242
fsMocked.lstat.mockRejectedValue('non-existing');
43-
let writeFile, writeContent;
44-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45-
fsMocked.writeFile.mockImplementation(function (name: any, data: any): Promise<void> {
46-
writeFile = name;
47-
writeContent = data;
48-
return new Promise((resolve) => {
49-
resolve();
50-
});
51-
});
52-
await NPM.install(nodeCtx, {
53-
cypress: '12.6.0'
54-
});
43+
await NPM.install(nodeCtx, ['cypress@12.6.0']);
5544
expect(interceptor.calledWith.command).toEqual(nodeCtx.nodePath);
56-
expect(interceptor.calledWith.args).toEqual([nodeCtx.npmPath, 'install']);
45+
expect(interceptor.calledWith.args).toEqual([nodeCtx.npmPath, 'install', 'cypress@12.6.0']);
5746
expect(fsMocked.lstat).toBeCalledTimes(4);
58-
expect(fsMocked.writeFile).toBeCalledTimes(1);
59-
expect(writeFile).toEqual('package.json');
60-
expect(writeContent).toEqual(JSON.stringify({dependencies: { cypress: '12.6.0' }}));
6147
});
6248

6349
it('.install moves package.json / package-lock.json', async function () {
@@ -68,11 +54,9 @@ describe('NPM', function () {
6854
fsMocked.rename.mockResolvedValue();
6955
fsMocked.writeFile.mockResolvedValue();
7056

71-
await NPM.install(nodeCtx, {
72-
cypress: '12.6.0'
73-
});
57+
await NPM.install(nodeCtx, ['cypress@12.6.0']);
7458
expect(interceptor.calledWith.command).toEqual(nodeCtx.nodePath);
75-
expect(interceptor.calledWith.args).toEqual([nodeCtx.npmPath, 'install']);
59+
expect(interceptor.calledWith.args).toEqual([nodeCtx.npmPath, 'install', 'cypress@12.6.0']);
7660
expect(fsMocked.rename.mock.calls).toEqual([
7761
['package.json', `.package.json-${process.pid}`],
7862
['package-lock.json', `.package-lock.json-${process.pid}`],
@@ -85,8 +69,5 @@ describe('NPM', function () {
8569
[`.package.json-${process.pid}`],
8670
[`.package-lock.json-${process.pid}`],
8771
]);
88-
expect(fsMocked.writeFile.mock.calls).toEqual([
89-
['package.json', JSON.stringify({dependencies: { cypress: '12.6.0' }})],
90-
]);
9172
});
9273
});

0 commit comments

Comments
 (0)