Skip to content

Commit

Permalink
Add required property (#2)
Browse files Browse the repository at this point in the history
* add-required-option

* add additional ci test

---------
  • Loading branch information
fiddlermikey authored Feb 2, 2024
1 parent 8a975ee commit 6b580d2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ jobs:
name: A test job to read a value from json as a variable
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Read a json file
uses: ./
id: read
with:
input-file: 'package.json'
input-property: 'author.name' # Exp: 'fiddlermikey'
- name: Read a json file
uses: ./
id: read-nofail
with:
input-file: 'package.json'
input-property: 'foo' # does not exist
required-value: 'false' # Ignore if not present
- name: Display property value for input-property in input-file
id: write
run: echo "The value for ${{ steps.read.outputs.output-property }} is ${{ steps.read.outputs.output-value }}"
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
description: 'Root-level or nested property using dot notation'
required: true
default: 'package.json'
required-value:
description: 'Fail if not found'
required: false
default: 'false'
outputs:
output-property:
description: 'The value of input-property'
Expand Down
6 changes: 4 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2836,12 +2836,14 @@ const fs = __nccwpck_require__(147);
const core = __nccwpck_require__(186);
try {
const item = core.getInput('input-property') || 'foo'
const requiredValue = core.getInput('required-value') || 'false'
const inputFile = core.getInput('input-file') || 'package.json'
const newdata = JSON.parse(fs.readFileSync(inputFile))
const outValue = eval("newdata." + item)
var outValue = ''
outValue = eval("newdata." + item)
core.setOutput('output-property', item);
core.setOutput('output-value', outValue);
if (outValue === undefined) {
if (outValue === undefined && requiredValue == 'true') {
core.setFailed('Property: ' + item + ' does not exist in ' + inputFile);
}
}
Expand Down
6 changes: 4 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const fs = require('fs');
const core = require('@actions/core');
try {
const item = core.getInput('input-property') || 'foo'
const requiredValue = core.getInput('required-value') || 'false'
const inputFile = core.getInput('input-file') || 'package.json'
const newdata = JSON.parse(fs.readFileSync(inputFile))
const outValue = eval("newdata." + item)
var outValue = ''
outValue = eval("newdata." + item)
core.setOutput('output-property', item);
core.setOutput('output-value', outValue);
if (outValue === undefined) {
if (outValue === undefined && requiredValue == 'true') {
core.setFailed('Property: ' + item + ' does not exist in ' + inputFile);
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"precommit": "npm run build && git add dist/"
},
"author": {
"name": "fiddlermikey"
"name": "fiddlermikey",
"email": "fake@fakestreet.org"
},
"devDependencies": {
"eslint": "^8.21.0"
Expand Down

0 comments on commit 6b580d2

Please sign in to comment.