import _ from 'lodash' ; import * as Micromatch from 'micromatch' ; import { addElementsToSequentialArray } from './array' ; export interface ExtendPackageScriptOptions { link after ? : string ; link before ? : string ; } export function extendPackageScript ( script , commands , { after : afterPattern , before : beforePattern } = { } ) { if ( typeof commands === 'string' ) { commands = [ commands ] ; } if ( ! script ) { return commands . join ( ' && ' ) ; } let existingCommands = script . split ( '&&' ) . map ( command => command . trim ( ) ) ; return addElementsToSequentialArray ( existingCommands , commands , { isAfterAnchor : afterPattern ? command => Micromatch . isMatch ( command , afterPattern ) : undefined , isBeforeAnchor : beforePattern ? command => Micromatch . isMatch ( command , beforePattern ) : undefined , } ) . join ( ' && ' ) ; }