Skip to content

Commit e7a32db

Browse files
Broccohansl
authored andcommitted
fix(@schematics/angular): Update CLI schematic should convert server apps
fixes angular#659
1 parent a0c9a61 commit e7a32db

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

packages/schematics/angular/migrations/update-6/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const defaults = {
2828
karma: 'karma.conf.js',
2929
protractor: 'protractor.conf.js',
3030
testTsConfig: 'tsconfig.spec.json',
31+
serverOutDir: 'dist-server',
32+
serverMain: 'main.server.ts',
33+
serverTsConfig: 'tsconfig.server.json',
3134
};
3235

3336
function getConfigPath(tree: Tree): Path {
@@ -195,8 +198,11 @@ function extractProjectsConfig(config: CliConfig, tree: Tree): JsonObject {
195198

196199
const apps = config.apps || [];
197200
// convert the apps to projects
198-
const projectMap = apps
199-
.map((app: AppConfig, idx: number) => {
201+
const browserApps = apps.filter(app => app.platform !== 'server');
202+
const serverApps = apps.filter(app => app.platform === 'server');
203+
204+
const projectMap = browserApps
205+
.map((app, idx) => {
200206
const defaultAppName = idx === 0 ? defaultAppNamePrefix : `${defaultAppNamePrefix}${idx}`;
201207
const name = app.name || defaultAppName;
202208
const outDir = app.outDir || defaults.outDir;
@@ -440,6 +446,22 @@ function extractProjectsConfig(config: CliConfig, tree: Tree): JsonObject {
440446
options: lintOptions,
441447
};
442448

449+
// server target
450+
const serverApp = serverApps
451+
.filter(serverApp => app.root === serverApp.root && app.index === serverApp.index)[0];
452+
453+
if (serverApp) {
454+
const serverOptions: JsonObject = {
455+
outputPath: serverApp.outDir || defaults.serverOutDir,
456+
main: serverApp.main || defaults.serverMain,
457+
tsConfig: serverApp.tsconfig || defaults.serverTsConfig,
458+
};
459+
const serverTarget: JsonObject = {
460+
builder: '@angular-devkit/build-angular:server',
461+
options: serverOptions,
462+
};
463+
architect.server = serverTarget;
464+
}
443465
const e2eProject: JsonObject = {
444466
root: project.root,
445467
projectType: 'application',

packages/schematics/angular/migrations/update-6/index_spec.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ describe('Migration to v6', () => {
107107
tree.create('/src/favicon.ico', '');
108108
});
109109

110+
// tslint:disable-next-line:no-any
111+
function getConfig(tree: UnitTestTree): any {
112+
return JSON.parse(tree.readContent(configPath));
113+
}
114+
110115
describe('file creation/deletion', () => {
111116
it('should delete the old config file', () => {
112117
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
@@ -123,11 +128,6 @@ describe('Migration to v6', () => {
123128
});
124129

125130
describe('config file contents', () => {
126-
// tslint:disable-next-line:no-any
127-
function getConfig(tree: UnitTestTree): any {
128-
return JSON.parse(tree.readContent(configPath));
129-
}
130-
131131
it('should set root values', () => {
132132
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
133133
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
@@ -720,4 +720,55 @@ describe('Migration to v6', () => {
720720
expect(blacklist).toEqual([]);
721721
});
722722
});
723+
724+
describe('server/universal apps', () => {
725+
let serverApp;
726+
beforeEach(() => {
727+
serverApp = {
728+
platform: 'server',
729+
root: 'src',
730+
outDir: 'dist/server',
731+
assets: [
732+
'assets',
733+
'favicon.ico',
734+
],
735+
index: 'index.html',
736+
main: 'main.server.ts',
737+
test: 'test.ts',
738+
tsconfig: 'tsconfig.server.json',
739+
testTsconfig: 'tsconfig.spec.json',
740+
prefix: 'app',
741+
styles: [
742+
'styles.css',
743+
],
744+
scripts: [],
745+
environmentSource: 'environments/environment.ts',
746+
environments: {
747+
dev: 'environments/environment.ts',
748+
prod: 'environments/environment.prod.ts',
749+
},
750+
};
751+
baseConfig.apps.push(serverApp);
752+
});
753+
754+
it('should not create a separate app for server apps', () => {
755+
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
756+
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
757+
const config = getConfig(tree);
758+
const appCount = Object.keys(config.projects).length;
759+
expect(appCount).toEqual(2);
760+
});
761+
762+
it('should create a server target', () => {
763+
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
764+
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
765+
const config = getConfig(tree);
766+
const target = config.projects.foo.architect.server;
767+
expect(target).toBeDefined();
768+
expect(target.builder).toEqual('@angular-devkit/build-angular:server');
769+
expect(target.options.outputPath).toEqual('dist/server');
770+
expect(target.options.main).toEqual('main.server.ts');
771+
expect(target.options.tsConfig).toEqual('tsconfig.server.json');
772+
});
773+
});
723774
});

0 commit comments

Comments
 (0)