11import { RunrealConfig } from './types.ts'
22
3- // This helper function will take a command string with placeholders and a substitutions object
4- // It will replace all placeholders in the command with their corresponding values
5- // If the key is not found in substitutions, keep the original placeholder
6- export function render ( input : string [ ] , cfg : RunrealConfig ) {
7- // This regular expression matches all occurrences of ${placeholder}
8- const substitutions : Record < string , string | undefined > = getSubstitutions ( cfg )
9-
10- const placeholderRegex = / \$ \{ ( [ ^ } ] + ) \} / g
11- return input . map ( ( arg ) =>
12- arg . replace ( placeholderRegex , ( _ , key : string ) => {
13- return key in substitutions ? substitutions [ key ] || key : _
14- } )
15- )
16- }
17-
18- // Object containing the allowed substitutions
3+ /**
4+ * Get the substitutions object with values from the config.
5+ * @param {RunrealConfig } cfg
6+ * @returns {Record<string, string | undefined> } the substitutions object
7+ */
198export const getSubstitutions = ( cfg : RunrealConfig ) : Record < string , string | undefined > => ( {
209 'engine.path' : cfg . engine ?. path ,
2110 'project.path' : cfg . project ?. path ,
@@ -26,3 +15,60 @@ export const getSubstitutions = (cfg: RunrealConfig): Record<string, string | un
2615 'build.commit' : cfg . build ?. commitShort ,
2716 'buildkite.buildNumber' : cfg . buildkite ?. buildNumber ,
2817} )
18+
19+ /**
20+ * Regular expression for matching ${placeholders}.
21+ */
22+ const placeholderRegex = / \$ \{ ( [ ^ } ] + ) \} / g
23+
24+ /**
25+ * Replace all ${placeholders} in the items with values from the substitutions object.
26+ * If a placeholder is not found in the substitutions object, it will be kept as is.
27+ * @param {string[] } input
28+ * @param {RunrealConfig } cfg
29+ * @returns {string[] } the rendered items
30+ */
31+ export function render ( input : string [ ] , cfg : RunrealConfig ) : string [ ] {
32+ const substitutions : Record < string , string | undefined > = getSubstitutions ( cfg )
33+ return input . map ( ( arg ) =>
34+ arg . replace ( placeholderRegex , ( _ , key : string ) => {
35+ return key in substitutions ? substitutions [ key ] || key : _
36+ } )
37+ )
38+ }
39+
40+ /**
41+ * Replace all ${placeholders} in the items with values from the substitutions object.
42+ * If a placeholder is not found in the substitutions object, it will be kept as is.
43+ * @param {any } item
44+ * @param {Record<string, string | undefined> } substitutions
45+ * @returns {any } the rendered items
46+ */
47+ function renderItems ( item : any , substitutions : Record < string , string | undefined > ) : any {
48+ if ( typeof item === 'string' ) {
49+ // Replace placeholders in strings
50+ return item . replace ( / \$ \{ ( [ ^ } ] + ) \} / g, ( _ , key : string ) => substitutions [ key ] || _ )
51+ } else if ( Array . isArray ( item ) ) {
52+ // Recursively process each item in an array
53+ return item . map ( ( subItem ) => renderItems ( subItem , substitutions ) )
54+ } else if ( typeof item === 'object' && item !== null ) {
55+ // Recursively process each property in an object
56+ const result : Record < string , any > = { }
57+ for ( const key of Object . keys ( item ) ) {
58+ result [ key ] = renderItems ( item [ key ] , substitutions )
59+ }
60+ return result
61+ }
62+ // Return the item as is if it's not a string, array, or object
63+ return item
64+ }
65+
66+ /**
67+ * Render the config by replacing all ${placeholders} with values from the substitutions object.
68+ * @param {RunrealConfig } cfg
69+ * @returns {RunrealConfig } rendered RunrealConfig
70+ */
71+ export function renderConfig ( cfg : RunrealConfig ) {
72+ const substitutions : Record < string , string | undefined > = getSubstitutions ( cfg )
73+ return renderItems ( cfg , substitutions ) as RunrealConfig
74+ }
0 commit comments