/**
 * Additional package.json spefific tool functions that are not part of normal package.ts
 */ import { SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics' ; import { OptionsAfterSetup } from './options-after-setup' ; import { extractScriptName } from './constants' ; import { ExtractScript } from './extract-script' ; /**
 * rudimentary interface of package.json (only what is used here).
 */ export interface IPackageJson { devDependencies : { [ packagename ] : string ; } ; scripts : { [ scriptname ] : string ; } ; } /**
 * Read and edit functionality on package.json
 * It allows multiple changes on package.json file.
 * At the end we call commit() to write angular.json.
 */ export class PackageJsonSnapshot { private readonly packageJson : IPackageJson ; param _path param host param context throws constructor ( private _path , private host , private context ? ) { this . packageJson = this . readPackageJson ( ) ; } /**
     * Patht of package json.
     */ public path ( ) { return this . _path ; } /**
     * Actual content of package json.
     */ public content ( ) { return JSON . stringify ( this . packageJson , null , 2 ) ; } /**
     * Commit all changes done on workspace.
     * (writes angular.json)
     */ public commit ( ) { this . host . overwrite ( `${ this . _path }/package.json` , this . content ( ) ) ; } param scriptName return public getScript ( scriptName ) { return this . packageJson . scripts [ scriptName ] ; } /**
     * Get the extract script, if contained in package.json.
     */ public getExtractScriptForProject ( project , isDefaultProject ) { const scriptname = extractScriptName ( project , isDefaultProject ) ; const content = this . getScript ( scriptname ) ; if ( content ) { return new ExtractScript ( scriptname , content ) ; } else { return null ; } } param scriptName param content public addOrReplaceScript ( scriptName , content ) { const scriptsSection = 'scripts' ; if ( ! this . packageJson [ scriptsSection ] ) { this . packageJson [ scriptsSection ] = { } ; } const isOverride = ! ! this . packageJson [ scriptsSection ] [ scriptName ] ; this . packageJson [ scriptsSection ] [ scriptName ] = content ; if ( this . context ) { if ( isOverride ) { this . context . logger . info ( `changed script ${ scriptName } in ${ this . _path }/package.json` ) ; } else { this . context . logger . info ( `added script ${ scriptName } to ${ this . _path }/package.json` ) ; } } } param scriptName public removeScript ( scriptName ) { const scriptsSection = 'scripts' ; if ( ! this . packageJson [ scriptsSection ] ) { return ; } const exists = ! ! this . packageJson [ scriptsSection ] [ scriptName ] ; if ( exists ) { delete this . packageJson [ scriptsSection ] [ scriptName ] ; } if ( this . context ) { if ( exists ) { this . context . logger . info ( `removed script ${ scriptName } from ${ this . _path }/package.json` ) ; } } } public addExtractScript ( options ) { const extractScript = ExtractScript . createExtractScript ( options ) ; this . addOrReplaceScript ( extractScript . name , extractScript . content ) ; if ( this . context ) { this . context . logger . info ( `added npm script to extract i18n message, run "npm run ${ extractScript . name }" for extraction` ) ; } } public changeExtractScript ( options ) { const existingScriptContent = this . getScript ( extractScriptName ( options . project , options . isDefaultProject ) ) ; const extractScript = ExtractScript . createExtractScript ( options ) ; if ( existingScriptContent !== extractScript . content ) { this . addOrReplaceScript ( extractScript . name , extractScript . content ) ; if ( this . context ) { this . context . logger . info ( `changed npm script to extract i18n message, run "npm run ${ extractScript . name }" for extraction` ) ; } } } param options param language public addStartScript ( options , language ) { const scriptName = ( options . isDefaultProject ) ? `start-${ language }` : `start-${ options . project }-${ language }` ; this . addOrReplaceScript ( scriptName , this . startScript ( options , language ) ) ; if ( this . context ) { this . context . logger . info ( `added npm script to start app for language ${ language }, run "npm run ${ scriptName }"` ) ; } } /**
     * returns the start script to be added.
     */ private startScript ( options , language ) { if ( options . isDefaultProject ) { return `ng serve --configuration=${ language }` ; } else { return `ng serve ${ options . project } --configuration=${ language }` ; } } host return private readPackageJson ( ) { const packageJsonPath = `${ this . _path }/package.json` ; const content = this . host . read ( packageJsonPath ) ; if ( ! content ) { throw new SchematicsException ( `${ packageJsonPath } does not exist` ) ; } const contentString = content . toString ( 'UTF-8' ) ; return JSON . parse ( contentString ) as IPackageJson ; } }