Skip to content

Commit ad55075

Browse files
authored
Merge pull request #111 from SwabianCoder/feature/97-add-ng-add-schematic-to-ngx-toolset-date-interceptors
Feature/97 add ng add schematic to ngx toolset date interceptors
2 parents de0dcfa + ee9989a commit ad55075

File tree

8 files changed

+997
-50
lines changed

8 files changed

+997
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"start": "ng serve",
66
"build:api-token-interceptor": "ng b @ngx-toolset/api-token-interceptor && cd ./projects/api-token-interceptor && npm run build",
77
"build:api-token-interceptor:watch": "npm run build:api-token-interceptor -- --watch -c development",
8-
"build:date-interceptors": "ng b @ngx-toolset/date-interceptors",
8+
"build:date-interceptors": "ng b @ngx-toolset/date-interceptors && cd ./projects/date-interceptors && npm run build",
99
"build:date-interceptors:watch": "npm run build:date-interceptors -- --watch -c development",
1010
"build:lazy-dialogs": "ng b @ngx-toolset/lazy-dialogs && cd ./projects/lazy-dialogs && npm run build",
1111
"build:lazy-dialogs:watch": "npm run build:lazy-dialogs -- --watch -c development",

projects/date-interceptors/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.9] - 2022-08-22
9+
10+
### Added
11+
12+
- Support for ng add schematic
13+
814
## [1.0.0-rc.8] - 2022-08-19
915

1016
### Changed

projects/date-interceptors/README.md

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
- [@ngx-toolset/date-interceptors](#ngx-toolsetdate-interceptors)
1111
- [Features](#features)
1212
- [Installation](#installation)
13-
- [NPM](#npm)
1413
- [Usage](#usage)
15-
- [Module Import](#module-import)
1614
- [Provide Injection Tokens](#provide-injection-tokens)
1715
- [Injection Tokens](#injection-tokens)
1816
- [API_DATE_FORMAT](#api_date_format)
@@ -28,15 +26,9 @@
2826
2927
## Installation
3028

31-
### NPM
32-
33-
`npm install @ngx-toolset/date-interceptors --save`
34-
35-
Choose the version corresponding to your Angular version:
36-
37-
| Angular | @ngx-toolset/date-interceptors |
38-
|---------|--------------------------------|
39-
| 14.x.x | 1.x.x |
29+
```
30+
ng add @ngx-toolset/date-interceptors
31+
```
4032

4133
## Usage
4234

@@ -63,43 +55,7 @@ export class AppModule {}
6355

6456
### Provide Injection Tokens
6557

66-
Provide `API_DATE_FORMAT`, `API_URL_REGEX` and `DATE_STRING_REGEX` in your `AppModule`:
67-
68-
```ts
69-
import { NgModule } from '@angular/core';
70-
import { BrowserModule } from '@angular/platform-browser';
71-
import { AppComponent } from './app.component';
72-
import {
73-
DateInterceptorsModule,
74-
API_URL_REGEX,
75-
DATE_STRING_REGEX,
76-
API_DATE_FORMAT
77-
} from '@ngx-toolset/date-interceptors';
78-
79-
@NgModule({
80-
declarations: [AppComponent],
81-
imports: [
82-
BrowserModule,
83-
DateInterceptorsModule.forRoot(),
84-
],
85-
providers: [
86-
{
87-
provide: API_URL_REGEX,
88-
useValue: /^https:\/\/test-url.com/
89-
},
90-
{
91-
provide: DATE_STRING_REGEX,
92-
useValue: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/,
93-
},
94-
{
95-
provide: API_DATE_FORMAT,
96-
useValue: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
97-
},
98-
],
99-
bootstrap: [AppComponent],
100-
})
101-
export class AppModule {}
102-
```
58+
Provide proper values for `API_DATE_FORMAT`, `API_URL_REGEX` and `DATE_STRING_REGEX` in your `AppModule`.
10359

10460
> Hint: The list of options to provide `API_DATE_FORMAT` value could be found here: [date-fns documentation](https://date-fns.org/v2.29.1/docs/parse).
10561

projects/date-interceptors/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/date-interceptors",
3-
"version": "1.0.0-rc.8",
3+
"version": "1.0.0-rc.9",
4+
"scripts": {
5+
"build": "tsc -p tsconfig.schematics.json",
6+
"postbuild": "copyfiles schematics/*/schema.json schematics/*/files/** schematics/collection.json ../../dist/date-interceptors/"
7+
},
8+
"schematics": "./schematics/collection.json",
9+
"ng-add": {
10+
"save": "dependencies"
11+
},
412
"description": "Angular date interceptors (parses HTTP response body date strings to date objects and converts HTTP request body date objects to date strings of given format)",
513
"homepage": "https://github.com/SwabianCoder/ngx-toolset",
614
"license": "MIT",
@@ -32,5 +40,9 @@
3240
},
3341
"dependencies": {
3442
"tslib": "^2.3.0"
43+
},
44+
"devDependencies": {
45+
"copyfiles": "file:../../node_modules/copyfiles",
46+
"typescript": "file:../../node_modules/typescript"
3547
}
3648
}
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/date-interceptors to the project.",
6+
"factory": "./ng-add/index#ngAdd"
7+
}
8+
}
9+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 DateInterceptorsModule 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+
const importPath = '@ngx-toolset/date-interceptors';
43+
44+
for (const symbol of [
45+
{
46+
metadataField: 'imports',
47+
symbolName: 'DateInterceptorsModule',
48+
importText: 'DateInterceptorsModule.forRoot()',
49+
},
50+
{
51+
metadataField: 'providers',
52+
symbolName: 'API_URL_REGEX',
53+
importText: '{ provide: API_URL_REGEX, useValue: /^/ },',
54+
},
55+
{
56+
metadataField: 'providers',
57+
symbolName: 'API_DATE_FORMAT',
58+
importText: "{ provide: API_DATE_FORMAT, useValue: '' },",
59+
},
60+
{
61+
metadataField: 'providers',
62+
symbolName: 'DATE_STRING_REGEX',
63+
importText: "{ provide: DATE_STRING_REGEX, useValue: '' },",
64+
},
65+
]) {
66+
applyToUpdateRecorder(
67+
recorder,
68+
addSymbolToNgModuleMetadata(
69+
sourceFile,
70+
appModulePath,
71+
symbol.metadataField,
72+
symbol.symbolName,
73+
importPath,
74+
symbol.importText
75+
)
76+
);
77+
}
78+
79+
tree.commitUpdate(recorder);
80+
81+
return tree;
82+
};
83+
}

0 commit comments

Comments
 (0)