@@ -30,6 +30,7 @@ import type {
3030} from './config.js' ;
3131import type { Agent } from './cli-options.js' ;
3232import { Logger } from './logging/logger.js' ;
33+ import { readEnvFile } from './read-env-file.js' ;
3334
3435export interface AnalyzeResult {
3536 config : Result < ScriptConfig , Failure [ ] > ;
@@ -630,7 +631,12 @@ export class Analyzer {
630631 files ,
631632 ) ;
632633
633- const env = this . #processEnv( placeholder , packageJson , syntaxInfo , command ) ;
634+ const entries = this . #processEnv( placeholder , packageJson , syntaxInfo , command ) ;
635+ await this . #processEnvFile( placeholder , packageJson , syntaxInfo , command , entries ) ;
636+
637+ // Sort for better fingerprint match rate.
638+ entries . sort ( ) ;
639+ const env = Object . fromEntries ( entries ) ;
634640
635641 if ( placeholder . failures . length > 0 ) {
636642 // A script with locally-determined errors doesn't get upgraded to
@@ -1319,13 +1325,13 @@ export class Analyzer {
13191325 packageJson : PackageJson ,
13201326 syntaxInfo : ScriptSyntaxInfo ,
13211327 command : JsonAstNode < string > | undefined ,
1322- ) : Record < string , string > {
1328+ ) : Array < [ string , string ] > {
13231329 if ( syntaxInfo . wireitConfigNode === undefined ) {
1324- return { } ;
1330+ return [ ] ;
13251331 }
13261332 const envNode = findNodeAtLocation ( syntaxInfo . wireitConfigNode , [ 'env' ] ) ;
13271333 if ( envNode === undefined ) {
1328- return { } ;
1334+ return [ ] ;
13291335 }
13301336 if ( command === undefined ) {
13311337 placeholder . failures . push ( {
@@ -1358,7 +1364,7 @@ export class Analyzer {
13581364 } ) ;
13591365 }
13601366 if ( envNode . children === undefined ) {
1361- return { } ;
1367+ return [ ] ;
13621368 }
13631369 const entries : Array < [ string , string ] > = [ ] ;
13641370 for ( const propNode of envNode . children ) {
@@ -1428,8 +1434,67 @@ export class Analyzer {
14281434 }
14291435 }
14301436 // Sort for better fingerprint match rate.
1431- entries . sort ( ) ;
1432- return Object . fromEntries ( entries ) ;
1437+ return entries ;
1438+ }
1439+
1440+ async #processEnvFile(
1441+ placeholder : UnvalidatedConfig ,
1442+ packageJson : PackageJson ,
1443+ syntaxInfo : ScriptSyntaxInfo ,
1444+ command : JsonAstNode < string > | undefined ,
1445+ entries : Array < [ string , string ] >
1446+ ) : Promise < void > {
1447+ if ( syntaxInfo . wireitConfigNode === undefined ) {
1448+ return ;
1449+ }
1450+ const envFileNode = findNodeAtLocation ( syntaxInfo . wireitConfigNode , [ 'env-file' ] ) ;
1451+ if ( envFileNode === undefined ) {
1452+ return ;
1453+ }
1454+ if ( command === undefined ) {
1455+ placeholder . failures . push ( {
1456+ type : 'failure' ,
1457+ reason : 'invalid-config-syntax' ,
1458+ script : placeholder ,
1459+ diagnostic : {
1460+ severity : 'error' ,
1461+ message : 'Can\'t set "env-file" unless "command" is set' ,
1462+ location : {
1463+ file : packageJson . jsonFile ,
1464+ range : { length : envFileNode . length , offset : envFileNode . offset } ,
1465+ } ,
1466+ } ,
1467+ } ) ;
1468+ }
1469+ if ( envFileNode . type !== 'array' ) {
1470+ placeholder . failures . push ( {
1471+ type : 'failure' ,
1472+ reason : 'invalid-config-syntax' ,
1473+ script : placeholder ,
1474+ diagnostic : {
1475+ severity : 'error' ,
1476+ message : 'Expected an array' ,
1477+ location : {
1478+ file : packageJson . jsonFile ,
1479+ range : { length : envFileNode . length , offset : envFileNode . offset } ,
1480+ } ,
1481+ } ,
1482+ } ) ;
1483+ }
1484+ if ( envFileNode . children === undefined ) {
1485+ return ;
1486+ }
1487+ const promiseEnvFiles : Array < Promise < void > > = [ ] ;
1488+ for ( const propNode of envFileNode . children ) {
1489+ if ( propNode . type !== 'string' ) {
1490+ throw new Error (
1491+ 'Internal error: expected array JSON node child to be string' ,
1492+ ) ;
1493+ }
1494+ const envFile = propNode . value as string ;
1495+ promiseEnvFiles . push ( readEnvFile ( envFile , entries ) ) ;
1496+ }
1497+ await Promise . all ( promiseEnvFiles ) ;
14331498 }
14341499
14351500 /**
0 commit comments