Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for blueprint updates #645

Merged
merged 1 commit into from
Aug 24, 2019
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
29 changes: 29 additions & 0 deletions src/check-for-blueprint-updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const parseBlueprint = require('./parse-blueprint');
const downloadBlueprint = require('./download-blueprint');

const toDefault = require('./args').to.default;

async function checkForBlueprintUpdates(blueprints) {
return await Promise.all(blueprints.map(async blueprint => {
let parsedBlueprint = await parseBlueprint(blueprint.location || blueprint.name);

let [
{ version: currentVersion },
{ version: latestVersion }
] = await Promise.all([
downloadBlueprint(parsedBlueprint.name, parsedBlueprint.url, blueprint.version),
downloadBlueprint(parsedBlueprint.name, parsedBlueprint.url, toDefault)
]);

return {
name: blueprint.name,
currentVersion,
latestVersion,
isUpToDate: currentVersion === latestVersion
};
}));
}

module.exports = checkForBlueprintUpdates;
43 changes: 34 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const loadBlueprintFile = require('./load-blueprint-file');
const loadSafeBlueprintFile = require('./load-safe-blueprint-file');
const saveBlueprint = require('./save-blueprint');
const saveDefaultBlueprint = require('./save-default-blueprint');
const checkForBlueprintUpdates = require('./check-for-blueprint-updates');

function formatBlueprintLine(blueprint) {
return `${blueprint.name}, current: ${blueprint.currentVersion}, latest: ${blueprint.latestVersion}`;
}

module.exports = async function emberCliUpdate({
blueprint: _blueprint,
Expand Down Expand Up @@ -62,20 +67,40 @@ module.exports = async function emberCliUpdate({
let { blueprints } = emberCliUpdateJson;

if (!blueprints.length) {
blueprints.splice(0, 0, defaultBlueprint);
}
blueprint = defaultBlueprint;
} else {
let blueprintUpdates = await checkForBlueprintUpdates(blueprints);

let areAllUpToDate = blueprintUpdates.every(blueprint => blueprint.isUpToDate);
if (areAllUpToDate) {
return `${blueprintUpdates.map(formatBlueprintLine).join(`
`)}

if (blueprints.length > 1) {
let answers = await inquirer.prompt([{
All blueprints are up-to-date!`;
}

let choicesByName = blueprintUpdates.reduce((choices, blueprint) => {
let name = formatBlueprintLine(blueprint);
choices[name] = {
realName: blueprint.name,
choice: {
name,
disabled: blueprint.isUpToDate
}
};
return choices;
}, {});

let answer = await inquirer.prompt([{
type: 'list',
message: 'Multiple blueprint updates have been found. Which would you like to update?',
message: 'Blueprint updates have been found. Which one would you like to update?',
name: 'blueprint',
choices: blueprints.map(blueprint => blueprint.name)
choices: Object.values(choicesByName).map(({ choice }) => choice)
}]);

blueprint = blueprints.find(blueprint => blueprint.name === answers.blueprint);
} else {
blueprint = blueprints[0];
let { realName } = choicesByName[answer.blueprint];

blueprint = blueprints.find(blueprint => blueprint.name === realName);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/ember-cli-update-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe(function() {

ps.stdout.on('data', data => {
let str = data.toString();
if (str.includes('Multiple blueprint updates have been found.')) {
if (str.includes('Blueprint updates have been found.')) {
ps.stdin.write('\n');
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"isPartial": false
},
{
"name": "unused-partial-blueprint-test",
"version": "0.0.1",
"name": "ember-cli-update-npm-blueprint-test",
"version": "0.0.5",
"isPartial": true
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"isPartial": false
},
{
"name": "unused-partial-blueprint-test",
"version": "0.0.1",
"name": "ember-cli-update-npm-blueprint-test",
"version": "0.0.5",
"isPartial": true
}
]
Expand Down
56 changes: 56 additions & 0 deletions test/integration/check-for-blueprint-updates-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const { describe, it } = require('../helpers/mocha');
const { expect } = require('../helpers/chai');
const checkForBlueprintUpdates = require('../../src/check-for-blueprint-updates');
const { initBlueprint } = require('../helpers/blueprint');

describe(checkForBlueprintUpdates, function() {
this.timeout(10 * 1000);

let cwd = process.cwd();

afterEach(function() {
process.chdir(cwd);
});

it('works', async function() {
// out of date test
let localBlueprint = require('../fixtures/blueprint/app/local-app/local/my-app/ember-cli-update').blueprints[0];
let urlBlueprint = require('../fixtures/blueprint/app/remote-app/local/my-app/ember-cli-update').blueprints[0];

// up to date test
let npmBlueprint = require('../fixtures/blueprint/app/npm-app/merge/my-app/ember-cli-update').blueprints[0];

let blueprintPath = await initBlueprint('test/fixtures/blueprint/app/local', localBlueprint.location);

process.chdir(blueprintPath);

let blueprintUpdates = await checkForBlueprintUpdates([
localBlueprint,
urlBlueprint,
npmBlueprint
]);

expect(blueprintUpdates).to.deep.equal([
{
name: localBlueprint.name,
currentVersion: localBlueprint.version,
latestVersion: require('../fixtures/blueprint/app/local-app/merge/my-app/ember-cli-update').blueprints[0].version,
isUpToDate: false
},
{
name: urlBlueprint.name,
currentVersion: urlBlueprint.version,
latestVersion: require('../fixtures/blueprint/app/remote-app/merge/my-app/ember-cli-update').blueprints[0].version,
isUpToDate: false
},
{
name: npmBlueprint.name,
currentVersion: npmBlueprint.version,
latestVersion: npmBlueprint.version,
isUpToDate: true
}
]);
});
});
7 changes: 7 additions & 0 deletions test/integration/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ applicable codemods: ember-modules-codemod, ember-qunit-codemod, ember-test-help
describe('ember-cli-update.json', function() {
it('can update a remote blueprint', async function() {
let {
name,
version: to
} = require('../fixtures/blueprint/app/remote-app/merge/my-app/ember-cli-update').blueprints[0];

Expand All @@ -357,6 +358,7 @@ applicable codemods: ember-modules-codemod, ember-qunit-codemod, ember-test-help
} = await merge({
fixturesPath: 'test/fixtures/blueprint/app/remote-app/local',
commitMessage: 'my-app',
blueprint: name,
to
});

Expand All @@ -368,11 +370,16 @@ applicable codemods: ember-modules-codemod, ember-qunit-codemod, ember-test-help
});

it('can update an npm blueprint', async function() {
let {
name
} = require('../fixtures/blueprint/app/npm-app/merge/my-app/ember-cli-update').blueprints[0];

let {
status
} = await merge({
fixturesPath: 'test/fixtures/blueprint/app/npm-app/local',
commitMessage: 'my-app',
blueprint: name,
to: toDefault
});

Expand Down