-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.js
123 lines (109 loc) · 3.76 KB
/
messages.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
var _ = require('underscore');
var colors = require('colors');
// Set underscore's template system to use handlebars syntax
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
// Configurations for colors module
colors.setTheme({
warning: 'cyan',
command: 'magenta'
});
/**
* Object containing context for template compilation
* and functions for text stylingContext.
* @type {Object}
*/
var stylingContext = {
warningSign: ('<'.cyan + '!'.red + '>'.cyan),
prompt: '$'.cyan,
locally: 'locally'.yellow,
globally: 'globally'.yellow
};
/**
* Creates a template function for a warning message
* @param {String} rawString template to be compiled
* @return {Function} wrapped underscore template function
*/
function warningTemplate(rawString) {
var template = _.template(rawString);
return function (context) {
return template(context).warning;
}
}
/**
* Creates a template function for a command message
* @param {String} rawString template to be compiled
* @return {Function} wrapped underscore template function
*/
function commandTemplate(rawString) {
var template = _.template(rawString);
return function (context) {
return template(context).command;
}
}
/**
* Template function for the warning message that is displayed post-build when
* the user does not have a global instillation of gulp.
*
* Any indentation added to this string will also show up as output.
* @type {Function}
*/
var gulpInstallWarningTemplate = warningTemplate(
'\n{{warningSign}} We have installed gulp {{locally}} to your project. \
To use the gulp tasks, you will need to use the \
npm run cli with the following command:\n\
{{localGulpCommand}}\n\n\
To remove gulp {{locally}} and setup gulp {{globally}}, run the following command:\n\
{{setupGlobalGulpCommand}}'
);
/**
* Template function for the command that the user can run to execute gulp tasks.
* @type {Function}
*/
var localGulpCmdTemplate = commandTemplate('{{prompt}} npm run gulp <task>');
/**
* Template function for the command that the user can run to uninstall a local gulp installation
* and install gulp globally.
* @type {Function}
*/
var setupGlobalGulpCmdTemplate = commandTemplate('{{prompt}} npm uninstall gulp && npm install -g gulp');
/**
* Template function for the message output from logWarningMessage.
* @type {Function}
*/
var warningMessageTemplate = warningTemplate('{{warningSign}} {{message}}');
/**
* Creates a new object containing both the stylingContext
* and additional elements specific to the template
* @param {Object} templateContext object containing elements specific to given template
* @return {Object} the full context object to be used to build the styled template
*/
function generateContext(templateContext) {
return _.extend({}, stylingContext, templateContext);
}
/**
* Compiles all the templates for the install warning that is triggered
* when no global installation of gulp is detected
* @return {String} the styled message for use in yeoman installation
*/
function compileGulpInstallWarningMessage() {
var context = generateContext({
localGulpCommand: localGulpCmdTemplate(stylingContext),
setupGlobalGulpCommand: setupGlobalGulpCmdTemplate(stylingContext)
});
return gulpInstallWarningTemplate(context);
}
/**
* Compiles the warning message template using the given message
* @param {String} message to be styled and injected into the proper template
* @return {String} the styled warning message
*/
function compileWarningMessage(message) {
var context = generateContext({message: message});
return warningMessageTemplate(context);
}
module.exports = {
gulpInstallWarningMessage: compileGulpInstallWarningMessage(),
compileWarningMessage: compileWarningMessage
};