This repository has been archived by the owner on Mar 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
script-base.js
89 lines (70 loc) · 2.51 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
'use strict';
var path = require('path');
var util = require('util');
var yeoman = require('yeoman-generator');
var backboneUtils = require('./util.js');
var ScriptBase = yeoman.generators.NamedBase.extend({
constructor: function (name) {
yeoman.generators.NamedBase.apply(this, arguments);
this.appname = this.config.get('appName') || path.basename(process.cwd());
this.env.options.appPath = this.config.get('appPath') || 'app';
this.options.coffee = this.config.get('coffee') || false;
if (typeof this.env.options.coffee === 'undefined') {
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.config.get('includeRequireJS') || false;
this.env.options.requirejs = this.options.requirejs;
}
},
_addScriptToIndex: function (script) {
try {
var appPath = this.env.options.appPath;
var fullPath = this.destinationPath(path.join(appPath, 'index.html'));
backboneUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="scripts/' + script + '.js"></script>'
]
});
} catch (e) {
this.log('\n Unable to find ' + fullPath + '. Reference to ' + script + '.js ' + 'not added.\n');
}
},
_setupSourceRootAndSuffix: function () {
var sourceRoot = '/generators/templates';
this.scriptSuffix = '.js';
if (this.env.options.coffee || this.options.coffee) {
sourceRoot = '/generators/templates/coffeescript';
this.scriptSuffix = '.coffee';
}
if (this.env.options.requirejs || this.options.requirejs) {
sourceRoot += '/requirejs';
}
this.sourceRoot(path.join(__dirname, sourceRoot));
},
_writeTemplate: function (source, destination, data) {
this._setupSourceRootAndSuffix();
var ext = this.scriptSuffix;
this.fs.copyTpl(
this.templatePath(source + ext),
this.destinationPath(destination + ext),
data
);
},
_canGenerateTests: function () {
return this.config.get('testFramework') === 'mocha' && !this.config.get('includeRequireJS');
},
_generateTest: function (type) {
if (this._canGenerateTests()) {
this.composeWith('backbone-mocha:' + type, { arguments: [this.name] }, {
coffee: this.config.get('coffee'),
ui: this.config.get('ui')
});
}
}
});
module.exports = ScriptBase;