import { JsonArray } from '@angular-devkit/core' ; import { chain , noop , SchematicContext , Tree } from '@angular-devkit/schematics' ; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ; import { PackageManager } from '@angular/cli/lib/config/workspace-schema' ; import { getPackageManager } from '@angular/cli/utilities/package-manager' ; import { fetchPackageMetadata } from '@angular/cli/utilities/package-metadata' ; import { updateWorkspace } from '@schematics/angular/utility/workspace' ; import { DEPENDENCIES } from '../dependencies' ; import { addPackageToPackageJson , getPackageVersionFromPackageJson , getProjectFromWorkspace } from '../utils' ; import { VERSION } from '../version' ; const TETHYS_PKG_NAME = 'ngx-tethys' ; interface NgAddSchema { project ? : string ; icon ? : boolean ; animations ? : boolean ; } function addStyleToWorkspace ( projectName ) { return ( tree ) => { return updateWorkspace ( workspace => { const project = getProjectFromWorkspace ( workspace , projectName ) ; const stylesList = ( project . targets . get ( 'build' ) . options . styles as any [ ] ) || [ ] ; const filePath = `./node_modules/ngx-tethys/styles/index.scss` ; if ( ! stylesList . includes ( filePath ) ) { stylesList . push ( filePath ) ; project . targets . get ( 'build' ) . options . styles = stylesList ; } } ) ; } ; } function addIconToWorkspace ( projectName ) { return updateWorkspace ( workspace => { const project = getProjectFromWorkspace ( workspace , projectName ) ; const list = ( project . targets . get ( 'build' ) . options . assets as any ) || [ ] ; list . push ( { glob : '**/*' , input : './node_modules/@tethys/icons' , output : '/assets/icons/' } ) ; project . targets . get ( 'build' ) . options . assets = list ; } ) ; } export function main ( options = { } ) { return async ( host , context ) => { const packageManager = await getPackageManager ( host . root . path ) ; const usingYarn = packageManager === PackageManager . Yarn ; for ( const pkg of Object . keys ( DEPENDENCIES ) ) { const version = DEPENDENCIES [ pkg ] ; if ( version === '*' ) { const packageMetadata = await fetchPackageMetadata ( pkg , context . logger , { usingYarn : usingYarn } ) ; const latestManifest = packageMetadata . tags [ 'latest' ] ; addPackageToPackageJson ( host , pkg , latestManifest . version ) ; } else { addPackageToPackageJson ( host , pkg , DEPENDENCIES [ pkg ] ) ; } } const tethysVersionRange = getPackageVersionFromPackageJson ( host , TETHYS_PKG_NAME ) ; if ( tethysVersionRange === null ) { addPackageToPackageJson ( host , TETHYS_PKG_NAME , VERSION ) ; } context . addTask ( new NodePackageInstallTask ( ) ) ; return chain ( [ options . icon ? addIconToWorkspace ( options . project ) : noop ( ) , addStyleToWorkspace ( options . project ) ] ) ; } ; }