UNPKG

4.25 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.10.0
2(function() {
3 var Stream, coffee, coffee_helpers, compile, conf, convert, errx, fs, optparse, parse_opts, path, read_file, read_stdin, user_agent;
4
5 fs = require('fs');
6
7 path = require('path');
8
9 Stream = require('stream');
10
11 coffee = require('coffee-script');
12
13 coffee_helpers = require('coffee-script/lib/coffee-script/helpers');
14
15 convert = require('convert-source-map');
16
17 optparse = require('optparse');
18
19 conf = {
20 quiet: false,
21 progname: path.basename(process.argv[1]),
22 maps: true,
23 output: process.stdout,
24 stdin_literate: false,
25 bare: false,
26 header: true
27 };
28
29 errx = function(exit_code, msg) {
30 if (!conf.quiet) {
31 console.error(conf.progname + " error: " + msg);
32 }
33 if (exit_code) {
34 return process.exit(exit_code);
35 }
36 };
37
38 user_agent = function() {
39 var ver, ver_cs;
40 ver = JSON.parse(fs.readFileSync(__dirname + "/../package.json")).version;
41 ver_cs = require('coffee-script/lib/coffee-script/coffee-script').VERSION;
42 return conf.progname + "/" + ver + " (CoffeeScript " + ver_cs + "; " + process.platform + "; " + process.arch + ") node/" + process.versions.node;
43 };
44
45 parse_opts = function(src) {
46 var opt, p;
47 opt = [["-h", "--help", "output usage information & exit"], ["-V", "--version", "output the version number & exit"], ["-o", "--output [FILE]", "write result to a FILE instead of stdout"], ["-l", "--literate", "treat stdin as literate style coffee-script"], ["-b", "--bare", "compile without a top-level function wrapper"], ["--no-map", "don't include inline source map (why?)"]];
48 p = new optparse.OptionParser(opt);
49 p.banner = "Usage: " + conf.progname + " [options] [file.coffee]";
50 p.on('no-map', function() {
51 return conf.maps = false;
52 });
53 p.on('bare', function() {
54 return conf.bare = true;
55 });
56 p.on('help', function() {
57 console.log(p.toString());
58 return process.exit(0);
59 });
60 p.on('version', function() {
61 console.log(user_agent());
62 return process.exit(0);
63 });
64 p.on('output', function(unused, val) {
65 return conf.output = val;
66 });
67 p.on('literate', function() {
68 return conf.stdin_literate = true;
69 });
70 p.on(function(o) {
71 return errx(1, "unknown option " + o);
72 });
73 return [p.parse(src), p];
74 };
75
76 read_file = function(fname) {
77 var e, error;
78 try {
79 return fs.readFileSync(fname).toString();
80 } catch (error) {
81 e = error;
82 return errx(1, fname + " reading: " + e.message);
83 }
84 };
85
86 read_stdin = function() {
87 return read_file('/dev/stdin');
88 };
89
90 compile = function(fname, fcontent, opt) {
91 var comment, compiled, e, error, key, options, val;
92 if (opt == null) {
93 opt = {};
94 }
95 if (fcontent.match(/^\s*$/)) {
96 return '';
97 }
98 options = {
99 sourceMap: conf.maps,
100 generatedFile: fname,
101 filename: fname,
102 bare: conf.bare,
103 header: conf.header,
104 inline: true,
105 literate: coffee_helpers.isLiterate(fname)
106 };
107 for (key in opt) {
108 val = opt[key];
109 options[key] = val;
110 }
111 try {
112 compiled = coffee.compile(fcontent, options);
113 } catch (error) {
114 e = error;
115 console.error(coffee_helpers.updateSyntaxError(e.toString(), fcontent, fname));
116 process.exit(1);
117 }
118 if (conf.maps) {
119 comment = convert.fromJSON(compiled.v3SourceMap).setProperty('sources', [fname]).toComment();
120 return compiled.js + "\n" + comment;
121 } else {
122 return compiled.toString();
123 }
124 };
125
126 exports.main = function() {
127 var args, js, p, ref;
128 ref = parse_opts(process.argv), args = ref[0], p = ref[1];
129 args = args.slice(2);
130 if (args.length) {
131 js = compile(args[0], read_file(args[0]));
132 } else {
133 js = compile('[stdin]', read_stdin(), {
134 literate: conf.stdin_literate
135 });
136 }
137 if (!(conf.output instanceof Stream)) {
138 conf.output = fs.createWriteStream(conf.output);
139 }
140 conf.output.on('error', function(err) {
141 return errx(1, "output stream: " + err.message);
142 });
143 conf.output.write(js);
144 if (conf.output.path) {
145 return conf.output.end();
146 }
147 };
148
149}).call(this);