forked from yeoman/generator-backbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-base.js
119 lines (95 loc) · 3.38 KB
/
script-base.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
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
'use strict';
var path = require('path');
var util = require('util');
var yeoman = require('yeoman-generator');
var backboneUtils = require('./util.js');
var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
this.env.options.appPath = this.config.get('appPath') || 'app';
if (this.env.options.minsafe) {
sourceRoot += '-min';
}
if (typeof this.env.options.coffee === 'undefined') {
this.option('coffee');
// attempt to detect if user is using CS or not
// if cml arg provided, use that; else look for the existence of cs
if (!this.options.coffee &&
this.expandFiles(path.join(this.env.options.appPath, '/scripts/**/*.coffee'), {}).length > 0) {
this.options.coffee = true;
}
this.env.options.coffee = this.options.coffee;
}
// check if --requirejs option provided or if require is setup
if (typeof this.env.options.requirejs === 'undefined') {
this.option('requirejs');
this.options.requirejs = this.checkIfUsingRequireJS();
this.env.options.requirejs = this.options.requirejs;
}
this.setupSourceRootAndSuffix();
};
util.inherits(Generator, yeoman.generators.NamedBase);
Generator.prototype.addScriptToIndex = function (script) {
try {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'index.html');
backboneUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="scripts/' + script + '.js"></script>'
]
});
} catch (e) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
}
};
/*
* Check whether the App is a RequireJS app or not
*
* @return boolean
*/
Generator.prototype.checkIfUsingRequireJS = function checkIfUsingRequireJS() {
if (typeof this.env.options.requirejs !== 'undefined') {
return this.env.options.requirejs;
}
var ext = this.env.options.coffee ? '.coffee' : '.js';
var filepath = path.join(process.cwd(), 'app/scripts/main' + ext);
try {
this.env.options.requirejs = (/require\.config/).test(this.read(filepath));
return this.env.options.requirejs;
} catch (e) {
return false;
}
};
Generator.prototype.getTemplateFramework = function getTemplateFramework() {
if (!(require('fs').existsSync(path.join(process.cwd(), 'Gruntfile.js')))) {
return 'lodash';
}
var ftest = (/templateFramework: '([^\']*)'/);
var match = ftest.exec(this.read(path.join(process.cwd(), 'Gruntfile.js')));
if (match) {
return match[1];
} else {
return 'lodash';
}
};
Generator.prototype.setupSourceRootAndSuffix = function setupSourceRootAndSuffix() {
var sourceRoot = '/templates';
this.scriptSuffix = '.js';
if (this.env.options.coffee || this.options.coffee) {
sourceRoot = '/templates/coffeescript';
this.scriptSuffix = '.coffee';
}
if (this.env.options.requirejs || this.options.requirejs) {
sourceRoot += '/requirejs';
}
this.sourceRoot(path.join(__dirname, sourceRoot));
};
Generator.prototype.writeTemplate = function writeTemplate(source, destination, data) {
this.setupSourceRootAndSuffix();
var ext = this.scriptSuffix;
this.template(source + ext, destination + ext, data);
};
Generator.prototype.geneateTests = function geneateTests(){
return this.config.get('testFramework') == 'mocha' && !this.config.get('includeRequireJS')
}