11import parseArgs from '../../util/parse-args.ts' ;
22import type { IsPluginEnabled , Plugin , Resolve , ResolveConfig } from '../../types/config.ts' ;
3- import { toDeferResolve , toEntry } from '../../util/input.ts' ;
3+ import { isFile } from '../../util/fs.ts' ;
4+ import { toDeferResolve , toEntry , toIgnore } from '../../util/input.ts' ;
45import type { BunfigConfig } from './types.ts' ;
56
67// https://bun.sh/docs/cli/test
78
89const title = 'Bun' ;
910
10- const enablers = [ 'bun' ] ;
11+ const enablers =
12+ 'This plugin is enabled when a `bun.lock` or `bun.lockb` file is found or a `bun test` script is configured.' ;
13+
14+ const getBunTestArgs = ( script : string ) => {
15+ const args = script . split ( / \s + / ) ;
16+ const bunIndex = args . indexOf ( 'bun' ) ;
17+ const testIndex = bunIndex === - 1 ? - 1 : args . indexOf ( 'test' , bunIndex + 1 ) ;
18+ if ( args . slice ( bunIndex + 1 , testIndex ) . includes ( 'run' ) ) return ;
19+ return testIndex === - 1 ? undefined : args . slice ( testIndex + 1 ) ;
20+ } ;
1121
1222const hasBunTest = ( scripts : Record < string , string > | undefined ) =>
13- scripts && Object . values ( scripts ) . some ( script => / (?< = ^ | \s ) b u n t e s t / . test ( script ) ) ;
23+ scripts && Object . values ( scripts ) . some ( script => typeof script === 'string' && getBunTestArgs ( script ) ) ;
1424
15- const isEnabled : IsPluginEnabled = ( { manifest } ) => ! ! hasBunTest ( manifest . scripts ) ;
25+ const isEnabled : IsPluginEnabled = ( { cwd, manifest } ) =>
26+ isFile ( cwd , 'bun.lock' ) || isFile ( cwd , 'bun.lockb' ) || ! ! hasBunTest ( manifest . scripts ) ;
1627
1728const config = [ 'bunfig.toml' ] ;
1829
@@ -33,15 +44,19 @@ const toPatterns = (arg: string) => {
3344const resolve : Resolve = options => {
3445 const scripts = { ...options . rootManifest ?. scripts , ...options . manifest . scripts } ;
3546 for ( const script of Object . values ( scripts ) ) {
36- if ( / (?< = ^ | \s ) b u n t e s t / . test ( script ) ) {
37- const parsed = parseArgs ( script . split ( ' ' ) , { string : [ 'timeout' , 'rerun-each' , 'preload' ] } ) ;
38- const args = parsed . _ . filter ( id => id !== 'bun' && id !== 'test' ) ;
39- const inputs = ( args . length === 0 ? patterns : args . flatMap ( toPatterns ) ) . map ( toEntry ) ;
47+ const bunTestArgs = getBunTestArgs ( script ) ;
48+ if ( bunTestArgs ) {
49+ const parsed = parseArgs ( bunTestArgs , { string : [ 'timeout' , 'rerun-each' , 'preload' ] } ) ;
50+ const args = parsed . _ ;
51+ const inputs = [
52+ toIgnore ( 'bun' , 'dependencies' ) ,
53+ ...( args . length === 0 ? patterns : args . flatMap ( toPatterns ) ) . map ( toEntry ) ,
54+ ] ;
4055 for ( const specifier of [ parsed . preload ?? [ ] ] . flat ( ) ) inputs . push ( toDeferResolve ( specifier ) ) ;
4156 return inputs ;
4257 }
4358 }
44- return [ ] ;
59+ return [ toIgnore ( 'bun' , 'dependencies' ) , ... patterns . map ( toEntry ) ] ;
4560} ;
4661
4762const plugin : Plugin = {
0 commit comments