1717
1818const fs = require ( 'fs' ) ;
1919const path = require ( 'path' ) ;
20+ const isBinary = require ( 'isbinaryfile' ) . isBinaryFileSync ;
2021
2122/**
2223 * Create a new directory and any necessary subdirectories
@@ -29,9 +30,47 @@ function mkdirp(p) {
2930 }
3031}
3132
33+ function getMappingsFromVolatileFile ( volatileFilePath ) {
34+ const stampFileLines = fs . readFileSync ( volatileFilePath , { encoding : 'utf-8' } ) . trim ( ) . split ( '\n' ) ;
35+ const stampMap = { } ;
36+ for ( const line of stampFileLines ) {
37+ const [ key , value ] = line . split ( ' ' ) ;
38+ stampMap [ key ] = value ;
39+ }
40+ return stampMap ;
41+ }
42+
43+ function normalizeSubstitutions ( substitutionsArg , volatileFilePath ) {
44+ const substitutions = JSON . parse ( substitutionsArg ) ;
45+ const stampMap = getMappingsFromVolatileFile ( volatileFilePath ) ;
46+
47+ const normalizedSubstitutions = { } ;
48+
49+ for ( const occurrence in substitutions ) {
50+ let substituteWith = substitutions [ occurrence ] ;
51+ if ( substituteWith . match ( / ^ { .* ?} $ / ) ) {
52+ substituteWith = substituteWith . replace ( / ^ { ( .* ?) } $ / , '$1' ) ;
53+ if ( ! stampMap [ substituteWith ] ) {
54+ throw new Error ( `Could not find ${ substituteWith } key in volatile-status file.` ) ;
55+ }
56+ substituteWith = stampMap [ substituteWith ] ;
57+ }
58+ normalizedSubstitutions [ occurrence ] = substituteWith ;
59+ }
60+ return normalizedSubstitutions ;
61+ }
62+
3263function main ( params ) {
3364 const outdir = params . shift ( ) ;
3465
66+ const volatileFilePath = params . shift ( ) ;
67+
68+ const rawSubstitutions = params . shift ( ) . replace ( / ^ ' ( .* ) ' $ / , '$1' ) ;
69+
70+ const normalizedSubstitutions = normalizeSubstitutions ( rawSubstitutions , volatileFilePath )
71+
72+ const substitutions = Object . entries ( normalizedSubstitutions ) ;
73+
3574 const rootDirs = [ ] ;
3675 while ( params . length && params [ 0 ] !== '--assets' ) {
3776 let r = params . shift ( ) ;
@@ -42,6 +81,7 @@ function main(params) {
4281 }
4382 // Always trim the longest prefix
4483 rootDirs . sort ( ( a , b ) => b . length - a . length ) ;
84+
4585 params . shift ( ) ; // --assets
4686
4787 function relative ( execPath ) {
@@ -56,12 +96,20 @@ function main(params) {
5696 return execPath ;
5797 }
5898
59- function copy ( f ) {
99+ function copy ( f , substitutions ) {
60100 if ( fs . statSync ( f ) . isDirectory ( ) ) {
61101 for ( const file of fs . readdirSync ( f ) ) {
62102 // Change paths to posix
63- copy ( path . join ( f , file ) . replace ( / \\ / g, '/' ) ) ;
103+ copy ( path . join ( f , file ) . replace ( / \\ / g, '/' ) , substitutions ) ;
64104 }
105+ } else if ( ! isBinary ( f ) ) {
106+ const dest = path . join ( outdir , relative ( f ) ) ;
107+ let content = fs . readFileSync ( f , { encoding : 'utf-8' } ) ;
108+ substitutions . forEach ( ( [ occurrence , replaceWith ] ) => {
109+ content = content . replace ( occurrence , replaceWith ) ;
110+ } ) ;
111+ fs . mkdirSync ( path . dirname ( dest ) , { recursive : true } ) ;
112+ fs . writeFileSync ( dest , content ) ;
65113 } else {
66114 const dest = path . join ( outdir , relative ( f ) ) ;
67115 mkdirp ( path . dirname ( dest ) ) ;
@@ -75,7 +123,7 @@ function main(params) {
75123 // copied from within bazel-bin.
76124 // See https://github.com/bazelbuild/rules_nodejs/pull/546.
77125 for ( const f of new Set ( params ) ) {
78- copy ( f ) ;
126+ copy ( f , substitutions ) ;
79127 }
80128 return 0 ;
81129}
0 commit comments