Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"schema": "./generate/module/schema.json"
},


"common-module": {
"factory": "./generate/common-module",
"description": "Generate a common NgModule",
"schema": "./generate/common-module/schema.json"
},

"app-resources": {
"factory": "./app-resources",
"description": "Create App Resources",
Expand Down Expand Up @@ -108,6 +115,7 @@
"factory": "./migrate-component",
"schema": "./migrate-component/schema.json"
},

"migrate-module": {
"aliases": [ "mg" ],
"description": "Converts a module into a code sharing module",
Expand Down
10 changes: 10 additions & 0 deletions src/generate/common-module/_files/__name__.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Routes } from '@angular/router';

export const componentDeclarations: any[] = [
];

export const providerDeclarations: any[] = [
];

export const routes: Routes = [
];
21 changes: 21 additions & 0 deletions src/generate/common-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
Rule,
mergeWith,
apply,
url,
template,
move,
} from '@angular-devkit/schematics';

import { Schema as CommonModuleOptions } from './schema';

export default function(options: CommonModuleOptions): Rule {
const { name, path } = options;

return mergeWith(
apply(url('./_files'), [
template({ name }),
move(path),
]),
);
}
10 changes: 10 additions & 0 deletions src/generate/common-module/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Schema {
/**
* The name of the module.
*/
name: string;
/**
* The path to create the module.
*/
path: string;
}
19 changes: 19 additions & 0 deletions src/generate/common-module/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsNativeScriptAngularCommonModule",
"title": "NativeScript Angular Common Module Options Schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the module."
},
"path": {
"type": "string",
"format": "path",
"description": "The path to create the module."
}
},
"required": [],
"additionalProperties": false
}
38 changes: 6 additions & 32 deletions src/generate/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import {
chain,
externalSchematic,
SchematicsException,
mergeWith,
apply,
url,
template,
move,
branchAndMerge,
filter,
schematic,
} from '@angular-devkit/schematics';
import { InsertChange } from '@schematics/angular/utility/change';
import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
import { parseName } from '@schematics/angular/utility/parse-name';
import { dasherize } from '@angular-devkit/core/src/utils/strings';

import { Schema as CommonModuleSchema } from '../common-module/schema';
Copy link

@KristianDD KristianDD Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we import this just to load the Schema? I am asking as it seems that the next line will override the Schema value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema refers to the name of the export imported from the '../common-module/schema' module, whereas CommonModuleSchema refers to the binding imported into the local module scope. In short: CommonModuleSchema and ModuleOptions (next line) will be the local names of the imported symbols.

import { Schema as ModuleOptions } from './schema';
import { copy } from '../../utils';
import {
removeImport,
removeMetadataArrayValue,
getSourceFile,
} from '../../ts-utils';
import { dasherize } from '@angular-devkit/core/src/utils/strings';
import {
removeNsSchemaOptions,
getExtensions,
Expand All @@ -32,7 +29,6 @@ import {
addExtension,
validateGenerateOptions,
} from '../utils';
import { parseName } from '@schematics/angular/utility/parse-name';

class ModuleInfo {
name: string;
Expand Down Expand Up @@ -64,18 +60,6 @@ export default function(options: ModuleOptions): Rule {
let moduleInfo: ModuleInfo;

return branchAndMerge(chain([
// Filter existing modules with the same names so that they don't
// cause merge conflicts before the files are renamed.
// TODO: Fix. Huge performance hit! Filter goes trough node_modules + platforms.
filter((fileName) => {
const {
moduleName,
routingName,
} = getParsedName(options);

return ![moduleName, routingName].some((modName) => fileName.endsWith(modName));
}),

(tree: Tree) => {
platformUse = getPlatformUse(tree, options);

Expand Down Expand Up @@ -124,7 +108,8 @@ export default function(options: ModuleOptions): Rule {
},

(tree: Tree) => shouldCreateCommonFile(platformUse, options.common) ?
addCommonFile(moduleInfo) : tree,
schematic<CommonModuleSchema>('common-module', { name: moduleInfo.name, path: moduleInfo.path }) :
tree,
]));
}

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

const addCommonFile = (moduleInfo: ModuleInfo) => {
return mergeWith(
apply(url('./_files'), [
template({
name: moduleInfo.name,
}),
move(moduleInfo.path),
]),
);
};

const getParsedName = (options: ModuleOptions): { name: string, moduleName: string, routingName: string } => {
const parsedPath = parseName(options.path || '', options.name);
const name = dasherize(parsedPath.name);
Expand Down
2 changes: 1 addition & 1 deletion src/generate/module/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('Module Schematic', () => {
expect(tree.exists(webModulePath)).toBeTruthy();
});

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

Expand Down
16 changes: 16 additions & 0 deletions src/migrate-module/_ns-files/__name@dasherize__.module__nsext__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { NativeScriptCommonModule } from 'nativescript-angular/common';

@NgModule({
imports: [
NativeScriptCommonModule
],
declarations: [
],
providers: [
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class <%= classify(name) %>Module { }
57 changes: 43 additions & 14 deletions src/migrate-module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import {
chain,
schematic,
SchematicsException,
template,
mergeWith,
apply,
url,
move,
} from '@angular-devkit/schematics';
import { addProviderToModule } from '@schematics/angular/utility/ast-utils';
import { InsertChange } from '@schematics/angular/utility/change';
import { dasherize, classify } from '@angular-devkit/core/src/utils/strings';
import { dirname, Path } from '@angular-devkit/core';

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

import { Schema as ModuleSchema } from '../generate/module/schema';
import { Schema as MigrateComponentSchema } from '../migrate-component/schema';
import { Schema as CommonModuleSchema } from '../generate/common-module/schema';
import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema';
import { Schema as MigrateModuleSchema } from './schema';

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

addModuleFile(options.name, options.project),
(tree) => {
const moduleDir = dirname(moduleInfo.modulePath as Path);

return addModuleFile(options.name, nsext, moduleDir)(tree);
},

(tree, context) => migrateComponents(moduleInfo, options)(tree, context),
migrateProviders(),

() => addCommonModuleFile(options, moduleInfo),

schematic<ConvertRelativeImportsSchema>('convert-relative-imports', options),
]);
}

const addCommonModuleFile = (options, modInfo) => {
const { name } = options;
const { modulePath } = modInfo;
const moduleDirectory = dirname(modulePath);
const commonModuleOptions = {
name,
path: moduleDirectory,
};

return schematic<CommonModuleSchema>('common-module', commonModuleOptions);
};

const addModuleFile =
(name: string, project: string) =>
(tree: Tree, context: SchematicContext) =>
schematic('module', {
name,
project,
nsExtension: nsext,
flat: false,
web: false,
spec: false,
common: true,
})(tree, context);
(name: string, nsExtension: string, path: string) =>
(_tree: Tree) => {
const templateSource = apply(url('./_ns-files'), [
template({
name,
nsext: nsExtension,
dasherize,
classify,
}),
move(path),
]);

return mergeWith(templateSource);
};

const migrateComponents = (modInfo: ModuleInfo, options: MigrateModuleSchema) => {
const components = modInfo.declarations.filter((d) => d.name.endsWith('Component'));
const isComponent = (className: string) => className.endsWith('Component');
const components = modInfo.declarations.filter(({ name }) => isComponent(name));

return chain(
components.map((component) => {
Expand Down