forked from czottmann/js-beautify-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautify-node.js
217 lines (172 loc) · 5.64 KB
/
beautify-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*jslint adsafe: false, bitwise: true, browser: true, cap: false, css: false,
debug: false, devel: true, eqeqeq: true, es5: false, evil: true,
forin: false, fragment: false, immed: true, laxbreak: false, newcap: true,
nomen: false, on: false, onevar: true, passfail: false, plusplus: true,
regexp: false, rhino: true, safe: false, strict: true, sub: false,
undef: true, white: false, widget: false, windows: false */
/*global process: false, require: false */
"use strict";
/*
### JS Beautifier node command line script
Ported to use node by Carlo Zottmann, <[email protected]>. Original Rhino
version written by Patrick Hof, <[email protected]>.
This script is to be run with node[^1] on the command line.
Usage:
node beautify-node.js
You are free to use this in any way you want, in case you find this useful
or working for you.
[^1]: http://nodejs.org/
*/
require.paths.unshift( "./" );
( function() {
var fs = require( "fs" ),
sys = require( "sys" ),
http = require( "http" ),
url = require( "url" ),
jsb = require( "beautify" ),
options,
result = "";
function printUsage() {
sys.puts( [
"Usage: node beautify-node.js [options] [file || URL || STDIN]",
"",
"Reads from standard input if no file or URL is specified.",
"",
"Options:",
"-i NUM\tIndent size (1 for TAB)",
"-b\tPut braces on own line (Allman / ANSI style)",
"-a\tIndent arrays",
"-n\tPreserve newlines",
"-p\tJSLint-pedantic mode, currently only adds space between \"function ()\"",
"",
"-h\tPrint this help",
"",
"Examples:",
" beautify-node.js -i 2 example.js",
" beautify-node.js -i 1 http://www.example.org/example.js",
" beautify-node.js < example.js"
].join( "\n" ) );
}
function parseOpts( args ) {
var options = [],
param;
args.shift();
args.shift();
while ( args.length > 0 ) {
param = args.shift();
if ( param.substr( 0, 1 ) === "-" ) {
switch ( param ) {
case "-i":
options.indent = parseInt(args.shift());
break;
case "-b":
options.braces_on_own_line = true;
break;
case "-a":
options.keep_array_indentation = false;
break;
case "-p":
options.jslint_pedantic = true;
break;
case "-n":
options.preserve_newlines = true;
break;
case "-h":
printUsage();
process.exit();
break;
default:
console.error( "Unknown parameter: " + param + "\nAborting." );
process.exit( 0 );
}
}
else {
options.source = param;
}
}
return options;
}
function beautifySource( sourceFile ) {
var line,
indent_size = options.indent || 2,
indent_char = ( indent_size === 1 ) ? "\t" : " ";
sourceFile = sourceFile.replace( /^\s+/, "" );
if ( sourceFile && sourceFile[0] === "<" ) {
sys.puts( "HTML files not supported." );
process.exit( 0 );
}
else {
result = jsb.js_beautify( sourceFile, {
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines: !!options.preserve_newlines,
space_after_anon_function: options.jslint_pedantic,
keep_array_indentation: options.keep_array_indentation,
braces_on_own_line: options.braces_on_own_line
});
}
// Trying to output `result` in one go had funny side effects on OSX.
// Writing to a file would work fine, but the raw console output (printed
// on screen) was truncated. Really weird. So, line by line it is.
result.split( "\n" ).forEach( function( line, index, array ) {
sys.puts( line );
});
}
function getSourceFile() {
var req,
sourceFile = "",
sURL,
stdin;
if ( options.source ) {
if ( options.source.substring( 0, 4 ) === "http" ) {
// remote file
sURL = url.parse( options.source );
req = http
.createClient( ( sURL.port || 80 ), sURL.host )
.request( "GET", sURL.pathname + ( sURL.search || "" ), { host: sURL.hostname });
req.end();
req.on( "response", function( response ) {
response.setEncoding( "utf8" );
response
.on( "data", function( chunk ) {
sourceFile += chunk;
})
.on( "end", function() {
beautifySource( sourceFile );
});
// TODO: error handling
});
}
else {
// local file
sourceFile = fs.readFileSync( options.source, "utf-8" );
beautifySource( sourceFile );
}
}
else {
// I'll be honest: I don't know yet how to check whether there is any
// STDIN or not. When the script is called w/o parameters, it should
// print out usage information; when there's STDIN input, it should
// process that. So I figured that when there's no such input within
// 25ms, there won't be anything later on, either. If you know of a
// cleaner way to do this, please let me know. :) --Carlo
setTimeout( function() {
if ( sourceFile.length === 0 ) {
printUsage();
process.exit( 1 );
}
}, 25 );
stdin = process.openStdin();
stdin.setEncoding( "utf8" );
stdin
.on( "data", function( chunk ) {
sourceFile += chunk;
})
.on( "end", function() {
beautifySource( sourceFile );
});
}
}
options = parseOpts( process.argv );
getSourceFile();
}() );