Skip to content

Commit 9642451

Browse files
authored
Merge pull request #318 from AndriiNeverov/schematics-angular11-support
Upgraded Schematics to 11.1.2
2 parents 920cfc2 + d5744a1 commit 9642451

File tree

6 files changed

+107
-114
lines changed

6 files changed

+107
-114
lines changed

package-lock.json

Lines changed: 61 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"@angular/language-service": "^11.1.2",
6666
"@nguniversal/builders": "^11.1.1",
6767
"@release-it/conventional-changelog": "^1.1.4",
68-
"@schematics/angular": "^9.1.13",
68+
"@schematics/angular": "^11.1.2",
6969
"@types/express": "^4.17.11",
7070
"@types/jasmine": "^3.6.3",
7171
"@types/jasminewd2": "~2.0.3",

projects/angular-material-extensions/password-strength/schematics/helpers/angular/ast-utils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
addSymbolToNgModuleMetadata as originalAddSymbolToNgModuleMetadata,
1010
findNode as originalFindNode,
1111
findNodes as originalFindNodes,
12-
getContentOfKeyLiteral as originalGetContentOfKeyLiteral,
1312
getDecoratorMetadata as originalGetDecoratorMetadata,
14-
getFirstNgModuleName as originalGetFirstNgModuleName,
1513
getMetadataField as originalGetMetadataField,
1614
getRouterModuleDeclaration as originalGetRouterModuleDeclaration,
1715
getSourceNodes as originalGetSourceNodes,
@@ -91,24 +89,10 @@ export function insertAfterLastOccurrence(
9189
return originalInsertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind);
9290
}
9391

94-
export function getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string | null {
95-
return originalGetContentOfKeyLiteral(_source, node);
96-
}
97-
9892
export function getDecoratorMetadata(source: ts.SourceFile, identifier: string, module: string): ts.Node[] {
9993
return originalGetDecoratorMetadata(source, identifier, module);
10094
}
10195

102-
/**
103-
* Given a source file with @NgModule class(es), find the name of the first @NgModule class.
104-
*
105-
* @param source source file containing one or more @NgModule
106-
* @returns the name of the first @NgModule, or `undefined` if none is found
107-
*/
108-
export function getFirstNgModuleName(source: ts.SourceFile): string | undefined {
109-
return originalGetFirstNgModuleName(source);
110-
}
111-
11296
export function getMetadataField(node: ts.ObjectLiteralExpression, metadataField: string): ts.ObjectLiteralElement[] {
11397
return originalGetMetadataField(node, metadataField);
11498
}

projects/angular-material-extensions/password-strength/schematics/helpers/angular/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export * from './change';
33
export * from './component-schema';
44
export * from './dependencies';
55
export * from './find-module';
6-
export * from './json-utils';
76
export * from './latest-versions';
87
export * from './ng-ast-utils';
98
export * from './parse-name';

projects/angular-material-extensions/password-strength/schematics/helpers/angular/json-utils.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

projects/angular-material-extensions/password-strength/schematics/ng-add/index.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {chain, noop, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
1+
import { virtualFs, workspaces } from '@angular-devkit/core';
2+
import {chain, noop, Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
23
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
34
import {addPackageJsonDependency, NodeDependency, NodeDependencyType} from '../helpers';
4-
import {getWorkspace} from '@schematics/angular/utility/config';
5-
import {addModuleImportToRootModule, getProjectFromWorkspace} from '@angular/cdk/schematics';
5+
import {addModuleImportToRootModule} from '@angular/cdk/schematics';
66

77
/** Loads the full version from the given Angular package gracefully. */
88
function loadPackageVersionGracefully(context: SchematicContext): string | null {
@@ -50,15 +50,12 @@ export function installPackageJsonDependencies(): Rule {
5050
};
5151
}
5252

53-
export function addModuleToImports(options: any): Rule {
53+
export function addModuleToImports(options: any, project: any): Rule {
5454
return (host: Tree, context: SchematicContext) => {
55-
const workspace = getWorkspace(host);
56-
// @ts-ignore
57-
const project = getProjectFromWorkspace(workspace, options.project);
5855
const moduleName = 'MatPasswordStrengthModule';
5956

6057
addModuleImportToRootModule(host, moduleName, '@angular-material-extensions/password-strength', project);
61-
context.logger.log('info', `✅️ "${moduleName}" is imported`);
58+
context.logger.log('info', `✅️ "${moduleName}" is imported into project ${options.project}`);
6259

6360
return host;
6461
};
@@ -79,10 +76,45 @@ export function getPackageVersionFromPackageJson(tree: Tree, name: string): stri
7976
return null;
8077
}
8178

79+
function createHost(tree: Tree): workspaces.WorkspaceHost {
80+
return {
81+
async readFile(path: string): Promise<string> {
82+
const data = tree.read(path);
83+
if (!data) {
84+
throw new SchematicsException('File not found.');
85+
}
86+
return virtualFs.fileBufferToString(data);
87+
},
88+
async writeFile(path: string, data: string): Promise<void> {
89+
return tree.overwrite(path, data);
90+
},
91+
async isDirectory(path: string): Promise<boolean> {
92+
return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
93+
},
94+
async isFile(path: string): Promise<boolean> {
95+
return tree.exists(path);
96+
},
97+
};
98+
}
99+
82100
export default function (options: any): Rule {
83-
return chain([
84-
options && options.skipPackageJson ? noop() : addPackageJsonDependencies(),
85-
options && options.skipPackageJson ? noop() : installPackageJsonDependencies(),
86-
options && options.skipModuleImport ? noop() : addModuleToImports(options),
87-
]);
101+
return async (tree: Tree) => {
102+
const host = createHost(tree);
103+
const { workspace } = await workspaces.readWorkspace('/', host);
104+
105+
if (!options.project) {
106+
options.project = workspace.extensions.defaultProject;
107+
}
108+
109+
const project = workspace.projects.get(options.project);
110+
if (!project) {
111+
throw new SchematicsException(`Invalid project name: ${options.project}`);
112+
}
113+
114+
return chain([
115+
options && options.skipPackageJson ? noop() : addPackageJsonDependencies(),
116+
options && options.skipPackageJson ? noop() : installPackageJsonDependencies(),
117+
options && options.skipModuleImport ? noop() : addModuleToImports(options, project),
118+
]);
119+
};
88120
}

0 commit comments

Comments
 (0)