diff --git a/npm/ng-packs/packages/schematics/src/utils/common.ts b/npm/ng-packs/packages/schematics/src/utils/common.ts new file mode 100644 index 0000000000..8a5aa5fd93 --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/utils/common.ts @@ -0,0 +1,25 @@ +import { SchematicsException, Tree } from '@angular-devkit/schematics'; +import * as ts from 'typescript'; +import { Exception } from '../enums'; + +export function interpolate(text: string, ...params: (string | number | boolean)[]) { + params.forEach((param, i) => { + const pattern = new RegExp('{\\s*' + i + '\\s*}'); + text = text.replace(pattern, String(param)); + }); + + return text; +} + +export function isNullOrUndefined(value: any): value is null | undefined { + return value === null || value === undefined; +} + +export function readFileInTree(tree: Tree, filePath: string): ts.SourceFile { + const buffer = tree.read(filePath); + if (isNullOrUndefined(buffer)) + throw new SchematicsException(interpolate(Exception.FileNotFound, filePath)); + + const text = buffer.toString('utf-8'); + return ts.createSourceFile(filePath, text, ts.ScriptTarget.Latest, true); +} diff --git a/npm/ng-packs/packages/schematics/src/utils/index.ts b/npm/ng-packs/packages/schematics/src/utils/index.ts index 2110caa3d8..f489303b89 100644 --- a/npm/ng-packs/packages/schematics/src/utils/index.ts +++ b/npm/ng-packs/packages/schematics/src/utils/index.ts @@ -1,4 +1,5 @@ export * from './angular'; +export * from './common'; export * from './project'; export * from './text'; export * from './workspace';