Skip to content

Commit 87432d3

Browse files
committed
✨ Added ng add schematic support to lazy-dialogs project
1 parent b542e3a commit 87432d3

File tree

7 files changed

+971
-33
lines changed

7 files changed

+971
-33
lines changed

projects/lazy-dialogs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.0-rc.5] - 2022-08-22
9+
10+
### Added
11+
12+
- Support for ng add schematic
13+
814
## [1.0.0-rc.4] - 2022-08-19
915

1016
### Changed

projects/lazy-dialogs/README.md

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
- [@ngx-toolset/lazy-dialogs](#ngx-toolsetlazy-dialogs)
1111
- [Features](#features)
1212
- [Installation](#installation)
13-
- [NPM](#npm)
1413
- [Usage](#usage)
15-
- [Module Import](#module-import)
1614
- [Dialog Container and Background Overlay Styles](#dialog-container-and-background-overlay-styles)
1715
- [Standalone Component](#standalone-component)
1816
- [Open Standalone Component Dialog](#open-standalone-component-dialog)
@@ -25,39 +23,12 @@
2523

2624
## Installation
2725

28-
### NPM
29-
30-
`npm install @ngx-toolset/lazy-dialogs --save`
31-
32-
Choose the version corresponding to your Angular version:
33-
34-
| Angular | @ngx-toolset/lazy-dialogs |
35-
|---------|---------------------------|
36-
| 14.x.x | 1.x.x |
26+
```
27+
ng add @ngx-toolset/lazy-dialogs
28+
```
3729

3830
## Usage
3931

40-
### Module Import
41-
42-
Import the `LazyDialogModule` in your `AppModule`:
43-
44-
```ts
45-
import { NgModule } from '@angular/core';
46-
import { BrowserModule } from '@angular/platform-browser';
47-
import { AppComponent } from './app.component';
48-
import { LazyDialogModule } from '@ngx-toolset/lazy-dialogs';
49-
50-
@NgModule({
51-
declarations: [AppComponent],
52-
imports: [
53-
BrowserModule,
54-
LazyDialogModule.forRoot(),
55-
],
56-
bootstrap: [AppComponent],
57-
})
58-
export class AppModule {}
59-
```
60-
6132
### Dialog Container and Background Overlay Styles
6233

6334
Provide your own CSS styles for the dialog's container which can also act as the dialog's background overlay to the `forRoot` function of the `LazyDialogModule`:

projects/lazy-dialogs/package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
{
22
"name": "@ngx-toolset/lazy-dialogs",
3-
"version": "1.0.0-rc.4",
3+
"version": "1.0.0-rc.5",
4+
"scripts": {
5+
"build": "tsc -p tsconfig.schematics.json",
6+
"postbuild": "copyfiles schematics/*/schema.json schematics/*/files/** schematics/collection.json ../../dist/lazy-dialogs/"
7+
},
8+
"schematics": "./schematics/collection.json",
9+
"ng-add": {
10+
"save": "dependencies"
11+
},
412
"description": "Angular lazy dialogs support via service",
513
"homepage": "https://github.com/SwabianCoder/ngx-toolset",
614
"license": "MIT",
@@ -29,5 +37,9 @@
2937
},
3038
"dependencies": {
3139
"tslib": "^2.3.0"
40+
},
41+
"devDependencies": {
42+
"copyfiles": "file:../../node_modules/copyfiles",
43+
"typescript": "file:../../node_modules/typescript"
3244
}
3345
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"ng-add": {
5+
"description": "Add @ngx-toolset/lazy-dialogs to the project.",
6+
"factory": "./ng-add/index#ngAdd"
7+
}
8+
}
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
Rule,
3+
SchematicContext,
4+
SchematicsException,
5+
Tree,
6+
} from '@angular-devkit/schematics';
7+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
8+
import { applyToUpdateRecorder } from '@schematics/angular/utility/change';
9+
import * as ts from 'typescript';
10+
import { addSymbolToNgModuleMetadata } from '../utility/ast-utils';
11+
12+
export function ngAdd(): Rule {
13+
return (tree: Tree, context: SchematicContext) => {
14+
context.logger.info('Installing dependencies...');
15+
context.addTask(new NodePackageInstallTask());
16+
17+
context.logger.info('Adding LazyDialogModule to the app...');
18+
const appModulePath = './src/app/app.module.ts';
19+
20+
if (!tree.exists(appModulePath)) {
21+
throw new SchematicsException(
22+
`The file ${appModulePath} doesn't exist...`
23+
);
24+
}
25+
26+
const recorder = tree.beginUpdate(appModulePath);
27+
const appModuleFileContent = tree.read(appModulePath);
28+
29+
if (appModuleFileContent === null) {
30+
throw new SchematicsException(
31+
`The content of ${appModulePath} couldn't be read...`
32+
);
33+
}
34+
35+
const appModuleFileText = appModuleFileContent.toString('utf-8');
36+
const sourceFile = ts.createSourceFile(
37+
appModulePath,
38+
appModuleFileText,
39+
ts.ScriptTarget.Latest,
40+
true
41+
);
42+
43+
applyToUpdateRecorder(
44+
recorder,
45+
addSymbolToNgModuleMetadata(
46+
sourceFile,
47+
appModulePath,
48+
'imports',
49+
'LazyDialogModule',
50+
'@ngx-toolset/lazy-dialogs',
51+
'LazyDialogModule.forRoot({})'
52+
)
53+
);
54+
55+
tree.commitUpdate(recorder);
56+
57+
return tree;
58+
};
59+
}

0 commit comments

Comments
 (0)