fix(angular): ensure apps/libs layout is created when migrating from angular cli to nx monorepo layout (#13577)

This commit is contained in:
Leosvel Pérez Espinosa 2022-12-02 15:10:44 +01:00 committed by GitHub
parent 63820a9e50
commit 2dd2eeae63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 14 deletions

View File

@ -284,19 +284,17 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
// Restore e2e directory // Restore e2e directory
runCommand('mv e2e-bak e2e'); runCommand('mv e2e-bak e2e');
// TODO: this functionality is currently broken, this validation doesn't exist // Remove src
// // Remove src runCommand('mv src src-bak');
// runCommand('mv src src-bak'); expect(() =>
// expect(() => runNgAdd('@nrwl/angular', '--npm-scope projscope --skip-install')).toThrow( runNgAdd('@nrwl/angular', '--npm-scope projscope --skip-install')
// 'Path: src does not exist' ).toThrow('The project source root "src" could not be found.');
// );
// // Put src back // Put src back
// runCommand('mv src-bak src'); runCommand('mv src-bak src');
}); });
//TODO: reenable it('should handle a workspace with cypress v9', () => {
xit('should handle a workspace with cypress v9', () => {
addCypress9(); addCypress9();
// Remove cypress.json // Remove cypress.json
@ -383,8 +381,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
}); });
}); });
//TODO: reenable it('should handle a workspace with cypress v10', () => {
xit('should handle a workspace with cypress v10', () => {
addCypress10(); addCypress10();
// Remove cypress.config.ts // Remove cypress.config.ts

View File

@ -269,6 +269,101 @@ describe('workspace', () => {
expect(prettierIgnore).toBe('# existing ignore rules'); expect(prettierIgnore).toBe('# existing ignore rules');
}); });
it('should generate .gitkeep file in apps directory when there are no applications', async () => {
tree.write('projects/lib1/README.md', '');
tree.write('projects/lib1/src/public-api.ts', '');
writeJson(tree, 'angular.json', {
$schema: './node_modules/@angular/cli/lib/config/schema.json',
version: 1,
defaultProject: 'lib1',
newProjectRoot: 'projects',
projects: {
lib1: {
root: 'projects/lib1',
sourceRoot: 'projects/lib1/src',
projectType: 'library',
architect: {
build: {
builder: '@angular-devkit/build-angular:ng-packagr',
options: { tsConfig: 'projects/lib1/tsconfig.lib.json' },
},
test: {
builder: '@angular-devkit/build-angular:karma',
options: { tsConfig: 'projects/lib1/tsconfig.spec.json' },
},
},
},
},
});
await migrateFromAngularCli(tree, {});
expect(tree.exists('apps/.gitkeep')).toBe(true);
});
it('should not generate .gitkeep file in apps directory when there is at least one application', async () => {
await migrateFromAngularCli(tree, {});
expect(tree.exists('apps/.gitkeep')).toBe(false);
});
it('should generate .gitkeep file in libs directory when there are no libraries', async () => {
await migrateFromAngularCli(tree, {});
expect(tree.exists('libs/.gitkeep')).toBe(true);
});
it('should not generate .gitkeep file in libs directory when there is at least one library', async () => {
tree.write('projects/lib1/README.md', '');
tree.write('projects/lib1/src/public-api.ts', '');
writeJson(tree, 'angular.json', {
$schema: './node_modules/@angular/cli/lib/config/schema.json',
version: 1,
defaultProject: 'app1',
newProjectRoot: 'projects',
projects: {
app1: {
root: '',
sourceRoot: 'src',
projectType: 'application',
architect: {
build: {
builder: '@angular-devkit/build-angular:browser',
options: { tsConfig: 'tsconfig.app.json' },
},
test: {
builder: '@angular-devkit/build-angular:karma',
options: { tsConfig: 'tsconfig.spec.json' },
},
e2e: {
builder: '@angular-devkit/build-angular:protractor',
options: { protractorConfig: 'e2e/protractor.conf.js' },
},
},
},
lib1: {
root: 'projects/lib1',
sourceRoot: 'projects/lib1/src',
projectType: 'library',
architect: {
build: {
builder: '@angular-devkit/build-angular:ng-packagr',
options: { tsConfig: 'projects/lib1/tsconfig.lib.json' },
},
test: {
builder: '@angular-devkit/build-angular:karma',
options: { tsConfig: 'projects/lib1/tsconfig.spec.json' },
},
},
},
},
});
await migrateFromAngularCli(tree, {});
expect(tree.exists('libs/.gitkeep')).toBe(false);
});
it('should create a root eslint config', async () => { it('should create a root eslint config', async () => {
await migrateFromAngularCli(tree, {}); await migrateFromAngularCli(tree, {});

View File

@ -3,9 +3,7 @@ import {
addDependenciesToPackageJson, addDependenciesToPackageJson,
installPackagesTask, installPackagesTask,
readJson, readJson,
readWorkspaceConfiguration,
updateJson, updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { convertToNxProjectGenerator } from '@nrwl/workspace/generators'; import { convertToNxProjectGenerator } from '@nrwl/workspace/generators';
import { prettierVersion } from '@nrwl/workspace/src/utils/versions'; import { prettierVersion } from '@nrwl/workspace/src/utils/versions';
@ -20,6 +18,7 @@ import {
createWorkspaceFiles, createWorkspaceFiles,
decorateAngularCli, decorateAngularCli,
deleteAngularJson, deleteAngularJson,
deleteGitKeepFilesIfNotNeeded,
formatFilesTask, formatFilesTask,
getAllProjects, getAllProjects,
getWorkspaceRootFileTypesInfo, getWorkspaceRootFileTypesInfo,
@ -119,6 +118,8 @@ export async function migrateFromAngularCli(
updateRootEsLintConfig(tree, eslintConfig, options.unitTestRunner); updateRootEsLintConfig(tree, eslintConfig, options.unitTestRunner);
cleanupEsLintPackages(tree); cleanupEsLintPackages(tree);
} }
deleteGitKeepFilesIfNotNeeded(tree);
} }
deleteAngularJson(tree); deleteAngularJson(tree);

View File

@ -400,3 +400,12 @@ export function deleteAngularJson(tree: Tree): void {
} }
tree.delete('angular.json'); tree.delete('angular.json');
} }
export function deleteGitKeepFilesIfNotNeeded(tree: Tree): void {
if (tree.children('apps').length > 1 && tree.exists('apps/.gitkeep')) {
tree.delete('apps/.gitkeep');
}
if (tree.children('libs').length > 1 && tree.exists('libs/.gitkeep')) {
tree.delete('libs/.gitkeep');
}
}