@@ -4,6 +4,14 @@ const YAML = require('yaml');
4
4
5
5
const samplesDir = process . argv [ 2 ] ;
6
6
7
+ // Inspired by https://github.com/compose-spec/compose-go/blob/main/template/template.go
8
+ const interpolationRegex =
9
+ / (?< ! \$ ) \$ (?: { ( [ _ a - z ] [ _ a - z 0 - 9 ] * ) } | ( [ _ a - z ] [ _ a - z 0 - 9 ] * ) ) / gi; // [1] = ${var}, [2] = $var
10
+
11
+ function interpolatedVars ( str ) {
12
+ return Array . from ( str . matchAll ( interpolationRegex ) , ( match ) => match [ 1 ] || match [ 2 ] ) ;
13
+ }
14
+
7
15
// categories are directories in the current directory (i.e. we're running in samples/ and we might have a samples/ruby/ directory)
8
16
const directories = fs . readdirSync ( samplesDir ) . filter ( file => fs . statSync ( path . join ( samplesDir , file ) ) . isDirectory ( ) ) ;
9
17
@@ -31,7 +39,7 @@ directories.forEach((sample) => {
31
39
const tags = readme . match ( / T a g s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( tag => tag . trim ( ) ) ;
32
40
const languages = readme . match ( / L a n g u a g e s : ( .* ) / ) [ 1 ] . split ( ',' ) . map ( language => language . trim ( ) ) ;
33
41
34
- let configs = [ ] ;
42
+ let configs = new Set ( ) ;
35
43
try {
36
44
composeFile = fs . readFileSync ( path . join ( samplesDir , sample , 'compose.yaml' ) , 'utf8' ) ;
37
45
compose = YAML . parse ( composeFile ) ;
@@ -41,22 +49,30 @@ directories.forEach((sample) => {
41
49
if ( Array . isArray ( service . environment ) ) {
42
50
service . environment . forEach ( env => {
43
51
if ( ! env . includes ( "=" ) ) {
44
- configs . push ( env ) ;
52
+ configs . add ( env ) ;
45
53
}
54
+ interpolatedVars ( env ) . forEach ( v => {
55
+ configs . add ( v ) ;
56
+ } ) ;
46
57
} ) ;
47
58
} else {
48
59
for ( var name in service . environment ) {
49
60
value = service . environment [ name ] ;
50
61
if ( value === null || value === undefined || value === "" ) {
51
- configs . push ( name ) ;
62
+ configs . add ( name ) ;
63
+ }
64
+ if ( typeof value === 'string' ) {
65
+ interpolatedVars ( value ) . forEach ( v => {
66
+ configs . add ( v ) ;
67
+ } ) ;
52
68
}
53
69
}
54
70
}
55
71
}
56
72
} catch ( error ) {
57
73
// Ignore if the sample doesn't have a compose file
58
74
if ( error . code != 'ENOENT' ) {
59
- console . log ( `failed to parese compose for configs for sample` , sample , error ) ;
75
+ console . log ( `failed to parse compose for configs for sample` , sample , error ) ;
60
76
}
61
77
}
62
78
@@ -70,8 +86,8 @@ directories.forEach((sample) => {
70
86
tags,
71
87
languages,
72
88
} ;
73
- if ( configs . length > 0 ) {
74
- sampleSummary . configs = configs ;
89
+ if ( configs . size > 0 ) {
90
+ sampleSummary . configs = Array . from ( configs ) ;
75
91
}
76
92
jsonArray . push ( sampleSummary ) ;
77
93
0 commit comments