Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"fs-extra": "^10.0.0",
"inquirer": "^8.0.0",
"npm-package-arg": "^9.0.0",
"pacote": "^13.6.0",
"resolve": "^1.10.0",
"semver": "^7.3.2",
"tmp": "0.2.1",
Expand Down
10 changes: 7 additions & 3 deletions src/get-default-blueprint-name-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs-extra');
const path = require('path');
const npm = require('boilerplate-update/src/npm');
const pacote = require('pacote');

/**
* Run npm view to get the package.json and parse it to check if it contains property
Expand All @@ -20,9 +20,13 @@ module.exports = async function getBlueprintNameOverride(packageNameOrPath, cwd
packageJson = JSON.parse(await fs.readFile(localPackageJsonPath));
} else {
try {
packageJson = await npm.json('view', packageNameOrPath);
packageJson = await pacote.manifest(packageNameOrPath, { fullMetadata: true });
} catch (err) {
return null;
if (err.statusCode !== 404) {
throw err;
}

packageJson = {};
}
}

Expand Down
14 changes: 10 additions & 4 deletions test/integration/get-default-blueprint-name-override-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ const { expect } = require('../helpers/chai');
const getBlueprintNameOverride = require('../../src/get-default-blueprint-name-override');

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

let defaultBlueprintOverride = await getBlueprintNameOverride(localPackageFixture);
let defaultBlueprintOverride = await getBlueprintNameOverride(localPackageFixture, __dirname);

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

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

expect(defaultBlueprintOverride).to.be.equal('hello-world');
});

it('missing NPM package returns null', async function() {
let defaultBlueprintOverride = await getBlueprintNameOverride('this-is-hopefully-missing');

expect(defaultBlueprintOverride).to.be.null;
});
});
44 changes: 31 additions & 13 deletions test/unit/get-default-blueprint-name-override-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getBlueprintNameOverride = require('../../src/get-default-blueprint-name-o
const sinon = require('sinon');
const path = require('path');
const fs = require('fs-extra');
const npm = require('boilerplate-update/src/npm');
const pacote = require('pacote');

const cwd = 'a/made/up/path';
const packageNameOrPath = '../a-package-name';
Expand All @@ -16,12 +16,12 @@ const localPackageJsonPath = path.join(path.resolve(cwd, packageNameOrPath), 'pa
describe(getBlueprintNameOverride, function() {
let pathExistsStub;
let readFileStub;
let jsonStub;
let manifestStub;

beforeEach(function() {
pathExistsStub = sinon.stub(fs, 'pathExists');
readFileStub = sinon.stub(fs, 'readFile');
jsonStub = sinon.stub(npm, 'json');
manifestStub = sinon.stub(pacote, 'manifest');
});

afterEach(function() {
Expand All @@ -43,7 +43,7 @@ describe(getBlueprintNameOverride, function() {
expect(defaultBlueprintOverride).to.equal(defaultBlueprint);
});

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

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

await getBlueprintNameOverride(packageNameOrPath, cwd);

expect(jsonStub).to.not.have.been.called;
expect(manifestStub).to.not.have.been.called;
});

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

jsonStub.withArgs('view', packageNameOrPath).resolves({
manifestStub.withArgs(packageNameOrPath).resolves({
name: packageNameOrPath,
'ember-addon': {
defaultBlueprint
Expand All @@ -73,10 +73,10 @@ describe(getBlueprintNameOverride, function() {
expect(defaultBlueprintOverride).to.equal(defaultBlueprint);
});

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

jsonStub.withArgs('view', packageNameOrPath).resolves({
manifestStub.withArgs(packageNameOrPath).resolves({
name: packageNameOrPath,
'ember-addon': {
defaultBlueprint
Expand All @@ -88,7 +88,7 @@ describe(getBlueprintNameOverride, function() {
expect(readFileStub).to.not.have.been.called;
});

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

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

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

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

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

jsonStub.withArgs('view', packageNameOrPath).rejects();
manifestStub.withArgs(packageNameOrPath).rejects({
statusCode: 404
});

let defaultBlueprintOverride = await getBlueprintNameOverride(packageNameOrPath, cwd);

expect(defaultBlueprintOverride).to.be.null;
});

it('doesn\'t swallow all NPM errors', async function() {
pathExistsStub.withArgs(localPackageJsonPath).resolves(false);

let err = {
statusCode: 123
};

manifestStub.withArgs(packageNameOrPath).rejects({
statusCode: 123
});

let promise = getBlueprintNameOverride(packageNameOrPath, cwd);

await expect(promise).to.eventually.be.rejectedWith(err);
});
});