Javascript in IntelliJ's "IDE scripting console" - javascript

Recent versions of IntelliJ have an "IDE scripting console" option under the tools menu. I've had a hard time finding blog posts about it.
Some of these posts pointed out, and the gui proves this, that it has support for javascript.
Could someone please point me to an example or blogpost how to use this feature (api docs) with javascript please?

Here is how to use it: https://www.jetbrains.com/help/idea/ide-scripting-console.html
Here is the API docs: http://www.jetbrains.org/intellij/sdk/docs/welcome.html
Best/largest(?) set of examples I could find: https://gist.github.com/gregsh/b7ef2e4ebbc4c4c11ee9
Unfortunately for JS there is the least amount of examples.
That said, I've tried to implement an Action that starts the first Run Configuration. The code actually works pretty well. I've got stuck, however, on how to extend the Java Action abstract class from JS. Below is the working code:
var result = function run() {
var executor = com.intellij.execution.executors.DefaultDebugExecutor.getDebugExecutorInstance();
var runConfigsSettings = com.intellij.execution.RunManager.getInstance(IDE.project).allSettings;
var a = com.intellij.execution.ProgramRunnerUtil.executeConfiguration(IDE.project, runConfigsSettings[0], executor);
return a;
}()
I found the necessary docs here:
http://www.jetbrains.org/intellij/sdk/docs/basics/run_configurations/run_configuration_execution.html#starting-a-run-configuration-from-code
Hope this could help anyone.
Write in the comments if you know how to create an Action in JS.

IDE scripting console for JavaScript requires NodeJS installed on your computer (and make sure it's available on you PATH system variable for Windows OS, whereas for Mac OS you just need to install it with brew).
Main reason why it may not work is NodeJS installation missing/misconfigured, or due to IntelliJ being unable to get an access to NodeJS binaries (the latter is mainly Windows specific problem).
Once you have NodeJS up & running, simply write any JS code as if you did it in regular JS file, and press F10 (or control + enter in some configurations) in order to run the script
NOTE: I use fully licensed IntelliJ Ultimate 2017.x, all the above is for this particular version, I haven't used the community edition for a while, therefore I doubt I'll be able to help you with that one

Related

How can I find Htmlunit rhino version?

I am using the version of htmlunit 2.56.0.
As soon as JavaScript is executed in Java, the CPU is increasing a lot.
So I want to check the Rhino JavaScript engine version. How can I find it?
Or let me know if you know how to lower the CPU.
Thanks developers!!!
HtmlUnit uses a customized version of Rhino - you can find the repo here: https://github.com/HtmlUnit/htmlunit-core-js.
As HtmlUnit provides a bunch of patches to rhino the code is in sync with the latest code from the repo.
There are some issues open regarding js code (usually some obfuscated stuff) that shows this effect. If the url of the page is public please open an issue on github and i will try to have a look.
Even better if you can profile this or nail down the problem we are usually able to fix it or make improve Rhino itself to get this working.

How to debug Nodejs/Expressjs(MEAN stack) [duplicate]

I'm looking for effective way for step-by-step debugging NodeJS server code. At this moment I use dozens of console.log()'s and it's extremely hard. The perfect tool is one that would allow me to check the values of every variable in stack and trace my program line by line. Prefered OS = MacOS/Linux. Is it possible?
Basically, Node.js is built on top of V8, so its debugging capabilities are also built on top of V8's debugging capabilities.
V8 has an included debugger which can be accessed via TCP on port 5858.
So basically all you need is a frontend which is able to connect to port 5858 and talk V8's debugging protocol.
One option is to use node-inspector which basically provides a debugging UI in your browser. Unfortunately, it does only work with Google Chrome and Apple Safari (which for me is no problem, but there may be others ;-)).
Another option is to use a plugin for Eclipse.
And, last but not least, the built-in debugger of Node.js (which always reminds me of MS-DOS's edlin) is also just a front-end for this TCP debugger, just a built-in one.
And of course, there are much more options ... these three were just the first three ones that came to my mind ;-)
How about this?
You can try to test for Nodeclipse version 0.2.0 beta.
http://www.tomotaro1065.com/nodeclipse/
It will help you to debug node apps easily.
GENERATING OF EXPRESS PROJECT
Select the [File]-[New]-[Project] menu.
Select [Node]-[Express Project], and push [Next] button.
Enter [Project name], and push [Finish] button.
DEBUGGING
Open the JavaScript source files that you want to set breakpoints.
Double-click on the ruler at the left end of the line you want to set a breakpoint.
If you want to remove a breakpoint, double-click on the ruler again.
Select the main source file of Node Application on the Project Explorer,
open the context menu by right-clicking,
select the [Debug As]-[Node Application] menu.
Use node-inspector to provide the node debugging environment you're looking for. It's fantastic.
Check WebStorm. It is a great IDE, and also you can directly run your nodejs code, or connect to a debugging-enabled node process already running. In both cases, WebStorm provides what you are looking for: trace program execution line by line and on every line check the state of every variable.
Try nodeunit npm module for testing nodejs server side codes.
Even though question is very old, there is a awesome IDE from Microsoft "Visual Studio Code" which is developed in intention with Javascript (Typescript) which is capable of debugging Node JS projects
Checkout this link https://code.visualstudio.com/docs/nodejs/nodejs-debugging
node inspect built-in CLI step debugging
It took a while, but at some point Node finally added built-in debugging:
node inspect main.js
and this leaves you at the first line of the program.
Navigate with:
n step to next line
s step into function
c continue
o step out
h get help
In order to view variables or inject code, you need to first enter REPL mode with:
repl
and then you can run commands as in a node interactive session, e.g. to see the value of a variable just write the variable name:
myvar
It is awkward to have to type those four characters repl all the time, but I don't know any alternative.
You can also add a debugger statement to where you want to break in the program after a c:
const i = 1;
debugger;
i = 2;
Related question: How do I debug Node.js applications?
Tested in Node.js v10.15.1, Ubuntu 19.10.

How can I debug/step-through/watch my Node.js in Windows?

Sorry, maybe this belongs in programmers stack exchange, but I'm trying to get in to Node.js web development, and I really need to ability to step through my code in order to gain a deeper understanding of just what is happening in all the tutorials I'm using.
I've done some googling, but it looks like everything is written assuming you're in a *nix or OSX environment.
I've tried node-inspector, but I'm being greeted with errors whenever I try to run process._debugProcess() with the PID.
JetBrains WebStorm is relatively inexpensive IDE you can use with Node.js, which is quite feature rich considering the price.
Watch the demonstration video and you should get an idea to see if it's the kind of thing which could be helpful.
http://www.jetbrains.com/webstorm/
Alternatively you could use Eclipse and get this up and running.
https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger

Best ways to develop painlessly in Javascript on a local machine

I'm pretty new to workign with Javascript.
In most languages you can run the code quickly locally on your machine. From what I've seen, in JS you generally only use it via the browser, and so I've been uploading my code an viewing its effects in the browser. This has proven very tiresome. Also, if I mak one error, it seems like my JS/JQuery will just do NOTHING, instead of giving me a useful error, message, which is making it painfully slow to code in.
IS there some way to run JS locally to see that it is working as I go? And then only upload it to the web when I'm mostly done? What ways are there for me to do this? What ways aer there for me to unit test the Javascript locally? Say I have some JAML that should render as <p>HI</p>, how do I run this locally in a unit test?
Thanks for the help,
Alex
EDIT:
Thanks for all the great suggestions. I'll have to take a bit of time and go through them to see which ones best help me in my situation.
Since you're using jQuery, I assume that you actually want to manipulate the various elements on your page. So depending on your specific development enviroment, uploading it each time is probably the way to go anyway. If you can set up a dev enviroment on your local machine (not always possible) then go with that.
As an actual answer to your question, I suggest using Chrome's developer tools, it doesn't just have the console, but an element inspector, and a resource tracker (resource tracker is invaluable when working with JSON and AJAX, since invalid json will fail silently)
As far as I know, the firebug plugin for firefox (dont use it myself) has a similar feature set, so if you're more comfortable with that go with it.
Just remember, as a developer, your development (and debuggin) enviroment is just as important as the code that you are writing.
EDIT: Noticed that you mentioned unit testing. There are several unit testing frameworks out there for JS including one that integrates with firebug called FireUnit. Do a quick google search to find more if you want.
You don't need to upload the JS file to a server to test it. Just write an html and declare the js binding
<script
src="js/yourJSFile.js"
type="text/javascript"></script>
Edit the JS file in your favorite editor and then refresh the page to test it.
For unit testing the best option is Selenium. It allows you to record an interaction with the browser and then play it back.
You can use Firebug with Firefox to debug JS, and Google Chrome has a debugger built-in (use the Tools -> Developer Tools menu).
You can run Javascript from the local file on your machine in your browser, so you can skip the uploading step.
Also, I'd recommend using Firefox/Firebug combo for developing Javascript as it will be very handy, especially for the part you mentioned about not seeing what's going wrong with your code.
Even if you upload your javascript it gets downloaded back to you as soon as you visit the webpage that invoques it. Its run client side always. So stick to local and use firebug as the others have said. Google`s developer tool is quite nice too.
In the browser if you open the developer tools, follow the following steps:
1) Navigate to sources
2) Under sources, click snippet and open run.js
3) You can use run.js to write as much code as you want and run it locally only to see if your code is working or not (it will give you output on the console)
4) Also you can get used to some keyboard shortcuts to make it faster for you.
5) For small javascript codes, you can navigate to console and run your code there
If you want to do unit testing with Javascript there are extension of Firebug that can help you with that. I haven't try any of them, so I can't really tell you which one are worth considering, but you can easily find them if you search for the keyword "Firebug unit testing" on Google.
What seems to be comming on top is FireUnit. You can find some information about how it works here.
Consider Spider Monkey, which is a javascript engine separate from a browser. If what you are developing does not involve rendering to a webpage or can be separated from the rendering code (good practice!), then this could be useful.
I prefer Chrome to Firefox and I just found Web Server for Chrome.
It's just a Google App that quickly sets up a web server for you and will be set up anywhere you are logged into Chrome. It only allows file access to your current devices, or if you specify, other devices only on the current LAN.
You just point it to the directory with your index.html file and type http://127.0.0.1:8887 in your browser.
Additionally to the answers given you can use Jasmine for automated testing.
A tutorial that seems to help get started with automated testing on Jasmine is provided by Evan Hahn.
I used it and for me it works like a charm. Especially if test driven development is what you are going for!

Quick Test javascript snippet for browser compatibility

Just as we have http://www.sliver.com/dotnet/SnippetCompiler/ (link inactive on 2021-04-24) to test a C# code quickly, I can also do the same for javascript in chrome debugger tools and Firebug (firefox).
I would be more interested in a tool or some online tool which can run a small piece of javascript and tell if it properly runs in prominent browsers and the result.
UPDATE:
I'm a developer and not tester. I don't want to install all browsers on my PC. I like chrome and I can debug/test (for syntax/result) almost every javascript quickly in chrome by just clicking Ctrl+Shift+J and pasting my javascript in its console. But that will just test if it works in chrome. I'm not interested in creating a library right now. Google and Stackoverflow helps me get the greatest and latest javascript for a specific task very quickly.
I found one way. But it is not that quick and still would like to know better answer. May be some kind of tool which can do this.
One Way:-
Write your javascript enclosed in try-catch block on JsBin and create its public link. Now check your link with BrowserShots.
Write considerable amount of html (conditionally) using javascript to be able to see the difference on Images given by BrowserShots.
To propose another option 10 years down the line: I would suggest using JSHint for this.
The first page as you navigate to their site allows you to simply paste a JS snippet and get information regarding everything from potentially confusing syntax to "minimum ES version" warnings.
Furthermore, you can also install JSHint to your project and have a command to either run manually or slot into your existing build chain to do the same thing locally with custom rules.
I've also found this tool: JS Compatibility Checker, which is based on Can I use.
Could be helpful, but won't outline every issue.
You can easily make a test page to house your snippets, then try them in your target browsers. That has the added advantage of providing a central library for the snippets you do create, so you don't have to go hunting through entire directories looking for that cool little widget you created back in 2010.
Also, you can check the MDN Web Docs Browser Compatibility section if that interface is available (Eg. window.location - https://developer.mozilla.org/en-US/docs/Web/API/Location#browser_compatibility)

Categories

Resources