I am currently writing a string of text to the console for testing reasons, as below:
console.log JSON.stringify #classification
How do I write this to a text file in coffeescript?
Cheers
James
In node.js - and I want to write to a filename of my choosing - not a default log file.
fs = require "fs"
fs.writeFile "classification.json", JSON.stringify(#classification), (error) ->
console.error("Error writing file", error) if error
Related
I am writing a VSC extension. What I wanna do is be able to edit a file by using the name of said file to find it?
I just don't know how I would edit the source of said file. I also don't know how to find that file.
EX:
I pass in a name to a function that finds and returns the file needed to edit.
function -> (name: string) -> File
function -> ("script.py") -> ./src/script.py
I hope this makes sense???
Do you not know the filePath?
// note escaped backslashes below
const file = await vscode.workspace.openTextDocument("C:\\Users\\Mark\\jump-and-select\\command-alias\\jsconfig.json");
// or use showTextDocument() instead if you want to open and see the document and changes
console.log(file);
// file.getText() has all your file text
// then look at edit(callback: (editBuilder: TextEditorEdit) => functionality
I have a specialized WebView extension in VS Code that I use to generate .Net Classes. These files are generated through an external command line tool. One of the features that the command line tool provides is that it writes to a specific file, the location of the generated file in JSON format.
I setup a file watcher on this particular file so that anytime it is updated, I run an extension method that parses that json file, extracts the file path from within the json and then opens that file inside VS Code.
While this works, my intent is to open this file inside a split editor, such that on one side I have my WebView (html) showing, and the other to show the file that was just opened (aka, that who's path came from the JSON file as mentioned above).
How do I open a file to be opposite side of a split window, keeping my webview ext. view on one side and the other side showing the newly opened file?
I have this working such that it opens the file, but not in a split-view editor
// uri points to the file to read JSON from
let fileUri: vscode.Uri = vscode.Uri.file(uri.fsPath);
// read JSON from relative path of this file
fss.readFile(fileUri.fsPath, 'utf8', function (err, data)
{
if(!err) {
try{
// parse the data read from file as JSON
var jsonObj = JSON.parse(data);
try{
// create uri from path within json
let fileToOpenUri: vscode.Uri = vscode.Uri.file(jsonObj.path);
// open and show the file inside VS code editor
vscode.window.showTextDocument(fileToOpenUri);
}
catch(ex)
{
// handle file Open error
vscode.window.showErrorMessage(ex);
}
}
catch(ex)
{
// handle JSON Parse error
vscode.window.showErrorMessage(ex);
}
}
else
{
// handle file read error
vscode.window.showErrorMessage(err.message);
}
});
Looking to open the file into the opposite side of a splitview.
vscode.window.showTextDocument(document, {
viewColumn: vscode.ViewColumn.Beside
});
https://code.visualstudio.com/api/references/vscode-api#TextDocumentShowOptions
https://code.visualstudio.com/api/references/vscode-api#ViewColumn
Actually I want to get user input from user whoever runs the script. I do not want to hardcode the testdata path in the script. for example when I run a script to test angularjs using protractor and javascript. User should be able to give path of the testdata, so that I can use that variable inside the script.
You can do this by passing in a params.testData value from the command line.
protractor conf.js --params.testData=D:\path\to\testdata.xlsx
Then in your test you will reference it using the global browser.params object. You will also need to use fs to read the file and process the data. Honestly, it would probably be easier if you created a .json file for the test data instead of an .xlsx but it looks there are libraries out there to help you parse an xlsx document already if you have to stick with that. Check this answer for some examples.
This code will not work as is but the basic idea will be something like this:
before(() => {
const testDataPath = browser.params.testData;
fs.readFile(testDataPath, (err, data) => {
if(err) { // fail? };
const testData = data;
// do some other stuff with test data ...
});
}
You are going to need to do some additional processing of the data from the .xlsx file to get it in the correct format but this should hopefully help get you on the right path.
I have created a taskpane addin for word that is using the Document.getFileAsync method to get the document contents in Compressed format (docx).
This works correctly for .docx files, but unsurprisingly fails if an old .doc file is used.
I get the following error:
code: 5001
message: "An internal error has occurred."
name: "Internal Error"
Is there a way to detect documents in invalid formats before calling getFileAsync?
I have tried reading the document properties format value using the following code:
return Word.run(function (context) {
var properties = context.document.properties;
context.load(properties, "format");
return context.sync()
.then(function () {
return properties.format;
});
});
But the returned value is always an empty string for both docx and doc files.
I would like to be able to detect old file formats so that I can display an appropriate error message to the users.
getFileAsync() method works for .docx file only. Just to detect the correct file you can simply check the extension of the file: fname.substr((~-fname.lastIndexOf('.') >>> 0) + 2) where fname is filename here. And prompt your message accordingly.
I have written a patcher for my game but I am stuck at the actual saving of the files part. I keep on getting the following error from unity:
System.ArgumentException: Name has invalid chars
at System.IO.FileStream..ctor....
Here is the code that is in charge of saving my files:
function downloadFile(file:String){
var download:WWW = WWW(rawDataFolder+""+file); //download file from platforms raw folder
yield download; // wait for download to finish
// var saveLoc = Application.persistentDataPath; //Location where the files will go
var saveLoc = "C:\\games";
try{
Debug.Log(saveLoc+"\\"+file);
File.WriteAllBytes (saveLoc+"\\"+file+".FILE", download.bytes); //<----PROBLEM HERE.
}
catch(error){
updateMsg ="Update Failed with error message:\n\n"+error.ToString();
errorOccured = true;
Debug.Log(error);
}
}
I am trying to download a file called "level0". It doesn't have a file extension... in windows explorer it says it is simply 'FILE'. So I was thinking it was a binary file. Am I wrong? What might be causing my null character problem? This missing extension? Any help on this would be amazing.
I found out that my problem originated in the text file that I was reading. The text file must have had spaces in it. Using the ".Trim()" command I was able to remove the invalid char error. Once that was removed it worked perfectly reading files without extensions (Binary Files).