import { join } from 'path' ; import { SchematicsException , Tree } from '@angular-devkit/schematics' ; const PACKAGE_JSON = 'package.json' ; export interface PackageJson { dependencies : PackageJsonConfigPart < string > ; devDependencies : PackageJsonConfigPart < string > ; name ? : string ; version ? : string ; license ? : string ; scripts ? : PackageJsonConfigPart < string > ; repository ? : PackageJsonConfigPart < string > ; bugs ? : PackageJsonConfigPart < string > ; config ? : { commitizen ? : { path ? : string ; } ; } ; husky ? : { hooks ? : { 'prepare-commit-msg' ? : string ; 'commit-msg' ? : string ; } ; } ; 'standard-version' : { issuePrefixes ? : string [ ] ; } ; } export interface PackageJsonConfigPart < T > { [ key ] : T ; } class FileNotFoundException extends Error { constructor ( fileName ) { const message = `File ${ fileName } not found!` ; super ( message ) ; } } export const getJsonFile = < T > ( tree , path ) : T => { const file = tree . get ( path ) ; if ( ! file ) { throw new FileNotFoundException ( path ) ; } try { const content = JSON . parse ( file . content . toString ( ) ) ; return content as T ; } catch ( e ) { throw new SchematicsException ( `File ${ path } could not be parsed!` ) ; } } ; export const getFileContents = ( tree , filePath ) : string => { const buffer = tree . read ( filePath ) || '' ; return buffer . toString ( ) ; } ; export const getPackageJson = ( tree , workingDirectory = '' ) : PackageJson => { const url = join ( workingDirectory , PACKAGE_JSON ) ; return getJsonFile ( tree , url ) ; } ; export const overwritePackageJson = ( tree , content , workingDirectory = '' ) => { const url = join ( workingDirectory , PACKAGE_JSON ) ; tree . overwrite ( url , JSON . stringify ( content , null , 2 ) ) ; } ; export const getMergedPackageJsonConfig = ( existingConfig , newConfig , overrideExisting = true ) => { let mergedConfig = { } ; if ( overrideExisting ) { mergedConfig = { ... existingConfig , ... newConfig } ; } else { mergedConfig = { ... newConfig , ... existingConfig } ; } return mergedConfig ; } ;