-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJakefile
262 lines (217 loc) · 6.73 KB
/
Jakefile
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var path = require('path')
, fs = require('fs')
, cwd = process.cwd()
, utilities = require('utilities')
, genutils = require('geddy-genutils')
, helpers = require('./helpers')
, genDirname = __dirname;
var ns = 'app';
// Tasks
task('default', function(name, engine, realtime) {
jake.Task['app:create'].invoke(name, engine, realtime);
});
namespace(ns, function() {
task('help', function() {
console.log(
fs.readFileSync(
path.join(__dirname, 'help.txt'),
{encoding: 'utf8'}
)
);
});
// Creates a new Geddy app scaffold
task('create', function(name) {
var basePath = path.join(genDirname, 'base')
, templatePath = path.join(genDirname, 'template')
, mkdirs
, cps
, text
, adapter;
if (!name) {
throw new Error('No app name specified.');
}
var appPath = path.join(process.cwd(), name);
var bower = genutils.flagSet(null, '--bower');
var views = genutils.flagSet(null, '--views');
// create app dir
jake.mkdirP(appPath);
// copy template/ into app dir
var templateFiles = fs.readdirSync(templatePath);
templateFiles.forEach(function(_path) {
var fullPath = path.join(templatePath, _path);
if (fs.statSync(fullPath).isDirectory()) {
jake.cpR(fullPath, appPath, { silent: true });
}
else {
fs.writeFileSync(path.join(appPath, _path), fs.readFileSync(fullPath));
}
});
cps = [
['gitignore.txt', '.gitignore']
];
cps.forEach(function (cp) {
jake.cpR(path.join(basePath, cp[0]), path.join(appPath, cp[1]), {silent: true});
});
// Compile Jakefile
genutils.template.write(
path.join(basePath, 'Jakefile.ejs'),
path.join(appPath, 'Jakefile'),
{appName: name}
);
// Compile package.json
genutils.template.write(
path.join(basePath, 'package.json.ejs'),
path.join(appPath, 'package.json'),
{appName: name}
);
console.log('Created app ' + name + '.');
process.chdir(appPath);
if (bower) {
jake.Task['app:bower'].invoke();
}
if (views) {
jake.Task['app:views'].invoke();
}
});
desc('Sets up bower for your app.');
task('bower', function(deps) {
if (!genutils.inAppRoot()) {
fail('You must run this generator from the root of your application.');
return;
}
if (!deps) {
var deps = {};
}
var numDeps = 0;
for(var key in deps) {
numDeps++;
}
var appPath = process.cwd();
var pkg = require(path.join(appPath, './package.json'));
var bowerJsonPath = path.join(appPath, 'bower.json');
var bowerrcPath = path.join(appPath, '.bowerrc');
// create bower.json
if (!fs.existsSync(bowerJsonPath)) {
genutils.template.write(
path.join(__dirname, 'bower', 'bower.json.ejs'),
bowerJsonPath,
{ deps: deps, numDeps: numDeps, pkg: pkg }
);
}
// update existing bower.json
else {
var bowerJson = fs.readFileSync(bowerJsonPath, 'utf8');
fs.writeFileSync(bowerJsonPath, helpers.updateBowerJson(bowerJson, pkg), {encoding: 'utf8'});
console.log('[Updated] bower.json');
}
// create .bowerrc
if (!fs.existsSync(bowerrcPath)) {
jake.cpR(path.join(__dirname, 'bower', '.bowerrc'), bowerrcPath, {silent: true});
console.log('[Added] .bowerrc');
}
console.log('Setup bower for your app.');
});
desc('Creates the main views for your app.');
task('views', {async: true}, function(engine, framework, templatesDir) {
if (!genutils.inAppRoot()) {
fail('You must run this generator from the root of your application.');
return;
}
if (!engine) {
var engine = process.env['engine'] || 'ejs';
}
if (!framework) {
var framework = process.env['framework'] || 'bootstrap/none';
}
// parse the framework
framework = framework.split('/', 2);
framework = {
css: framework[0],
js: (framework.length == 2) ? framework[1] : 'none'
};
if (!templatesDir) {
var templatesDir = process.env['templates'] || path.join(__dirname, 'views');
}
var bower = genutils.flagSet(null, '--bower');
var ext = genutils.template.getExtFromEngine(engine);
var appPath = process.cwd();
var viewData = {
engine: engine,
framework: framework,
genutils: genutils
};
// create views
genutils.jake.loadFiles(genutils.getGenDir('geddy-gen-view'));
var viewTask = jake.Task['view:create'];
var files = new jake.FileList();
files.include(templatesDir + '/**/*.html.ejs');
files.toArray().forEach(function(file) {
var relFile = path.relative(templatesDir, file);
viewTask.reenable();
viewTask.invoke(
path.join(path.dirname(relFile), path.basename(relFile, path.extname(file)) + ext),
file,
viewData,
transformTemplate
);
});
// copy templates from the public/ folder
var publicDir = path.join(__dirname, 'public');
files = new jake.FileList();
files.include(publicDir + '/**/*.*');
files.toArray().forEach(function(file) {
var relFile = path.relative(publicDir, file);
var ext = path.extname(file);
var dest;
// render templates
if (ext === '.ejs') {
dest = path.join(appPath, 'public', path.dirname(relFile), path.basename(relFile, ext));
jake.mkdirP(path.dirname(dest));
genutils.template.write(
file, dest,
viewData,
transformTemplate
);
}
// but copy regular files
else {
dest = path.join(appPath, 'public', relFile);
jake.mkdirP(path.dirname(dest));
jake.cpR(file, dest, {silent: true});
}
});
// install bower components based on frameworks
if (bower) {
console.log('installing bower components now ...');
var deps = ['jquery'];
if (['bootstrap', 'foundation'].indexOf(framework.css) !== -1) {
deps.push(framework.css);
}
genutils.bower.install(deps, onBowerInstalled)
}
function transformTemplate(content)
{
return content.replace(/\<\@/g, '<%').replace(/\@\>/g, '%>').trim();
}
function onBowerInstalled(err)
{
if (err) {
console.error(err);
}
complete();
}
});
desc('Clears the test temp directory.');
task('clean', function() {
console.log('Cleaning temp files ...');
var tmpDir = path.join(__dirname, 'test', 'tmp');
utilities.file.rmRf(tmpDir, {silent:true});
fs.mkdirSync(tmpDir);
});
});
testTask('App', ['app:clean'], function() {
this.testFiles.exclude('test/helpers/**');
this.testFiles.exclude('test/geddy-test-app');
this.testFiles.exclude('test/tmp/**');
this.testFiles.include('test/**/*.js');
});