-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
330 lines (286 loc) · 8.34 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
import { create } from '../sasjs-create'
import { getUserInput } from '../utils/input-utils'
import chalk from 'chalk'
import path from 'path'
import SASjs from '@sasjs/adapter/node'
import validUrl from 'valid-url'
import {
getGlobalRcFile,
saveGlobalRcFile,
getConfiguration
} from '../utils/config-utils'
import { createFile } from '../utils/file-utils'
import { getNewAccessToken } from '../utils/auth-utils'
export async function addTarget() {
const scope = await getAndValidateScope()
const serverType = await getAndValidateServerType()
const name = await getAndValidateTargetName(scope, serverType)
let localAppLoc
if (scope === 1) localAppLoc = await getAndValidateLocalAppLoc(name)
const serverUrl = await getAndValidateServerUrl()
let buildTarget = {
name,
serverType: serverType === 1 ? 'SASVIYA' : 'SAS9',
serverUrl
}
if (serverType === 2) {
const sas9FieldValues = await getAndValidateSas9Fields()
buildTarget = {
...buildTarget,
tgtBuildVars: sas9FieldValues,
tgtDeployVars: sas9FieldValues
}
} else {
const {
client,
secret,
contextName,
authInfo
} = await getAndValidateSasViyaFields(serverUrl)
buildTarget = {
...buildTarget,
tgtBuildVars: { client, secret, contextName },
tgtDeployVars: { client, secret, contextName },
authInfo: authInfo,
deployServicePack: true,
tgtDeployScripts: []
}
}
if (scope === 1) {
buildTarget = {
...buildTarget,
appLoc: localAppLoc
}
await saveToLocalConfig(buildTarget)
} else if (scope === 2) {
await saveToGlobalConfig(buildTarget)
}
}
async function getLocalConfig() {
const buildSourceFolder = require('../constants').buildSourceFolder
const config = await getConfiguration(
path.join(buildSourceFolder, 'sasjsconfig.json')
)
if (!config)
await create('.', 'sasonly')
return config
}
async function saveToLocalConfig(buildTarget) {
const buildSourceFolder = require('../constants').buildSourceFolder
let config = await getLocalConfig()
if (config) {
if (config.targets && config.targets.length) {
config.targets.push(buildTarget)
} else {
config.targets = [buildTarget]
}
} else {
config = { targets: [buildTarget] }
}
await createFile(
path.join(buildSourceFolder, 'sasjsconfig.json'),
JSON.stringify(config, null, 2)
)
}
async function saveToGlobalConfig(buildTarget) {
let globalConfig = await getGlobalRcFile()
if (globalConfig) {
if (globalConfig.targets && globalConfig.targets.length) {
globalConfig.targets.push(buildTarget)
} else {
globalConfig.targets = [buildTarget]
}
} else {
globalConfig = { targets: [buildTarget] }
}
await saveGlobalRcFile(JSON.stringify(globalConfig, null, 2))
}
async function getAndValidateScope() {
const scopeField = {
name: 'scope',
type: 'integer',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please pick a scope for the new target:'
)}\n1. Local project config file\n2. Global config file`
),
default: 1
}
const validator = (value) => {
return value === 1 || value === 2
}
const message = `Invalid input. Please choose option 1 or 2.`
return await getAndValidateField(scopeField, validator, message)
}
async function getAndValidateServerType() {
const serverTypeField = {
name: 'serverType',
type: 'integer',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please pick a server type:'
)}\n1. SAS Viya\n2. SAS 9`
),
default: 1
}
const validator = (value) => {
return value === 1 || value === 2
}
const message = `Invalid input. Please choose option 1 or 2.`
return await getAndValidateField(serverTypeField, validator, message)
}
async function getAndValidateServerUrl() {
const serverUrlField = {
name: 'serverUrl',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please enter a target server URL (including port, if relevant):'
)}`
)
}
const validator = validUrl.isUri
const message = 'Invalid input. Please enter a valid URI.'
return await getAndValidateField(serverUrlField, validator, message)
}
async function getAndValidateTargetName(targetScope, serverType) {
const validator = async (value, scope) => {
let config
if (scope === 1) {
config = await getLocalConfig()
} else {
config = await getGlobalRcFile()
}
let existingTargetNames = []
if (config && config.targets) {
existingTargetNames = config.targets.map((t) => t.name)
}
return !existingTargetNames.includes(value)
}
const defaultName = serverType === 1 ? 'viya' : 'sas9'
const isDefaultNameValid = await validator(defaultName, targetScope)
const targetNameField = {
name: 'name',
description: chalk.cyanBright(
`${chalk.greenBright('Please enter a target name:')}`
),
default: isDefaultNameValid ? defaultName : ''
}
const message =
'A target with that name already exists!\nPlease try again with a different target name.'
return await getAndValidateField(
targetNameField,
validator,
message,
targetScope
)
}
async function getAndValidateSas9Fields() {
const serverNameField = {
name: 'serverName',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please enter a server name' + chalk.cyanBright('(default is SASApp):')
)}`
)
}
const repositoryNameField = {
name: 'repositoryName',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please enter a repository name' +
chalk.cyanBright('(default is Foundation):')
)}`
)
}
let serverName = await getAndValidateField(serverNameField, () => true, '')
let repositoryName = await getAndValidateField(
repositoryNameField,
() => true,
''
)
serverName = serverName || 'SASApp'
repositoryName = repositoryName || 'Foundation'
return { serverName, repositoryName }
}
async function getAndValidateSasViyaFields(serverUrl) {
const clientField = {
name: 'client',
description: chalk.cyanBright(
`${chalk.greenBright('Please enter your SAS Viya app client ID: ')}`
)
}
const secretField = {
name: 'secret',
description: chalk.cyanBright(
`${chalk.greenBright('Please enter your SAS Viya app client secret: ')}`
)
}
const contextNameField = {
name: 'contextName',
type: 'integer',
description: chalk.cyanBright(
`${chalk.greenBright(
'Please enter your SAS Viya execution context number: '
)}`
)
}
const nonEmptyValidator = (value) => !!value
const message = 'This value can not be empty. Please enter a value'
const client = await getAndValidateField(
clientField,
nonEmptyValidator,
message
)
const secret = await getAndValidateField(
secretField,
nonEmptyValidator,
message
)
const sasjs = new SASjs({
serverUrl: serverUrl,
serverType: 'SASVIYA'
})
const authResponse = await getNewAccessToken(sasjs, client, secret, {
serverUrl
})
const contexts = await sasjs.getAllContexts(authResponse.access_token)
console.log(
chalk.cyanBright(
'Here are all the available execution contexts on this server:\n'
)
)
console.log(
chalk.cyanBright(
`${contexts
.map((c, i) => chalk.yellowBright(`${i + 1}. `) + c.name)
.join('\n')}`
)
)
const contextNumber =
(await getAndValidateField(contextNameField, nonEmptyValidator, message)) -
1
const contextName = contexts[contextNumber].name
return { client, secret, contextName, authInfo: authResponse }
}
async function getAndValidateField(field, validator, message, validatorParams) {
const input = await getUserInput([field])
const isValid = await validator(input[field.name], validatorParams)
if (!isValid) {
console.log(chalk.redBright.bold(message))
return await getAndValidateField(field, validator, message)
}
return input[field.name]
}
async function getAndValidateLocalAppLoc(target) {
const appLoc = {
name: 'app location',
type: 'string',
description: chalk.cyanBright(
`${chalk.greenBright('Please provide an app location:')}`
),
default: `/Public/app/${target}`
}
const validator = (value) => value !== ''
const message = 'Invalid input.'
return await getAndValidateField(appLoc, validator, message)
}