diff --git a/packages/cli/src/commands/versions/migrate.test.ts b/packages/cli/src/commands/versions/migrate.test.ts index 2465d3fc7d..043e1893a4 100644 --- a/packages/cli/src/commands/versions/migrate.test.ts +++ b/packages/cli/src/commands/versions/migrate.test.ts @@ -226,12 +226,10 @@ describe('versions:migrate', () => { jest.spyOn(run, 'run').mockResolvedValue(undefined); - const s = await withLogCollector(async () => { + await withLogCollector(async () => { await migrate({}); }); - console.log(s); - expect(run.run).toHaveBeenCalledTimes(1); expect(run.run).toHaveBeenCalledWith( 'yarn', @@ -257,4 +255,91 @@ describe('versions:migrate', () => { "import { myThing } from '@backstage-community/custom-two';", ); }); + + it('should replaces the occurences of changed packages, and is careful', async () => { + mockDir.setContent({ + 'package.json': JSON.stringify({ + workspaces: { + packages: ['packages/*'], + }, + }), + node_modules: { + '@backstage': { + custom: { + 'package.json': JSON.stringify({ + name: '@backstage-extra/custom', + version: '1.0.1', + backstage: { + moved: '@backstage-community/custom', + }, + }), + }, + 'custom-two': { + 'package.json': JSON.stringify({ + name: '@backstage-extra/custom-two', + version: '1.0.0', + }), + }, + }, + }, + packages: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + dependencies: { + '@backstage/core': '^1.0.5', + '@backstage/custom': '^1.0.1', + '@backstage/custom-two': '^1.0.0', + }, + }), + src: { + 'index.ts': "import { myThing } from '@backstage/custom';", + 'index.test.ts': "import { myThing } from '@backstage/custom-two';", + }, + }, + b: { + 'package.json': JSON.stringify({ + name: 'b', + dependencies: { + '@backstage/core': '^1.0.3', + '@backstage/theme': '^1.0.0', + '@backstage/custom': '^1.1.0', + '@backstage/custom-two': '^1.0.0', + }, + }), + }, + }, + }); + + jest.spyOn(run, 'run').mockResolvedValue(undefined); + + await withLogCollector(async () => { + await migrate({}); + }); + + expect(run.run).toHaveBeenCalledTimes(1); + expect(run.run).toHaveBeenCalledWith( + 'yarn', + ['install'], + expect.any(Object), + ); + + const indexA = await fs.readFile( + mockDir.resolve('packages/a/src/index.ts'), + 'utf-8', + ); + + expect(indexA).toEqual( + "import { myThing } from '@backstage-community/custom';", + ); + + const indexTestA = await fs.readFile( + mockDir.resolve('packages/a/src/index.test.ts'), + 'utf-8', + ); + + expect(indexTestA).toEqual( + "import { myThing } from '@backstage/custom-two';", + ); + }); }); diff --git a/packages/cli/src/commands/versions/migrate.ts b/packages/cli/src/commands/versions/migrate.ts index fc93f56e36..5397efc958 100644 --- a/packages/cli/src/commands/versions/migrate.ts +++ b/packages/cli/src/commands/versions/migrate.ts @@ -120,15 +120,15 @@ export async function migrateMovedPackages(options?: { if (!options?.skipCodeChanges) { // Replace all occurrences of the old package names in the code. const files = await replace({ - files: join(pkg.dir, '**', '*.{ts,tsx,js,jsx}'), - ignore: ['/node_modules/', '/yarn.lock/'], + files: join(pkg.dir, 'src', '**'), allowEmptyPaths: true, processor: content => { return Array.from(movedPackages.entries()).reduce( (newContent, [oldName, newName]) => { return newContent + .replace(new RegExp(`"${oldName}"`, 'g'), `"${newName}"`) .replace(new RegExp(`'${oldName}'`, 'g'), `'${newName}'`) - .replace(new RegExp(`"${oldName}"`, 'g'), `"${newName}"`); + .replace(new RegExp(`${oldName}/`, 'g'), `${newName}/`); }, content, );