1
+ const yaml = require ( 'js-yaml' )
1
2
const logWrapper = require ( './log' )
2
3
const { getFiles, getLevels } = require ( './levels' )
3
4
const { getType, mergeLevels } = require ( './merge' )
4
- const { modifyPattern } = require ( './parser' )
5
+ const { getByPath , modifyPattern } = require ( './parser' )
5
6
6
7
function configLevels ( patterns , options , log = console ) {
7
8
const files = getFiles ( patterns )
@@ -11,7 +12,7 @@ function configLevels(patterns, options, log = console) {
11
12
}
12
13
13
14
function safeKey ( key ) {
14
- return key . replace ( / [ ^ \w _ - ] + / gu, '_' )
15
+ return key . toString ( ) . replace ( / [ ^ \w _ - ] + / gu, '_' )
15
16
}
16
17
17
18
function setOutputAndPrint ( core , key , value ) {
@@ -30,11 +31,26 @@ function setOutputAndPrint(core, key, value) {
30
31
core . setOutput ( outputKey , outputValue )
31
32
}
32
33
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
+
33
47
function run ( core ) {
34
48
const log = logWrapper ( core )
35
49
const patterns = core . getInput ( 'patterns' , { required : true } )
36
50
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' )
38
54
const options = {
39
55
mergeObject : core . getInput ( 'merge_object' ) ,
40
56
mergeArray : core . getInput ( 'merge_array' ) ,
@@ -53,29 +69,43 @@ function run(core) {
53
69
core . error ( `Wrong value of "merge_plain": "${ options . mergePlain } ". Should be one of "concatenating" or "overwrite".` )
54
70
return
55
71
}
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
+ }
56
76
57
77
let result
58
78
59
79
/* TODO: Loop it using external action (action-loop) based on
60
80
* - https://github.com/nektos/act/blob/master/pkg/runner/step_action_remote.go
61
81
* - https://github.com/cardinalby/github-action-ts-run-api
62
82
*/
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
+
63
89
if ( loop . length ) {
64
90
result = { }
65
91
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 ] )
69
95
const resultValue = processingLevels ( core , modifiedPattern , options , log )
70
96
if ( resultValue === null ) {
71
97
core . endGroup ( )
72
98
continue
73
99
}
74
100
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
76
106
77
107
if ( outputProperties ) {
78
- setOutputAndPrint ( core , item , resultValue )
108
+ setOutputAndPrint ( core , key , resultValue )
79
109
}
80
110
core . endGroup ( )
81
111
}
@@ -113,6 +143,7 @@ function processingLevels(core, patterns, options, log) {
113
143
}
114
144
115
145
module . exports = {
146
+ getLoopItems,
116
147
configLevels,
117
148
run
118
149
}
0 commit comments