@loopback/cli 4.0.0-alpha.7 → 4.0.0
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 +1697 -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 +279 -0
- package/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs +150 -0
- package/generators/controller/templates/src/controllers/controller-template.ts.ejs +8 -0
- 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 +349 -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 +42 -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 +401 -0
- package/generators/rest-crud/templates/src/model-endpoints/model.rest-config-template.ts.ejs +10 -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 +189 -0
- 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 +746 -22
- package/lib/version-helper.js +299 -0
- package/package.json +183 -32
- package/CHANGELOG.md +0 -64
- package/bin/cli.js +0 -65
- 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,401 @@
|
|
|
1
|
+
// Copyright IBM Corp. 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 _ = require('lodash');
|
|
8
|
+
const ArtifactGenerator = require('../../lib/artifact-generator');
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const debug = require('../../lib/debug')('rest-crud-generator');
|
|
11
|
+
const inspect = require('util').inspect;
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const chalk = require('chalk');
|
|
14
|
+
const utils = require('../../lib/utils');
|
|
15
|
+
const g = require('../../lib/globalize');
|
|
16
|
+
const cliPkg = require('../../lib/version-helper').cliPkg;
|
|
17
|
+
|
|
18
|
+
const {updateApplicationTs} = require('./crud-rest-component');
|
|
19
|
+
|
|
20
|
+
const VALID_CONNECTORS_FOR_REPOSITORY = ['PersistedModel'];
|
|
21
|
+
|
|
22
|
+
const REST_CONFIG_TEMPLATE = 'model.rest-config-template.ts.ejs';
|
|
23
|
+
|
|
24
|
+
const PROMPT_MESSAGE_MODEL = g.f(
|
|
25
|
+
'Select the model(s) you want to generate a CRUD REST endpoint',
|
|
26
|
+
);
|
|
27
|
+
const PROMPT_MESSAGE_DATA_SOURCE = g.f('Please select the datasource');
|
|
28
|
+
const PROMPT_MESSAGE_BASE_PATH = g.f('Please specify the base path');
|
|
29
|
+
|
|
30
|
+
const ERROR_NO_DATA_SOURCES_FOUND = g.f('No datasources found at');
|
|
31
|
+
const ERROR_NO_MODELS_FOUND = g.f('No models found at');
|
|
32
|
+
const ERROR_NO_MODEL_SELECTED = g.f('You did not select a valid model');
|
|
33
|
+
|
|
34
|
+
module.exports = class RestCrudGenerator extends ArtifactGenerator {
|
|
35
|
+
// Note: arguments and options should be defined in the constructor.
|
|
36
|
+
constructor(args, opts) {
|
|
37
|
+
super(args, opts);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* helper method to inspect and validate a datasource type
|
|
42
|
+
*/
|
|
43
|
+
async _inferDataSourceNames() {
|
|
44
|
+
if (!this.artifactInfo.dataSourceClass) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.artifactInfo.dataSourceName = utils.getDataSourceName(
|
|
49
|
+
this.artifactInfo.datasourcesDir,
|
|
50
|
+
this.artifactInfo.dataSourceClass,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
this.artifactInfo.dataSourceClassName =
|
|
54
|
+
utils.toClassName(this.artifactInfo.dataSourceName) + 'DataSource';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_setupGenerator() {
|
|
58
|
+
this.artifactInfo = {
|
|
59
|
+
type: 'rest-config',
|
|
60
|
+
rootDir: utils.sourceRootDir,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
this.artifactInfo.outDir = path.resolve(
|
|
64
|
+
this.artifactInfo.rootDir,
|
|
65
|
+
utils.modelEndpointsDir,
|
|
66
|
+
);
|
|
67
|
+
this.artifactInfo.datasourcesDir = path.resolve(
|
|
68
|
+
this.artifactInfo.rootDir,
|
|
69
|
+
utils.datasourcesDir,
|
|
70
|
+
);
|
|
71
|
+
this.artifactInfo.modelDir = path.resolve(
|
|
72
|
+
this.artifactInfo.rootDir,
|
|
73
|
+
utils.modelsDir,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
this.artifactInfo.defaultTemplate = REST_CONFIG_TEMPLATE;
|
|
77
|
+
|
|
78
|
+
this.option('model', {
|
|
79
|
+
type: String,
|
|
80
|
+
required: false,
|
|
81
|
+
description: g.f('A valid model name'),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
this.option('datasource', {
|
|
85
|
+
type: String,
|
|
86
|
+
required: false,
|
|
87
|
+
description: g.f('A valid datasource name'),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
this.option('basePath', {
|
|
91
|
+
type: String,
|
|
92
|
+
required: false,
|
|
93
|
+
description: g.f('A valid base path'),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return super._setupGenerator();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
setOptions() {
|
|
100
|
+
return super.setOptions();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
checkLoopBackProject() {
|
|
104
|
+
if (this.shouldExit()) return;
|
|
105
|
+
return super.checkLoopBackProject();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async checkPaths() {
|
|
109
|
+
if (this.shouldExit()) return;
|
|
110
|
+
// check for datasources
|
|
111
|
+
if (!fs.existsSync(this.artifactInfo.datasourcesDir)) {
|
|
112
|
+
return this.exit(
|
|
113
|
+
new Error(
|
|
114
|
+
`${ERROR_NO_DATA_SOURCES_FOUND} ${this.artifactInfo.datasourcesDir}.
|
|
115
|
+
${chalk.yellow(
|
|
116
|
+
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
|
|
117
|
+
)}`,
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// check for models
|
|
123
|
+
if (!fs.existsSync(this.artifactInfo.modelDir)) {
|
|
124
|
+
return this.exit(
|
|
125
|
+
new Error(
|
|
126
|
+
`${ERROR_NO_MODELS_FOUND} ${this.artifactInfo.modelDir}.
|
|
127
|
+
${chalk.yellow(
|
|
128
|
+
'Please visit https://loopback.io/doc/en/lb4/Model-generator.html for information on how models are discovered',
|
|
129
|
+
)}`,
|
|
130
|
+
),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async promptDataSourceName() {
|
|
136
|
+
if (this.shouldExit()) return false;
|
|
137
|
+
|
|
138
|
+
debug('Prompting for a datasource ');
|
|
139
|
+
let datasourcesList;
|
|
140
|
+
|
|
141
|
+
// grab the datasourcename from the command line
|
|
142
|
+
const cmdDatasourceName = this.options.datasource
|
|
143
|
+
? utils.toClassName(this.options.datasource) + 'Datasource'
|
|
144
|
+
: '';
|
|
145
|
+
|
|
146
|
+
debug(`command line datasource is ${cmdDatasourceName}`);
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
datasourcesList = await utils.getArtifactList(
|
|
150
|
+
this.artifactInfo.datasourcesDir,
|
|
151
|
+
'datasource',
|
|
152
|
+
true,
|
|
153
|
+
);
|
|
154
|
+
debug(
|
|
155
|
+
`datasourcesList from ${utils.sourceRootDir}/${utils.datasourcesDir} : ${datasourcesList}`,
|
|
156
|
+
);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
return this.exit(err);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const availableDatasources = datasourcesList.filter(item => {
|
|
162
|
+
debug(`data source inspecting item: ${item}`);
|
|
163
|
+
const result = utils.isConnectorOfType(
|
|
164
|
+
VALID_CONNECTORS_FOR_REPOSITORY,
|
|
165
|
+
this.artifactInfo.datasourcesDir,
|
|
166
|
+
item,
|
|
167
|
+
);
|
|
168
|
+
return result !== false;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
debug(`artifactInfo.dataSourceClass ${this.artifactInfo.dataSourceClass}`);
|
|
172
|
+
|
|
173
|
+
if (availableDatasources.length === 0) {
|
|
174
|
+
return this.exit(
|
|
175
|
+
new Error(
|
|
176
|
+
`${ERROR_NO_DATA_SOURCES_FOUND} ${this.artifactInfo.datasourcesDir}.
|
|
177
|
+
${chalk.yellow(
|
|
178
|
+
'Please visit https://loopback.io/doc/en/lb4/DataSource-generator.html for information on how datasources are discovered',
|
|
179
|
+
)}`,
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (availableDatasources.includes(cmdDatasourceName)) {
|
|
185
|
+
Object.assign(this.artifactInfo, {
|
|
186
|
+
dataSourceClass: cmdDatasourceName,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return this.prompt([
|
|
191
|
+
{
|
|
192
|
+
type: 'list',
|
|
193
|
+
name: 'dataSourceClass',
|
|
194
|
+
message: PROMPT_MESSAGE_DATA_SOURCE,
|
|
195
|
+
choices: availableDatasources,
|
|
196
|
+
when: !this.artifactInfo.dataSourceClass,
|
|
197
|
+
default: availableDatasources[0],
|
|
198
|
+
validate: utils.validateClassName,
|
|
199
|
+
},
|
|
200
|
+
])
|
|
201
|
+
.then(props => {
|
|
202
|
+
Object.assign(this.artifactInfo, props);
|
|
203
|
+
debug(`props after datasource prompt: ${inspect(props)}`);
|
|
204
|
+
return props;
|
|
205
|
+
})
|
|
206
|
+
.catch(err => {
|
|
207
|
+
debug(`Error during datasource prompt: ${err}`);
|
|
208
|
+
return this.exit(err);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async promptModels() {
|
|
213
|
+
if (this.shouldExit()) return false;
|
|
214
|
+
|
|
215
|
+
await this._inferDataSourceNames();
|
|
216
|
+
|
|
217
|
+
let modelList;
|
|
218
|
+
try {
|
|
219
|
+
debug(`model list dir ${this.artifactInfo.modelDir}`);
|
|
220
|
+
modelList = await utils.getArtifactList(
|
|
221
|
+
this.artifactInfo.modelDir,
|
|
222
|
+
'model',
|
|
223
|
+
);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return this.exit(err);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (this.options.model) {
|
|
229
|
+
debug(`Model name received from command line: ${this.options.model}`);
|
|
230
|
+
|
|
231
|
+
this.options.model = utils.toClassName(this.options.model);
|
|
232
|
+
// assign the model name from the command line only if it is valid
|
|
233
|
+
if (
|
|
234
|
+
modelList &&
|
|
235
|
+
modelList.length > 0 &&
|
|
236
|
+
modelList.includes(this.options.model)
|
|
237
|
+
) {
|
|
238
|
+
Object.assign(this.artifactInfo, {modelNameList: [this.options.model]});
|
|
239
|
+
} else {
|
|
240
|
+
modelList = [];
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (modelList.length === 0) {
|
|
245
|
+
return this.exit(
|
|
246
|
+
new Error(
|
|
247
|
+
`${ERROR_NO_MODELS_FOUND} ${this.artifactInfo.modelDir}.
|
|
248
|
+
${chalk.yellow(
|
|
249
|
+
'Please visit https://loopback.io/doc/en/lb4/Model-generator.html for information on how models are discovered',
|
|
250
|
+
)}`,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return this.prompt([
|
|
256
|
+
{
|
|
257
|
+
type: 'checkbox',
|
|
258
|
+
name: 'modelNameList',
|
|
259
|
+
message: PROMPT_MESSAGE_MODEL,
|
|
260
|
+
choices: modelList.map(m => ({name: m, value: m, checked: true})),
|
|
261
|
+
when: this.artifactInfo.modelNameList === undefined,
|
|
262
|
+
// Require at least one model to be selected
|
|
263
|
+
// This prevents users from accidentally pressing ENTER instead of SPACE
|
|
264
|
+
// to select a model from the list
|
|
265
|
+
validate: result => !!result.length,
|
|
266
|
+
},
|
|
267
|
+
])
|
|
268
|
+
.then(props => {
|
|
269
|
+
Object.assign(this.artifactInfo, props);
|
|
270
|
+
debug(`props after model list prompt: ${inspect(props)}`);
|
|
271
|
+
return props;
|
|
272
|
+
})
|
|
273
|
+
.catch(err => {
|
|
274
|
+
debug(`Error during model list prompt: ${err}`);
|
|
275
|
+
return this.exit(err);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Prompt for basePath if only one model is selected
|
|
281
|
+
*/
|
|
282
|
+
async promptBasePath() {
|
|
283
|
+
if (this.options.basePath) {
|
|
284
|
+
this.artifactInfo.basePath = this.options.basePath;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
this.artifactInfo.modelNameList &&
|
|
290
|
+
this.artifactInfo.modelNameList.length === 1
|
|
291
|
+
) {
|
|
292
|
+
const model = this.artifactInfo.modelNameList[0];
|
|
293
|
+
const props = await this.prompt([
|
|
294
|
+
{
|
|
295
|
+
type: 'input',
|
|
296
|
+
name: 'basePath',
|
|
297
|
+
message: PROMPT_MESSAGE_BASE_PATH,
|
|
298
|
+
default: utils.prependBackslash(
|
|
299
|
+
utils.pluralize(utils.urlSlug(model)),
|
|
300
|
+
),
|
|
301
|
+
validate: utils.validateUrlSlug,
|
|
302
|
+
filter: utils.prependBackslash,
|
|
303
|
+
},
|
|
304
|
+
]);
|
|
305
|
+
if (props) {
|
|
306
|
+
this.artifactInfo.basePath = props.basePath;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async scaffold() {
|
|
312
|
+
if (this.shouldExit()) return false;
|
|
313
|
+
|
|
314
|
+
if (_.isEmpty(this.artifactInfo.modelNameList)) {
|
|
315
|
+
return this.exit(new Error(`${ERROR_NO_MODEL_SELECTED}`));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
this.artifactInfo.disableIndexUpdate = true;
|
|
319
|
+
for (const model of this.artifactInfo.modelNameList) {
|
|
320
|
+
this.artifactInfo.modelName = model;
|
|
321
|
+
this.artifactInfo.outFile = utils.getRestConfigFileName(model);
|
|
322
|
+
if (
|
|
323
|
+
!this.artifactInfo.basePath ||
|
|
324
|
+
this.artifactInfo.modelNameList.length > 1
|
|
325
|
+
) {
|
|
326
|
+
const defaultBasePath =
|
|
327
|
+
this.options.basePath ||
|
|
328
|
+
utils.prependBackslash(utils.pluralize(utils.urlSlug(model)));
|
|
329
|
+
this.artifactInfo.basePath = defaultBasePath;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const source = this.templatePath(
|
|
333
|
+
path.join(
|
|
334
|
+
utils.sourceRootDir,
|
|
335
|
+
utils.modelEndpointsDir,
|
|
336
|
+
this.artifactInfo.defaultTemplate,
|
|
337
|
+
),
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
const dest = this.destinationPath(
|
|
341
|
+
path.join(this.artifactInfo.outDir, this.artifactInfo.outFile),
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
if (debug.enabled) {
|
|
345
|
+
debug(`artifactInfo: ${inspect(this.artifactInfo)}`);
|
|
346
|
+
debug(`Copying artifact to: ${dest}`);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.copyTemplatedFiles(source, dest, this.artifactInfo);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
this.log(
|
|
353
|
+
'Updating src/application.ts to register CrudRestComponent from @loopback/rest-crud',
|
|
354
|
+
);
|
|
355
|
+
await updateApplicationTs(this.destinationPath());
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
install() {
|
|
360
|
+
if (this.shouldExit()) return false;
|
|
361
|
+
debug('install npm dependencies');
|
|
362
|
+
const pkgJson = this.packageJson || {};
|
|
363
|
+
const deps = pkgJson.dependencies || {};
|
|
364
|
+
const pkgs = [];
|
|
365
|
+
|
|
366
|
+
const version = cliPkg.config.templateDependencies['@loopback/rest-crud'];
|
|
367
|
+
|
|
368
|
+
if (!deps['@loopback/rest-crud']) {
|
|
369
|
+
pkgs.push(`@loopback/rest-crud@${version}`);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (pkgs.length === 0) return;
|
|
373
|
+
|
|
374
|
+
this.pkgManagerInstall(pkgs, {
|
|
375
|
+
npm: {
|
|
376
|
+
save: true,
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async end() {
|
|
382
|
+
this.artifactInfo.type =
|
|
383
|
+
this.artifactInfo.modelNameList &&
|
|
384
|
+
this.artifactInfo.modelNameList.length > 1
|
|
385
|
+
? 'RestConfigs'
|
|
386
|
+
: 'RestConfig';
|
|
387
|
+
|
|
388
|
+
this.artifactInfo.modelNameList = _.map(
|
|
389
|
+
this.artifactInfo.modelNameList,
|
|
390
|
+
name => {
|
|
391
|
+
return name + '.rest-config';
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
this.artifactInfo.name = this.artifactInfo.modelNameList
|
|
396
|
+
? this.artifactInfo.modelNameList.join(this.classNameSeparator)
|
|
397
|
+
: this.artifactInfo.modelName;
|
|
398
|
+
|
|
399
|
+
await super.end();
|
|
400
|
+
}
|
|
401
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {ModelCrudRestApiConfig} from '@loopback/rest-crud';
|
|
2
|
+
import {<%= modelName %>} from '../models';
|
|
3
|
+
|
|
4
|
+
const config: ModelCrudRestApiConfig = {
|
|
5
|
+
model: <%= modelName %>,
|
|
6
|
+
pattern: 'CrudRest',
|
|
7
|
+
dataSource: '<%= dataSourceName %>',
|
|
8
|
+
basePath: '<%= basePath %>',
|
|
9
|
+
};
|
|
10
|
+
module.exports = config;
|