/
init.js
345 lines (308 loc) · 12.3 KB
/
init.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const BaseCommand = require('../../BaseCommand')
const yeoman = require('yeoman-environment')
const path = require('path')
const fs = require('fs-extra')
const ora = require('ora')
const chalk = require('chalk')
// const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:init', { provider: 'debug' })
const { flags } = require('@oclif/command')
const generators = require('@adobe/generator-aio-app')
const { loadAndValidateConfigFile, importConfigJson } = require('../../lib/import')
const { installPackages, atLeastOne } = require('../../lib/app-helper')
const { ENTP_INT_CERTS_FOLDER, SERVICE_API_KEY_ENV, implPromptChoices } = require('../../lib/defaults')
const cloneDeep = require('lodash.clonedeep')
const DEFAULT_WORKSPACE = 'Stage'
class InitCommand extends BaseCommand {
async run () {
const { args, flags } = this.parse(InitCommand)
if (!flags.login && flags.workspace !== DEFAULT_WORKSPACE) {
this.error('--no-login and --workspace flags cannot be used together.')
}
if (flags.import) {
// resolve to absolute path before any chdir
flags.import = path.resolve(flags.import)
}
if (args.path !== '.') {
const destDir = path.resolve(args.path)
fs.ensureDirSync(destDir)
process.chdir(destDir)
}
const spinner = ora()
if (flags.import || !flags.login) {
// import a console config - no login required!
await this.initNoLogin(flags)
} else {
// we can login
await this.initWithLogin(flags)
}
// install packages, always at the end, so user can ctrl+c
if (!flags['skip-install']) {
await installPackages('.', { spinner, verbose: flags.verbose })
} else {
this.log('--skip-install, make sure to run \'npm install\' later on')
}
this.log(chalk.bold(chalk.green('✔ App initialization finished!')))
this.log('> Tip: you can add more actions, web-assets and events to your project via the `aio app add` commands')
}
/** @private */
async initNoLogin (flags) {
// 1. load console details - if any
let consoleConfig
if (flags.import) {
consoleConfig = loadAndValidateConfigFile(flags.import).values
this.log(chalk.green(`Loaded Adobe Developer Console configuration file for the Project '${consoleConfig.project.title}' in the Organization '${consoleConfig.project.org.name}'`))
}
// 2. prompt for extension points to be implemented
const extensionPoints = await this.selectExtensionPoints(flags)
// 3. run extension point code generators
const projectName = (consoleConfig && consoleConfig.project.name) || path.basename(process.cwd())
await this.runCodeGenerators(flags, extensionPoints, projectName)
// 4. import config - if any
if (flags.import) {
await this.importConsoleConfig(consoleConfig)
}
// 5. This flow supports non logged in users so we can't now for sure if the project has
// required services installed. So we output a note on required services instead.
const requiredServices = this.getAllRequiredServicesFromExtPoints(extensionPoints)
if (requiredServices.length > 0) {
this.log(chalk.bold(`Please ensure the following service(s) are enabled in the Organization and added to the Console Workspace: '${requiredServices}'`))
}
}
async initWithLogin (flags) {
// this will trigger a login
const consoleCLI = await this.getLibConsoleCLI()
// 1. select org
const org = await this.selectConsoleOrg(consoleCLI)
// 2. get supported services
const orgSupportedServices = await consoleCLI.getEnabledServicesForOrg(org.id)
// 3. select or create project
const project = await this.selectOrCreateConsoleProject(consoleCLI, org)
// 4. retrieve workspace details, defaults to Stage
const workspace = await this.retrieveWorkspaceFromName(consoleCLI, org, project, flags.workspace)
// 5. ask for exensionPoints, only allow selection for extensions that have services enabled in Org
const extensionPoints = await this.selectExtensionPoints(flags, orgSupportedServices)
// 6. add any required services to Workspace
const requiredServices = this.getAllRequiredServicesFromExtPoints(extensionPoints)
await this.addServices(
consoleCLI,
org,
project,
workspace,
orgSupportedServices,
requiredServices
)
// 7. download workspace config
const consoleConfig = await consoleCLI.getWorkspaceConfig(org.id, project.id, workspace.id, orgSupportedServices)
// 8. run code generators
await this.runCodeGenerators(flags, extensionPoints, consoleConfig.project.name)
// 9. import config
await this.importConsoleConfig(consoleConfig)
this.log(chalk.blue(chalk.bold(`Project initialized for Workspace ${workspace.name}, you can run 'aio app use -w <workspace>' to switch workspace.`)))
}
async selectExtensionPoints (flags, orgSupportedServices = null) {
if (!flags.extensions) {
return [implPromptChoices.find(i => i.value.name === 'application').value]
} else if (flags.extension) {
const extList = implPromptChoices.filter(i => flags.extension.indexOf(i.value.name) > -1)
.map(i => i.value)
if (extList.length < 1) {
throw new Error(`--extension=${flags.extension} not found.`)
}
return extList
} else {
const choices = cloneDeep(implPromptChoices).filter(i => i.value.name !== 'application')
// disable extensions that lack required services
if (orgSupportedServices) {
const supportedServiceCodes = new Set(orgSupportedServices.map(s => s.code))
// filter choices
choices.forEach(c => {
const missingServices = c.value.requiredServices.filter(s => !supportedServiceCodes.has(s))
if (missingServices.length > 0) {
c.disabled = true
c.name = `${c.name}: missing service(s) in Org: '${missingServices}'`
}
})
}
const answers = await this.prompt([{
type: 'checkbox',
name: 'res',
message: 'Which extension point(s) do you wish to implement ?',
choices,
validate: atLeastOne
}])
return answers.res
}
}
async selectConsoleOrg (consoleCLI) {
const organizations = await consoleCLI.getOrganizations()
const selectedOrg = await consoleCLI.promptForSelectOrganization(organizations)
return selectedOrg
}
async selectOrCreateConsoleProject (consoleCLI, org) {
const projects = await consoleCLI.getProjects(org.id)
let project = await consoleCLI.promptForSelectProject(
projects,
{},
{ allowCreate: true }
)
if (!project) {
// user has escaped project selection prompt, let's create a new one
const projectDetails = await consoleCLI.promptForCreateProjectDetails()
project = await consoleCLI.createProject(org.id, projectDetails)
project.isNew = true
}
return project
}
async retrieveWorkspaceFromName (consoleCLI, org, project, workspaceName) {
// get workspace details
const workspaces = await consoleCLI.getWorkspaces(org.id, project.id)
const workspace = workspaces.find(w => w.name.toLowerCase() === workspaceName.toLowerCase())
if (!workspace) {
throw new Error(`'--workspace=${workspaceName}' in Project '${project.name}' not found.`)
}
return workspace
}
async addServices (consoleCLI, org, project, workspace, orgSupportedServices, requiredServices) {
// add required services if needed (for extension point)
const currServiceProperties = await consoleCLI.getServicePropertiesFromWorkspace(
org.id,
project.id,
workspace,
orgSupportedServices
)
const serviceCodesToAdd = requiredServices.filter(s => !currServiceProperties.some(sp => sp.sdkCode === s))
if (serviceCodesToAdd.length > 0) {
const servicePropertiesToAdd = serviceCodesToAdd
.map(s => {
// previous check ensures orgSupportedServices has required services
const orgServiceDefinition = orgSupportedServices.find(os => os.code === s)
return {
sdkCode: s,
name: orgServiceDefinition.name,
roles: orgServiceDefinition.properties.roles,
// add all licenseConfigs
licenseConfigs: orgServiceDefinition.properties.licenseConfigs
}
})
await consoleCLI.subscribeToServices(
org.id,
project,
workspace,
// certDir if need to create integration
path.join(this.config.dataDir, ENTP_INT_CERTS_FOLDER),
// new service properties
currServiceProperties.concat(servicePropertiesToAdd)
)
}
return workspace
}
getAllRequiredServicesFromExtPoints (extensionPoints) {
const requiredServicesWithDuplicates = extensionPoints
.map(e => e.requiredServices)
// flat not supported in node 10
.reduce((res, arr) => res.concat(arr), [])
return [...new Set(requiredServicesWithDuplicates)]
}
async runCodeGenerators (flags, extensionPoints, projectName) {
let env = yeoman.createEnv()
// first run app generator that will generate the root skeleton
const appGen = env.instantiate(generators['base-app'], {
options: {
'skip-prompt': flags.yes,
'project-name': projectName
}
})
await env.runGenerator(appGen)
// Creating new Yeoman env here to workaround an issue where yeoman reuses the conflicter from previous environment.
// https://github.com/yeoman/environment/issues/324
env = yeoman.createEnv()
// try to use appGen.composeWith
for (let i = 0; i < extensionPoints.length; ++i) {
const extGen = env.instantiate(
extensionPoints[i].generator,
{
options: {
'skip-prompt': flags.yes,
// do not prompt for overwrites
force: true
}
})
await env.runGenerator(extGen)
}
}
// console config is already loaded into object
async importConsoleConfig (config) {
// get jwt client id
const jwtConfig = config.project.workspace.details.credentials && config.project.workspace.details.credentials.find(c => c.jwt)
const serviceClientId = (jwtConfig && jwtConfig.jwt.client_id) || ''
const configBuffer = Buffer.from(JSON.stringify(config))
const interactive = false
const merge = true
await importConfigJson(
// NOTE: importConfigJson should support reading json directly
configBuffer,
process.cwd(),
{ interactive, merge },
{ [SERVICE_API_KEY_ENV]: serviceClientId }
)
}
}
InitCommand.description = `Create a new Adobe I/O App
`
InitCommand.flags = {
...BaseCommand.flags,
yes: flags.boolean({
description: 'Skip questions, and use all default values',
default: false,
char: 'y'
}),
'skip-install': flags.boolean({
description: 'Skip npm installation after files are created',
char: 's',
default: false
}),
import: flags.string({
description: 'Import an Adobe I/O Developer Console configuration file',
char: 'i'
}),
login: flags.boolean({
description: 'Login using your Adobe ID for interacting with Adobe I/O Developer Console',
default: true,
allowNo: true
}),
extensions: flags.boolean({
description: 'Use --no-extensions to create a blank application that does not integrate with Exchange',
default: true,
allowNo: true
}),
extension: flags.string({
description: 'Extension point(s) to implement',
char: 'e',
multiple: true,
exclusive: ['extensions']
}),
workspace: flags.string({
description: 'Specify the Adobe Developer Console Workspace to init from, defaults to Stage',
default: DEFAULT_WORKSPACE,
char: 'w',
exclusive: ['import'] // also no-login
})
}
InitCommand.args = [
{
name: 'path',
description: 'Path to the app directory',
default: '.'
}
]
module.exports = InitCommand