k4d3
/

English
Not-For-All-Audiences
k4d3 commited on
Commit
8bea2b9
1 Parent(s): 5fc02b5

Signed-off-by: Balazs Horvath <acsipont@gmail.com>

scripts/Format-JSONFiles.ps1 CHANGED
@@ -1,3 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  function Format-JSONFiles {
2
  param (
3
  [string]$DirectoryPath = "C:\Users\kade\Desktop\yiff_toolkit\ponyxl_loras"
@@ -8,13 +29,11 @@ function Format-JSONFiles {
8
 
9
  # Loop through each JSON file and format it using jq
10
  foreach ($file in $jsonFiles) {
11
- # Construct the command to format the JSON file
12
  $command = "jq '.' '$($file.FullName)' > '$($file.FullName.Replace('.json', '_formatted.json'))'"
13
-
14
  # Execute the command
15
  Invoke-Expression -Command $command
16
-
17
  Write-Host "Formatted $($file.Name) successfully."
18
  }
19
  }
20
-
 
1
+ <#
2
+ .SYNOPSIS
3
+ Formats JSON files from single-line to multi-line format using the jq command-line JSON processor.
4
+
5
+ .DESCRIPTION
6
+ The Format-JSONFiles function formats JSON files found in a specified directory using the jq command-line JSON processor.
7
+ It loops through each JSON file in the directory, applies jq formatting, and saves the formatted JSON to new files with "_formatted" appended to their names.
8
+
9
+ .PARAMETER DirectoryPath
10
+ Specifies the directory path where the JSON files are located. Default value is "C:\Users\kade\Desktop\yiff_toolkit\ponyxl_loras".
11
+
12
+ .EXAMPLE
13
+ Format-JSONFiles -DirectoryPath "C:\path\to\json_files"
14
+ Formats all JSON files in the "C:\path\to\json_files" directory.
15
+
16
+ .NOTES
17
+ Author: _ka_de
18
+ Date: 2024-04-20
19
+ Version: 1.0
20
+ #>
21
+
22
  function Format-JSONFiles {
23
  param (
24
  [string]$DirectoryPath = "C:\Users\kade\Desktop\yiff_toolkit\ponyxl_loras"
 
29
 
30
  # Loop through each JSON file and format it using jq
31
  foreach ($file in $jsonFiles) {
32
+ # Construct the command to format the JSON file from single-line to multi-line
33
  $command = "jq '.' '$($file.FullName)' > '$($file.FullName.Replace('.json', '_formatted.json'))'"
34
+
35
  # Execute the command
36
  Invoke-Expression -Command $command
 
37
  Write-Host "Formatted $($file.Name) successfully."
38
  }
39
  }
 
scripts/Format-JSONFilesToSingleLine.ps1 ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <#
2
+ .SYNOPSIS
3
+ Formats JSON files to a single-line format using the jq utility.
4
+
5
+ .DESCRIPTION
6
+ This script contains two functions: Format-JSONFilesToSingleLine and Format-JSONFileToSingleLine.
7
+ The Format-JSONFilesToSingleLine function accepts a path as input and processes either a single file or all JSON files within a directory to convert them to a single-line format.
8
+ The Format-JSONFileToSingleLine function formats a single JSON file to a single-line format.
9
+
10
+ .PARAMETER Path
11
+ Specifies the path to either a JSON file or a directory containing JSON files to be formatted.
12
+
13
+ .EXAMPLE
14
+ Format-JSONFilesToSingleLine -Path "C:\path\to\your\directory"
15
+ Formats all JSON files in the specified directory to a single-line format.
16
+
17
+ .EXAMPLE
18
+ Format-JSONFileToSingleLine -FilePath "C:\path\to\your\file.json"
19
+ Formats the specified JSON file to a single-line format.
20
+
21
+ .NOTES
22
+ - Requires the jq utility to be installed and added to the system PATH.
23
+ - Overwrites the original JSON files with their single-line formatted versions.
24
+ - If the jq utility is not installed, the script displays an error message and exits.
25
+ - If the specified file or directory does not exist, the script displays an error message.
26
+
27
+ Author: _ka_de
28
+ Date: 2024-04-20
29
+ Version: 1.0
30
+ #>
31
+
32
+ function Format-JSONFilesToSingleLine {
33
+ param (
34
+ [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
35
+ [string]$Path
36
+ )
37
+
38
+ process {
39
+ if (Test-Path $Path -PathType Container) {
40
+ # If the path is a directory, format all JSON files in the directory
41
+ Get-ChildItem -Path $Path -Filter "*.json" | ForEach-Object {
42
+ Format-JSONFileToSingleLine -FilePath $_.FullName
43
+ }
44
+ }
45
+ elseif (Test-Path $Path -PathType Leaf) {
46
+ # If the path is a single file, format that file
47
+ Format-JSONFileToSingleLine -FilePath $Path
48
+ }
49
+ else {
50
+ Write-Host "Invalid path: $Path"
51
+ }
52
+ }
53
+ }
54
+
55
+ function Format-JSONFileToSingleLine {
56
+ param (
57
+ [Parameter(Mandatory = $true)]
58
+ [string]$FilePath
59
+ )
60
+
61
+ # Check if jq is installed
62
+ if (-not (Test-Path "jq")) {
63
+ Write-Host "jq is not installed. Please install jq and ensure it's added to the system PATH."
64
+ return
65
+ }
66
+
67
+ # Check if the file exists
68
+ if (-not (Test-Path $FilePath -PathType Leaf)) {
69
+ Write-Host "File not found: $FilePath"
70
+ return
71
+ }
72
+
73
+ # Construct the command to format the JSON file to single line using jq
74
+ $command = "jq -c . '$FilePath' > '$FilePath.tmp' && move /Y '$FilePath.tmp' '$FilePath'"
75
+
76
+ # Execute the command
77
+ Invoke-Expression -Command $command
78
+ Write-Host "Formatted $($FilePath) to single-line successfully."
79
+ }
80
+
81
+ # Usage examples:
82
+ # Format a single JSON file to single-line format
83
+ # Format-JSONFileToSingleLine -FilePath "C:\path\to\your\file.json"
84
+
85
+ # Format all JSON files in a directory to single-line format
86
+ # Format-JSONFilesToSingleLine -Path "C:\path\to\your\directory"