Skip to content

Commit 8765b82

Browse files
committed
fix: stop using filter in module generation schematic
fixes #185
1 parent 56cb8ba commit 8765b82

File tree

9 files changed

+134
-47
lines changed

9 files changed

+134
-47
lines changed

src/collection.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
"schema": "./generate/module/schema.json"
3838
},
3939

40+
41+
"common-module": {
42+
"factory": "./generate/common-module",
43+
"description": "Generate a common NgModule",
44+
"schema": "./generate/common-module/schema.json"
45+
},
46+
4047
"app-resources": {
4148
"factory": "./app-resources",
4249
"description": "Create App Resources",
@@ -108,6 +115,7 @@
108115
"factory": "./migrate-component",
109116
"schema": "./migrate-component/schema.json"
110117
},
118+
111119
"migrate-module": {
112120
"aliases": [ "mg" ],
113121
"description": "Converts a module into a code sharing module",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Routes } from '@angular/router';
2+
3+
export const componentDeclarations: any[] = [
4+
];
5+
6+
export const providerDeclarations: any[] = [
7+
];
8+
9+
export const routes: Routes = [
10+
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
Rule,
3+
mergeWith,
4+
apply,
5+
url,
6+
template,
7+
move,
8+
} from '@angular-devkit/schematics';
9+
10+
import { Schema as CommonModuleOptions } from './schema';
11+
12+
export default function(options: CommonModuleOptions): Rule {
13+
const { name, path } = options;
14+
15+
return mergeWith(
16+
apply(url('./_files'), [
17+
template({ name }),
18+
move(path),
19+
]),
20+
);
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Schema {
2+
/**
3+
* The name of the module.
4+
*/
5+
name: string;
6+
/**
7+
* The path to create the module.
8+
*/
9+
path: string;
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "SchematicsNativeScriptAngularCommonModule",
4+
"title": "NativeScript Angular Common Module Options Schema",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "The name of the module."
10+
},
11+
"path": {
12+
"type": "string",
13+
"format": "path",
14+
"description": "The path to create the module."
15+
}
16+
},
17+
"required": [],
18+
"additionalProperties": false
19+
}

src/generate/module/index.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@ import {
44
chain,
55
externalSchematic,
66
SchematicsException,
7-
mergeWith,
8-
apply,
9-
url,
10-
template,
11-
move,
127
branchAndMerge,
13-
filter,
8+
schematic,
149
} from '@angular-devkit/schematics';
1510
import { InsertChange } from '@schematics/angular/utility/change';
1611
import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
12+
import { parseName } from '@schematics/angular/utility/parse-name';
13+
import { dasherize } from '@angular-devkit/core/src/utils/strings';
1714

15+
import { Schema as CommonModuleSchema } from '../common-module/schema';
1816
import { Schema as ModuleOptions } from './schema';
1917
import { copy } from '../../utils';
2018
import {
2119
removeImport,
2220
removeMetadataArrayValue,
2321
getSourceFile,
2422
} from '../../ts-utils';
25-
import { dasherize } from '@angular-devkit/core/src/utils/strings';
2623
import {
2724
removeNsSchemaOptions,
2825
getExtensions,
@@ -32,7 +29,6 @@ import {
3229
addExtension,
3330
validateGenerateOptions,
3431
} from '../utils';
35-
import { parseName } from '@schematics/angular/utility/parse-name';
3632

3733
class ModuleInfo {
3834
name: string;
@@ -64,18 +60,6 @@ export default function(options: ModuleOptions): Rule {
6460
let moduleInfo: ModuleInfo;
6561

6662
return branchAndMerge(chain([
67-
// Filter existing modules with the same names so that they don't
68-
// cause merge conflicts before the files are renamed.
69-
// TODO: Fix. Huge performance hit! Filter goes trough node_modules + platforms.
70-
filter((fileName) => {
71-
const {
72-
moduleName,
73-
routingName,
74-
} = getParsedName(options);
75-
76-
return ![moduleName, routingName].some((modName) => fileName.endsWith(modName));
77-
}),
78-
7963
(tree: Tree) => {
8064
platformUse = getPlatformUse(tree, options);
8165

@@ -124,7 +108,8 @@ export default function(options: ModuleOptions): Rule {
124108
},
125109

126110
(tree: Tree) => shouldCreateCommonFile(platformUse, options.common) ?
127-
addCommonFile(moduleInfo) : tree,
111+
schematic<CommonModuleSchema>('common-module', { name: moduleInfo.name, path: moduleInfo.path }) :
112+
tree,
128113
]));
129114
}
130115

@@ -133,17 +118,6 @@ const shouldCreateCommonFile = (platformUse: PlatformUse, useCommon?: boolean) =
133118
!platformUse.nsOnly && // it's a shared project
134119
platformUse.useWeb && platformUse.useNs; // and we generate a shared module
135120

136-
const addCommonFile = (moduleInfo: ModuleInfo) => {
137-
return mergeWith(
138-
apply(url('./_files'), [
139-
template({
140-
name: moduleInfo.name,
141-
}),
142-
move(moduleInfo.path),
143-
]),
144-
);
145-
};
146-
147121
const getParsedName = (options: ModuleOptions): { name: string, moduleName: string, routingName: string } => {
148122
const parsedPath = parseName(options.path || '', options.name);
149123
const name = dasherize(parsedPath.name);

src/generate/module/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('Module Schematic', () => {
226226
expect(tree.exists(webModulePath)).toBeTruthy();
227227
});
228228

229-
it('should not create a common file', async () => {
229+
it('should create a common file', async () => {
230230
const options = { ...nsWebOptions };
231231
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
232232

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { NativeScriptCommonModule } from 'nativescript-angular/common';
3+
4+
@NgModule({
5+
imports: [
6+
NativeScriptCommonModule
7+
],
8+
declarations: [
9+
],
10+
providers: [
11+
],
12+
schemas: [
13+
NO_ERRORS_SCHEMA
14+
]
15+
})
16+
export class <%= classify(name) %>Module { }

src/migrate-module/index.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import {
55
chain,
66
schematic,
77
SchematicsException,
8+
template,
9+
mergeWith,
10+
apply,
11+
url,
12+
move,
813
} from '@angular-devkit/schematics';
914
import { addProviderToModule } from '@schematics/angular/utility/ast-utils';
1015
import { InsertChange } from '@schematics/angular/utility/change';
16+
import { dasherize, classify } from '@angular-devkit/core/src/utils/strings';
17+
import { dirname, Path } from '@angular-devkit/core';
1118

1219
import { addExtension } from '../utils';
1320
import { getSourceFile } from '../ts-utils';
1421
import { getNsConfigExtension } from '../generate/utils';
1522
import { parseModuleInfo, ModuleInfo } from './module-info-utils';
1623

17-
import { Schema as ModuleSchema } from '../generate/module/schema';
1824
import { Schema as MigrateComponentSchema } from '../migrate-component/schema';
25+
import { Schema as CommonModuleSchema } from '../generate/common-module/schema';
1926
import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema';
2027
import { Schema as MigrateModuleSchema } from './schema';
2128

@@ -36,30 +43,52 @@ export default function(options: MigrateModuleSchema): Rule {
3643
moduleInfo = parseModuleInfo(options)(tree, context);
3744
},
3845

39-
addModuleFile(options.name, options.project),
46+
(tree) => {
47+
const moduleDir = dirname(moduleInfo.modulePath as Path);
48+
49+
return addModuleFile(options.name, nsext, moduleDir)(tree);
50+
},
4051

4152
(tree, context) => migrateComponents(moduleInfo, options)(tree, context),
4253
migrateProviders(),
4354

55+
() => addCommonModuleFile(options, moduleInfo),
56+
4457
schematic<ConvertRelativeImportsSchema>('convert-relative-imports', options),
4558
]);
4659
}
4760

61+
const addCommonModuleFile = (options, modInfo) => {
62+
const { name } = options;
63+
const { modulePath } = modInfo;
64+
const moduleDirectory = dirname(modulePath);
65+
const commonModuleOptions = {
66+
name,
67+
path: moduleDirectory,
68+
};
69+
70+
return schematic<CommonModuleSchema>('common-module', commonModuleOptions);
71+
};
72+
4873
const addModuleFile =
49-
(name: string, project: string) =>
50-
(tree: Tree, context: SchematicContext) =>
51-
schematic('module', {
52-
name,
53-
project,
54-
nsExtension: nsext,
55-
flat: false,
56-
web: false,
57-
spec: false,
58-
common: true,
59-
})(tree, context);
74+
(name: string, nsExtension: string, path: string) =>
75+
(_tree: Tree) => {
76+
const templateSource = apply(url('./_ns-files'), [
77+
template({
78+
name,
79+
nsext: nsExtension,
80+
dasherize,
81+
classify,
82+
}),
83+
move(path),
84+
]);
85+
86+
return mergeWith(templateSource);
87+
};
6088

6189
const migrateComponents = (modInfo: ModuleInfo, options: MigrateModuleSchema) => {
62-
const components = modInfo.declarations.filter((d) => d.name.endsWith('Component'));
90+
const isComponent = (className: string) => className.endsWith('Component');
91+
const components = modInfo.declarations.filter(({ name }) => isComponent(name));
6392

6493
return chain(
6594
components.map((component) => {

0 commit comments

Comments
 (0)