Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Jul 6, 2023
1 parent fc0abf4 commit 25d10c1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
13 changes: 12 additions & 1 deletion lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ function getCommand (mode, chosenPlugin) {
dependentPlugins
} = chosenPlugin
const dependents = dependentPlugins?.map(({ packageName }) => packageName).join(' ')
const dependencies = dependencyPlugins?.map(({ packageName }) => packageName).join(' ')
const dependentsToUpgrade = dependentPlugins?.map(({
packageName,
latestVersion
}) => packageName + '@' + latestVersion).join(' ')
const dependencies = dependencyPlugins?.map(({
packageName,
latestVersion
}) => packageName + '@' + latestVersion).join(' ')

if (version && installCommand) {
installCommand += `@${version}`
Expand All @@ -458,6 +465,10 @@ function getCommand (mode, chosenPlugin) {
upgradeCommand += ' ' + dependencies
}

if (dependentsToUpgrade) {
upgradeCommand += ' ' + dependentsToUpgrade
}

switch (mode) {
case 'upgrade':
return upgradeCommand + ' --save-exact'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

<ul class="govuk-list govuk-list--bullet">
{% for plugin in chosenPlugin.dependencyPlugins %}
<li>{{ plugin.name }}{% if plugin.scope %} from {{ plugin.scope }} {% endif %}</li>
<li>{{ plugin.name }} {{ plugin.latestVersion }}{% if plugin.scope %} from {{ plugin.scope }} {% endif %}</li>
{% endfor %}
{% for plugin in chosenPlugin.dependentPlugins %}
<li>{{ plugin.name }}{% if plugin.scope %} from {{ plugin.scope }} {% endif %}</li>
<li>{{ plugin.name }} {{ plugin.latestVersion }}{% if plugin.scope %} from {{ plugin.scope }} {% endif %}</li>
{% endfor %}
</ul>

Expand Down
53 changes: 36 additions & 17 deletions lib/plugins/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,12 @@ async function refreshPackageInfo (packageName, version) {
pluginConfig = getProxyPluginConfig(packageName)
}

const { version: installedVersion } = installed ? packageJson : {}
const { version: installedVersion } = packageJson && installed ? packageJson : {}
if (installedLocally) {
localVersion = path.resolve(installedPackageVersion.replace('file:', ''))
}

const pluginDependencies = pluginConfig?.pluginDependencies?.map((dependency) => {
if (typeof dependency === 'string') {
dependency = {
packageName: dependency
}
}
return dependency
})
const pluginDependencies = pluginConfig?.pluginDependencies ? normaliseDependencies(pluginConfig.pluginDependencies) : undefined

const packageInfo = {
packageName,
Expand Down Expand Up @@ -204,6 +197,17 @@ async function waitForPackagesCache () {
}
}

function normaliseDependencies (dependencies) {
return dependencies.map((dependency) => {
if (typeof dependency === 'string') {
dependency = {
packageName: dependency
}
}
return dependency
})
}

async function getDependentPackages (packageName, version, mode) {
if (!Object.keys(packagesCache).length) {
await startPackageTracker()
Expand All @@ -227,14 +231,15 @@ async function getDependentPackages (packageName, version, mode) {
})
.map(async (packageInfo) => {
const latestPluginConfig = await getConfigForPackage(packageInfo.packageName, version)
const dependency = latestPluginConfig?.pluginDependencies?.find((pluginDependency) => pluginDependency.packageName === packageName)
const pluginDependencies = latestPluginConfig?.pluginDependencies ? normaliseDependencies(latestPluginConfig.pluginDependencies) : undefined
const dependency = pluginDependencies?.find((pluginDependency) => pluginDependency.packageName === packageName)
if (!dependency) {
return undefined
}
if (typeof dependency === 'string') {
const { minVersion } = dependency
if (!minVersion) {
return packageInfo
}
const { minVersion } = dependency
if (minVersion && minVersion >= packageInfo.installedVersion) {
return packageInfo
}
Expand All @@ -249,14 +254,28 @@ async function getDependencyPackages (packageName, version, mode) {
}
await waitForPackagesCache()
const pkg = await lookupPackageInfo(packageName, version)
if (mode) {
console.log('mode')
let pluginDependencies = pkg?.pluginDependencies
if (version || mode === 'upgrade') {
const targetVersion = version || pkg.latestVersion
if (targetVersion !== pkg.installedVersion) {
const latestPluginConfig = await getConfigForPackage(pkg.packageName, version)
if (latestPluginConfig) {
pluginDependencies = latestPluginConfig.pluginDependencies
}
}
}
return !pkg?.pluginDependencies
const dependencyPlugins = !pluginDependencies
? []
: await Promise.all(pkg.pluginDependencies.map((pluginDependency) => {
return typeof pluginDependency === 'string' ? lookupPackageInfo(pluginDependency) : lookupPackageInfo(pluginDependency.packageName)
: await Promise.all(normaliseDependencies(pluginDependencies).map((pluginDependency) => {
return lookupPackageInfo(pluginDependency.packageName)
}))

return dependencyPlugins.filter(({
latestVersion,
installedVersion
}) => {
return installedVersion !== latestVersion
})
}

if (!config.getConfig().isTest) {
Expand Down
10 changes: 8 additions & 2 deletions lib/plugins/packages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,29 @@ describe('packages', () => {
const availableInstalledPackage = {
packageName: 'available-installed-package',
installed: true,
available: true
available: true,
installedVersion: '1.0.0',
latestVersion: '1.0.0'
}
const availableUninstalledPackage = {
packageName: 'available-uninstalled-package',
installed: false,
available: true
available: true,
latestVersion: '1.0.0'
}
const unavailableInstalledPackage = {
packageName: 'unavailable-installed-package',
installed: true,
available: false,
installedVersion: '1.0.0',
latestVersion: '1.0.0',
pluginDependencies: [availableInstalledPackage]
}
const unavailableUninstalledPackage = {
packageName: 'unavailable-uninstalled-package',
installed: false,
available: false,
latestVersion: '1.0.0',
pluginDependencies: [availableUninstalledPackage]
}

Expand Down

0 comments on commit 25d10c1

Please sign in to comment.