-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated JSON actions and added new json action. (#1039)
* Updated with 2 things Added create primitive mod Added seperator mod * Updated these with lint * Updated these with lint * Updated canvas_create_primitive_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Fixed lint issue I hope * Fixed lint issue I hope * Fixed prettier issues Fixed linting issues inside json write Fixed issue with canvas paint not using cases * Added json random * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * added nested check * **Changelog** * removed error handler * **Changelog** * removed error handler
- Loading branch information
1 parent
fbcd592
commit 54934ba
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
name: 'Check JSON File', | ||
|
||
section: 'File Stuff', | ||
|
||
subtitle(data) { | ||
return `Check JSON file "${data.filepath}"`; | ||
}, | ||
|
||
meta: { | ||
version: '2.1.7', | ||
preciseCheck: false, | ||
author: 'DBM Mods', | ||
authorUrl: 'https://github.com/dbm-network/mods', | ||
}, | ||
|
||
fields: ['filepath', 'checkType', 'title', 'contentTitle', 'branch'], | ||
|
||
html() { | ||
return ` | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">File Path</span> | ||
<input id="filepath" class="round" type="text" placeholder="./data.json"> | ||
</div> | ||
<div style="padding: 10px;"> | ||
<span class="dbminputlabel">Check Type</span><br> | ||
<select id="checkType" class="round" onchange="glob.onCheckTypeChanged(this)"> | ||
<option value="0">Check if File is JSON Formatted</option> | ||
<option value="1">Check if Title Exists</option> | ||
<option value="2">Check if Content Exists Under Title</option> | ||
</select> | ||
</div> | ||
<div id="titleSection" style="padding: 10px; display: none;"> | ||
<span class="dbminputlabel">Title</span> | ||
<input id="title" class="round" type="text" placeholder="Title"> | ||
</div> | ||
<div id="contentTitleSection" style="padding: 10px; display: none;"> | ||
<span class="dbminputlabel">Content Title</span> | ||
<input id="contentTitle" class="round" type="text" placeholder="Content Title"> | ||
</div> | ||
<conditional-input id="branch" style="padding-top: 8px;"></conditional-input>`; | ||
}, | ||
|
||
init() { | ||
const { glob, document } = this; | ||
|
||
glob.onCheckTypeChanged = function handleCheckTypeChange(event) { | ||
const titleSection = document.getElementById('titleSection'); | ||
const contentTitleSection = document.getElementById('contentTitleSection'); | ||
switch (event.value) { | ||
case '0': | ||
titleSection.style.display = 'none'; | ||
contentTitleSection.style.display = 'none'; | ||
break; | ||
case '1': | ||
titleSection.style.display = null; | ||
contentTitleSection.style.display = 'none'; | ||
break; | ||
case '2': | ||
titleSection.style.display = null; | ||
contentTitleSection.style.display = null; | ||
break; | ||
} | ||
}; | ||
|
||
glob.onCheckTypeChanged(document.getElementById('checkType')); | ||
}, | ||
|
||
async action(cache) { | ||
const data = cache.actions[cache.index]; | ||
let filepath = this.evalMessage(data.filepath, cache); | ||
const checkType = parseInt(data.checkType, 10); | ||
const title = this.evalMessage(data.title, cache); | ||
const contentTitle = this.evalMessage(data.contentTitle, cache); | ||
|
||
if (filepath.startsWith('./')) { | ||
filepath = path.join(__dirname, '..', filepath.substring(2)); | ||
} | ||
|
||
let jsonData; | ||
|
||
if (fs.existsSync(filepath)) { | ||
try { | ||
const fileData = fs.readFileSync(filepath); | ||
if (fileData.length === 0) throw new Error('JSON file is empty.'); | ||
jsonData = JSON.parse(fileData); | ||
} catch (error) { | ||
console.error(`Error reading JSON file: ${error}`); | ||
this.executeResults(false, data.branch, cache); | ||
return; | ||
} | ||
} else { | ||
this.executeResults(false, data.branch, cache); | ||
return; | ||
} | ||
|
||
let result = false; | ||
|
||
try { | ||
switch (checkType) { | ||
case 0: | ||
result = true; | ||
break; | ||
case 1: { | ||
result = jsonData.some((item) => item.Title === title); | ||
break; | ||
} | ||
case 2: { | ||
const titleData = jsonData.find((item) => item.Title === title); | ||
if (!titleData) throw new Error('Title not found'); | ||
const nestedTitles = contentTitle.split('/'); | ||
let nestedData = titleData; | ||
for (const nestedTitle of nestedTitles) { | ||
if (!(nestedTitle in nestedData)) { | ||
throw new Error('Content title not found'); | ||
} | ||
nestedData = nestedData[nestedTitle]; | ||
} | ||
result = true; | ||
break; | ||
} | ||
} | ||
} catch (error) { | ||
console.error(`Error checking JSON data: ${error}`); | ||
} | ||
|
||
this.executeResults(result, data.branch, cache); | ||
}, | ||
|
||
mod() {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters