Skip to content

Commit

Permalink
feat(auto-install): support pnpm (#1012)
Browse files Browse the repository at this point in the history
* feat(auto-install): support pnpm

* chore: update test

* test: do not snap package-lock.json
  • Loading branch information
antfu authored Oct 19, 2021
1 parent 5cbbba0 commit 61e4a01
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 31 deletions.
5 changes: 3 additions & 2 deletions packages/auto-install/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ export default function autoInstall(opts: RollupAutoInstallOptions = {}): Plugin
// intentionally undocumented options. used for tests
commands: {
npm: 'npm install',
pnpm: 'pnpm install',
yarn: 'yarn add'
},
manager: fs.existsSync('yarn.lock') ? 'yarn' : 'npm',
manager: fs.existsSync('yarn.lock') ? 'yarn' : fs.existsSync('pnpm-lock.yaml') ? 'pnpm' : 'npm',
pkgFile: path.resolve(opts.pkgFile || 'package.json')
};

const options = Object.assign({}, defaults, opts);
const { manager, pkgFile } = options;
const validManagers = ['npm', 'yarn'];
const validManagers = ['npm', 'yarn', 'pnpm'];

if (!validManagers.includes(manager)) {
throw new RangeError(
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-install/test/fixtures/npm/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion packages/auto-install/test/npm-bare.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('npm, bare', async (t) => {
plugins: [autoInstall(), nodeResolve()]
});
t.snapshot(readFileSync('package.json', 'utf-8'));
t.snapshot(readFileSync('package-lock.json', 'utf-8'));
t.truthy(readFileSync('package-lock.json', 'utf-8').includes('"node-noop"'));
});

test.after(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-install/test/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('npm', async (t) => {
},
plugins: [autoInstall({ pkgFile, manager }), nodeResolve()]
});
t.snapshot(readFileSync('package-lock.json', 'utf-8'));
t.snapshot(readFileSync('package.json', 'utf-8'));
});

test.after(async () => {
Expand Down
34 changes: 34 additions & 0 deletions packages/auto-install/test/pnpm-bare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { readFileSync } = require('fs');
const { join } = require('path');

const test = require('ava');
const del = require('del');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { rollup } = require('rollup');

const autoInstall = require('..');

const cwd = join(__dirname, 'fixtures/pnpm-bare');
const file = join(cwd, 'output/bundle.js');
const input = join(cwd, '../input.js');

process.chdir(cwd);

test('pnpm, bare', async (t) => {
t.timeout(50000);
await rollup({
input,
output: {
file,
format: 'cjs'
},
plugins: [autoInstall({ manager: 'pnpm' }), nodeResolve()]
});
const json = JSON.parse(readFileSync('package.json', 'utf-8'));
// snapshots for this are a nightmare cross-platform
t.truthy('node-noop' in json.dependencies);
});

test.after(async () => {
await del(['node_modules', 'package.json', 'pnpm-lock.yaml']);
});
36 changes: 36 additions & 0 deletions packages/auto-install/test/pnpm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { readFileSync, writeFileSync } = require('fs');
const { join } = require('path');

const test = require('ava');
const del = require('del');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { rollup } = require('rollup');

const autoInstall = require('..');

const cwd = join(__dirname, 'fixtures/pnpm');
const file = join(cwd, 'output/bundle.js');
const input = join(cwd, '../input.js');

process.chdir(cwd);

test('pnpm', async (t) => {
t.timeout(50000);
await rollup({
input,
output: {
file,
format: 'cjs'
},
plugins: [autoInstall(), nodeResolve()]
});

const json = JSON.parse(readFileSync('package.json', 'utf-8'));
// snapshots for this are a nightmare cross-platform
t.truthy('node-noop' in json.dependencies);
});

test.after(async () => {
await del(['node_modules', 'package.json']);
writeFileSync('pnpm-lock.yaml', '');
});
17 changes: 1 addition & 16 deletions packages/auto-install/test/snapshots/npm-bare.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The actual snapshot is saved in `npm-bare.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## npm, bare

Expand All @@ -14,18 +14,3 @@ Generated by [AVA](https://ava.li).
}␊
}␊
`

> Snapshot 2
`{␊
"requires": true,␊
"lockfileVersion": 1,␊
"dependencies": {␊
"node-noop": {␊
"version": "1.0.0",␊
"resolved": "https://registry.npmjs.org/node-noop/-/node-noop-1.0.0.tgz",␊
"integrity": "sha1-R6Pn2Az/qmRYNkvSLthcqzMHvnk="␊
}␊
}␊
}␊
`
Binary file modified packages/auto-install/test/snapshots/npm-bare.js.snap
Binary file not shown.
12 changes: 3 additions & 9 deletions packages/auto-install/test/snapshots/npm.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@

The actual snapshot is saved in `npm.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## invalid manager

> Snapshot 1
'\'foo\' is not a valid package manager. Valid managers include: \'npm\', \'yarn\'.'
'\'foo\' is not a valid package manager. Valid managers include: \'npm\', \'yarn\', \'pnpm\'.'

## npm

> Snapshot 1
`{␊
"requires": true,␊
"lockfileVersion": 1,␊
"dependencies": {␊
"node-noop": {␊
"version": "1.0.0",␊
"resolved": "https://registry.npmjs.org/node-noop/-/node-noop-1.0.0.tgz",␊
"integrity": "sha1-R6Pn2Az/qmRYNkvSLthcqzMHvnk="␊
}␊
"node-noop": "^1.0.0"␊
}␊
}␊
`
Binary file modified packages/auto-install/test/snapshots/npm.js.snap
Binary file not shown.
13 changes: 12 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61e4a01

Please sign in to comment.