Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic JavaScript Compression with PowerShell

I'm looking to write a script that will take a bunch of .js files, compress them, and then replace the old ones with the new ones in that same folder. I've tried a few things but I find myself continuously encountering new problems one way or another, so I thought it'd be best to turn to the people who have a better understanding that I do and start fresh.

Can anyone point me in the right direction?

Update: I am using a set of commands similar to this:

>Get-ChildItem c:\NewFolder\ -recurse |
&java -jar yuicompressor-2.4.6

It does not seem like it wants to allow these sort of output usage though. I'm sure there is a way to make this work, but being fairly new still to PowerShell, I'm not too confident I can figure it out on my own just yet.

Update: Using the suggested command string below, I can get powershell to give me what seems to be a read out of a newly compressed .js but it won't replace the existing file with the compressed one or write it to the standard out which i believe to be in the same directory in [filename].min.js format.

Update: A modified version of the suggested command seems to do the trick!

>Get-ChildItem c:\NewFolder\ -exclude jquery*.js,*min.js -recurse | %{java -jar yuicompressor-2.4.6.jar ($_.fullname) -o ($_.fullname)}

However, when the command runs in PowerShell, oddly enough, I am getting an error message from PowerShell regarding the Java command...

java.exe : At line:4 char:72 + Get-ChildItem c:\Scripts\ -exclude jquery*.js,*min.js -recurse | %{java <<<< -jar yuicompressor-2.4.6.jar ($.fullname) -o ($.fullname)} + CategoryInfo : NotSpecified: (:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]

Global Options -h, --help Displays this information --type Specifies the type of the input file --charset Read the input file using --line-break Insert a line break after the specified column number -v, --verbose Display informational messages and warnings -o Place the output into . Defaults to stdout. Multiple files can be processed using the following syntax: java -jar yuicompressor.jar -o '.css$:-min.css' *.css java -jar yuicompressor.jar -o '.js$:-min.js' *.js

JavaScript Options nomunge Minify only, do not obfuscate preserve-semi Preserve all semicolons disable-optimizations Disable all micro optimizations

If no input file is specified, it defaults to stdin. In this case, the 'type' option is required. Otherwise, the 'type' option is required only if the input file extension is neither 'js' nor 'css'.

Any idea what PowerShell is trying to tell me?

like image 822
ShowStopper Avatar asked Sep 20 '11 15:09

ShowStopper


2 Answers

Try doing like this:

Get-ChildItem c:\NewFolder\ -recurse | %{java -jar yuicompressor-x.y.z.jar $_.fullname}

%{..} is alias for foreach-object. You get a set of files from c:\Newfolder ( and its subdirs) and pass each of these, as objects to the next component in the pipeline. This part being an external component that supports neither pipelining nor the objects, you wrap it in the foreach and also supply the file in the form it can understand - the fullname of the file ( which includes the path).

like image 105
manojlds Avatar answered Sep 19 '22 04:09

manojlds


This thread might offer you some answers. What do you use to minimize and compress JavaScript libraries?

That said, I believe the YUI compressor has a stand alone executable that could be launched from PowerShell.

like image 35
Brandon Boone Avatar answered Sep 21 '22 04:09

Brandon Boone