1+ const yaml = require ( 'js-yaml' )
12const logWrapper = require ( './log' )
23const { getFiles, getLevels } = require ( './levels' )
34const { getType, mergeLevels } = require ( './merge' )
4- const { modifyPattern } = require ( './parser' )
5+ const { getByPath , modifyPattern } = require ( './parser' )
56
67function configLevels ( patterns , options , log = console ) {
78 const files = getFiles ( patterns )
@@ -11,7 +12,7 @@ function configLevels(patterns, options, log = console) {
1112}
1213
1314function safeKey ( key ) {
14- return key . replace ( / [ ^ \w _ - ] + / gu, '_' )
15+ return key . toString ( ) . replace ( / [ ^ \w _ - ] + / gu, '_' )
1516}
1617
1718function setOutputAndPrint ( core , key , value ) {
@@ -30,11 +31,26 @@ function setOutputAndPrint(core, key, value) {
3031 core . setOutput ( outputKey , outputValue )
3132}
3233
34+ function getLoopItems ( content , format ) {
35+ if ( format === 'yaml' ) {
36+ return yaml . load ( content )
37+ }
38+ if ( format === 'json' ) {
39+ return JSON . parse ( content )
40+ }
41+
42+ return content . split ( '\n' )
43+ . map ( ( item ) => item . trim ( ) )
44+ . filter ( ( item ) => item !== '' )
45+ }
46+
3347function run ( core ) {
3448 const log = logWrapper ( core )
3549 const patterns = core . getInput ( 'patterns' , { required : true } )
3650 const outputProperties = core . getBooleanInput ( 'output_properties' )
37- const loop = core . getMultilineInput ( 'loop' )
51+ const loopContent = core . getInput ( 'loop' )
52+ const loopItemsFormat = core . getInput ( 'loop_items_format' )
53+ const loopItemsKey = core . getInput ( 'loop_items_key' )
3854 const options = {
3955 mergeObject : core . getInput ( 'merge_object' ) ,
4056 mergeArray : core . getInput ( 'merge_array' ) ,
@@ -53,29 +69,43 @@ function run(core) {
5369 core . error ( `Wrong value of "merge_plain": "${ options . mergePlain } ". Should be one of "concatenating" or "overwrite".` )
5470 return
5571 }
72+ if ( ! ( [ 'text' , 'json' , 'yaml' ] . includes ( loopItemsFormat ) ) ) {
73+ core . error ( `Wrong value of "loop_items_format": "${ loopItemsFormat } ". Should be one of "text", "json" or "yaml".` )
74+ return
75+ }
5676
5777 let result
5878
5979 /* TODO: Loop it using external action (action-loop) based on
6080 * - https://github.com/nektos/act/blob/master/pkg/runner/step_action_remote.go
6181 * - https://github.com/cardinalby/github-action-ts-run-api
6282 */
83+ const loop = getLoopItems ( loopContent , loopItemsFormat )
84+ if ( ! Array . isArray ( loop ) ) {
85+ core . error ( '"loop" must contain a list of items.' )
86+ return
87+ }
88+
6389 if ( loop . length ) {
6490 result = { }
6591
66- for ( const item of loop ) {
67- core . startGroup ( `Pattern processing with ' ${ item } ' item` )
68- const modifiedPattern = modifyPattern ( patterns , item )
92+ for ( let i = 0 ; i < loop . length ; i ++ ) {
93+ core . startGroup ( `Pattern processing with ${ JSON . stringify ( loop [ i ] ) } item` )
94+ const modifiedPattern = modifyPattern ( patterns , loop [ i ] )
6995 const resultValue = processingLevels ( core , modifiedPattern , options , log )
7096 if ( resultValue === null ) {
7197 core . endGroup ( )
7298 continue
7399 }
74100
75- result [ item ] = resultValue
101+ const objectKey = ! ! loop [ i ] && loop [ i ] . constructor === Object && ! ! loopItemsKey
102+ ? getByPath ( loop [ i ] , loopItemsKey )
103+ : i
104+ const key = ( typeof loop [ i ] === 'string' || typeof loop [ i ] === 'number' ) ? loop [ i ] : objectKey
105+ result [ key ] = resultValue
76106
77107 if ( outputProperties ) {
78- setOutputAndPrint ( core , item , resultValue )
108+ setOutputAndPrint ( core , key , resultValue )
79109 }
80110 core . endGroup ( )
81111 }
@@ -113,6 +143,7 @@ function processingLevels(core, patterns, options, log) {
113143}
114144
115145module . exports = {
146+ getLoopItems,
116147 configLevels,
117148 run
118149}
0 commit comments