Skip to content

Commit b542e3a

Browse files
committed
🐛 Fixed ng add schematic of api-token-interceptor project
1 parent 6cfcd2e commit b542e3a

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

projects/api-token-interceptor/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.10] - 2022-08-20
9+
10+
### Fixed
11+
12+
- ng add schematic
13+
814
## [1.0.0-rc.9] - 2022-08-20
915

1016
### Added

projects/api-token-interceptor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngx-toolset/api-token-interceptor",
3-
"version": "1.0.0-rc.9",
3+
"version": "1.0.0-rc.10",
44
"scripts": {
55
"build": "tsc -p tsconfig.schematics.json",
66
"postbuild": "copyfiles schematics/*/schema.json schematics/*/files/** schematics/collection.json ../../dist/api-token-interceptor/"

projects/api-token-interceptor/schematics/ng-add/index.ts

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,74 @@ import {
66
} from '@angular-devkit/schematics';
77
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
88
import { applyToUpdateRecorder } from '@schematics/angular/utility/change';
9-
import { addSymbolToNgModuleMetadata } from '../utility/ast-utils';
109
import * as ts from 'typescript';
10+
import { addSymbolToNgModuleMetadata } from '../utility/ast-utils';
1111

1212
export function ngAdd(): Rule {
1313
return (tree: Tree, context: SchematicContext) => {
14-
addModuleAndInjectionTokens(tree, context);
15-
installDependencies(context);
14+
context.logger.info('Installing dependencies...');
15+
context.addTask(new NodePackageInstallTask());
1616

17-
return tree;
18-
};
19-
}
17+
context.logger.info('Adding ApiTokenInterceptorModule to the app...');
18+
const appModulePath = './src/app/app.module.ts';
2019

21-
function addModuleAndInjectionTokens(
22-
tree: Tree,
23-
context: SchematicContext
24-
): void {
25-
context.logger.info('Adding ApiTokenInterceptorModule to the app...');
26-
const appModulePath = '/src/app/app.module.ts';
20+
if (!tree.exists(appModulePath)) {
21+
throw new SchematicsException(
22+
`The file ${appModulePath} doesn't exist...`
23+
);
24+
}
2725

28-
if (!tree.exists(appModulePath)) {
29-
throw new SchematicsException(`The file ${appModulePath} doesn't exist...`);
30-
}
26+
const recorder = tree.beginUpdate(appModulePath);
27+
const appModuleFileContent = tree.read(appModulePath);
3128

32-
const recorder = tree.beginUpdate(appModulePath);
33-
const appModuleFileContent = tree.read(appModulePath);
29+
if (appModuleFileContent === null) {
30+
throw new SchematicsException(
31+
`The content of ${appModulePath} couldn't be read...`
32+
);
33+
}
3434

35-
if (appModuleFileContent === null) {
36-
throw new SchematicsException(
37-
`The content of ${appModulePath} couldn't be read...`
35+
const appModuleFileText = appModuleFileContent.toString('utf-8');
36+
const sourceFile = ts.createSourceFile(
37+
appModulePath,
38+
appModuleFileText,
39+
ts.ScriptTarget.Latest,
40+
true
3841
);
39-
}
42+
const importPath = '@ngx-toolset/api-token-interceptor';
4043

41-
const sourceFile = ts.createSourceFile(
42-
appModulePath,
43-
appModuleFileContent.toString(),
44-
ts.ScriptTarget.ES2020
45-
);
46-
const importPath = '@ngx-toolset/api-token-interceptor';
44+
for (const symbol of [
45+
{
46+
metadataField: 'imports',
47+
symbolName: 'ApiTokenInterceptorModule',
48+
importText: 'ApiTokenInterceptorModule.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: 'BEARER_TOKEN_CALLBACK_FN',
58+
importText:
59+
"{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', },",
60+
},
61+
]) {
62+
applyToUpdateRecorder(
63+
recorder,
64+
addSymbolToNgModuleMetadata(
65+
sourceFile,
66+
appModulePath,
67+
symbol.metadataField,
68+
symbol.symbolName,
69+
importPath,
70+
symbol.importText
71+
)
72+
);
73+
}
4774

48-
for (const symbol of [
49-
{
50-
metadataField: 'imports',
51-
symbolName: 'ApiTokenInterceptorModule',
52-
importText: 'ApiTokenInterceptorModule.forRoot()',
53-
},
54-
{
55-
metadataField: 'providers',
56-
symbolName: 'API_URL_REGEX',
57-
importText:
58-
'{ provide: API_URL_REGEX, useValue: /^https://sample-regex.com/ }',
59-
},
60-
{
61-
metadataField: 'providers',
62-
symbolName: 'BEARER_TOKEN_CALLBACK_FN',
63-
importText:
64-
"{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', }",
65-
},
66-
]) {
67-
applyToUpdateRecorder(
68-
recorder,
69-
addSymbolToNgModuleMetadata(
70-
sourceFile,
71-
appModulePath,
72-
symbol.metadataField,
73-
symbol.symbolName,
74-
importPath,
75-
symbol.importText
76-
)
77-
);
78-
}
79-
80-
tree.commitUpdate(recorder);
81-
}
75+
tree.commitUpdate(recorder);
8276

83-
function installDependencies(context: SchematicContext): void {
84-
context.logger.info('Installing dependencies...');
85-
context.addTask(new NodePackageInstallTask());
77+
return tree;
78+
};
8679
}

0 commit comments

Comments
 (0)