diff --git a/.gitignore b/.gitignore index 4d428e9..cb5bd77 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ jsconfig.json node_modules/ npm-debug.log* yarn-error.log* +package-lock.json # Mac OSX Finder files. **/.DS_Store diff --git a/src/angular-project-parser.ts b/src/angular-project-parser.ts index 1f5a99f..58d246e 100644 --- a/src/angular-project-parser.ts +++ b/src/angular-project-parser.ts @@ -9,8 +9,8 @@ import { findBootstrapModulePath, } from '@schematics/angular/utility/ng-ast-utils'; -import { getSourceFile, safeGet } from './utils'; -import { findNode, findImportPath } from './ast-utils'; +import { safeGet } from './utils'; +import { findNode, findImportPath, getSourceFile } from './ts-utils'; export interface AngularProjectSettings { /** default: '' */ diff --git a/src/convert-relative-imports/index.ts b/src/convert-relative-imports/index.ts index 4888492..d62694d 100644 --- a/src/convert-relative-imports/index.ts +++ b/src/convert-relative-imports/index.ts @@ -6,7 +6,8 @@ import { LoggerApi } from '@angular-devkit/core/src/logger'; import { PreferMappedImportsRule } from '@nativescript/tslint-rules'; import { parseCompilerOptions } from '@nativescript/tslint-rules/dist/preferMappedImportsRule'; -import { parseTsConfigFile, getFileContents } from '../utils'; +import { parseTsConfigFile } from '../ts-utils'; +import { getFileContents } from '../utils'; import { getTsConfigFromProject } from '../angular-project-parser'; import { Schema as ConvertRelativeImportsSchema } from './schema'; diff --git a/src/decorator-utils.ts b/src/decorator-utils.ts index b42e5a4..66aaf48 100644 --- a/src/decorator-utils.ts +++ b/src/decorator-utils.ts @@ -1,8 +1,7 @@ import * as ts from 'typescript'; -import { findNode, findMatchingNodes, findImportPath } from './ast-utils'; import { Tree } from '@angular-devkit/schematics'; -import { getSourceFile } from './utils'; +import { findNode, findMatchingNodes, findImportPath, getSourceFile } from './ts-utils'; export interface ClassImport { name: string; diff --git a/src/generate-template/master-detail/index_spec.ts b/src/generate-template/master-detail/index_spec.ts index 07c18ac..b39e022 100644 --- a/src/generate-template/master-detail/index_spec.ts +++ b/src/generate-template/master-detail/index_spec.ts @@ -5,8 +5,8 @@ import { HostTree } from '@angular-devkit/schematics'; import { Schema as MasterDetailOptions } from './schema'; import { createEmptyNsOnlyProject, createEmptySharedProject } from '../../test-utils'; -import { getSourceFile, toComponentClassName } from '../../utils'; -import { findImports } from '../../ast-utils'; +import { toComponentClassName } from '../../utils'; +import { getSourceFile, findImports } from '../../ts-utils'; describe('Master-detail schematic', () => { const master = 'heroes'; diff --git a/src/generate/component/ast-utils.ts b/src/generate/component/ast-utils.ts index 0d9a461..5ba7017 100644 --- a/src/generate/component/ast-utils.ts +++ b/src/generate/component/ast-utils.ts @@ -6,8 +6,7 @@ import { buildRelativePath } from '@schematics/angular/utility/find-module'; import { InsertChange, Change } from '@schematics/angular/utility/change'; import { addEntryComponentToModule, addExportToModule, addDeclarationToModule } from '@schematics/angular/utility/ast-utils'; import { Schema as ComponentOptions } from './schema'; -import { getSourceFile } from '../../utils'; -import { addSymbolToDecoratorMetadata } from '../../ast-utils'; +import { addSymbolToDecoratorMetadata, getSourceFile } from '../../ts-utils'; export const insertModuleId = (tree: Tree, component: string) => { const componentSource = getSourceFile(tree, component); diff --git a/src/generate/component/index_spec.ts b/src/generate/component/index_spec.ts index 0d5a954..73cfffa 100644 --- a/src/generate/component/index_spec.ts +++ b/src/generate/component/index_spec.ts @@ -3,12 +3,12 @@ import { join } from 'path'; import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; import { getFileContent } from '@schematics/angular/utility/test'; -import { toComponentClassName, getSourceFile } from '../../utils'; +import { toComponentClassName } from '../../utils'; import { createEmptyNsOnlyProject, createEmptySharedProject } from '../../test-utils'; import { DEFAULT_SHARED_EXTENSIONS } from '../utils'; import { isInComponentMetadata, isInModuleMetadata } from '../../test-utils'; import { Schema as ComponentOptions } from './schema'; -import { findImports } from '../../ast-utils'; +import { findImports, getSourceFile } from '../../ts-utils'; describe('Component Schematic', () => { const name = 'foo'; diff --git a/src/generate/module/index.ts b/src/generate/module/index.ts index e22fef1..920cf88 100644 --- a/src/generate/module/index.ts +++ b/src/generate/module/index.ts @@ -16,14 +16,12 @@ import { InsertChange } from '@schematics/angular/utility/change'; import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils'; import { Schema as ModuleOptions } from './schema'; -import { - getSourceFile, - copy, -} from '../../utils'; +import { copy } from '../../utils'; import { removeImport, removeMetadataArrayValue, -} from '../../ast-utils'; + getSourceFile, +} from '../../ts-utils'; import { dasherize } from '@angular-devkit/core/src/utils/strings'; import { removeNsSchemaOptions, getExtensions, PlatformUse, getPlatformUse, Extensions, addExtension, validateGenerateOptions } from '../utils'; import { parseName } from '@schematics/angular/utility/parse-name'; diff --git a/src/migrate-component/component-info-utils.ts b/src/migrate-component/component-info-utils.ts index a12a7fa..23c76ab 100644 --- a/src/migrate-component/component-info-utils.ts +++ b/src/migrate-component/component-info-utils.ts @@ -6,8 +6,7 @@ import { join, dirname } from 'path'; import * as ts from 'typescript'; import { AngularProjectSettings, getAngularProjectSettings } from '../angular-project-parser'; -import { getSourceFile } from '../utils'; -import { findImportPath, findMatchingNodes } from '../ast-utils'; +import { findImportPath, findMatchingNodes, getSourceFile } from '../ts-utils'; import { findDecoratorPropertyNode } from '../decorator-utils'; export interface ComponentInfo { diff --git a/src/migrate-component/index.ts b/src/migrate-component/index.ts index 654903b..dc11af6 100644 --- a/src/migrate-component/index.ts +++ b/src/migrate-component/index.ts @@ -1,3 +1,4 @@ +import { dirname, basename } from 'path'; import { Rule, SchematicContext, @@ -15,15 +16,13 @@ import { import { InsertChange } from '@schematics/angular/utility/change'; import { addDeclarationToModule } from '@schematics/angular/utility/ast-utils'; -import { dirname, basename } from 'path'; - -import { Schema as MigrateComponentSchema } from './schema'; - -import { getSourceFile, addExtension, findRelativeImportPath, getFileContents } from '../utils'; -import { ComponentInfo, parseComponentInfo } from './component-info-utils'; +import { addExtension, findRelativeImportPath, getFileContents } from '../utils'; import { getNsConfigExtension, Extensions } from '../generate/utils'; - import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema'; +import { getSourceFile } from '../ts-utils'; + +import { ComponentInfo, parseComponentInfo } from './component-info-utils'; +import { Schema as MigrateComponentSchema } from './schema'; let extensions: Extensions; diff --git a/src/migrate-component/index_spec.ts b/src/migrate-component/index_spec.ts index 3eeae09..a579289 100644 --- a/src/migrate-component/index_spec.ts +++ b/src/migrate-component/index_spec.ts @@ -8,9 +8,9 @@ import { isInModuleMetadata } from '../test-utils'; import { Schema as ApplicationOptions } from '../ng-new/shared/schema'; import { Schema as ModuleOptions } from '../generate/module/schema'; import { Schema as ComponentOptions } from '../generate/component/schema'; -import { moveToRoot, getSourceFile } from '../utils'; +import { moveToRoot } from '../utils'; +import { findImports, getSourceFile } from '../ts-utils'; import { Schema as MigrateComponentOptions } from './schema'; -import { findImports } from '../ast-utils'; describe('Migrate component schematic', () => { const project = 'some-project'; diff --git a/src/migrate-module/index.ts b/src/migrate-module/index.ts index d7e84f8..36c5b61 100644 --- a/src/migrate-module/index.ts +++ b/src/migrate-module/index.ts @@ -6,19 +6,18 @@ import { schematic, SchematicsException } from '@angular-devkit/schematics'; - -import { addExtension, getSourceFile } from '../utils'; - -import { Schema as MigrateModuleSchema } from './schema'; -import { Schema as ModuleSchema } from '../generate/module/schema'; -import { Schema as MigrateComponentSchema } from '../migrate-component/schema'; - -import { parseModuleInfo, ModuleInfo } from './module-info-utils'; import { addProviderToModule } from '@schematics/angular/utility/ast-utils'; import { InsertChange } from '@schematics/angular/utility/change'; + +import { addExtension } from '../utils'; +import { getSourceFile } from '../ts-utils'; import { getNsConfigExtension } from '../generate/utils'; +import { parseModuleInfo, ModuleInfo } from './module-info-utils'; +import { Schema as ModuleSchema } from '../generate/module/schema'; +import { Schema as MigrateComponentSchema } from '../migrate-component/schema'; import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema'; +import { Schema as MigrateModuleSchema } from './schema'; let nsext: string; let moduleInfo: ModuleInfo; diff --git a/src/migrate-module/index_spec.ts b/src/migrate-module/index_spec.ts index 5100a85..0d49f8c 100644 --- a/src/migrate-module/index_spec.ts +++ b/src/migrate-module/index_spec.ts @@ -10,9 +10,9 @@ import { Schema as MigrateModuleOptions } from './schema'; import { Schema as ApplicationOptions } from '../ng-new/shared/schema'; import { Schema as ComponentOptions } from '../generate/component/schema'; import { Schema as ModuleOptions } from '../generate/module/schema'; -import { getSourceFile, moveToRoot } from '../utils'; +import { moveToRoot } from '../utils'; import { isInModuleMetadata } from '../test-utils'; -import { findImports } from '../ast-utils'; +import { findImports, getSourceFile } from '../ts-utils'; describe('Migrate module Schematic', () => { const project = 'some-project'; diff --git a/src/refactor-nsng-modules/index.ts b/src/refactor-nsng-modules/index.ts index 4cc8987..c96a523 100644 --- a/src/refactor-nsng-modules/index.ts +++ b/src/refactor-nsng-modules/index.ts @@ -7,7 +7,7 @@ import { import { insertImport } from '../route-utils'; import { Schema } from './schema'; -import { getJsonFile, getSourceFile, removeNode } from '../utils'; +import { getJsonFile, removeNode } from '../utils'; import { collectDeepNodes, filterByChildNode, @@ -18,7 +18,8 @@ import { getSymbolsToAddToObject, removeImport, getDecoratedClass, -} from '../ast-utils'; + getSourceFile, +} from '../ts-utils'; import * as ts from 'typescript'; import { SchematicsException } from '@angular-devkit/schematics/src/exception/exception'; diff --git a/src/route-utils.ts b/src/route-utils.ts index b9ff0ac..bcedb8e 100644 --- a/src/route-utils.ts +++ b/src/route-utils.ts @@ -1,7 +1,7 @@ import * as ts from 'typescript'; import { Change, NoopChange } from '@schematics/angular/utility/change'; import { findNodes } from '@schematics/angular/utility/ast-utils'; -import { insertBeforeFirstOccurence } from './ast-utils'; +import { insertBeforeFirstOccurence } from './ts-utils'; /** * Add Import `import { symbolName } from fileName` if the import doesn't exit diff --git a/src/ast-utils.ts b/src/ts-utils.ts similarity index 94% rename from src/ast-utils.ts rename to src/ts-utils.ts index 067d079..96acf99 100644 --- a/src/ast-utils.ts +++ b/src/ts-utils.ts @@ -3,7 +3,8 @@ import { InsertChange, Change } from '@schematics/angular/utility/change'; import { SchematicsException, Rule, Tree } from '@angular-devkit/schematics'; import * as ts from 'typescript'; -import { toComponentClassName, Node, getSourceFile, removeNode } from './utils'; +import { toComponentClassName, Node, removeNode, getFileContents, getJsonFile } from './utils'; +import { dirname } from 'path'; class RemoveContent implements Node { constructor(private pos: number, private end: number) { @@ -617,3 +618,31 @@ export const replaceTextInNode = (tree: Tree, node: ts.Node, oldText: string, ne recorder.insertLeft(index, newText); tree.commitUpdate(recorder); } + +export function parseTsConfigFile(tree: Tree, tsConfigPath: string): ts.ParsedCommandLine { + const config = getJsonFile(tree, tsConfigPath); + const host: ts.ParseConfigHost = { + useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, + readDirectory: ts.sys.readDirectory, + fileExists: (file: string) => tree.exists(file), + readFile: (file: string) => getFileContents(tree, file) + }; + const basePath = dirname(tsConfigPath); + + const tsConfigObject = ts.parseJsonConfigFileContent(config, host, basePath); + + return tsConfigObject; +} + +export const getSourceFile = (host: Tree, path: string): ts.SourceFile => { + const buffer = host.read(path); + if (!buffer) { + throw new SchematicsException( + `Could not find file at ${path}. See https://github.com/NativeScript/nativescript-schematics/issues/172.` + ); + } + const content = buffer.toString(); + const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); + + return source; +} diff --git a/src/utils.ts b/src/utils.ts index d7fa664..34c7fed 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { relative, join, dirname } from 'path'; +import { relative, join } from 'path'; import { SchematicsException, @@ -7,11 +7,9 @@ import { move, } from '@angular-devkit/schematics'; import { strings as angularStringUtils } from '@angular-devkit/core'; -import * as ts from 'typescript'; import { NsConfig } from './models/nsconfig'; import { UnitTestTree, SchematicTestRunner } from '@angular-devkit/schematics/testing'; - const PACKAGE_JSON = 'package.json'; export interface Node { @@ -43,19 +41,6 @@ export const schematicRunner = new SchematicTestRunner( join(__dirname, 'collection.json'), ); -export const getSourceFile = (host: Tree, path: string): ts.SourceFile => { - const buffer = host.read(path); - if (!buffer) { - throw new SchematicsException( - `Could not find file at ${path}. See https://github.com/NativeScript/nativescript-schematics/issues/172.` - ); - } - const content = buffer.toString(); - const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); - - return source; -} - export const removeNode = (node: Node, filePath: string, tree: Tree) => { const recorder = tree.beginUpdate(filePath); @@ -284,20 +269,3 @@ function callRuleSync( return newTree; } - -export function parseTsConfigFile(tree: Tree, tsConfigPath: string): ts.ParsedCommandLine { - const config = getJsonFile(tree, tsConfigPath); - const host: ts.ParseConfigHost = { - useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, - readDirectory: ts.sys.readDirectory, - fileExists: (file: string) => tree.exists(file), - readFile: (file: string) => getFileContents(tree, file) - }; - const basePath = dirname(tsConfigPath); - - const tsConfigObject = ts.parseJsonConfigFileContent(config, host, basePath); - - return tsConfigObject; -} - -