This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
197 lines (187 loc) · 6.5 KB
/
index.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
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
'use strict';
var path = require('path');
var Base = require('yeoman-generator').Base;
var chalk = require('chalk');
var dasherize = require('underscore.string/dasherize');
var utils = require('./utils');
var WidgetGenerator = Base.extend({
askFor: function () {
var done = this.async();
console.log(chalk.green('Welcome to the ArcGIS Web AppBuilder widget generator!'));
var prompts = [{
name: 'widgetName',
message: 'Widget Name:',
'default': 'MyWidget',
// test for valid folder name
validate: function(answer) {
var validFolderNameRegExp = /^[^\\/?%*"":|<>\.\s]+$/;
return validFolderNameRegExp.test(answer);
}
}, {
name: 'widgetTitle',
message: 'Widget Title:',
// default to widget name split on caplital letters
// Ex: MyWidget => My Widget
'default': function(answers) {
var title;
if (answers && answers.widgetName && answers.widgetName.match) {
title = answers.widgetName.match(/([A-Z]?[^A-Z]*)/g).slice(0,-1).join(' ');
} else {
title = 'My Widget';
}
return title;
}
}, {
name: 'description',
message: 'Description:',
'default': 'A custom Web AppBuilder widget.'
},
{
name: 'baseClass',
message: 'Base Class:',
// default to dasherized widget name
// Ex: MyWidget => my-widget
'default': function(answers) {
var baseClass;
if (answers && answers.widgetName) {
baseClass = dasherize(answers.widgetName).replace(/^-/,'');
} else {
baseClass = 'my-widget';
}
return baseClass;
}
// TODO: validate not empty string?
},
{
type: 'checkbox',
message: 'Which features would you like to include?',
name: 'features',
choices: [
{
value: 'inPanel',
name: 'Run inside a panel'
},
{
value: 'hasLocale',
name: 'Locale (i18n) file'
},
{
value: 'hasStyle',
name: 'Style (CSS) file'
},
{
value: 'hasConfig',
name: 'Config (JSON) file'
},
{
value: 'hasUIFile',
name: 'Template (HTML) file'
}
],
'default': [ 'inPanel', 'hasLocale', 'hasStyle', 'hasConfig', 'hasUIFile' ]
},
{
when: function (response) {
// only show this step if user answered TRUE to 'hasConfig'
return response.features.indexOf('hasConfig') > -1;
},
type: 'confirm',
message: 'Would you like a settings page?',
name: 'hasSettingPage'
},
{
when: function (response) {
// only show this step if user answered TRUE to 'hasSettingPage'
return response.hasSettingPage;
},
type: 'checkbox',
message: 'Which settings page features would you like to include?',
name: 'settingsFeatures',
choices: [
{
value: 'hasSettingUIFile',
name: 'Settings template (HTML) file'
},
{
value: 'hasSettingLocale',
name: 'Settings locale (i18n) file'
},
{
value: 'hasSettingStyle',
name: 'Settings style (CSS) file'
}
],
'default': [ 'hasSettingUIFile', 'hasSettingLocale', 'hasSettingStyle' ]
}, {
name: 'jsVersion',
type: 'list',
message: 'Which JavaScript syntax version would you like to develop in?',
choices: ['ES5', 'ES2015']
}];
this.prompt(prompts, function (props) {
this.widgetName = props.widgetName;
this.widgetTitle = props.widgetTitle;
this.description = props.description;
// properties that we need to get from the package json, if it exists:
this.author = utils.authorToString(utils.getPackageInfo('author'));
this.license = (utils.getPackageInfo('license') !== false ? utils.getPackageInfo('license') : '');
this.widgetsType = this.config.get('widgetsType');
this.is2d = (this.widgetsType === 'is2d');
this.is3d = (this.widgetsType === 'is3d');
if (this.is3d) {
this.platform = 'HTML3D';
this.wabVersion = '2.0beta';
} else {
this.platform = 'HTML';
this.wabVersion = '1.4';
this.is2d = true;
}
this.baseClass = props.baseClass;
this.inPanel = props.features.indexOf('inPanel') > -1;
this.hasLocale = props.features.indexOf('hasLocale') > -1;
this.hasStyle = props.features.indexOf('hasStyle') > -1;
this.hasConfig = props.features.indexOf('hasConfig') > -1;
this.hasUIFile = props.features.indexOf('hasUIFile') > -1;
this.jsVersion = props.jsVersion;
// settings
this.hasSettingPage = props.hasSettingPage;
this.hasSettingUIFile = this.hasSettingPage ? (props.settingsFeatures.indexOf('hasSettingUIFile') > -1) : false;
this.hasSettingLocale = this.hasSettingPage ? (props.settingsFeatures.indexOf('hasSettingLocale') > -1) : false;
this.hasSettingStyle = this.hasSettingPage ? (props.settingsFeatures.indexOf('hasSettingStyle') > -1) : false;
this.needsManifestProps = (!this.inPanel || !this.hasLocale);
done();
}.bind(this));
},
files: function () {
var basePath = path.join('widgets', this.widgetName);
this.template('_Widget_' + this.jsVersion + '.js', path.join(basePath, 'Widget.js'));
if (this.hasUIFile) {
this.template('_Widget.html', path.join(basePath, 'Widget.html'));
}
if (this.hasConfig) {
this.template('_config.json', path.join(basePath, 'config.json'));
}
if (this.hasStyle) {
this.template('css/_style.css', path.join(basePath, 'css/style.css'));
}
if (this.hasLocale) {
this.template('nls/_strings.js', path.join(basePath, 'nls/strings.js'));
}
this.copy('images/icon.png', path.join(basePath, 'images/icon.png'));
this.template('_manifest.json', path.join(basePath, 'manifest.json'));
// Settings:
if(this.hasSettingPage) {
this.template('setting/_Setting_' + this.jsVersion + '.js', path.join(basePath, 'setting/Setting.js'));
if (this.hasSettingUIFile) {
this.template('setting/_Setting.html', path.join(basePath, 'setting/Setting.html'));
}
if (this.hasSettingLocale) {
this.template('setting/nls/_strings.js', path.join(basePath, 'setting/nls/strings.js'));
}
if (this.hasSettingStyle) {
this.template('setting/css/_style.css', path.join(basePath, 'setting/css/style.css'));
}
}
}
});
module.exports = WidgetGenerator;