diff --git a/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.from-nx-init.spec.ts b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.from-nx-init.spec.ts new file mode 100644 index 0000000000000..f36943ae0ee43 --- /dev/null +++ b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.from-nx-init.spec.ts @@ -0,0 +1,34 @@ +import 'nx/src/internal-testing-utils/mock-project-graph'; + +import { + readJson, + readProjectConfiguration, + Tree, + updateJson, +} from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { monorepoGenerator } from './convert-to-monorepo'; + +describe('monorepo generator', () => { + let tree: Tree; + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should convert a root package without a project.json', async () => { + updateJson(tree, 'package.json', (json) => { + json.name = 'demo'; + // Make this a root project + json.nx = {}; + return json; + }); + + await monorepoGenerator(tree, {}); + + expect(tree.exists('packages/demo/project.json')).toBeTruthy(); + expect(readProjectConfiguration(tree, 'demo')).toMatchObject({ + name: 'demo', + }); + expect(readJson(tree, 'package.json').name).toBe('@demo/source'); + }); +}); diff --git a/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.spec.ts b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.spec.ts index 86468fd95cd3b..0c10759cc931f 100644 --- a/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.spec.ts +++ b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.spec.ts @@ -1,6 +1,6 @@ import 'nx/src/internal-testing-utils/mock-project-graph'; -import { readJson, readProjectConfiguration, Tree } from '@nx/devkit'; +import { readProjectConfiguration, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { monorepoGenerator } from './convert-to-monorepo'; diff --git a/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.ts b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.ts index b44523fe14592..a2f4a14064658 100644 --- a/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.ts +++ b/packages/workspace/src/generators/convert-to-monorepo/convert-to-monorepo.ts @@ -4,7 +4,9 @@ import { ProjectConfiguration, readNxJson, Tree, + updateJson, updateNxJson, + writeJson, } from '@nx/devkit'; import { moveGenerator } from '../move/move'; @@ -34,6 +36,19 @@ export async function monorepoGenerator(tree: Tree, options: {}) { const appsDir = isRootProjectApp ? 'apps' : 'packages'; const libsDir = isRootProjectApp ? 'libs' : 'packages'; + if (rootProject) { + // If project was created using `nx init` then it might not have project.json. + // Need to create one to avoid name conflicts with root package.json. + if (!tree.exists('project.json')) { + writeJson(tree, 'project.json', { name: rootProject.name }); + } + updateJson(tree, 'package.json', (json) => { + // Avoid name conflicts once we move root project into its own folder. + json.name = `@${rootProject.name}/source`; + return json; + }); + } + for (const project of projectsToMove) { await moveGenerator(tree, { projectName: project.name, diff --git a/packages/workspace/src/generators/move/lib/move-project-files.ts b/packages/workspace/src/generators/move/lib/move-project-files.ts index 70c6ebcd01394..d57c8c1fdd440 100644 --- a/packages/workspace/src/generators/move/lib/move-project-files.ts +++ b/packages/workspace/src/generators/move/lib/move-project-files.ts @@ -18,10 +18,7 @@ export function moveProjectFiles( const knownRootProjectFiles = [ // Config files 'project.json', - 'tsconfig.json', - 'tsconfig.app.json', - 'tsconfig.lib.json', - 'tsconfig.spec.json', + /^tsconfig(?!\.base\.json$)((\..+)?\.json$|json$)/, '.babelrc', '.eslintrc.json', 'eslint.config.js',