1 |
|
2 | var OptionParser, argv, coffeecup, compile, fs, handle_error, log, options, path, switches, usage, write;
|
3 |
|
4 | coffeecup = require('./coffeecup');
|
5 |
|
6 | fs = require('fs');
|
7 |
|
8 | path = require('path');
|
9 |
|
10 | log = console.log;
|
11 |
|
12 | OptionParser = require('coffee-script/lib/coffee-script/optparse').OptionParser;
|
13 |
|
14 | argv = process.argv.slice(2);
|
15 |
|
16 | options = null;
|
17 |
|
18 | handle_error = function(err) {
|
19 | if (err) {
|
20 | return console.log(err.stack);
|
21 | }
|
22 | };
|
23 |
|
24 | compile = function(input_path, output_directory, js, namespace) {
|
25 | if (namespace == null) {
|
26 | namespace = 'templates';
|
27 | }
|
28 | return fs.readFile(input_path, 'utf-8', function(err, contents) {
|
29 | var ext, func, name, output;
|
30 | handle_error(err);
|
31 | name = path.basename(input_path, path.extname(input_path));
|
32 | if (!js) {
|
33 | output = coffeecup.render(contents, options);
|
34 | ext = '.html';
|
35 | } else {
|
36 | func = coffeecup.compile(contents, options);
|
37 | output = "(function(){ \n this." + namespace + " || (this." + namespace + " = {});\n this." + namespace + "[" + (JSON.stringify(name)) + "] = " + func + ";\n}).call(this);";
|
38 | ext = '.js';
|
39 | }
|
40 | return write(input_path, name, output, output_directory, ext);
|
41 | });
|
42 | };
|
43 |
|
44 | write = function(input_path, name, contents, output_directory, ext) {
|
45 | var dir, filename;
|
46 | filename = name + ext;
|
47 | dir = output_directory || path.dirname(input_path);
|
48 | return path.exists(dir, function(exists) {
|
49 | var output_path;
|
50 | if (!exists) {
|
51 | fs.mkdirSync(dir);
|
52 | }
|
53 | output_path = path.join(dir, filename);
|
54 | if (contents.length <= 0) {
|
55 | contents = ' ';
|
56 | }
|
57 | return fs.writeFile(output_path, contents, function(err) {
|
58 | handle_error(err);
|
59 | if (options.print) {
|
60 | log(contents);
|
61 | }
|
62 | if (options.watch) {
|
63 | return log("Compiled " + input_path);
|
64 | }
|
65 | });
|
66 | });
|
67 | };
|
68 |
|
69 | usage = 'Usage:\n coffeecup [options] path/to/template.coffee';
|
70 |
|
71 | switches = [['-j', '--js', 'compile template to js function (template + embedded renderer)'], ['-b', '--bare', 'use with -j to compile template to js (template only)'], ['-c', '--core', 'use with -j to compile renderer to js (renderer only)'], ['-n', '--namespace [name]', 'global object holding the templates (default: "templates")'], ['-w', '--watch', 'watch templates for changes, and recompile'], ['-o', '--output [dir]', 'set the directory for compiled html'], ['-p', '--print', 'print the compiled html to stdout'], ['-f', '--format', 'apply line breaks and indentation to html output'], ['-u', '--utils', 'add helper locals (currently only "render")'], ['-z', '--optimize', 'optimize resulting JS'], ['-v', '--version', 'display coffeecup version'], ['-h', '--help', 'display this help message']];
|
72 |
|
73 | this.run = function() {
|
74 | var args, file, parser, _ref;
|
75 | parser = new OptionParser(switches, usage);
|
76 | options = parser.parse(argv);
|
77 | args = options["arguments"];
|
78 | delete options["arguments"];
|
79 | if (options.help || argv.length === 0) {
|
80 | log(parser.help());
|
81 | }
|
82 | if (options.version) {
|
83 | log(coffeecup.version);
|
84 | }
|
85 | if (options.utils) {
|
86 | if ((_ref = options.locals) == null) {
|
87 | options.locals = {};
|
88 | }
|
89 | options.locals.render = function(file) {
|
90 | var contents;
|
91 | contents = fs.readFileSync(file, 'utf-8');
|
92 | return coffeecup.render(contents, options);
|
93 | };
|
94 | }
|
95 | if (args.length > 0) {
|
96 | file = args[0];
|
97 | if (options.watch) {
|
98 | fs.watchFile(file, {
|
99 | persistent: true,
|
100 | interval: 500
|
101 | }, function(curr, prev) {
|
102 | if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
|
103 | return;
|
104 | }
|
105 | return compile(file, options.output, options.js, options.namespace);
|
106 | });
|
107 | }
|
108 | return compile(file, options.output, options.js, options.namespace);
|
109 | }
|
110 | };
|