feat(angular): add preserveAngularCLILayout option to ng-add

This commit is contained in:
Victor Savkin 2020-05-28 16:14:53 -04:00 committed by Victor Savkin
parent 690be207be
commit 48d953e4d7
5 changed files with 305 additions and 223 deletions

View File

@ -293,22 +293,6 @@ forEachCli('angular', () => {
]);
});
// TODO(FrozenPandaz): reenable after angular 9
xit('should convert a project with common libraries in the ecosystem', () => {
// create a new AngularCLI app
runNew();
// Add some Angular libraries
runNgAdd('add @angular/elements');
runNgAdd('add @angular/material');
runNgAdd('add @angular/pwa');
runNgAdd('add @ngrx/store');
runNgAdd('add @ngrx/effects');
// Add Nx
runNgAdd('add @nrwl/workspace --skip-install');
});
it('should handle different types of errors', () => {
// create a new AngularCLI app
runNew();
@ -353,6 +337,18 @@ forEachCli('angular', () => {
// Put src back
runCommand('mv src-bak src');
});
it('should support preserveAngularCLILayout', () => {
runNew('', false, false);
runNgAdd('add @nrwl/workspace --preserveAngularCLILayout');
const updatedAngularCLIJson = readJson('angular.json');
expect(updatedAngularCLIJson.projects.proj.root).toEqual('');
expect(updatedAngularCLIJson.projects.proj.sourceRoot).toEqual('src');
const output = runCLI('build');
expect(output).toContain(`> ng run proj:build`);
});
});
});

View File

@ -9,6 +9,7 @@ describe('workspace', () => {
appTree = new UnitTestTree(Tree.empty());
});
describe('move to nx layout', () => {
it('should error if no package.json is present', async () => {
try {
await runSchematic('ng-add', { name: 'myApp' }, appTree);
@ -228,3 +229,38 @@ describe('workspace', () => {
expect(tree.exists('/karma.conf.js')).toBe(true);
});
});
describe('preserve angular cli layout', () => {
beforeEach(() => {
appTree.create('/package.json', JSON.stringify({ devDependencies: {} }));
appTree.create(
'/angular.json',
JSON.stringify({ projects: { myproj: {} } })
);
});
it('should update package.json', async () => {
const tree = await runSchematic(
'ng-add',
{ preserveAngularCLILayout: true },
appTree
);
const d = JSON.parse(tree.readContent('/package.json')).devDependencies;
expect(d['@nrwl/workspace']).toBeDefined();
expect(d['@nrwl/angular']).not.toBeDefined();
});
it('should create nx.json', async () => {
const tree = await runSchematic(
'ng-add',
{ preserveAngularCLILayout: true },
appTree
);
const nxJson = JSON.parse(tree.readContent('/nx.json'));
expect(nxJson.projects).toEqual({ myproj: { tags: [] } });
expect(nxJson.npmScope).toEqual('myproj');
});
});
});

View File

@ -29,6 +29,7 @@ import {
renameSyncInTree,
renameDirSyncInTree,
addInstallTask,
addDepsToPackageJson,
} from '@nrwl/workspace';
import { DEFAULT_NRWL_PRETTIER_CONFIG } from '../workspace/workspace';
import { JsonArray } from '@angular-devkit/core';
@ -557,7 +558,49 @@ function checkCanConvertToWorkspace(options: Schema) {
};
}
const createNxJson = (host: Tree) => {
const json = JSON.parse(host.read('angular.json').toString());
if (Object.keys(json.projects || {}).length !== 1) {
throw new Error(
`The schematic can only be used with Angular CLI workspaces with a single project.`
);
}
const name = Object.keys(json.projects)[0];
host.create(
'nx.json',
serializeJson({
npmScope: name,
implicitDependencies: {
'angular.json': '*',
'package.json': '*',
'tsconfig.json': '*',
'tslint.json': '*',
'nx.json': '*',
},
projects: {
[name]: {
tags: [],
},
},
tasksRunnerOptions: {
default: {
runner: '@nrwl/workspace/tasks-runners/default',
options: {
cacheableOperations: ['build', 'lint', 'test', 'e2e'],
},
},
},
})
);
};
export default function (schema: Schema): Rule {
if (schema.preserveAngularCLILayout) {
return chain([
addDepsToPackageJson({}, { '@nrwl/workspace': nxVersion }),
createNxJson,
]);
} else {
const options = {
...schema,
npmScope: toFileName(schema.npmScope || schema.name),
@ -581,3 +624,4 @@ export default function (schema: Schema): Rule {
addInstallTask(options),
]);
}
}

View File

@ -2,4 +2,5 @@ export interface Schema {
name: string;
skipInstall: boolean;
npmScope?: string;
preserveAngularCLILayout: boolean;
}

View File

@ -14,6 +14,11 @@
"description": "Skip installing after adding @nrwl/workspace",
"default": false
},
"preserveAngularCLILayout": {
"type": "boolean",
"description": "Preserve the Angular CLI layout instead of moving the app into apps.",
"default": false
},
"name": {
"type": "string",
"description": "Project name.",