Skip to content

Commit

Permalink
🐛 Fixed ng add schematic of api-token-interceptor project
Browse files Browse the repository at this point in the history
  • Loading branch information
SwabianCoder committed Aug 20, 2022
1 parent 6cfcd2e commit b542e3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 65 deletions.
6 changes: 6 additions & 0 deletions projects/api-token-interceptor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0-rc.10] - 2022-08-20

### Fixed

- ng add schematic

## [1.0.0-rc.9] - 2022-08-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion projects/api-token-interceptor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngx-toolset/api-token-interceptor",
"version": "1.0.0-rc.9",
"version": "1.0.0-rc.10",
"scripts": {
"build": "tsc -p tsconfig.schematics.json",
"postbuild": "copyfiles schematics/*/schema.json schematics/*/files/** schematics/collection.json ../../dist/api-token-interceptor/"
Expand Down
121 changes: 57 additions & 64 deletions projects/api-token-interceptor/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,74 @@ import {
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { applyToUpdateRecorder } from '@schematics/angular/utility/change';
import { addSymbolToNgModuleMetadata } from '../utility/ast-utils';
import * as ts from 'typescript';
import { addSymbolToNgModuleMetadata } from '../utility/ast-utils';

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

return tree;
};
}
context.logger.info('Adding ApiTokenInterceptorModule to the app...');
const appModulePath = './src/app/app.module.ts';

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

if (!tree.exists(appModulePath)) {
throw new SchematicsException(`The file ${appModulePath} doesn't exist...`);
}
const recorder = tree.beginUpdate(appModulePath);
const appModuleFileContent = tree.read(appModulePath);

const recorder = tree.beginUpdate(appModulePath);
const appModuleFileContent = tree.read(appModulePath);
if (appModuleFileContent === null) {
throw new SchematicsException(
`The content of ${appModulePath} couldn't be read...`
);
}

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

const sourceFile = ts.createSourceFile(
appModulePath,
appModuleFileContent.toString(),
ts.ScriptTarget.ES2020
);
const importPath = '@ngx-toolset/api-token-interceptor';
for (const symbol of [
{
metadataField: 'imports',
symbolName: 'ApiTokenInterceptorModule',
importText: 'ApiTokenInterceptorModule.forRoot()',
},
{
metadataField: 'providers',
symbolName: 'API_URL_REGEX',
importText: '{ provide: API_URL_REGEX, useValue: /^/ },',
},
{
metadataField: 'providers',
symbolName: 'BEARER_TOKEN_CALLBACK_FN',
importText:
"{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', },",
},
]) {
applyToUpdateRecorder(
recorder,
addSymbolToNgModuleMetadata(
sourceFile,
appModulePath,
symbol.metadataField,
symbol.symbolName,
importPath,
symbol.importText
)
);
}

for (const symbol of [
{
metadataField: 'imports',
symbolName: 'ApiTokenInterceptorModule',
importText: 'ApiTokenInterceptorModule.forRoot()',
},
{
metadataField: 'providers',
symbolName: 'API_URL_REGEX',
importText:
'{ provide: API_URL_REGEX, useValue: /^https://sample-regex.com/ }',
},
{
metadataField: 'providers',
symbolName: 'BEARER_TOKEN_CALLBACK_FN',
importText:
"{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', }",
},
]) {
applyToUpdateRecorder(
recorder,
addSymbolToNgModuleMetadata(
sourceFile,
appModulePath,
symbol.metadataField,
symbol.symbolName,
importPath,
symbol.importText
)
);
}

tree.commitUpdate(recorder);
}
tree.commitUpdate(recorder);

function installDependencies(context: SchematicContext): void {
context.logger.info('Installing dependencies...');
context.addTask(new NodePackageInstallTask());
return tree;
};
}

0 comments on commit b542e3a

Please sign in to comment.