/
create.js
84 lines (79 loc) · 2.45 KB
/
create.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
var sys = require('sys');
var fs = require('fs');
var utilities = require('josi/utilities');
this.task = {
name: 'create',
doc: 'create a new josi app',
execute: function(opts) {
var appName = opts.name;
if (utilities.fileOrDirectoryExists(appName)) {
sys.puts('Can\'t create a new josi app with the name "' + appName + '",' +
' as a file or directory with that name already exists.');
return;
}
sys.puts('Creating app...');
createApp(appName);
sys.puts('App created. Type "cd ' + appName + '; josi run" to start the web server.');
},
}
var createApp = function(appName) {
fs.mkdirSync(appName, 0777);
fs.writeFileSync(appName + '/app.js',
[
'this.init = function() {',
' this.router.add(',
' // this route matches: /<controller>/<action>/<id>',
' /^\\/(?:(\\w+)\\/?)?(?:(\\w+)\\/?)?(?:([0-9]+)\\/?)?$/,',
' { controller: \'home\', action: \'index\' }',
' );',
'};', ''
].join('\r\n')
);
createController(appName, 'home');
fs.mkdirSync(appName + '/views', 0777);
fs.writeFileSync(appName + '/views/master.html',
[
'<html>',
' <head><title><%= title %></title></head>',
' <body>',
' <h1><%= title %></h1>',
' <%= main %>',
' </body>',
'</html>', ''
].join('\r\n')
);
createView(appName, 'home', 'index');
};
var createController = function(appName, controllerName) {
var controllersDir = appName + '/controllers';
if (!utilities.fileOrDirectoryExists(controllersDir)) {
fs.mkdirSync(controllersDir, 0777);
}
fs.writeFileSync(appName + '/controllers/' + controllerName + '.js',
[
'var view = require(\'josi/actionresults\').view;',
'',
'this.index = function() {',
' return view({',
' title: \'' + appName + ' - a <a href="http://thatismatt.github.com/josi/">josi</a> app\',',
' name: \'' + appName + '\'',
' });',
'};', ''
].join('\r\n')
);
};
var createView = function(appName, controllerName, viewName) {
var viewsDir = appName + '/views';
if (!utilities.fileOrDirectoryExists(viewsDir)) {
fs.mkdirSync(viewsDir, 0777);
}
viewsDir = viewsDir + '/' + controllerName;
if (!utilities.fileOrDirectoryExists(viewsDir)) {
fs.mkdirSync(viewsDir, 0777);
}
fs.writeFileSync(appName + '/views/' + controllerName + '/' + viewName + '.html',
[
'<p>App name: <b><%= name %></b></p>', ''
].join('\r\n')
);
};