Skip to content

Commit

Permalink
feat(upgrade): skip when not available packages are installed
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jan 25, 2024
1 parent f663f31 commit 73d888e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/patchers/upgrade_packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export class UpgradePackages extends BasePatcher {
#rootDir!: string
#skipInstall = false

#packageNotAvailable = [
{ name: '@adonisjs/drive' },
{ name: '@adonisjs/limiter' },
{ name: '@adonisjs/attachment-lite' },
{ name: '@adonisjs/lucid-slugify' },
{ name: '@adonisjs/route-model-binding' },
]

#packagesToRemove = ['@adonisjs/repl', 'source-map-support', 'youch', 'youch-terminal']

#packagesToSwap = [
Expand Down Expand Up @@ -61,6 +69,12 @@ export class UpgradePackages extends BasePatcher {
this.#skipInstall = skipInstall ?? false
}

#detectNotAvailablePackages() {
const installedPackages = this.runner.pkgJsonFile.getInstalledPackages()

return this.#packageNotAvailable.filter((pkg) => installedPackages.includes(pkg.name))
}

#filterDevAndProdPackages(packages: Array<{ name: string; isDev: boolean }>) {
const devPackages = packages.filter((pkg) => pkg.isDev).map((pkg) => pkg.name)
const prodPackages = packages.filter((pkg) => !pkg.isDev).map((pkg) => pkg.name)
Expand Down Expand Up @@ -216,6 +230,18 @@ export class UpgradePackages extends BasePatcher {
this.#rootDir = this.runner.project.getRootDirectories()[0].getPath()
if (!this.#pkgManager) this.#pkgManager = (await detectPackageManager(this.#rootDir)) ?? 'pnpm'

const notAvailablePackages = this.#detectNotAvailablePackages()

if (notAvailablePackages.length > 0) {
this.logger.warning(
`The following packages are not available for AdonisJS v6: ${notAvailablePackages
.map((pkg) => pkg.name)
.join(', ')}`
)

return
}

if (!this.#skipInstall) {
await this.#upgradePackages()
await this.#removeDeprecatedPackages()
Expand Down
20 changes: 20 additions & 0 deletions tests/upgrade_packages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import { upgradePackages } from '../src/patchers/upgrade_packages/index.js'
test.group('Upgrade Packages', (group) => {
group.tap((t) => t.timeout(0).skip(!process.env.CI, 'Only run on CI'))

test('Skip when not available packages are installed', async ({ assert, fs }) => {
await fs.addTsConfig()
await fs.addRcFile()
await fs.addPackageJsonFile({
dependencies: {
'@adonisjs/limiter': '^5.0.0',
},
})

const runner = createRunner({
projectPath: fs.basePath,
patchers: [upgradePackages({ packageManager: 'pnpm' })],
})
await runner.run()

const pkgJson = await fs.contentsJson('package.json')

assert.isTrue(pkgJson.dependencies['@adonisjs/limiter'].startsWith('5'))
})

test('Upgrade installed packages', async ({ assert, fs }) => {
await fs.addTsConfig()
await fs.addRcFile()
Expand Down

0 comments on commit 73d888e

Please sign in to comment.