import { execSync as exec } from 'child_process' ; import * as fs from './fs' ; import * as path from './path' ; import { logError , logInfo , logWarn } from '../helpers' ; import { NPMPackageConfig } from '../types' ; param { string } homepage export function homepage ( homepage ) { logInfo ( `updating web/package.json homepage` ) ; fs . update ( packageJSONPath ( ) , '{' , `{"homepage": "${ homepage }",` ) ; } /**
 * Performs a yarn install for web packages.
 */ export function install ( ) { logInfo ( `running yarn for Web` ) ; const webPath = path . project . resolve ( 'web' ) ; try { exec ( `cd ${ webPath } && yarn` , { stdio : [ 0 , 1 , 2 ] } ) ; fs . removeSync ( path . project . resolve ( 'web' , 'node_modules' , 'react' ) ) ; } catch ( err ) { logError ( `yarn for Web` , err ) ; process . exit ( 1 ) ; } } param { object } packageJSON export function link ( packageJSON ) { logInfo ( 'linking web node_modules to parent node_modules' ) ; const parentNodeModules = path . project . resolve ( '..' , '..' , 'node_modules' ) ; const mainNodeModules = path . project . resolve ( 'node_modules' ) ; const webNodeModules = path . project . resolve ( 'web' , 'node_modules' ) ; Object . keys ( packageJSON . dependencies || { } ) . forEach ( dependency => { const dependencyPath = dependency . split ( '/' ) . join ( path . sep ) ; const destinationDependencyPath = path . resolve ( webNodeModules , dependencyPath ) ; const sourceDependencyPath = path . resolve ( mainNodeModules , dependencyPath ) ; const parentSourceDependencyPath = path . resolve ( parentNodeModules , dependencyPath ) ; if ( fs . pathExistsSync ( sourceDependencyPath ) ) { if ( fs . pathExistsSync ( dependencyPath ) ) { fs . removeSync ( dependencyPath ) ; logWarn ( `dependency ${ dependency } existed in web node_modules and was removed` ) ; } fs . ensureSymlinkSync ( sourceDependencyPath , destinationDependencyPath ) ; } else if ( fs . pathExistsSync ( parentSourceDependencyPath ) ) { if ( fs . pathExistsSync ( dependencyPath ) ) { fs . removeSync ( dependencyPath ) ; logWarn ( `dependency ${ dependency } existed in web node_modules and was removed` ) ; } fs . ensureSymlinkSync ( parentSourceDependencyPath , destinationDependencyPath ) ; } else { logWarn ( `could not locate dependency ${ dependency } in node_modules` ) ; } } ) ; } param { * } fragmentPath export function headerScripts ( fragmentPath ? ) { if ( ! fragmentPath ) { return ; } logInfo ( `injecting web header scripts from ${ fragmentPath }` ) ; try { const scripts = require ( path . project . resolve ( fragmentPath ) ) ; fs . update ( indexHTMLPath ( ) , /<!--FLAGSHIP_SCRIPT_INJECT_HEADER_START-->[.\s]+<!--FLAGSHIP_SCRIPT_INJECT_HEADER_END-->/ , `<!--FLAGSHIP_SCRIPT_INJECT_HEADER_START-->${ scripts }<!--FLAGSHIP_SCRIPT_INJECT_HEADER_END-->` ) ; } catch ( err ) { logError ( `injecting web header scripts from ${ fragmentPath }` , err ) ; process . exit ( 1 ) ; } } param { * } fragmentPath export function footerScripts ( fragmentPath ? ) { if ( ! fragmentPath ) { return ; } logInfo ( `injecting web footer scripts from ${ fragmentPath }` ) ; try { const scripts = require ( path . project . resolve ( fragmentPath ) ) ; fs . update ( indexHTMLPath ( ) , /<!--FLAGSHIP_SCRIPT_INJECT_FOOTER_START-->[.\s]+<!--FLAGSHIP_SCRIPT_INJECT_FOOTER_END-->/ , `<!--FLAGSHIP_SCRIPT_INJECT_FOOTER_START-->${ scripts }<!--FLAGSHIP_SCRIPT_INJECT_FOOTER_END-->` ) ; } catch ( err ) { logError ( `injecting web footer scripts from ${ fragmentPath }` , err ) ; process . exit ( 1 ) ; } } param { string } title export function title ( title ? ) { if ( ! title ) { logWarn ( 'No <title> specified for web, use "webTitle" in package.json to specify' ) ; return ; } logInfo ( `updating web title to [${ title }]` ) ; fs . update ( indexHTMLPath ( ) , /<title>[^<]+<\/title>/ , `<title>${ title }</title>` ) ; } returns { string } export function packageJSONPath ( ) { return path . project . resolve ( 'web' , 'package.json' ) ; } returns { string } export function indexHTMLPath ( ) { return path . project . resolve ( 'web' , 'public' , 'index.html' ) ; }