import { posix as path } from 'path' ; import { Schema , validate , ValidatorResult , ValidationError } from 'jsonschema' ; import { AngularPackageConfig , AngularSubPackageConfig } from './angular-package-config.interface' ; import { AngularPackage } from './angular-package' ; import { readFile } from './utilities/read-file' ; import * as angularPackageSchema from '../angular-package.schema.json' ; /**
 * Angular Package Reader
 */ export class AngularPackageReader { /**
     * Current working directory
     */ private static readonly cwd : string = process . cwd ( ) . replace ( /\\/g , '/' ) ; param angularPackageJsonPaths returns public static async readAngularPackageJsonFiles ( angularPackageJsonPaths ) { return await Promise . all ( angularPackageJsonPaths . map ( async ( angularPackageJsonPath ) : Promise < Array < AngularPackage > > => { let angularPackageJson ; try { angularPackageJson = await readFile ( angularPackageJsonPath ) ; } catch ( error ) { this . handleReadError ( error , angularPackageJsonPath ) ; } const validatorResult = validate ( angularPackageJson , < Schema > angularPackageSchema ) ; if ( ! validatorResult . valid ) { this . handleValidationError ( validatorResult . errors , angularPackageJsonPath ) ; } const angularPackageCwd = path . dirname ( path . join ( this . cwd , angularPackageJsonPath ) ) ; const primaryAngularPackage = await new AngularPackage ( ) . setRoot ( angularPackageCwd ) . setEntryFile ( angularPackageJson . entryFile ) . setOutDir ( angularPackageJson . outDir ) . setTypescriptCompilerOptions ( angularPackageJson . typescriptCompilerOptions || { } ) . setAngularCompilerOptions ( angularPackageJson . angularCompilerOptions || { } ) . setDependencies ( angularPackageJson . dependencies || { } ) . asPrimaryEntry ( ) . init ( ) ; const secondaryAngularPackages = await Promise . all ( ( angularPackageJson . secondaryEntries || [ ] ) . map ( async ( secondaryEntry ) : Promise < AngularPackage > => { return new AngularPackage ( ) . setRoot ( angularPackageCwd ) . setEntryFile ( secondaryEntry . entryFile ) . setOutDir ( angularPackageJson . outDir ) . setTypescriptCompilerOptions ( angularPackageJson . typescriptCompilerOptions || { } ) . setAngularCompilerOptions ( angularPackageJson . angularCompilerOptions || { } ) . setDependencies ( angularPackageJson . dependencies || { } ) . asSecondaryEntry ( ) . init ( ) ; } ) ) ; return [ primaryAngularPackage , ... secondaryAngularPackages ] ; } ) ) ; } param error private static handleReadError ( error , angularPackageJsonPath ) { const errorMessage = [ 'An error occured while starting the build.' , '' , 'Message:    Cannot read the angular package configuration file.' , '' , 'Caused by:  File System' , `File:       ${ angularPackageJsonPath [ 0 ] === './' ? '' : './' }${ angularPackageJsonPath }` , `Details:    ${ error . message . split ( '[' ) [ 1 ] . split ( ']' ) [ 0 ] }` , '' , 'Tip: Verify the path is correct, the file exists and the file is valid JSON.' , '' , '' ] . join ( '\n' ) ; throw new Error ( errorMessage ) ; } param errors private static handleValidationError ( errors , angularPackageJsonPath ) { const errorDetails = errors . map ( ( error ) : string => { return `${ error . property . replace ( 'instance.' , '' ) . replace ( 'instance' , '' ) } ${ error . message }` . trim ( ) ; } ) . map ( ( error , index ) : string => { return index === 0 ? `Details:    ${ error [ 0 ] . toUpperCase ( ) }${ error . slice ( 1 ) }` : `            ${ error [ 0 ] . toUpperCase ( ) }${ error . slice ( 1 ) }` ; } ) ; const errorMessage = [ 'An error occured while starting the build.' , '' , 'Message:    The angular package file is not valid.' , '' , 'Caused by:  Angular Package File Validator' , `File:       ${ angularPackageJsonPath [ 0 ] === './' ? '' : './' }${ angularPackageJsonPath }` , ... errorDetails , '' , 'Tip: Verify that the angular package file is valid, following the schema defined in "angular-package.schema.json".' , '' , '' ] . join ( '\n' ) ; throw new Error ( errorMessage ) ; } }