Skip to content

Commit 6c835b1

Browse files
d-koppenhagensis0k0
authored andcommitted
feat: flag to skip adding auto-generated component on ng new and ng add (#171)
1 parent 2e83f18 commit 6c835b1

File tree

9 files changed

+83
-14
lines changed

9 files changed

+83
-14
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<!-- https://docs.nativescript.org/angular/core-concepts/angular-navigation.html#page-router-outlet -->
22
<page-router-outlet></page-router-outlet>
3-
<!-- <Label text="Entry Component works" textWrap="true"></Label> -->
3+
<% if (skipAutoGeneratedComponent && !sample) { %><Label text="Entry Component works" textWrap="true"></Label>
4+
<% } %>

src/add-ns/_ns-files/__sourceDir__/app/app-routing.module__nsext__.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { NgModule } from '@angular/core';
22
import { NativeScriptRouterModule } from 'nativescript-angular/router';
33
import { Routes } from '@angular/router';
4-
4+
<% if (!skipAutoGeneratedComponent) { %>
55
import { AutoGeneratedComponent } from './auto-generated/auto-generated.component';
6-
7-
export const routes: Routes = [
6+
<% } %>
7+
export const routes: Routes = [<% if (!skipAutoGeneratedComponent || sample) { %>
88
{
99
path: '',
10-
redirectTo: <% if (sample) { %>'/players'<% } else { %>'/auto-generated'<% } %>,
10+
redirectTo: <% if (sample) { %>'/players'<% } else if (!skipAutoGeneratedComponent) { %>'/auto-generated'<% } %>,
1111
pathMatch: 'full',
12-
},
12+
},<% if (!skipAutoGeneratedComponent) { %>
1313
{
1414
path: 'auto-generated',
1515
component: AutoGeneratedComponent,
16-
},
17-
];
16+
},<% } %>
17+
<% } %>];
1818

1919
@NgModule({
2020
imports: [NativeScriptRouterModule.forRoot(routes)],

src/add-ns/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export default function (options: MigrationOptions): Rule {
3333

3434
return chain([
3535
validateOptions(options),
36-
3736
getProjectSettings(options.project),
3837

3938
addNativeScriptSchematics,
@@ -48,7 +47,10 @@ export default function (options: MigrationOptions): Rule {
4847
addRunScriptsToPackageJson,
4948
addNativeScriptProjectId,
5049
excludeNsFilesFromTsconfig,
51-
addSampleComponent(options.nsExtension, options.webExtension, options.project),
50+
51+
options.skipAutoGeneratedComponent ?
52+
noop() :
53+
addSampleComponent(options.nsExtension, options.webExtension, options.project),
5254

5355
addDependencies(),
5456

@@ -100,6 +102,7 @@ const addNsFiles = (options: MigrationOptions) => (_tree: Tree, context: Schemat
100102
context.logger.info('Adding {N} files');
101103
const templateOptions = {
102104
sample: options.sample,
105+
skipAutoGeneratedComponent: options.skipAutoGeneratedComponent,
103106
theme: true,
104107

105108
dasherize,
@@ -148,7 +151,6 @@ const addSampleFiles = () => (_tree: Tree, context: SchematicContext) => {
148151
const addSampleComponent = (nsExtension: string, webExtension: string, project: string) =>
149152
(_tree, context: SchematicContext) => {
150153
context.logger.info('Adding Sample Shared Component');
151-
152154
return schematic('component', {
153155
nsExtension: nsExtension,
154156
webExtension: webExtension,

src/add-ns/index_spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('Add {N} schematic', () => {
1717
nsExtension: 'tns',
1818
webExtension: '',
1919
sample: false,
20+
skipAutoGeneratedComponent: false
2021
};
2122

2223
let appTree: UnitTestTree;
@@ -121,9 +122,49 @@ describe('Add {N} schematic', () => {
121122

122123
it('should generate a sample shared component', () => {
123124
const { files } = appTree;
125+
const appRoutingModuleContent = appTree.readContent('/foo/src/app/app-routing.module.tns.ts');
126+
const appComponentTemplate = appTree.readContent('/foo/src/app/app.component.tns.html');
124127
expect(files).toContain('/foo/src/app/auto-generated/auto-generated.component.ts');
125128
expect(files).toContain('/foo/src/app/auto-generated/auto-generated.component.html');
126129
expect(files).toContain('/foo/src/app/auto-generated/auto-generated.component.tns.html');
130+
expect(appRoutingModuleContent).toMatch(
131+
/import { AutoGeneratedComponent } from '.\/auto-generated\/auto-generated.component'/
132+
);
133+
expect(appRoutingModuleContent).toMatch(
134+
/{\s+path: 'auto-generated',\s+component: AutoGeneratedComponent,\s+},/g
135+
);
136+
expect(appComponentTemplate).not.toContain(
137+
'<Label text="Entry Component works" textWrap="true"></Label>'
138+
);
139+
});
140+
});
141+
142+
describe('when the skipAutoGeneratedComponent flag is raised', () => {
143+
beforeEach(() => {
144+
const options = {
145+
...defaultOptions,
146+
skipAutoGeneratedComponent: true,
147+
};
148+
149+
appTree = schematicRunner.runSchematic('add-ns', options, appTree);
150+
});
151+
152+
it('should not add a sample shared component', () => {
153+
const { files } = appTree;
154+
const appRoutingModuleContent = appTree.readContent('/foo/src/app/app-routing.module.tns.ts');
155+
const appComponentTemplate = appTree.readContent('/foo/src/app/app.component.tns.html');
156+
expect(files.includes('/foo/src/app/auto-generated/auto-generated.component.css')).toBeFalsy();
157+
expect(files.includes('/foo/src/app/auto-generated/auto-generated.component.html')).toBeFalsy();
158+
expect(files.includes('/foo/src/app/auto-generated/auto-generated.component.ts')).toBeFalsy();
159+
expect(appRoutingModuleContent).not.toMatch(
160+
/import { AutoGeneratedComponent } from '.\/auto-generated\/auto-generated.component'/
161+
);
162+
expect(appRoutingModuleContent).toContain(
163+
'export const routes: Routes = []'
164+
);
165+
expect(appComponentTemplate).toContain(
166+
'<Label text="Entry Component works" textWrap="true"></Label>'
167+
);
127168
});
128169
});
129170

@@ -154,6 +195,13 @@ describe('Add {N} schematic', () => {
154195
expect(files.includes('/foo/src/app/barcelona/player-detail/player-detail.component.html')).toBeTruthy();
155196
expect(files.includes('/foo/src/app/barcelona/player-detail/player-detail.component.tns.html')).toBeTruthy();
156197
});
198+
199+
it('should configure routing for redirection', () => {
200+
const appRoutingModuleContent = appTree.readContent('/foo/src/app/app-routing.module.tns.ts');
201+
expect(appRoutingModuleContent).toMatch(
202+
/{\s+path: '',\s+redirectTo: '\/players',\s+pathMatch: 'full',\s+},/g
203+
);
204+
});
157205
});
158206
});
159207

src/add-ns/schema.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ export interface Schema {
1515
/**
1616
* Specifies whether a sample master detail should be generated.
1717
*/
18-
sample: boolean;
18+
sample: boolean;
1919
/**
2020
* Skip installing dependency packages.
2121
*/
2222
skipInstall?: boolean;
23+
/**
24+
* Skip adding an example component.
25+
*/
26+
skipAutoGeneratedComponent?: boolean;
2327
}

src/add-ns/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"description": "Skip installing dependency packages.",
3131
"type": "boolean",
3232
"default": false
33+
},
34+
"skipAutoGeneratedComponent": {
35+
"description": "Skip adding an example component.",
36+
"type": "boolean",
37+
"default": false
3338
}
3439
},
3540
"required": [

src/ng-new/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const parseToSharedOptions = (options: NgNewOptions): SharedOptions => {
4242
style: options.style,
4343
theme: options.theme,
4444
sample: options.sample,
45+
skipAutoGeneratedComponent: options.skipAutoGeneratedComponent
4546
};
4647
}
4748

src/ng-new/schema.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ export interface Schema {
3030
/**
3131
* Specifies whether a sample master detail should be generated.
3232
*/
33-
sample: boolean;
33+
sample: boolean;
34+
/**
35+
* Skip adding an example component.
36+
*/
37+
skipAutoGeneratedComponent?: boolean;
3438
}

src/ng-new/shared/schema.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ export interface Schema {
2222
/**
2323
* Specifies whether a sample master detail should be generated.
2424
*/
25-
sample: boolean;
25+
sample: boolean;
26+
/**
27+
* Skip adding an example component.
28+
*/
29+
skipAutoGeneratedComponent?: boolean;
2630
}

0 commit comments

Comments
 (0)