Skip to content

Commit dde5f88

Browse files
authored
Merge pull request #1211 from ember-cli/use_pacote
use pacote instead of npm cli
2 parents 57838be + 274703e commit dde5f88

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"fs-extra": "^10.0.0",
4545
"inquirer": "^8.0.0",
4646
"npm-package-arg": "^9.0.0",
47+
"pacote": "^13.6.0",
4748
"resolve": "^1.10.0",
4849
"semver": "^7.3.2",
4950
"tmp": "0.2.1",

src/get-default-blueprint-name-override.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5-
const npm = require('boilerplate-update/src/npm');
5+
const pacote = require('pacote');
66

77
/**
88
* Run npm view to get the package.json and parse it to check if it contains property
@@ -20,9 +20,13 @@ module.exports = async function getBlueprintNameOverride(packageNameOrPath, cwd
2020
packageJson = JSON.parse(await fs.readFile(localPackageJsonPath));
2121
} else {
2222
try {
23-
packageJson = await npm.json('view', packageNameOrPath);
23+
packageJson = await pacote.manifest(packageNameOrPath, { fullMetadata: true });
2424
} catch (err) {
25-
return null;
25+
if (err.statusCode !== 404) {
26+
throw err;
27+
}
28+
29+
packageJson = {};
2630
}
2731
}
2832

test/integration/get-default-blueprint-name-override-test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ const { expect } = require('../helpers/chai');
55
const getBlueprintNameOverride = require('../../src/get-default-blueprint-name-override');
66

77
describe(getBlueprintNameOverride, function() {
8-
it('Local package with nondefault returns expected value', async function() {
9-
let localPackageFixture = 'test/fixtures/blueprint/addon/default-blueprint-different-than-name/v0.0.1';
8+
it('local package with non-default returns expected value', async function() {
9+
let localPackageFixture = '../fixtures/blueprint/addon/default-blueprint-different-than-name/v0.0.1';
1010

11-
let defaultBlueprintOverride = await getBlueprintNameOverride(localPackageFixture);
11+
let defaultBlueprintOverride = await getBlueprintNameOverride(localPackageFixture, __dirname);
1212

1313
expect(defaultBlueprintOverride).to.be.equal('custom-blueprint');
1414
});
1515

16-
it('NPM package with nondefault returns expected value', async function() {
16+
it('NPM package with non-default returns expected value', async function() {
1717
let defaultBlueprintOverride = await getBlueprintNameOverride('ember-cli-update-default-blueprint-override-test');
1818

1919
expect(defaultBlueprintOverride).to.be.equal('hello-world');
2020
});
21+
22+
it('missing NPM package returns null', async function() {
23+
let defaultBlueprintOverride = await getBlueprintNameOverride('this-is-hopefully-missing');
24+
25+
expect(defaultBlueprintOverride).to.be.null;
26+
});
2127
});

test/unit/get-default-blueprint-name-override-test.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getBlueprintNameOverride = require('../../src/get-default-blueprint-name-o
66
const sinon = require('sinon');
77
const path = require('path');
88
const fs = require('fs-extra');
9-
const npm = require('boilerplate-update/src/npm');
9+
const pacote = require('pacote');
1010

1111
const cwd = 'a/made/up/path';
1212
const packageNameOrPath = '../a-package-name';
@@ -16,12 +16,12 @@ const localPackageJsonPath = path.join(path.resolve(cwd, packageNameOrPath), 'pa
1616
describe(getBlueprintNameOverride, function() {
1717
let pathExistsStub;
1818
let readFileStub;
19-
let jsonStub;
19+
let manifestStub;
2020

2121
beforeEach(function() {
2222
pathExistsStub = sinon.stub(fs, 'pathExists');
2323
readFileStub = sinon.stub(fs, 'readFile');
24-
jsonStub = sinon.stub(npm, 'json');
24+
manifestStub = sinon.stub(pacote, 'manifest');
2525
});
2626

2727
afterEach(function() {
@@ -43,7 +43,7 @@ describe(getBlueprintNameOverride, function() {
4343
expect(defaultBlueprintOverride).to.equal(defaultBlueprint);
4444
});
4545

46-
it('doesn\'t use npm if found locally', async function() {
46+
it('doesn\'t use NPM if found locally', async function() {
4747
pathExistsStub.withArgs(localPackageJsonPath).resolves(true);
4848

4949
readFileStub.withArgs(localPackageJsonPath).resolves(JSON.stringify({
@@ -55,13 +55,13 @@ describe(getBlueprintNameOverride, function() {
5555

5656
await getBlueprintNameOverride(packageNameOrPath, cwd);
5757

58-
expect(jsonStub).to.not.have.been.called;
58+
expect(manifestStub).to.not.have.been.called;
5959
});
6060

61-
it('uses npm if not found locally', async function() {
61+
it('uses NPM if not found locally', async function() {
6262
pathExistsStub.withArgs(localPackageJsonPath).resolves(false);
6363

64-
jsonStub.withArgs('view', packageNameOrPath).resolves({
64+
manifestStub.withArgs(packageNameOrPath).resolves({
6565
name: packageNameOrPath,
6666
'ember-addon': {
6767
defaultBlueprint
@@ -73,10 +73,10 @@ describe(getBlueprintNameOverride, function() {
7373
expect(defaultBlueprintOverride).to.equal(defaultBlueprint);
7474
});
7575

76-
it('doesn\'t read local file if using npm', async function() {
76+
it('doesn\'t read local file if using NPM', async function() {
7777
pathExistsStub.withArgs(localPackageJsonPath).resolves(false);
7878

79-
jsonStub.withArgs('view', packageNameOrPath).resolves({
79+
manifestStub.withArgs(packageNameOrPath).resolves({
8080
name: packageNameOrPath,
8181
'ember-addon': {
8282
defaultBlueprint
@@ -88,7 +88,7 @@ describe(getBlueprintNameOverride, function() {
8888
expect(readFileStub).to.not.have.been.called;
8989
});
9090

91-
it('Null if ember-addon does not exist in package.json', async function() {
91+
it('null if ember-addon does not exist in package.json', async function() {
9292
pathExistsStub.withArgs(localPackageJsonPath).resolves(true);
9393

9494
readFileStub.withArgs(localPackageJsonPath).resolves(JSON.stringify({
@@ -100,7 +100,7 @@ describe(getBlueprintNameOverride, function() {
100100
expect(defaultBlueprintOverride).to.be.null;
101101
});
102102

103-
it('Null if defaultBlueprint does not exist in ember-addon', async function() {
103+
it('null if defaultBlueprint does not exist in ember-addon', async function() {
104104
pathExistsStub.withArgs(localPackageJsonPath).resolves(true);
105105

106106
readFileStub.withArgs(localPackageJsonPath).resolves(JSON.stringify({
@@ -113,13 +113,31 @@ describe(getBlueprintNameOverride, function() {
113113
expect(defaultBlueprintOverride).to.be.null;
114114
});
115115

116-
it('Error in spawn returns null', async function() {
116+
it('missing NPM package returns null', async function() {
117117
pathExistsStub.withArgs(localPackageJsonPath).resolves(false);
118118

119-
jsonStub.withArgs('view', packageNameOrPath).rejects();
119+
manifestStub.withArgs(packageNameOrPath).rejects({
120+
statusCode: 404
121+
});
120122

121123
let defaultBlueprintOverride = await getBlueprintNameOverride(packageNameOrPath, cwd);
122124

123125
expect(defaultBlueprintOverride).to.be.null;
124126
});
127+
128+
it('doesn\'t swallow all NPM errors', async function() {
129+
pathExistsStub.withArgs(localPackageJsonPath).resolves(false);
130+
131+
let err = {
132+
statusCode: 123
133+
};
134+
135+
manifestStub.withArgs(packageNameOrPath).rejects({
136+
statusCode: 123
137+
});
138+
139+
let promise = getBlueprintNameOverride(packageNameOrPath, cwd);
140+
141+
await expect(promise).to.eventually.be.rejectedWith(err);
142+
});
125143
});

0 commit comments

Comments
 (0)