From 47f0321dc70aa796a6ca512f9789ae57422f3744 Mon Sep 17 00:00:00 2001 From: Kelly Selden Date: Mon, 19 Aug 2019 12:19:31 +0100 Subject: [PATCH] check for blueprint updates --- src/check-for-blueprint-updates.js | 29 ++++++++++ src/index.js | 43 +++++++++++--- test/acceptance/ember-cli-update-test.js | 2 +- .../local/my-app/ember-cli-update.json | 4 +- .../merge/my-app/ember-cli-update.json | 4 +- .../check-for-blueprint-updates-test.js | 56 +++++++++++++++++++ test/integration/index-test.js | 7 +++ 7 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 src/check-for-blueprint-updates.js create mode 100644 test/integration/check-for-blueprint-updates-test.js diff --git a/src/check-for-blueprint-updates.js b/src/check-for-blueprint-updates.js new file mode 100644 index 000000000..75610b32f --- /dev/null +++ b/src/check-for-blueprint-updates.js @@ -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; diff --git a/src/index.js b/src/index.js index 7a3d545cf..85981bd55 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -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); } } diff --git a/test/acceptance/ember-cli-update-test.js b/test/acceptance/ember-cli-update-test.js index 52d3294d0..adfa3b78c 100644 --- a/test/acceptance/ember-cli-update-test.js +++ b/test/acceptance/ember-cli-update-test.js @@ -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'); } }); diff --git a/test/fixtures/blueprint/app/local-app/local/my-app/ember-cli-update.json b/test/fixtures/blueprint/app/local-app/local/my-app/ember-cli-update.json index e26e44f1c..f51dfe867 100644 --- a/test/fixtures/blueprint/app/local-app/local/my-app/ember-cli-update.json +++ b/test/fixtures/blueprint/app/local-app/local/my-app/ember-cli-update.json @@ -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 } ] diff --git a/test/fixtures/blueprint/app/local-app/merge/my-app/ember-cli-update.json b/test/fixtures/blueprint/app/local-app/merge/my-app/ember-cli-update.json index 394db3826..603657415 100644 --- a/test/fixtures/blueprint/app/local-app/merge/my-app/ember-cli-update.json +++ b/test/fixtures/blueprint/app/local-app/merge/my-app/ember-cli-update.json @@ -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 } ] diff --git a/test/integration/check-for-blueprint-updates-test.js b/test/integration/check-for-blueprint-updates-test.js new file mode 100644 index 000000000..5ed3ddf03 --- /dev/null +++ b/test/integration/check-for-blueprint-updates-test.js @@ -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 + } + ]); + }); +}); diff --git a/test/integration/index-test.js b/test/integration/index-test.js index 4d6f7808b..b530eeb6b 100644 --- a/test/integration/index-test.js +++ b/test/integration/index-test.js @@ -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]; @@ -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 }); @@ -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 });