import { posix as path } from 'path' ; import Project from 'ts-simple-ast' ; import { readFile } from './utilities/read-file' ; import { ImportAnalyzer } from './analyzer/import.analyzer' ; import { createTypescriptProject } from './utilities/create-typescript-project' ; /**
 * Angular Package
 */ export class AngularPackage { /**
     * Angular Package project directory
     */ public root : string ; /**
     * Entry file path (relative to cwd)
     */ public entryFile : string ; /**
     * Output directory path (relative to cwd)
     */ public outDir : string ; /**
     * Package name
     */ public packageName : string ; /**
     * Flag, describing whether the trny is primary (true) or secondary (false)
     */ public isPrimary : boolean ; /**
     * Custom TypeScript compiler options
     */ public typescriptCompilerOptions : { [ option ] : any ; } ; /**
     * Custom Angular compiler options
     */ public angularCompilerOptions : { [ option ] : any ; } ; /**
     * Dependencies (name -> id)
     */ public dependencies : { [ dependency ] : string ; } ; /**
     * List of external import sources
     */ public externalImportSources : Array < string > ; /**
     * TypeScript project
     */ public typescriptProject : Project ; public setRoot ( root ) { this . root = root ; return this ; } public setEntryFile ( entryFile ) { this . entryFile = path . normalize ( entryFile ) ; return this ; } public setOutDir ( outDir ) { this . outDir = path . normalize ( outDir ) ; return this ; } public setTypescriptCompilerOptions ( typescriptCompilerOptions ) { this . typescriptCompilerOptions = typescriptCompilerOptions ; return this ; } public setAngularCompilerOptions ( angularCompilerOptions ) { this . angularCompilerOptions = angularCompilerOptions ; return this ; } public setDependencies ( customDependencies ) { this . dependencies = customDependencies ; return this ; } public asPrimaryEntry ( ) { this . isPrimary = true ; return this ; } public asSecondaryEntry ( ) { this . isPrimary = false ; return this ; } public async init ( ) { const outDirSubFolder = this . isPrimary ? '' : path . dirname ( this . entryFile ) . split ( '/' ) . slice ( - 1 ) [ 0 ] ; this . outDir = path . join ( this . outDir , outDirSubFolder ) ; const absolutePackageJsonPath = path . join ( this . root , 'package.json' ) ; const packageJson = await readFile ( absolutePackageJsonPath ) ; this . packageName = path . join ( packageJson . name , outDirSubFolder ) ; const packageDependencies = [ ... Object . keys ( packageJson . dependencies || { } ) , ... Object . keys ( packageJson . optionalDependencies || { } ) , ... Object . keys ( packageJson . peerDependencies || { } ) ] . reduce ( ( dependencyMap , dependency ) : { [ dependency ] : string ; } => { dependencyMap [ dependency ] = '' ; return dependencyMap ; } , { } ) ; this . dependencies = { ... packageDependencies , ... this . dependencies } ; this . typescriptProject = createTypescriptProject ( path . join ( this . root , this . entryFile ) ) ; this . externalImportSources = ImportAnalyzer . getExternalImportSources ( this . typescriptProject ) ; return this ; } public addCustomModulePaths ( moduleNameWithPaths ) { this . typescriptCompilerOptions . paths = { ... ( this . typescriptCompilerOptions . paths || { } ) , ... moduleNameWithPaths } ; } }