appmake 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/appmake.rb +69 -2
  2. metadata +1 -1
data/lib/appmake.rb CHANGED
@@ -23,7 +23,7 @@ class Appmake
23
23
  def self.watch
24
24
  tpl_callback = Proc.new do |modified, added, removed|
25
25
  puts "=> rebuilding TPL"
26
- system("node bin/compile_templates.js")
26
+ system("node -e #{COMPILE_TEMPLATES_JS}")
27
27
  end
28
28
 
29
29
  tpl_listener = Listen.to "tpl", :filter => /\.html$/
@@ -48,4 +48,71 @@ class Appmake
48
48
  js_listener.change(&js_callback)
49
49
  js_listener.start
50
50
  end
51
- end
51
+ end
52
+
53
+ COMPILE_TEMPLATES_JS = <<EOS
54
+ var app_dir = __dirname + '/../',
55
+ tpl_dir = app_dir + 'tpl/',
56
+ js_dir = app_dir + 'js/',
57
+
58
+ fs = require('fs'),
59
+ dot = require('../js/lib/doT.js'),
60
+
61
+ templates = {};
62
+
63
+ walk();
64
+ fs.writeFile(js_dir + 'templates.js', buildModule(templates));
65
+
66
+
67
+ function walk(subdir) {
68
+ var dir = tpl_dir,
69
+ files;
70
+
71
+ if (subdir)
72
+ dir += subdir;
73
+
74
+ files = fs.readdirSync(dir);
75
+ files.forEach(function (filename) {
76
+ var stats = fs.statSync(dir + filename);
77
+
78
+ if (stats.isDirectory()) {
79
+ if (subdir) filename = subdir + filename;
80
+ filename += '/';
81
+
82
+ walk(filename);
83
+ }
84
+
85
+ if (!isHTML(filename))
86
+ return;
87
+
88
+ readFile(filename, subdir, fs.readFileSync(dir + filename));
89
+ });
90
+ }
91
+
92
+ function readFile (filename, dir, data) {
93
+ var name = filename.split('.').slice(0, -1).join('.'),
94
+ compiled = dot.template(data).toString().replace(/^function anonymous/, 'function ');
95
+
96
+ if (dir)
97
+ name = dir + name;
98
+
99
+ templates[name] = compiled;
100
+ }
101
+
102
+ function isHTML(name) {
103
+ if (!~name.indexOf('.'))
104
+ return false;
105
+
106
+ return name.split('.').slice(-1)[0].toLowerCase() === 'html';
107
+ }
108
+
109
+ function buildModule() {
110
+ var content = [],
111
+ n;
112
+
113
+ for (n in templates)
114
+ content.push('"' + n + '": ' + templates[n]);
115
+
116
+ return ['module.exports = {', content.join(','), '};'].join('').replace(/(\n|\r|\r\n)/, '');
117
+ }
118
+ EOS
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appmake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: