import { SchematicsException , Tree , SchematicContext } from '@angular-devkit/schematics' ; import { FirebaseHostingSite , FirebaseRc } from './interfaces' ; import * as semver from 'semver' ; export const shortSiteName = ( site ? ) => site ?. name && site . name . split ( '/' ) . pop ( ) ; export const stringifyFormatted = ( obj ) => JSON . stringify ( obj , null , 2 ) ; export const overwriteIfExists = ( tree , path , content ) => { if ( tree . exists ( path ) ) { tree . overwrite ( path , content ) ; } else { tree . create ( path , content ) ; } } ; function emptyFirebaseRc ( ) { return { targets : { } } ; } function generateFirebaseRcTarget ( firebaseProject , firebaseHostingSite , project ) { return { hosting : { [ project ] : [ shortSiteName ( firebaseHostingSite ) ?? firebaseProject ] } } ; } export function generateFirebaseRc ( tree , path , firebaseProject , firebaseHostingSite , project ) { const firebaseRc = tree . exists ( path ) ? safeReadJSON ( path , tree ) : emptyFirebaseRc ( ) ; firebaseRc . targets = firebaseRc . targets || { } ; firebaseRc . targets [ firebaseProject ] = generateFirebaseRcTarget ( firebaseProject , firebaseHostingSite , project ) ; firebaseRc . projects = { default : firebaseProject } ; overwriteIfExists ( tree , path , stringifyFormatted ( firebaseRc ) ) ; } export function safeReadJSON ( path , tree ) { try { return JSON . parse ( tree . read ( path ) ! . toString ( ) ) ; } catch ( e ) { throw new SchematicsException ( `Error when parsing ${ path }: ${ e . message }` ) ; } } export const addDependencies = ( host , deps , context ) => { const packageJson = host . exists ( 'package.json' ) && safeReadJSON ( 'package.json' , host ) ; if ( packageJson === undefined ) { throw new SchematicsException ( 'Could not locate package.json' ) ; } packageJson . devDependencies ??= { } ; packageJson . dependencies ??= { } ; Object . keys ( deps ) . forEach ( depName => { const dep = deps [ depName ] ; const existingDeps = dep . dev ? packageJson . devDependencies : packageJson . dependencies ; const existingVersion = existingDeps [ depName ] ; if ( existingVersion ) { try { if ( ! semver . intersects ( existingVersion , dep . version ) ) { context . logger . warn ( `⚠️ The ${ depName } devDependency specified in your package.json (${ existingVersion }) does not fulfill AngularFire's dependency (${ dep . version })` ) ; } } catch ( e ) { if ( existingVersion !== dep . version ) { context . logger . warn ( `⚠️ The ${ depName } devDependency specified in your package.json (${ existingVersion }) does not fulfill AngularFire's dependency (${ dep . version })` ) ; } } } else { existingDeps [ depName ] = dep . version ; } } ) ; overwriteIfExists ( host , 'package.json' , stringifyFormatted ( packageJson ) ) ; } ;