/
input.ts
210 lines (179 loc) · 5.49 KB
/
input.ts
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
import {
getNumber,
getString,
getConfirmation,
getChoice,
getUrl
} from '@sasjs/utils/input'
import { Target, ServerType } from '@sasjs/utils/types'
import { Logger, LogLevel } from '@sasjs/utils/logger'
import chalk from 'chalk'
import path from 'path'
import dotenv from 'dotenv'
import SASjs from '@sasjs/adapter/node'
import { TargetScope } from '../../types/TargetScope'
import {
findTargetInConfiguration,
getGlobalRcFile
} from '../../utils/config-utils'
import { addCredential } from './add-credential'
import { getLocalConfig } from './config'
export async function getCommonFields() {
const scope = await getAndValidateScope()
const serverType = await getAndValidateServerType()
const name = await getAndValidateTargetName(scope, serverType)
const appLoc = await getAndValidateAppLoc(name)
const serverUrl = await getAndValidateServerUrl()
return { scope, serverType, name, appLoc, serverUrl }
}
async function getAndValidateScope(): Promise<TargetScope> {
const { scope } = await getChoice(
'scope',
'Please pick a scope for the new target: ',
(value: number) =>
value === 0 || value === 1 || 'Please choose either option 1 or 2.',
0,
[
{ title: '1. Local project config file' },
{ title: '2. Global config file' }
]
)
return scope === 0 ? TargetScope.Local : TargetScope.Global
}
async function getAndValidateServerType(): Promise<ServerType> {
const { serverType } = await getChoice(
'serverType',
'Please pick a server type: ',
(value: any) =>
value === 0 || value === 1 || 'Please choose either option 1 or 2.',
0,
[{ title: '1. SAS Viya' }, { title: '2. SAS 9' }]
)
return serverType === 0 ? ServerType.SasViya : ServerType.Sas9
}
async function getAndValidateServerUrl() {
const { serverUrl } = await getUrl(
'serverUrl',
'Please enter a target server URL (including port, if relevant): ',
'Server URL is required.'
)
return serverUrl
}
async function getAndValidateTargetName(
targetScope: TargetScope,
serverType: ServerType
): Promise<string> {
const validator = async (value: string) => {
let config
if (targetScope === TargetScope.Local) {
config = await getLocalConfig()
} else {
config = await getGlobalRcFile()
}
let existingTargetNames = []
if (config && config.targets) {
existingTargetNames = config.targets.map((t: Target) => t.name)
}
if (existingTargetNames.includes(value)) {
return `A target with the name ${value} already exists. Please try again with a different target name.`
}
if (!value) {
return 'Target name is required.'
}
if (value.trim().includes(' ')) {
return 'Target names cannot include spaces. Please try again with a valid target name.'
}
if (!/^[a-z0-9]+$/i.test(value))
return 'Target names can only contain alphanumeric characters. Please try again with a valid target name.'
return true
}
const defaultName = serverType === ServerType.SasViya ? 'viya' : 'sas9'
const { targetName } = await getString(
'targetName',
'Please enter a target name: ',
validator,
defaultName
)
return targetName
}
export async function getAndValidateSas9Fields() {
const { serverName } = await getString(
'serverName',
'Please enter a server name (default is SASApp): ',
(v) => !!v || 'Server name is required.',
'SASApp'
)
const { repositoryName } = await getString(
'repositoryName',
'Please enter a repository name (default is Foundation): ',
(v) => !!v || 'Repository name is required.',
'Foundation'
)
return { serverName, repositoryName }
}
export async function getAndValidateSasViyaFields(
targetName: string,
scope: TargetScope,
serverUrl: string,
logger: Logger
): Promise<{
contextName: string
}> {
let contextName = 'SAS Job Execution compute context'
const { shouldAuthenticate } = await getConfirmation(
'shouldAuthenticate',
'Would you like to authenticate against this target?',
true
)
if (shouldAuthenticate) {
await addCredential(targetName)
const sasjs = new SASjs({
serverUrl,
serverType: ServerType.SasViya,
debug: logger.logLevel === LogLevel.Debug
})
let contexts = []
if (scope === TargetScope.Local) {
dotenv.config({
path: path.join(process.projectDir, `.env.${targetName}`)
})
contexts = await sasjs.getAllContexts(process.env.ACCESS_TOKEN as string)
} else {
const { target } = await findTargetInConfiguration(targetName)
contexts = await sasjs.getAllContexts(target.authInfo.access_token)
}
logger.log(
chalk.cyanBright(
'Here are all the available execution contexts on this server:\n'
)
)
logger.log(
chalk.cyanBright(
`${contexts
.map((c, i) => chalk.yellowBright(`${i + 1}. `) + c.name)
.join('\n')}`
)
)
const { contextNumber } = await getNumber(
'contextNumber',
'Please enter your SAS Viya execution context number: ',
(v: number) =>
(v >= 1 && v <= contexts.length) || 'Context number is invalid.',
1
)
contextName = contexts[contextNumber - 1].name
return { contextName }
}
return {
contextName
}
}
async function getAndValidateAppLoc(targetName: string): Promise<string> {
const { appLoc } = await getString(
'appLoc',
'Please provide an app location: ',
(v: string) => !!v || 'App location is required.',
`/Public/app/${targetName}`
)
return appLoc
}