@loopback/cli 4.0.0-alpha.9 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.yo-rc.json +1719 -0
- package/{generators/project/templates/LICENSE → LICENSE} +2 -1
- package/README.md +44 -43
- package/bin/cli-main.js +61 -0
- package/generators/app/index.js +109 -15
- package/generators/app/templates/.dockerignore +5 -0
- package/generators/app/templates/Dockerfile +28 -0
- package/generators/app/templates/README.md.ejs +130 -0
- package/generators/app/templates/public/index.html.ejs +88 -0
- package/generators/app/templates/{test → src/__tests__}/README.md +0 -1
- package/generators/app/templates/src/__tests__/acceptance/home-page.acceptance.ts.ejs +31 -0
- package/generators/app/templates/src/__tests__/acceptance/ping.controller.acceptance.ts.ejs +21 -0
- package/generators/app/templates/src/__tests__/acceptance/test-helper.ts.ejs +32 -0
- package/generators/app/templates/src/application.ts.ejs +70 -0
- package/generators/app/templates/src/controllers/README.md +6 -0
- package/generators/app/templates/src/controllers/index.ts.ejs +1 -0
- package/generators/app/templates/src/controllers/ping.controller.ts.ejs +55 -0
- package/generators/app/templates/src/datasources/README.md +3 -0
- package/generators/app/templates/src/index.ts.ejs +39 -0
- package/generators/app/templates/src/migrate.ts.ejs +20 -0
- package/generators/app/templates/src/models/README.md +3 -0
- package/generators/app/templates/src/openapi-spec.ts.ejs +23 -0
- package/generators/app/templates/src/sequence.ts.ejs +3 -0
- package/generators/controller/index.js +240 -23
- package/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs +150 -0
- package/generators/controller/templates/src/controllers/{controller-template.ts → controller-template.ts.ejs} +2 -2
- package/generators/copyright/fs.js +46 -0
- package/generators/copyright/git.js +78 -0
- package/generators/copyright/header.js +306 -0
- package/generators/copyright/index.js +230 -0
- package/generators/copyright/license.js +105 -0
- package/generators/datasource/index.js +341 -0
- package/generators/datasource/templates/datasource.ts.ejs +22 -0
- package/generators/discover/import-discovered-model.js +70 -0
- package/generators/discover/index.js +411 -0
- package/generators/example/downloader.js +16 -0
- package/generators/example/index.js +176 -0
- package/generators/extension/index.js +34 -5
- package/generators/extension/templates/README.md.ejs +32 -0
- package/generators/extension/templates/{test → src/__tests__}/acceptance/README.md +0 -0
- package/generators/extension/templates/{test → src/__tests__}/integration/README.md +0 -0
- package/generators/extension/templates/{test → src/__tests__}/unit/README.md +0 -0
- package/generators/extension/templates/src/component.ts.ejs +22 -0
- package/generators/extension/templates/src/controllers/README.md +3 -2
- package/generators/extension/templates/src/decorators/README.md +10 -4
- package/generators/extension/templates/src/index.ts.ejs +3 -0
- package/generators/extension/templates/src/keys.ts.ejs +11 -0
- package/generators/extension/templates/src/mixins/README.md +77 -21
- package/generators/extension/templates/src/providers/README.md +51 -25
- package/generators/extension/templates/src/repositories/README.md +1 -1
- package/generators/extension/templates/src/types.ts.ejs +15 -0
- package/generators/import-lb3-models/index.js +197 -0
- package/generators/import-lb3-models/lb3app-loader.js +31 -0
- package/generators/import-lb3-models/migrate-model.js +249 -0
- package/generators/import-lb3-models/model-names.js +32 -0
- package/generators/interceptor/index.js +178 -0
- package/generators/interceptor/templates/interceptor-template.ts.ejs +62 -0
- package/generators/model/index.js +536 -0
- package/generators/model/property-definition.js +85 -0
- package/generators/model/templates/model.ts.ejs +49 -0
- package/generators/observer/index.js +132 -0
- package/generators/observer/templates/observer-template.ts.ejs +40 -0
- package/generators/openapi/README.md +211 -0
- package/generators/openapi/index.js +535 -0
- package/generators/openapi/schema-helper.js +447 -0
- package/generators/openapi/spec-helper.js +484 -0
- package/generators/openapi/spec-loader.js +75 -0
- package/generators/openapi/templates/src/controllers/controller-template.ts.ejs +43 -0
- package/generators/openapi/templates/src/datasources/datasource.ts.ejs +42 -0
- package/generators/openapi/templates/src/models/model-template.ts.ejs +71 -0
- package/generators/openapi/templates/src/models/type-template.ts.ejs +13 -0
- package/generators/openapi/templates/src/services/service-proxy-template.ts.ejs +55 -0
- package/generators/openapi/utils.js +322 -0
- package/generators/project/templates/.eslintignore +4 -0
- package/generators/project/templates/.eslintrc.js.ejs +3 -0
- package/generators/project/templates/.mocharc.json +5 -0
- package/generators/project/templates/.prettierignore +0 -2
- package/generators/project/templates/.prettierrc +2 -1
- package/generators/project/templates/.vscode/launch.json +38 -0
- package/generators/project/templates/.vscode/settings.json +32 -0
- package/generators/project/templates/.vscode/tasks.json +29 -0
- package/generators/project/templates/DEVELOPING.md +36 -0
- package/generators/project/templates/_.gitignore +3 -5
- package/generators/project/templates/package.json.ejs +175 -0
- package/generators/project/templates/package.plain.json.ejs +176 -0
- package/generators/project/templates/tsconfig.json.ejs +39 -0
- package/generators/relation/base-relation.generator.js +220 -0
- package/generators/relation/belongs-to-relation.generator.js +196 -0
- package/generators/relation/has-many-relation.generator.js +200 -0
- package/generators/relation/has-many-through-relation.generator.js +331 -0
- package/generators/relation/has-one-relation.generator.js +200 -0
- package/generators/relation/index.js +795 -0
- package/generators/relation/references-many-relation.generator.js +142 -0
- package/generators/relation/templates/controller-relation-template-belongs-to.ts.ejs +38 -0
- package/generators/relation/templates/controller-relation-template-has-many-through.ts.ejs +110 -0
- package/generators/relation/templates/controller-relation-template-has-many.ts.ejs +110 -0
- package/generators/relation/templates/controller-relation-template-has-one.ts.ejs +110 -0
- package/generators/relation/utils.generator.js +260 -0
- package/generators/repository/index.js +576 -0
- package/generators/repository/templates/src/repositories/repository-crud-default-template.ts.ejs +21 -0
- package/generators/repository/templates/src/repositories/repository-kv-template.ts.ejs +19 -0
- package/generators/rest-crud/crud-rest-component.js +63 -0
- package/generators/rest-crud/index.js +423 -0
- package/generators/rest-crud/templates/src/model-endpoints/model.rest-config-template.ts.ejs +11 -0
- package/generators/service/index.js +351 -0
- package/generators/service/templates/local-service-class-template.ts.ejs +10 -0
- package/generators/service/templates/local-service-provider-template.ts.ejs +19 -0
- package/generators/service/templates/remote-service-proxy-template.ts.ejs +21 -0
- package/generators/update/index.js +55 -0
- package/intl/cs/messages.json +204 -0
- package/intl/de/messages.json +204 -0
- package/intl/en/messages.json +204 -0
- package/intl/es/messages.json +204 -0
- package/intl/fr/messages.json +204 -0
- package/intl/it/messages.json +204 -0
- package/intl/ja/messages.json +204 -0
- package/intl/ko/messages.json +204 -0
- package/intl/nl/messages.json +204 -0
- package/intl/pl/messages.json +204 -0
- package/intl/pt/messages.json +204 -0
- package/intl/ru/messages.json +204 -0
- package/intl/tr/messages.json +204 -0
- package/intl/zh-Hans/messages.json +204 -0
- package/intl/zh-Hant/messages.json +204 -0
- package/lib/artifact-generator.js +138 -39
- package/lib/ast-helper.js +214 -0
- package/lib/base-generator.js +509 -0
- package/lib/cli.js +233 -0
- package/lib/connectors.json +894 -0
- package/lib/debug.js +16 -0
- package/lib/globalize.js +12 -0
- package/lib/model-discoverer.js +118 -0
- package/lib/project-generator.js +154 -57
- package/lib/tab-completion.js +127 -0
- package/lib/update-index.js +44 -0
- package/lib/utils.js +689 -20
- package/lib/version-helper.js +299 -0
- package/package.json +183 -39
- package/CHANGELOG.md +0 -86
- package/bin/cli.js +0 -66
- package/generators/app/templates/index.js +0 -14
- package/generators/app/templates/src/application.ts +0 -27
- package/generators/app/templates/src/controllers/ping-controller.ts +0 -25
- package/generators/app/templates/src/index.ts +0 -25
- package/generators/app/templates/test/ping-controller.test.ts +0 -46
- package/generators/extension/templates/index.js +0 -8
- package/generators/extension/templates/src/component.ts +0 -14
- package/generators/extension/templates/src/index.ts +0 -6
- package/generators/project/templates/.npmrc +0 -1
- package/generators/project/templates/.yo-rc.json +0 -1
- package/generators/project/templates/README.md +0 -4
- package/generators/project/templates/index.d.ts +0 -6
- package/generators/project/templates/index.ts +0 -11
- package/generators/project/templates/package.json +0 -79
- package/generators/project/templates/package.plain.json +0 -82
- package/generators/project/templates/test/mocha.opts +0 -1
- package/generators/project/templates/tsconfig.json +0 -29
- package/generators/project/templates/tslint.build.json +0 -17
- package/generators/project/templates/tslint.json +0 -33
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
|
|
2
|
+
// Node module: @loopback/cli
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
const BaseGenerator = require('../../lib/base-generator');
|
|
8
|
+
const {updateFileHeaders} = require('./header');
|
|
9
|
+
const {spdxLicenseList, updateLicense} = require('./license');
|
|
10
|
+
const g = require('../../lib/globalize');
|
|
11
|
+
const _ = require('lodash');
|
|
12
|
+
const chalk = require('chalk');
|
|
13
|
+
const autocomplete = require('inquirer-autocomplete-prompt');
|
|
14
|
+
|
|
15
|
+
module.exports = class CopyrightGenerator extends BaseGenerator {
|
|
16
|
+
// Note: arguments and options should be defined in the constructor.
|
|
17
|
+
constructor(args, opts) {
|
|
18
|
+
super(args, opts);
|
|
19
|
+
this.licenseList = [];
|
|
20
|
+
for (const id in spdxLicenseList) {
|
|
21
|
+
const license = spdxLicenseList[id];
|
|
22
|
+
if (
|
|
23
|
+
['mit', 'apache-2.0', 'isc', 'artistic-2.0', 'custom'].includes(
|
|
24
|
+
id.toLocaleLowerCase(),
|
|
25
|
+
)
|
|
26
|
+
) {
|
|
27
|
+
// Add well-known licenses in front of the list
|
|
28
|
+
this.licenseList.unshift(license);
|
|
29
|
+
} else {
|
|
30
|
+
this.licenseList.push(license);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this.licenseList = this.licenseList.map(lic => ({
|
|
34
|
+
value: lic,
|
|
35
|
+
name: `${lic.id} (${lic.name})`,
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
initializing() {
|
|
40
|
+
// Register `autocomplete` plugin
|
|
41
|
+
this.env.adapter.promptModule.registerPrompt('autocomplete', autocomplete);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_setupGenerator() {
|
|
45
|
+
this.option('owner', {
|
|
46
|
+
type: String,
|
|
47
|
+
required: false,
|
|
48
|
+
description: g.f('Copyright owner'),
|
|
49
|
+
});
|
|
50
|
+
this.option('license', {
|
|
51
|
+
type: String,
|
|
52
|
+
required: false,
|
|
53
|
+
description: g.f('License'),
|
|
54
|
+
});
|
|
55
|
+
this.option('updateLicense', {
|
|
56
|
+
type: Boolean,
|
|
57
|
+
required: false,
|
|
58
|
+
description: g.f('Update license in package.json and LICENSE'),
|
|
59
|
+
});
|
|
60
|
+
this.option('gitOnly', {
|
|
61
|
+
type: Boolean,
|
|
62
|
+
required: false,
|
|
63
|
+
default: true,
|
|
64
|
+
description: g.f('Only update git tracked files'),
|
|
65
|
+
});
|
|
66
|
+
this.option('exclude', {
|
|
67
|
+
type: String,
|
|
68
|
+
required: false,
|
|
69
|
+
default: '',
|
|
70
|
+
description: g.f('Exclude files that match the pattern'),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setOptions() {
|
|
75
|
+
return super.setOptions();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async promptOwnerAndLicense() {
|
|
79
|
+
const pkgFile = this.destinationPath('package.json');
|
|
80
|
+
const pkg = this.fs.readJSON(this.destinationPath('package.json'));
|
|
81
|
+
if (pkg == null) {
|
|
82
|
+
this.exit(`${pkgFile} does not exist.`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.packageJson = pkg;
|
|
86
|
+
let author = _.get(pkg, 'author');
|
|
87
|
+
if (typeof author === 'object') {
|
|
88
|
+
author = author.name;
|
|
89
|
+
}
|
|
90
|
+
const owner =
|
|
91
|
+
this.options.copyrightOwner || _.get(pkg, 'copyright.owner', author);
|
|
92
|
+
let license = this.options.license || _.get(pkg, 'license');
|
|
93
|
+
const licenses = [...this.licenseList];
|
|
94
|
+
if (license != null) {
|
|
95
|
+
// find the matching license by id and move it to the front of the list
|
|
96
|
+
for (let i = 0; i < licenses.length; i++) {
|
|
97
|
+
if (licenses[i].value.id.toLowerCase() === license.toLowerCase()) {
|
|
98
|
+
const lic = licenses.splice(i, 1);
|
|
99
|
+
licenses.unshift(...lic);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let answers = await this.prompt([
|
|
106
|
+
{
|
|
107
|
+
type: 'input',
|
|
108
|
+
name: 'owner',
|
|
109
|
+
message: g.f('Copyright owner:'),
|
|
110
|
+
default: owner,
|
|
111
|
+
when: this.options.owner == null,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
type: 'autocomplete',
|
|
115
|
+
name: 'license',
|
|
116
|
+
// choices: licenseList,
|
|
117
|
+
source: async (_answers, input) => {
|
|
118
|
+
if (input == null) return licenses;
|
|
119
|
+
const matched = licenses.filter(lic => {
|
|
120
|
+
const a = input.toLowerCase();
|
|
121
|
+
return (
|
|
122
|
+
lic.value.id.toLowerCase().startsWith(a) ||
|
|
123
|
+
lic.value.name.toLowerCase().startsWith(a)
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
return matched;
|
|
127
|
+
},
|
|
128
|
+
pageSize: 10,
|
|
129
|
+
message: g.f('License name:'),
|
|
130
|
+
default: license,
|
|
131
|
+
when: this.options.license == null,
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
answers = answers || {};
|
|
135
|
+
const exclude = this.options.exclude || '';
|
|
136
|
+
const excludePatterns = exclude.split(',').filter(p => p !== '');
|
|
137
|
+
excludePatterns.push('**/node_modules/**/*');
|
|
138
|
+
|
|
139
|
+
const copyrightOwner = answers.owner || this.options.owner;
|
|
140
|
+
license = answers.license || this.options.license;
|
|
141
|
+
|
|
142
|
+
let customLicenseLines = [];
|
|
143
|
+
if (
|
|
144
|
+
(license && license.id === 'CUSTOM') ||
|
|
145
|
+
license === 'custom' ||
|
|
146
|
+
license === 'CUSTOM'
|
|
147
|
+
) {
|
|
148
|
+
const templateFile = this.destinationPath('license-header.template');
|
|
149
|
+
if (this.fs.exists(templateFile)) {
|
|
150
|
+
const template = this.fs.read(templateFile);
|
|
151
|
+
customLicenseLines = template.match(/[^\r\n]+/g);
|
|
152
|
+
this.log(template);
|
|
153
|
+
} else {
|
|
154
|
+
this.log(
|
|
155
|
+
g.f(
|
|
156
|
+
'Please provide lines of text for the custom copyright/license headers.',
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
const example = ` Copyright <%= owner %> <%= years %>. All Rights Reserved.
|
|
160
|
+
Node module: <%= name %>',
|
|
161
|
+
This file is licensed under the <%= license %>.
|
|
162
|
+
License text available at <%= url %>`;
|
|
163
|
+
this.log(
|
|
164
|
+
chalk.green(
|
|
165
|
+
`Example (supported variables: owner/years/name):
|
|
166
|
+
${example}`,
|
|
167
|
+
),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
answers = await this.prompt([
|
|
171
|
+
{
|
|
172
|
+
type: 'editor',
|
|
173
|
+
name: 'customLicenseLines',
|
|
174
|
+
message: g.f('Custom license lines:'),
|
|
175
|
+
default: example,
|
|
176
|
+
when: customLicenseLines.length === 0,
|
|
177
|
+
},
|
|
178
|
+
]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
answers = answers || {};
|
|
183
|
+
if (answers.customLicenseLines) {
|
|
184
|
+
customLicenseLines = answers.customLicenseLines;
|
|
185
|
+
this.log(customLicenseLines);
|
|
186
|
+
customLicenseLines = customLicenseLines.match(/[^\r\n]+/g);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
this.headerOptions = {
|
|
190
|
+
copyrightOwner,
|
|
191
|
+
customLicenseLines,
|
|
192
|
+
license,
|
|
193
|
+
log: this.log,
|
|
194
|
+
gitOnly: this.options.gitOnly,
|
|
195
|
+
fs: this.fs,
|
|
196
|
+
excludePatterns,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async updateLicense() {
|
|
201
|
+
if (this.shouldExit()) return;
|
|
202
|
+
const answers = await this.prompt([
|
|
203
|
+
{
|
|
204
|
+
type: 'confirm',
|
|
205
|
+
name: 'updateLicense',
|
|
206
|
+
message: g.f('Do you want to update package.json and LICENSE?'),
|
|
207
|
+
default: false,
|
|
208
|
+
when: this.options.updateLicense == null,
|
|
209
|
+
},
|
|
210
|
+
]);
|
|
211
|
+
const updateLicenseFile =
|
|
212
|
+
(answers && answers.updateLicense) || this.options.updateLicense;
|
|
213
|
+
if (!updateLicenseFile) return;
|
|
214
|
+
this.headerOptions.updateLicense = updateLicenseFile;
|
|
215
|
+
await updateLicense(
|
|
216
|
+
this.destinationRoot(),
|
|
217
|
+
this.packageJson,
|
|
218
|
+
this.headerOptions,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async updateHeaders() {
|
|
223
|
+
if (this.shouldExit()) return;
|
|
224
|
+
await updateFileHeaders(this.destinationRoot(), this.headerOptions);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async end() {
|
|
228
|
+
await super.end();
|
|
229
|
+
}
|
|
230
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
|
|
2
|
+
// Node module: @loopback/cli
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const {FSE} = require('./fs');
|
|
8
|
+
const {getYears} = require('./git');
|
|
9
|
+
const {wrapText} = require('../../lib/utils');
|
|
10
|
+
const spdxLicenses = require('spdx-license-list/full');
|
|
11
|
+
|
|
12
|
+
spdxLicenses.CUSTOM = {
|
|
13
|
+
name: 'Custom License',
|
|
14
|
+
url: '',
|
|
15
|
+
osiApproved: false,
|
|
16
|
+
licenseText: '',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const spdxLicenseList = {};
|
|
20
|
+
for (const id in spdxLicenses) {
|
|
21
|
+
spdxLicenseList[id.toLowerCase()] = {id, ...spdxLicenses[id]};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Render license text
|
|
26
|
+
* @param name - Module name
|
|
27
|
+
* @param owner - Copyright owner
|
|
28
|
+
* @param years - Years of update
|
|
29
|
+
* @param license - License object
|
|
30
|
+
*/
|
|
31
|
+
function renderLicense({name, owner, years, license}) {
|
|
32
|
+
if (typeof license === 'string') {
|
|
33
|
+
license = spdxLicenseList[license.toLowerCase()];
|
|
34
|
+
}
|
|
35
|
+
let text = replaceCopyRight(license.licenseText, {owner, years});
|
|
36
|
+
text = wrapText(text, 80);
|
|
37
|
+
return `Copyright (c) ${owner} ${years}.
|
|
38
|
+
Node module: ${name}
|
|
39
|
+
This project is licensed under the ${license.name}, full text below.
|
|
40
|
+
|
|
41
|
+
--------
|
|
42
|
+
|
|
43
|
+
${text}
|
|
44
|
+
`;
|
|
45
|
+
/*
|
|
46
|
+
Copyright (c) IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
|
47
|
+
Node module: @loopback/boot
|
|
48
|
+
This project is licensed under the MIT License, full text below.
|
|
49
|
+
|
|
50
|
+
--------
|
|
51
|
+
|
|
52
|
+
MIT license
|
|
53
|
+
|
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
55
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
56
|
+
in the Software without restriction, including without limitation the rights
|
|
57
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
58
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
59
|
+
furnished to do so, subject to the following conditions:
|
|
60
|
+
|
|
61
|
+
The above copyright notice and this permission notice shall be included in
|
|
62
|
+
all copies or substantial portions of the Software.
|
|
63
|
+
|
|
64
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
65
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
66
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
67
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
68
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
69
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
70
|
+
THE SOFTWARE.
|
|
71
|
+
*/
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function replaceCopyRight(text, {owner, years}) {
|
|
75
|
+
// Copyright (c) <YEAR> <COPYRIGHT HOLDERS>
|
|
76
|
+
return text.replace(
|
|
77
|
+
/Copyright \(c\) <[^<>]+> <[^<>]+>/gi,
|
|
78
|
+
`Copyright (c) ${owner} ${years}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function updateLicense(projectRoot, pkg, options) {
|
|
83
|
+
if (!options.updateLicense) return;
|
|
84
|
+
const fs = options.fs || FSE;
|
|
85
|
+
let licenseId = options.license;
|
|
86
|
+
if (typeof licenseId === 'object') {
|
|
87
|
+
licenseId = licenseId.id;
|
|
88
|
+
}
|
|
89
|
+
pkg.license = licenseId;
|
|
90
|
+
pkg['copyright.owner'] = options.copyrightOwner;
|
|
91
|
+
await fs.writeJSON(path.join(projectRoot, 'package.json'), pkg);
|
|
92
|
+
await fs.write(
|
|
93
|
+
path.join(projectRoot, 'LICENSE'),
|
|
94
|
+
renderLicense({
|
|
95
|
+
name: pkg.name,
|
|
96
|
+
owner: options.copyrightOwner,
|
|
97
|
+
license: options.license,
|
|
98
|
+
years: await getYears(projectRoot),
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
exports.renderLicense = renderLicense;
|
|
104
|
+
exports.spdxLicenseList = spdxLicenseList;
|
|
105
|
+
exports.updateLicense = updateLicense;
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
|
+
// Node module: @loopback/cli
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
// no translation: Datasource
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
const ArtifactGenerator = require('../../lib/artifact-generator');
|
|
10
|
+
const debug = require('../../lib/debug')('datasource-generator');
|
|
11
|
+
const chalk = require('chalk');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const utils = require('../../lib/utils');
|
|
14
|
+
const connectors = require('../../lib/connectors.json');
|
|
15
|
+
const g = require('../../lib/globalize');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* DataSource Generator -- CLI
|
|
19
|
+
*
|
|
20
|
+
* Prompts for a name, connector and connector options. Creates json file
|
|
21
|
+
* for the DataSource as well as a Class for a user to modify. Also installs the
|
|
22
|
+
* appropriate connector from npm.
|
|
23
|
+
*/
|
|
24
|
+
module.exports = class DataSourceGenerator extends ArtifactGenerator {
|
|
25
|
+
constructor(args, opts) {
|
|
26
|
+
super(args, opts);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_setupGenerator() {
|
|
30
|
+
this.artifactInfo = {
|
|
31
|
+
type: 'datasource',
|
|
32
|
+
rootDir: 'src',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Datasources are stored in the datasources directory
|
|
36
|
+
this.artifactInfo.outDir = path.resolve(
|
|
37
|
+
this.artifactInfo.rootDir,
|
|
38
|
+
'datasources',
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const connectorChoices = [];
|
|
42
|
+
/**
|
|
43
|
+
* Creating a list of connectors -- and marking them as either supported by
|
|
44
|
+
* StrongLoop or community.
|
|
45
|
+
*/
|
|
46
|
+
Object.values(connectors).forEach(connector => {
|
|
47
|
+
const support = connector.supportedByStrongLoop
|
|
48
|
+
? '(supported by StrongLoop)'
|
|
49
|
+
: '(provided by community)';
|
|
50
|
+
connectorChoices.push({
|
|
51
|
+
name: `${connector.description} ${chalk.gray(support)}`,
|
|
52
|
+
value: connector.name,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.connectorChoices = connectorChoices;
|
|
57
|
+
// Add `other` so users can add a connector that isn't part of the list
|
|
58
|
+
// Though it can be added by creating a PR and adding it to
|
|
59
|
+
// connectors.json
|
|
60
|
+
this.connectorChoices.push('other');
|
|
61
|
+
|
|
62
|
+
return super._setupGenerator();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setOptions() {
|
|
66
|
+
return super.setOptions();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Ensure CLI is being run in a LoopBack 4 project.
|
|
71
|
+
*/
|
|
72
|
+
checkLoopBackProject() {
|
|
73
|
+
if (this.shouldExit()) return;
|
|
74
|
+
return super.checkLoopBackProject();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Ask for DataSource Name -- Must be unique
|
|
79
|
+
*/
|
|
80
|
+
promptArtifactName() {
|
|
81
|
+
debug('Prompting for artifact name');
|
|
82
|
+
if (this.shouldExit()) return false;
|
|
83
|
+
|
|
84
|
+
if (this.artifactInfo.name) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const prompts = [
|
|
89
|
+
{
|
|
90
|
+
type: 'input',
|
|
91
|
+
name: 'name',
|
|
92
|
+
// capitalization
|
|
93
|
+
message: g.f('%s name:', utils.toClassName(this.artifactInfo.type)),
|
|
94
|
+
when: this.artifactInfo.name === undefined,
|
|
95
|
+
validate: utils.validateClassName,
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
return this.prompt(prompts).then(props => {
|
|
100
|
+
Object.assign(this.artifactInfo, props);
|
|
101
|
+
return props;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Ask the user to select the connector for the DataSource
|
|
107
|
+
*/
|
|
108
|
+
promptConnector() {
|
|
109
|
+
debug('prompting for datasource connector');
|
|
110
|
+
if (this.shouldExit()) return;
|
|
111
|
+
const prompts = [
|
|
112
|
+
{
|
|
113
|
+
name: 'connector',
|
|
114
|
+
message: g.f(
|
|
115
|
+
'Select the connector for %s: ',
|
|
116
|
+
chalk.yellow(this.artifactInfo.name),
|
|
117
|
+
),
|
|
118
|
+
type: 'list',
|
|
119
|
+
default: 'memory',
|
|
120
|
+
choices: this.connectorChoices,
|
|
121
|
+
when: this.artifactInfo.connector === undefined,
|
|
122
|
+
},
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
return this.prompt(prompts).then(props => {
|
|
126
|
+
Object.assign(this.artifactInfo, props);
|
|
127
|
+
return props;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* If the user selected `other` for connector -- ask the user to provide
|
|
133
|
+
* `npm` module name for the connector.
|
|
134
|
+
*/
|
|
135
|
+
promptCustomConnectorInfo() {
|
|
136
|
+
if (this.shouldExit()) return;
|
|
137
|
+
if (this.artifactInfo.connector !== 'other') {
|
|
138
|
+
debug('custom connector option was not selected');
|
|
139
|
+
return;
|
|
140
|
+
} else {
|
|
141
|
+
debug('prompting for custom connector');
|
|
142
|
+
const prompts = [
|
|
143
|
+
{
|
|
144
|
+
name: 'customConnector',
|
|
145
|
+
message: g.f("Enter the connector's package name:"),
|
|
146
|
+
validate: utils.validate,
|
|
147
|
+
},
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
return this.prompt(prompts).then(props => {
|
|
151
|
+
this.artifactInfo.connector = props.customConnector;
|
|
152
|
+
return props;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Prompt the user for connector specific settings -- only applies to
|
|
159
|
+
* connectors in the connectors.json list
|
|
160
|
+
*/
|
|
161
|
+
promptConnectorConfig() {
|
|
162
|
+
debug('prompting for connector config');
|
|
163
|
+
if (this.shouldExit()) return;
|
|
164
|
+
// Check to make sure connector is from connectors list (not custom)
|
|
165
|
+
const settings =
|
|
166
|
+
(connectors[this.artifactInfo.connector] &&
|
|
167
|
+
connectors[this.artifactInfo.connector]['settings']) ||
|
|
168
|
+
{};
|
|
169
|
+
const prompts = [];
|
|
170
|
+
|
|
171
|
+
// Create list of questions to prompt the user
|
|
172
|
+
Object.entries(settings).forEach(([key, setting]) => {
|
|
173
|
+
// Set defaults and merge with `setting` to override properties
|
|
174
|
+
const question = Object.assign(
|
|
175
|
+
{},
|
|
176
|
+
{name: key, message: key, suffix: ':', default: null},
|
|
177
|
+
setting,
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Allowed Types: string, number, password, object, array, boolean
|
|
182
|
+
* Must be converted to inquirer types -- input, confirm, password
|
|
183
|
+
*/
|
|
184
|
+
switch ((setting.type || '').toLowerCase()) {
|
|
185
|
+
case 'string':
|
|
186
|
+
case 'number':
|
|
187
|
+
question.type = 'input';
|
|
188
|
+
break;
|
|
189
|
+
case 'object':
|
|
190
|
+
case 'array':
|
|
191
|
+
question.type = 'input';
|
|
192
|
+
question.validate = utils.validateStringObject(setting.type);
|
|
193
|
+
break;
|
|
194
|
+
case 'boolean':
|
|
195
|
+
question.type = 'confirm';
|
|
196
|
+
break;
|
|
197
|
+
case 'password':
|
|
198
|
+
break;
|
|
199
|
+
default:
|
|
200
|
+
console.warn(
|
|
201
|
+
g.f(
|
|
202
|
+
'Using default input of type input for setting %s as %s is not supported',
|
|
203
|
+
key,
|
|
204
|
+
setting.type || undefined,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
// Default to input type
|
|
208
|
+
question.type = 'input';
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
prompts.push(question);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
debug(`connector setting questions - ${JSON.stringify(prompts)}`);
|
|
215
|
+
|
|
216
|
+
// If no prompts, we need to return instead of attempting to ask prompts
|
|
217
|
+
if (!prompts.length) return;
|
|
218
|
+
|
|
219
|
+
debug('prompting the user - length > 0 for questions');
|
|
220
|
+
// Ask user for prompts
|
|
221
|
+
return this.prompt(prompts).then(props => {
|
|
222
|
+
// Convert user inputs to correct types
|
|
223
|
+
Object.entries(settings).forEach(([key, setting]) => {
|
|
224
|
+
switch ((setting.type || '').toLowerCase()) {
|
|
225
|
+
case 'number':
|
|
226
|
+
props[key] = Number(props[key]);
|
|
227
|
+
break;
|
|
228
|
+
case 'array':
|
|
229
|
+
case 'object':
|
|
230
|
+
if (props[key] == null || props[key] === '') {
|
|
231
|
+
delete props[key];
|
|
232
|
+
} else {
|
|
233
|
+
props[key] = JSON.parse(props[key]);
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
this.artifactInfo = Object.assign(this.artifactInfo, {settings: props});
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Scaffold DataSource related files
|
|
244
|
+
* super.scaffold() doesn't provide a way to rename files -- don't call it
|
|
245
|
+
*/
|
|
246
|
+
scaffold() {
|
|
247
|
+
// Exit if needed
|
|
248
|
+
if (this.shouldExit()) return false;
|
|
249
|
+
|
|
250
|
+
// Setting up data for templates
|
|
251
|
+
this.artifactInfo.className = utils.toClassName(this.artifactInfo.name);
|
|
252
|
+
this.artifactInfo.fileName = utils.toFileName(this.artifactInfo.name);
|
|
253
|
+
// prettier-ignore
|
|
254
|
+
this.artifactInfo.outFile = `${this.artifactInfo.fileName}.datasource.ts`;
|
|
255
|
+
|
|
256
|
+
// Resolved Output Paths.
|
|
257
|
+
const tsPath = this.destinationPath(
|
|
258
|
+
this.artifactInfo.outDir,
|
|
259
|
+
this.artifactInfo.outFile,
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// template path
|
|
263
|
+
const classTemplatePath = this.templatePath('datasource.ts.ejs');
|
|
264
|
+
|
|
265
|
+
// Debug Info
|
|
266
|
+
debug(`this.artifactInfo.name => ${this.artifactInfo.name}`);
|
|
267
|
+
debug(`this.artifactInfo.className => ${this.artifactInfo.className}`);
|
|
268
|
+
debug(`this.artifactInfo.fileName => ${this.artifactInfo.fileName}`);
|
|
269
|
+
debug(`this.artifactInfo.outFile => ${this.artifactInfo.outFile}`);
|
|
270
|
+
debug(`tsPath => ${tsPath}`);
|
|
271
|
+
|
|
272
|
+
// Data to save to DataSource JSON file
|
|
273
|
+
const dsConfig = Object.assign(
|
|
274
|
+
{name: this.artifactInfo.name, connector: this.artifactInfo.connector},
|
|
275
|
+
this.artifactInfo.settings,
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// From LB3
|
|
279
|
+
if (dsConfig.connector === 'ibm-object-storage') {
|
|
280
|
+
dsConfig.connector = 'loopback-component-storage';
|
|
281
|
+
dsConfig.provider = 'openstack';
|
|
282
|
+
dsConfig.useServiceCatalog = true;
|
|
283
|
+
dsConfig.useInternal = false;
|
|
284
|
+
dsConfig.keystoneAuthVersion = 'v3';
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
this.artifactInfo.dsConfigString = utils.stringifyObject(dsConfig, {
|
|
288
|
+
// Prevent inlining the config into a single line, e.g.
|
|
289
|
+
// const config = {name: 'db', connector: 'memory'};
|
|
290
|
+
inlineCharacterLimit: 0,
|
|
291
|
+
});
|
|
292
|
+
debug(`datasource configuration code: ${this.artifactInfo.dsConfigString}`);
|
|
293
|
+
|
|
294
|
+
this.copyTemplatedFiles(classTemplatePath, tsPath, this.artifactInfo);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
install() {
|
|
298
|
+
if (this.shouldExit()) return false;
|
|
299
|
+
debug('install npm dependencies');
|
|
300
|
+
const pkgJson = this.packageJson || {};
|
|
301
|
+
const deps = pkgJson.dependencies || {};
|
|
302
|
+
const pkgs = [];
|
|
303
|
+
|
|
304
|
+
// Connector package.
|
|
305
|
+
const connector = connectors[this.artifactInfo.connector];
|
|
306
|
+
if (connector && connector.package) {
|
|
307
|
+
if (!deps[connector.package.name]) {
|
|
308
|
+
pkgs.push(
|
|
309
|
+
connector.package.name +
|
|
310
|
+
`${
|
|
311
|
+
connector.package.version ? '@' + connector.package.version : ''
|
|
312
|
+
}`,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
debug(`npmModule - ${pkgs[0]}`);
|
|
317
|
+
} else {
|
|
318
|
+
const connectorName = this.artifactInfo.connector;
|
|
319
|
+
// Other connectors that are not listed in `connectors.json`.
|
|
320
|
+
// No install is needed for those in connectors.json but without a
|
|
321
|
+
// package name as they are built-in connectors
|
|
322
|
+
if (!deps[connectorName] && !connector) pkgs.push(connectorName);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (!deps['@loopback/repository']) {
|
|
326
|
+
pkgs.push('@loopback/repository');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (pkgs.length === 0) return;
|
|
330
|
+
|
|
331
|
+
this.pkgManagerInstall(pkgs, {
|
|
332
|
+
npm: {
|
|
333
|
+
save: true,
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async end() {
|
|
339
|
+
await super.end();
|
|
340
|
+
}
|
|
341
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
|
|
2
|
+
import {juggler} from '@loopback/repository';
|
|
3
|
+
|
|
4
|
+
const config = <%- dsConfigString %>;
|
|
5
|
+
|
|
6
|
+
// Observe application's life cycle to disconnect the datasource when
|
|
7
|
+
// application is stopped. This allows the application to be shut down
|
|
8
|
+
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
|
|
9
|
+
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
|
|
10
|
+
@lifeCycleObserver('datasource')
|
|
11
|
+
export class <%= className %>DataSource extends juggler.DataSource
|
|
12
|
+
implements LifeCycleObserver {
|
|
13
|
+
static dataSourceName = '<%= name %>';
|
|
14
|
+
static readonly defaultConfig = config;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
@inject('datasources.config.<%= name %>', {optional: true})
|
|
18
|
+
dsConfig: object = config,
|
|
19
|
+
) {
|
|
20
|
+
super(dsConfig);
|
|
21
|
+
}
|
|
22
|
+
}
|