Execute multiple terser commands (compression/mangling) inside a .bat file - javascript

Terser is a free tool for compressing and/or mangling javascript files.
As far as I have understood, the tool relies on node.js, so that is a pre-req for running Terser in the first place.
This will execute the terser command on a single file:
terser input.js -c -m -o output.js
This is fine for some files that aren't updated frequently, but if you have a large set of Javascript files in your project that are updated instantly, you would appreciate something more dynamic.
I was thinking about including each file command inside a .bat file and execute that as one file, but I cannot get that to work: I tried placing these between the ECHO AND PAUSE commands in the bat file but it doesn't work well unfortunately.
As far as I have seen, the documentation covers just running terser as CLI
Is there a way to "move" the command lines from the CLI environment to the .bat file?

I encountered the same problem today as I was trying to use "terser" in batch files for the first time (instead of uglify-es I used precedently)
After each "terser" command the execution of the batch file stops
After adding a pipe | pause at the end of each terser command, my batch file runs as usual and executes the next commands.

Related

How to see output on VS Code with Typescript?

I am doing some code challenges using Typescript in VS code. When I try to run the code and see the output, I get "Code Language is not supported or defined". The language mode is set to Typescript React (I also tried just Typescript). And the file has a .tsx ending. Finally, I also did compile the file and make a duplicate .js version. Is there something I am forgetting?
You cannot run a typescript file the way you would run a javascript file. You will need to first compile your typescript file into javascript with the command below in the directory containing your .ts file:
npx tsc -w <your-file-name.ts>
The command above will create a <your-file-name.js> file in the same directory and keep watching for any realtime changes with -w option. Then you can run the .js file in the same directory with either node or nodemon like this:
node <your-file-name.js>
---UPDATE---
Having to input these two commands mentioned above tsc -w and node <your-file-name.js> every time you might get cumbersome after a few times, you can use the ts-node command which combines both of the commands stated above into one:
ts-node <your-file-name.ts>
Run npx tsc -w
And in another terminal run node filename.js
Judging by the fact that your file ends with .tsx you are probably using React. I will also assume that you used create-react-app to init your project.
If both of my assumptions are correct, try:
npm start
If you are new to both React and Typescript, I would also suggest taking a look into that tool as it creates a very decent starting point for you to start learning both. https://create-react-app.dev/

How can we write a shell script using NodeJs

I need to read CSV file, process the file via REST API call and then write the output in another CSV file. The requirement is strictly shell script using NodeJs.
I am using Windows platform
You could add a shebang to your .js file and make it executable.
#!/usr/bin/env node
console.log('...')
Alternatively you make a wrapper bash script and execute whatever you want
#!/usr/bin/env bash
node whatever.js
If you are on linux, start your script with the following line:
#!/usr/bin/env node
This would indicate that node interpreter should be used to execute this file.
Then make the file executable:
chmod +x your_script.js
At this point it should be possible to run script as any regular executable, e.g:
./your_script.js

How to execute files directly from Notepad++

For the past few weeks, I have been working with Node JS a lot, and i have found it pretty annoying to have to execute the file through cmd every time i want to test.
To make my life easier, I have made a batch file that executes my file for me. My question is how do I execute files directly from notepad++?
Is there a menu for it? Currently on my left I have a menu that lets me navigate between my files easily. On my right, I have a NppFTP UI that lets me connect to the FTP server, if I need to work on files that are not on my system.
I highly suspect that there is a way to execute files directly from Notepad++.
Although my answer doesn't directly answer your question, it will resolve the problem you were facing in the first place.
There is something called nodemon which will restart your node process as soon as you save your changes. It's very simple to use:
To install :
npm install -g nodemon
To use:
nodemon app.js
or Just [it automatically grabs the default app.js file or other]
nodemon
You can learn more about this here:
http://nodemon.io/

How to interpret and run the following ant command?

I am new to nodeJS and Jake but in my company they are using it to run unit tests.This is how they are running unit tests through ant
<exec executable="cmd" dir="${nodeJsTests.basedir}/../nodejs/">
<arg value="/C"/>
<arg value="start cmd /C "npm install & .\node_modules\.bin\jake local dir=${basedir} --trace & pause"" />
</exec>
From what I understood is they are doing the following things in this piece of code, do correct me if I am wrong
Going to nodejs driectory.
Installing jake at a particular location (.\node_modules.bin\jake)
Run unit tests
I want to achieve the same(run the tests), without using ant.I think I am able to do first two steps but stuck in the third step.I tried running command - Jake local from various directories but no success
If anyone can help me on this?
It's easier to see what's happening if you parse the string in the second arg value:
npm install & .\node_modules\.bin\jake local dir=${basedir} --trace & pause
This is a shorthand way of essentially running these 3 commands in order:
npm install
.\node_modules\.bin\jake local dir=${basedir} --trace
pause
The first command installs all the dependencies defined in your package.json file (which presumably includes jake).
The second command runs the local version of jake (the one that is installed inside the node_modules folder after running the previous step).
The third command is simply a cmd util to pause execution.
As long as you have installed the dependencies you should have no problem running jake without ANT (simply run the 2nd command above, replacing the ${basedir} value, and make sure you run it in the same directory that your package.json is located in).

How can I beautify JavaScript code using Command Line?

I am writing a batch script in order to beautify JavaScript code. It needs to work on both Windows and Linux.
How can I beautify JavaScript code using the command line tools?
First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it's what I found first. Downloads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js
Second, download and install The Mozilla group's Java based Javascript engine, Rhino. "Install" is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or Library/Java/Extensions on OS X). You can then run scripts with an invocation similar to this
java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js
Use the Pretty Print/Beautifier from step 1 to write a small shell script that will read in your javascript file and run it through the Pretty Print/Beautifier from step one. For example
//original code
(function() { ... js_beautify code ... }());
//new code
print(global.js_beautify(readFile(arguments[0])));
Rhino gives javascript a few extra useful functions that don't necessarily make sense in a browser context, but do in a console context. The function print does what you'd expect, and prints out a string. The function readFile accepts a file path string as an argument and returns the contents of that file.
You'd invoke the above something like
java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js
You can mix and match Java and Javascript in your Rhino run scripts, so if you know a little Java it shouldn't be too hard to get this running with text-streams as well.
UPDATE April 2014:
The beautifier has been rewritten since I answered this in 2010. There is now a python module in there, an npm Package for nodejs, and the jar file is gone. Please read the project page on github.com.
Python style:
$ pip install jsbeautifier
NPM style:
$ npm -g install js-beautify
to use it (this will return the beatified js file on the terminal, the main file remains unchanged):
$ js-beautify file.js
To make the changes take effect on the file, you should use this command:
$ js-beautify -r file.js
Original answer
Adding to Answer of #Alan Storm
the command line beautifier based on http://jsbeautifier.org/ has gotten a bit easier to use, because it is now (alternatively) based on the V8 javascript engine (c++ code) instead of rhino (java-based JS engine, packaged as "js.jar"). So you can use V8 instead of rhino.
How to use:
download jsbeautifier.org zip file from
http://github.com/einars/js-beautify/zipball/master
(this is a download URL linked to a zip file such as http://download.github.com/einars-js-beautify-10384df.zip)
old (no longer works, jar file is gone)
java -jar js.jar name-of-script.js
new (alternative)
install/compile v8 lib FROM svn, see v8/README.txt in above-mentioned zip file
./jsbeautify somefile.js
-has slightly different command line options than the rhino version,
-and works great in Eclipse when configured as an "External Tool"
If you're using nodejs then try uglify-js
On Linux or Mac, assuming you already have nodejs installed, you can install uglify with:
sudo npm install -g uglify-js
And then get the options:
uglifyjs -h
So if I have a source file foo.js which looks like this:
// foo.js -- minified
function foo(bar,baz){console.log("something something");return true;}
I can beautify it like so:
uglifyjs foo.js --beautify --output cutefoo.js
uglify uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it through unexpand which Ubuntu 12.04 comes with:
unexpand --tabs=4 cutefoo.js > cuterfoo.js
Or you can do it all in one go:
uglifyjs foo.js --beautify | unexpand --tabs=4 > cutestfoo.js
You can find out more about unexpand here
so after all this I wind up with a file that looks like so:
function foo(bar, baz) {
console.log("something something");
return true;
}
update 2016-06-07
It appears that the maintainer of uglify-js is now working on version 2 though installation is the same.
On Ubuntu LTS
$ sudo apt install jsbeautifier
$ js-beautify ugly.js > beautiful.js
For in place beautifying, any of the follwing commands:
$ js-beautify -r file.js
$ js-beautify --replace file.js
You have a few one liner choices. Use with npm or standalone with npx.
Semistandar
npx semistandard "js/**/*.js" --fix
Standard
npx standard "js/**/*.js" --fix
Prettier
npx prettier --single-quote --write --trailing-comma all "js/**/*.js"
In the console, you can use Artistic Style (a.k.a. AStyle) with --mode=java.
It works great and it's free, open-source and cross-platform (Linux, Mac OS X, Windows).
Use the modern JavaScript way:
Use Grunt in combination with the jsbeautifier plugin for Grunt
You can install everything easily into your dev environment using npm.
All you will need is set up a Gruntfile.js with the appropriate tasks, which can also involve file concatenation, lint, uglify, minify etc, and run the grunt command.
I'm not able to add a comment to the accepted answer so that's why you see a post that should have not existed in the first place.
Basically I also needed a javascript beautifier in a java code and to my surprise none is available as far as I could find. So I coded one myself entirely based on the accepted answer (it wraps the jsbeautifier.org beautifier .js script but is callable from java or the command line).
The code is located at https://github.com/belgampaul/JsBeautifier
I used rhino and beautifier.js
USAGE from console: java -jar jsbeautifier.jar script indentation
example: java -jar jsbeautifier.jar "function ff() {return;}" 2
USAGE from java code:
public static String jsBeautify(String jsCode, int indentSize)
You are welcome to extend the code. In my case I only needed the indentation so I could check the generated javascript while developing.
In the hope it'll save you some time in your project.
I've written an article explaining how to build a command-line JavaScript beautifier implemented in JavaScript in under 5 minutes. YMMV.
Download the latest stable Rhino and unpack it somewhere, e.g. ~/dev/javascript/rhino
Download beautify.js which is referenced from aforementioned jsbeautifier.org then copy it somewhere, e.g. ~/dev/javascript/bin/cli-beautifier.js
Add this at the end of beautify.js (using some additional top-level properties to JavaScript):
// Run the beautifier on the file passed as the first argument.
print( j23s_beautify( readFile( arguments[0] )));
Copy-paste the following code in an executable file, e.g. ~/dev/javascript/bin/jsbeautifier.sh:
#!/bin/sh
java -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*
(optional) Add the folder with jsbeautifier.js to PATH or moving to some folder already there.
I believe when you asked about command line tool you just wanted to beautify all your js files in batch.
In this case Intellij IDEA (tested with 11.5) can do this.
You just need to select any of your project files and select "Code"->"Reformat code.." in main IDE menu. Then in the dialog select "all files in directory ..." and press "enter".
Just make sure you dedicated enough memory for the JVM.

Categories