UNPKG

3.62 kBJavaScriptView Raw
1var OptionParser, argv, coffeecup, compile, fs, handle_error, log, options, path, switches, usage, write;
2
3coffeecup = require('./coffeecup');
4
5fs = require('fs');
6
7path = require('path');
8
9log = console.log;
10
11OptionParser = require('coffee-script/lib/coffee-script/optparse').OptionParser;
12
13argv = process.argv.slice(2);
14
15options = null;
16
17handle_error = function(err) {
18 if (err) return console.log(err.stack);
19};
20
21compile = function(input_path, output_directory, js, namespace) {
22 if (namespace == null) namespace = 'templates';
23 return fs.readFile(input_path, 'utf-8', function(err, contents) {
24 var ext, func, name, output;
25 handle_error(err);
26 name = path.basename(input_path, path.extname(input_path));
27 if (!js) {
28 output = coffeecup.render(contents, options);
29 ext = '.html';
30 } else {
31 func = coffeecup.compile(contents, options);
32 output = "(function(){ \n this." + namespace + " || (this." + namespace + " = {});\n this." + namespace + "[" + (JSON.stringify(name)) + "] = " + func + ";\n}).call(this);";
33 ext = '.js';
34 }
35 return write(input_path, name, output, output_directory, ext);
36 });
37};
38
39write = function(input_path, name, contents, output_directory, ext) {
40 var dir, filename;
41 filename = name + ext;
42 dir = output_directory || path.dirname(input_path);
43 return path.exists(dir, function(exists) {
44 var output_path;
45 if (!exists) fs.mkdirSync(dir, 0777);
46 output_path = path.join(dir, filename);
47 if (contents.length <= 0) contents = ' ';
48 return fs.writeFile(output_path, contents, function(err) {
49 handle_error(err);
50 if (options.print) log(contents);
51 if (options.watch) return log("Compiled " + input_path);
52 });
53 });
54};
55
56usage = 'Usage:\n coffeecup [options] path/to/template.coffee';
57
58switches = [['-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']];
59
60this.run = function() {
61 var args, file, parser;
62 parser = new OptionParser(switches, usage);
63 options = parser.parse(argv);
64 args = options.arguments;
65 delete options.arguments;
66 if (options.help || argv.length === 0) log(parser.help());
67 if (options.version) log(coffeecup.version);
68 if (options.utils) {
69 if (options.locals == null) options.locals = {};
70 options.locals.render = function(file) {
71 var contents;
72 contents = fs.readFileSync(file, 'utf-8');
73 return coffeecup.render(contents, options);
74 };
75 }
76 if (args.length > 0) {
77 file = args[0];
78 if (options.watch) {
79 fs.watchFile(file, {
80 persistent: true,
81 interval: 500
82 }, function(curr, prev) {
83 if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
84 return;
85 }
86 return compile(file, options.output, options.js, options.namespace);
87 });
88 }
89 return compile(file, options.output, options.js, options.namespace);
90 }
91};