I have a program that is currently taking a specific Javascript file and it’s going to prettify it for me. The method provided by ChatGPT 4.0 is supposed to work but I am having a hard time having my code see that I do have node.exe embedded as a file. It’s there, and I have the .exe in multiple locations within the project folders for verification. What Am I doing wrong? Here is the code:
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using Flurl.Http;
namespace JSFilePrettifier
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static async Task DownloadFileAsync(string fileUrl, string fileName, Action<string> updateStatus)
{
try
{
var tempFilePath = Path.GetTempFileName();
await fileUrl.DownloadFileAsync(Path.GetDirectoryName(tempFilePath), Path.GetFileName(tempFilePath));
var totalBytes = new FileInfo(tempFilePath).Length;
updateStatus($"Total file size: {totalBytes} bytes");
using (var inputStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
var buffer = new byte[8192];
long totalRead = 0L;
int bytesRead;
while ((bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
totalRead += bytesRead;
if (totalBytes != -1L)
{
double progress = (double)totalRead / totalBytes * 100;
updateStatus($"Downloaded {totalRead} of {totalBytes} bytes ({progress:0.00}%)");
}
else
{
updateStatus($"Downloaded {totalRead} bytes");
}
}
updateStatus($"Final downloaded size: {totalRead} bytes");
}
File.Delete(tempFilePath);
updateStatus("Download complete.");
}
catch (Exception ex)
{
updateStatus($"Error: {ex.Message}");
}
}
public static string PrettifyJavaScript(string jsCode)
{
try
{
string nodePath = ExtractResource("node");
string jsBeautifierPath = ExtractResource("JSFilePrettifier.Resources.js_beautify");
string tempInputPath = Path.GetTempFileName();
string tempOutputPath = Path.GetTempFileName();
File.WriteAllText(tempInputPath, jsCode);
var startInfo = new ProcessStartInfo
{
FileName = nodePath,
Arguments = $"{jsBeautifierPath} {tempInputPath} -o {tempOutputPath}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
}
string prettifiedJsCode = File.ReadAllText(tempOutputPath);
File.Delete(tempInputPath);
File.Delete(tempOutputPath);
return prettifiedJsCode;
}
catch (Exception ex)
{
throw new Exception($"Error during prettification: {ex.Message}");
}
}
public static string ExtractResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourcePath = $"{resourceName}";
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream == null)
throw new Exception($"Resource '{resourcePath}' not found.");
var tempPath = Path.GetTempFileName();
using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
}
return tempPath;
}
}
public static string GetOutputFilePath(string inputFilePath)
{
string directory = Path.GetDirectoryName(inputFilePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputFilePath);
string outputFileName = fileNameWithoutExtension + "-Decompiled.js";
return Path.Combine(directory, outputFileName);
}
}
}
I am getting an error at this line:
catch (Exception ex)
{
throw new Exception($"Error during prettification: {ex.Message}");
}
And this is the Error I am receiving:
- System.Exception: ‘Error during prettification: Resource ‘node’ not found.’
Here are the photos showing what I have in the Resources:
Showing the Resources
Node.exe is set as an embedded Resource
Can someone look at my code and see what is wrong and why I am getting this error?
I tried revising the code multiple time, including the name of the resource as well as the name of the file that it’s looking for in the code.
1