Question

I want to minify my JavaScript files immediately in Gedit. I tried do it via external tools option with jsmin.js script. I have SpiderMonkey engine installed. I stored jsmin.js file at /home/mushex/use/js/jsmin.js and created a new js file named jsmin-low.js in the same directory with content

#!/usr/bin/js 

load('/home/mushex/use/js/jsmin.js');

var body = arguments[0],
    result = jsmin('', body, 1);
if (result) {
    print(result);
} else {
    print(body);
}

It prints out undefined. For debugging I've changed script to perform only in argument printing action , and saw that input is null (undefined). Source of file for debugging was

#!/usr/bin/js 
print(arguments[0]);

But when I'm running it in command line it's output is correct. And via gedit other js command line tools are working normal. Wandering why my input arguments don't passing.

Here are settings I set for this tool in gedit.

Settings http://imagebin.org/index.php?mode=image&id=63960

Any help will be greatly appreciated Thanks.

Was it helpful?

Solution

The problem is that gedit is sending the document into your program's standard input, not as a command-line argument. The SpiderMonkey shell has a readline() function that reads a line from stdin, but it doesn't have a way of knowing when you reach EOF.

If you compile SpiderMonkey with File support, you could probably do it, but I've never tried that.

If you use the Rhino shell, you can use Java classes directly like this:

function readStdin() {
    var stdin = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System["in"]));
    var lines = [];
    var line;
    while ((line = stdin.readLine()) !== null) {
        lines.push(line);
    }
    return lines.join("\n");
}

var body = readStdin();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top