This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathscript-base.js
126 lines (111 loc) · 4.25 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
120
121
122
123
124
125
126
/**
* This class is inherited by all sub-generators.
*
* Inspired by similar file of the same name found in generator-angular
*
*/
'use strict';
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
/**
* Shortcuts for use in templates(or other generator js files):
* Examples:
* <%= classedName %>
* <%= dasherizedName %>
*/
this.classedName = this._.classify(this._.dasherize(this.name));
this.dasherizedName = this._.dasherize(this.name);
/**
* Tell a sub generator to output js or .coffee files based on whether
* any .coffee files already exist
*
* Override the auto detection via --js or --coffee
*/
if (typeof this.options.coffee === 'undefined') {
if (this.expandFiles('js/**/*.coffee').length > 0) { // choose cs if any cs files exist in js/
this.options.coffee = true;
}
if (this.options.js) {
this.options.coffee = false;
}
}
this.env.options.coffee = this.options.coffee;
/**
* set path where templates will be looked for when using this.template,
* this.copy or this.read
*/
var sourceRoot = '/templates/javascript';
var scriptSuffix = '.js';
if (this.env.options.coffee) {
sourceRoot = '/templates/coffeescript';
scriptSuffix = '.coffee';
}
this.scriptSuffix = scriptSuffix;
this.sourceRoot(path.join(__dirname, sourceRoot));
/**
* Set the appPath and testPath for all sub generators.
*
* appPath: where generated app files will be output to
* testPath: where generated test files will be output to
*
* TODO: ensure user is in root of their app, error otherwise, match cwd to appName in package.json
*/
this.env.options.appPath = path.join(process.cwd(), 'js');
this.env.options.testPath = path.join(process.cwd(), 'test');
};
util.inherits(Generator, yeoman.generators.NamedBase);
/**
* Similar to built in `this.template('src', 'dest'), but accounts for
* cs/js extensions and pre-sets the template directory to /templates/{extension-type}
*
* Example: `this.appTemplate('router', 'routers');` just like calling -> this.template('templates/javascript/router.js', 'js/routers/your-router.js')
*/
Generator.prototype.appTemplate = function (src, dest) {
// calculate the destination file, without extension
// ex: if dest is 'collections' and this.name is 'myTodos' -> collections/my-todos
var abstractDest = path.join(dest, this.dasherizedName);
yeoman.generators.Base.prototype.template.apply(this, [
src + this.scriptSuffix,
path.join(this.env.options.appPath, abstractDest) + this.scriptSuffix
// -> 'js/collections/my-todos.{js,coffee}'
]);
};
Generator.prototype.testTemplate = function (src, dest) {
var abstractDest = path.join(dest, this.dasherizedName) + '.spec';
yeoman.generators.Base.prototype.template.apply(this, [
src + this.scriptSuffix,
path.join(this.env.options.testPath, abstractDest) + this.scriptSuffix
// -> 'test/collections/my-todos.{js,coffee}'
]);
/**
* console.log a message to the user to add the generated test file to
* test/test-setup-browser until #66 lands
*/
// when module is coffeescript, require module using cs!./test-module-name
var csBang;
if (this.scriptSuffix == '.coffee') { csBang = 'cs!' } else { csBang = '' }
var copyPastable = csBang + './' + abstractDest;
var testModuleMsg = '\n\n' +
chalk.yellow.bold("For browser testing, add the following line:") +
'\n\n' +
chalk.red.bold("'" + copyPastable + "',") +
'\n\n' +
chalk.yellow.bold("to test/test-setup-browser.js") +
'\n\n';
console.log(testModuleMsg);
};
/**
* Shortcut for calling:
* this.appTemplate('collection', 'collections')
* this.testTemplate('spec/collection.spec', 'collections')
*
* Usage: this.generateSourceAndTest('collection', 'spec/collection.spec', 'collections');
*/
Generator.prototype.generateSourceAndTest = function (appTemplate, testTemplate, targetDirectory) {
this.appTemplate(appTemplate, targetDirectory);
this.testTemplate(testTemplate, targetDirectory);
};