How to run JavaScript using jsc on OS X? - javascript

I got this when I was trying to find some way to run my JavaScript programs through terminal. The run and load command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store locally on my system.
EDIT: I am trying to solve Project Euler Q10 in JavaScript. So this is the program that I want to run in NodeJs or JSC. I need help in running the JavaScript files in Node and JSC.
Any example will be really helpful.
Thank You all.

Complements of this post, JSC lives at
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
and is not in the shell PATH by default. You can fix that with
ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin
jsc takes filenames as arguments. You could run a file named demo.js with
jsc demo.js
Note that you'll have to use debug() instead of the conventional console.log() in your script to see any output.

NOTE for MacOS Catalina (10.15.x) users
jsc now lives on a new path:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc
Make that adjustment to #hurrymaplelad's instructions and you'll be good to go.

You're not very clear on what sort of programs these scripts are. Do they do local tasks (e.g. interact with the local file system)? Or are they web scripts that are supposed to interact with (HT|X)ML documents?
If your usage scenario is the former, I recommend using node.js. It is under heavy development, but it is already very usable. For my first project with Node I wrote an XMPP chat bot, completely in JavaScript.
Edit:
I seem to have overlooked "using jsc" in the question and answered "How to run JavaScript on OS X?" instead. Still, I think Node is a better alternative if the author is looking to use JavaScript in place of something like Python or Perl.

For future reference: I made a GitHub gist with updated info on using JSC, is available here.

Related

How to implement Git in pure Javascript to create a GUI?

after searching quite a lot without findind nothing about it I'm here to ask you if there is a way to implement Git in a pure Javascript web application.
I already know about Git.js but it implements just some basic things and I also wanted to build my own library to learn more in depth about Git.
What I'm not looking for is an API or a lib that could help me.
What I'm looking for is something like:
var command = {{git commit -m "Hello world"}} // Also pure git implementation
gitExecute(command);
I'm still a junior developer and maybe this could be impossible...thanks for the reply :)
What you are asking for may be difficult to do in a browser (because you will need access to the file system to run git commands). What you may need to do is create a NodeJS server which exposes REST endpoints which can be accessed by code in the browser which provides the GUI. The NodeJS server code can run commands as needed and respond to the REST HTTP requests which can be then used by your code in the browser to show/update the GUI.
The disadvantage with this method is that you will need to run your NodeJS server on the computer which has the repository and will not work if the repo is not local to the server.
Another alternative is to use the REST APIs exposed by popular GIT providers like GitHub.
EDIT:
Come to think of it, your usecase may be a good fit for an Electron App. That will allow you to build a desktop app (with access to the filesystem and privileges to execute commands) using Javascript.
For this purpose, NodeJS is mandatory.
You could install git on your server machine, and thene execute your cmds through nodejs' child processes (DOCs)

Format a drive in node.js

I'm working on a project in node.js and I'm stuck at this. I need to convert an external device ( a usb stick etc.) into an "exFAT" partition if it's not an exFAT . I used an npm package called "drivelist-scanner" to get the list of usb devices connected and also their mountpoints. Now the only thing left to do is to convert them into exFAT and I don't know how to do it. I came across an npm package called "gpt" which said it can format drives but the documentation is not so clear. Here's a link to the github repository for gpt : Github link
Any ideas on how to accomplish this? I'm even willing to launch a script based on the operating system to do the job, but I'm not sure on how to do this too. Any help is appreciated. Thanks.
Actually you can not directly format a drive with programming languages. You will need to invoke a operating system specific program to do the actual work for you.
Create a bash script
You can use this info for do that.
https://superuser.com/questions/274687/how-do-i-format-a-usb-drive-on-a-pc-that-was-formatted-on-a-mac
Run a bash script with node.js
You can use exec command for run a bash script with node.js
Run shell script with node.js (childProcess)

How can I create a Javascript application to run in the Linux terminal?

I want to create a Javascript (using Electron) app, but I want this app to be run and executed with terminal commands, like how you run git, is there a way to accomplish this?
I know that python and ruby are better languages for this purpose but I have a reason to use electron.
For non-GUI applications, you can just use node.js directly. If you want to make a TUI, you can use node.js + a module like blessed (and possibly blessed-contrib).
Electron is basically Chromium browser with tabs and all that stuff stripped out, plus a pile of tools to work with the user's desktop environment added in. It lets you use add HTML and CSS to a Node.JS application to create a GUI.
If all you need is a terminal command, Electron is completely unnecessary.
Here's a little pile of links to help you get started creating your command line app:
Writing command line applications in Node (Free Code Camp)
Scripting with Node (Atlassian)
Node.js with Commander npm module would work very well for your requirement.

How to ensure Just My Code is stepped into when debugging Javascript in NODE environment?

Given: Node.js console application (using Visual Studio) which has downloaded several packages via NPM. When NPM does this it downloads the source code of those packages and puts them into the node modules folder.
Problem: The javascript code that I'm writing needs debugging of which I will often single step into to see the code paths. But the problem is it defines "Just My Code" as being anything it used during the "compile" of the project. This includes those node modules so I wind up on long goose chases often.
Is there a way to exclude single stepping into the node modules and in effect only step into "just my code"...?
The answer to this question is that VS2015 code debugger code itself must do this. NTVS hasn't done it yet, and the MSFT team knows this is an issue. When they fix it I'll post back.

Why do frameworks and git hub ask you to use Terminal?

I'm a newbie and that's ok.
But when I use tutorials, they always tell me to go straight to my terminal and install documents from there in which I have no clue what the next step is and no idea why I have to do it. Is it essential that I install frameworks through terminal or can I just work without it?
ex. http://net.tutsplus.com/tutorials/javascript-ajax/real-time-messaging-for-meteor-with-meteor-streams/
"Let’s create a very simple, browser console based chat application with Meteor Streams. We’ll first create a new Meteor application:"
meteor create hello-stream
I understand this is may not be very challenging, but I don't have a clear answer when I try and simply google it. Anything would help, thanks a lot!
Being a newbie is ok.
The terminal is the go to because writing programs, tools, and scripts with a couple lines of bash or python is, well, a couple of lines versus an entire gui and all the lines of code to go with it.
meteor create hello-stream
That's telling you to run the program called meteor with the supplied arguments (create hello-stream).
If you haven't installed meteor, do that next. From their website, open your terminal and type/paste:
curl https://install.meteor.com | sh
That downloads a file from the URL and pipes (|) it to the sh command. The file it downloads is a shell script that takes care of setup/installation. Now go back and try the meteor command again.
More reading
You might try and find a terminal tutorial. This is the first one I found:
http://www.ee.surrey.ac.uk/Teaching/Unix/
Answer from someone who only recently (as in: within the last year) got into using the Terminal more and more frequently:
It's generally a good idea to get used to using the Terminal for code-related things as you can pretty much use it platform-independently (meaning you can use the same commands on Mac, Linux, Windows,...) and because it makes you understand better what you're actually doing.
Once you've got the hang of it, it can make navigating your system as well as making changes to it easier. Hidden files, for example (like .gitignore files, or files in your .ssh directory, or your .bash_profile), can be viewed, opened and edited easily from the Terminal whereas you have to take multiple, complicated steps (or install other software) to view them from your Finder.
With the help of package managers (like Homebrew), which are also operated from the Terminal, you can also install software that isn't otherwise available for Mac.

Categories