-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
330 lines (292 loc) · 9.71 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const ArtifactGenerator = require('../../lib/artifact-generator');
const debug = require('../../lib/debug')('datasource-generator');
const chalk = require('chalk');
const path = require('path');
const utils = require('../../lib/utils');
const connectors = require('./connectors.json');
/**
* DataSource Generator -- CLI
*
* Prompts for a name, connector and connector options. Creates json file
* for the DataSource as well as a Class for a user to modify. Also installs the
* appropriate connector from npm.
*/
module.exports = class DataSourceGenerator extends ArtifactGenerator {
constructor(args, opts) {
super(args, opts);
}
_setupGenerator() {
this.artifactInfo = {
type: 'datasource',
rootDir: 'src',
};
// Datasources are stored in the datasources directory
this.artifactInfo.outDir = path.resolve(
this.artifactInfo.rootDir,
'datasources',
);
const connectorChoices = [];
/**
* Creating a list of connectors -- and marking them as either supported by
* StrongLoop or community.
*/
Object.values(connectors).forEach(connector => {
const support = connector.supportedByStrongLoop
? '(supported by StrongLoop)'
: '(provided by community)';
connectorChoices.push({
name: `${connector.description} ${chalk.gray(support)}`,
value: connector.name,
});
});
this.connectorChoices = connectorChoices;
// Add `other` so users can add a connector that isn't part of the list
// Though it can be added by creating a PR and adding it to
// connectors.json
this.connectorChoices.push('other');
return super._setupGenerator();
}
setOptions() {
return super.setOptions();
}
/**
* Ensure CLI is being run in a LoopBack 4 project.
*/
checkLoopBackProject() {
if (this.shouldExit()) return;
return super.checkLoopBackProject();
}
/**
* Ask for DataSource Name -- Must be unique
*/
promptArtifactName() {
debug('Prompting for artifact name');
if (this.shouldExit()) return false;
const prompts = [
{
type: 'input',
name: 'name',
// capitalization
message: utils.toClassName(this.artifactInfo.type) + ' name:',
when: this.artifactInfo.name === undefined,
validate: utils.validateClassName,
},
];
return this.prompt(prompts).then(props => {
Object.assign(this.artifactInfo, props);
return props;
});
}
/**
* Ask the user to select the connector for the DataSource
*/
promptConnector() {
debug('prompting for datasource connector');
if (this.shouldExit()) return;
const prompts = [
{
name: 'connector',
message: `Select the connector for ${chalk.yellow(
this.artifactInfo.name,
)}:`,
type: 'list',
default: 'memory',
choices: this.connectorChoices,
when: this.artifactInfo.connector === undefined,
},
];
return this.prompt(prompts).then(props => {
Object.assign(this.artifactInfo, props);
return props;
});
}
/**
* If the user selected `other` for connector -- ask the user to provide
* `npm` module name for the connector.
*/
promptCustomConnectorInfo() {
if (this.shouldExit()) return;
if (this.artifactInfo.connector !== 'other') {
debug('custom connector option was not selected');
return;
} else {
debug('prompting for custom connector');
const prompts = [
{
name: 'customConnector',
message: "Enter the connector's package name:",
validate: utils.validate,
},
];
return this.prompt(prompts).then(props => {
this.artifactInfo.connector = props.customConnector;
return props;
});
}
}
/**
* Prompt the user for connector specific settings -- only applies to
* connectors in the connectors.json list
*/
promptConnectorConfig() {
debug('prompting for connector config');
if (this.shouldExit()) return;
// Check to make sure connector is from connectors list (not custom)
const settings =
(connectors[this.artifactInfo.connector] &&
connectors[this.artifactInfo.connector]['settings']) ||
{};
const prompts = [];
// Create list of questions to prompt the user
Object.entries(settings).forEach(([key, setting]) => {
// Set defaults and merge with `setting` to override properties
const question = Object.assign(
{},
{name: key, message: key, suffix: ':'},
setting,
);
/**
* Allowed Types: string, number, password, object, array, boolean
* Must be converted to inquirer types -- input, confirm, password
*/
switch ((setting.type || '').toLowerCase()) {
case 'string':
case 'number':
question.type = 'input';
break;
case 'object':
case 'array':
question.type = 'input';
question.validate = utils.validateStringObject(setting.type);
break;
case 'boolean':
question.type = 'confirm';
break;
case 'password':
break;
default:
console.warn(
`Using default input of type input for setting ${key} as ${setting.type ||
undefined} is not supported`,
);
// Default to input type
question.type = 'input';
}
prompts.push(question);
});
debug(`connector setting questions - ${JSON.stringify(prompts)}`);
// If no prompts, we need to return instead of attempting to ask prompts
if (!prompts.length) return;
debug('prompting the user - length > 0 for questions');
// Ask user for prompts
return this.prompt(prompts).then(props => {
// Convert user inputs to correct types
Object.entries(settings).forEach(([key, setting]) => {
switch ((setting.type || '').toLowerCase()) {
case 'number':
props[key] = Number(props[key]);
break;
case 'array':
case 'object':
if (props[key] == null || props[key] === '') {
delete props[key];
} else {
props[key] = JSON.parse(props[key]);
}
break;
}
});
this.artifactInfo = Object.assign(this.artifactInfo, {settings: props});
});
}
/**
* Scaffold DataSource related files
* super.scaffold() doesn't provide a way to rename files -- don't call it
*/
scaffold() {
// Exit if needed
if (this.shouldExit()) return false;
// Setting up data for templates
this.artifactInfo.className = utils.toClassName(this.artifactInfo.name);
this.artifactInfo.fileName = utils.kebabCase(this.artifactInfo.name);
// prettier-ignore
this.artifactInfo.jsonFileName = `${this.artifactInfo.fileName}.datasource.json`;
// prettier-ignore
this.artifactInfo.outFile = `${this.artifactInfo.fileName}.datasource.ts`;
// Resolved Output Paths.
const jsonPath = this.destinationPath(
this.artifactInfo.outDir,
this.artifactInfo.jsonFileName,
);
const tsPath = this.destinationPath(
this.artifactInfo.outDir,
this.artifactInfo.outFile,
);
// template path
const classTemplatePath = this.templatePath('datasource.ts.ejs');
// Debug Info
debug(`this.artifactInfo.name => ${this.artifactInfo.name}`);
debug(`this.artifactInfo.className => ${this.artifactInfo.className}`);
debug(`this.artifactInfo.fileName => ${this.artifactInfo.fileName}`);
// prettier-ignore
debug(`this.artifactInfo.jsonFileName => ${this.artifactInfo.jsonFileName}`);
debug(`this.artifactInfo.outFile => ${this.artifactInfo.outFile}`);
debug(`jsonPath => ${jsonPath}`);
debug(`tsPath => ${tsPath}`);
// Data to save to DataSource JSON file
const ds = Object.assign(
{name: this.artifactInfo.name, connector: this.artifactInfo.connector},
this.artifactInfo.settings,
);
// From LB3
if (ds.connector === 'ibm-object-storage') {
ds.connector = 'loopback-component-storage';
ds.provider = 'openstack';
ds.useServiceCatalog = true;
ds.useInternal = false;
ds.keystoneAuthVersion = 'v3';
}
debug(`datasource information going to file: ${JSON.stringify(ds)}`);
// Copy Templates
this.fs.writeJSON(jsonPath, ds);
this.copyTemplatedFiles(classTemplatePath, tsPath, this.artifactInfo);
}
install() {
if (this.shouldExit()) return false;
debug('install npm dependencies');
const pkgJson = this.packageJson || {};
const deps = pkgJson.dependencies || {};
const pkgs = [];
// Connector package.
const connector = connectors[this.artifactInfo.connector];
if (connector && connector.package) {
if (!deps[connector.package.name]) {
pkgs.push(
connector.package.name +
`${
connector.package.version ? '@' + connector.package.version : ''
}`,
);
}
debug(`npmModule - ${pkgs[0]}`);
} else {
const connectorName = this.artifactInfo.connector;
// Other connectors that are not listed in `connectors.json`.
// No install is needed for those in connectors.json but without a
// package name as they are built-in connectors
if (!deps[connectorName] && !connector) pkgs.push(connectorName);
}
if (!deps['@loopback/repository']) {
pkgs.push('@loopback/repository');
}
if (pkgs.length) this.npmInstall(pkgs, {save: true});
}
async end() {
await super.end();
}
};