Skip to content

Commit fbb5dee

Browse files
authored
Merge pull request #200 from sasjs/issue-139
refactor: improve sasjs-commands related logic
2 parents d782376 + 8c3b720 commit fbb5dee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+459
-655
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"@sasjs/adapter": "^1.17.0",
39-
"@sasjs/core": "^1.7.3",
39+
"@sasjs/core": "^1.7.4",
4040
"base64-img": "^1.0.4",
4141
"btoa": "^1.2.1",
4242
"chalk": "^4.1.0",

src/cli.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ function parseCommand(rawArgs) {
3838

3939
if (args.length) {
4040
const name = getUnaliasedCommand(args[0])
41-
let parameters
42-
43-
switch (name) {
44-
case 'run':
45-
parameters = processRunParameters(args.slice(1))
46-
return { name, parameters }
47-
case 'request':
48-
parameters = processRequestParameters(args.slice(1))
49-
return { name, parameters }
50-
}
5141

5242
return { name, parameters: args }
5343
}
@@ -170,25 +160,19 @@ export async function cli(args) {
170160
}
171161
switch (command.name) {
172162
case 'create': {
173-
const { projectName, appType } = processCreateParameters(
174-
command.parameters
175-
)
176-
await createFileStructure(projectName, appType)
163+
await createFileStructure(command.parameters)
177164
break
178165
}
179166
case 'compile': {
180167
await compileServices(command.parameters[1])
181168
break
182169
}
183170
case 'build': {
184-
await buildServices(command.parameters[1])
171+
await buildServices(command.parameters)
185172
break
186173
}
187174
case 'deploy': {
188-
await deployServices(
189-
command.parameters[1],
190-
command.parameters[2] === '-f'
191-
)
175+
await deployServices(command.parameters)
192176
break
193177
}
194178
case 'servicepack': {
@@ -216,27 +200,19 @@ export async function cli(args) {
216200
break
217201
}
218202
case 'web': {
219-
await buildWebApp(command.parameters[1])
203+
await buildWebApp(command.parameters)
220204
break
221205
}
222206
case 'add': {
223-
await add(command.parameters[1])
207+
await add(command.parameters)
224208
break
225209
}
226210
case 'run': {
227-
const { filePath, targetName } = command.parameters
228-
await run(filePath, targetName)
211+
await run(command.parameters)
229212
break
230213
}
231214
case 'request': {
232-
const {
233-
sasJobLocation,
234-
dataFilePath,
235-
configFilePath,
236-
targetName
237-
} = command.parameters
238-
239-
await runRequest(sasJobLocation, dataFilePath, configFilePath, targetName)
215+
await runRequest(command.parameters)
240216

241217
break
242218
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { create } from '../sasjs-create'
1+
import { create } from './create'
22
import { getAndValidateField } from '../utils/input-utils'
33
import chalk from 'chalk'
44
import path from 'path'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import * as thisModule from './index'
1+
import * as thisModule from './build'
22

33
import find from 'find'
44
import path from 'path'
55
import chalk from 'chalk'
66
import uniqBy from 'lodash.uniqby'
77
import groupBy from 'lodash.groupby'
8-
import { deploy } from '../sasjs-deploy'
9-
import { createWebAppServices } from '../sasjs-web'
8+
import { deploy } from './deploy'
9+
import { createWebAppServices } from './web'
1010
import {
1111
readFile,
1212
getSubFoldersInFolder,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { displayResult } from '../utils/displayResult'
1+
import { displayResult } from '../../utils/displayResult'
22

33
/**
44
* Creates compute context using provided config.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { displayResult } from '../utils/displayResult'
1+
import { displayResult } from '../../utils/displayResult'
22

33
/**
44
* Edits existing compute context.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { displayResult } from '../utils/displayResult'
1+
import { displayResult } from '../../utils/displayResult'
22
import path from 'path'
3-
import { createFile, sanitizeFileName } from '../utils/file-utils'
3+
import { createFile, sanitizeFileName } from '../../utils/file-utils'
44

55
/**
66
* Export compute context to json file in current folder.
Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,107 +4,68 @@ import { edit } from './edit'
44
import { remove } from './remove'
55
import { list } from './list'
66
import { exportContext } from './export'
7-
import { fileExists, readFile } from '../utils/file-utils'
8-
import { getBuildTarget, getAccessToken } from '../utils/config-utils'
9-
import { displayResult } from '../utils/displayResult'
7+
import { fileExists, readFile } from '../../utils/file-utils'
8+
import { getBuildTarget, getAccessToken } from '../../utils/config-utils'
9+
import { displayResult } from '../../utils/displayResult'
10+
import { Command } from '../../utils/command'
1011
import SASjs from '@sasjs/adapter/node'
1112

1213
export async function processContext(commandLine) {
13-
const command = commandLine[1]
14-
const commands = {
14+
const command = new Command(commandLine)
15+
const subCommand = command.getSubCommand()
16+
const subCommands = {
1517
create: 'create',
1618
edit: 'edit',
1719
delete: 'delete',
1820
list: 'list',
1921
export: 'export'
2022
}
2123

22-
if (!commands.hasOwnProperty(command)) {
24+
if (!subCommands.hasOwnProperty(subCommand)) {
2325
console.log(
2426
chalk.redBright(
2527
`Not supported context command. Supported commands are:\n${Object.keys(
26-
commands
28+
subCommands
2729
).join('\n')}`
2830
)
2931
)
3032

3133
return
3234
}
3335

34-
const commandExample =
35-
'sasjs context <command> --source ../contextConfig.json --target targetName'
36-
37-
let targetName = []
38-
let targetNameFlagIndex = commandLine.indexOf('--target')
39-
40-
if (targetNameFlagIndex === -1)
41-
targetNameFlagIndex = commandLine.indexOf('-t')
42-
43-
if (targetNameFlagIndex !== -1) {
44-
for (let i = targetNameFlagIndex + 1; i < commandLine.length; i++) {
45-
if (commandLine[i] === '--source' || commandLine[i] === '-s') {
46-
throw `Target name has to be provided as the last argument (eg ${commandExample})`
47-
}
48-
49-
targetName.push(commandLine[i])
50-
}
51-
}
52-
53-
targetName = targetName.join(' ')
54-
36+
const targetName = command.getFlagValue('target')
5537
const target = await getBuildTarget(targetName)
5638

57-
let configPath
58-
let config
59-
let parsedConfig
60-
let configPathFlagIndex
39+
const commandExample =
40+
'sasjs context <command> --source ../contextConfig.json --target targetName'
6141

6242
const getConfig = async () => {
63-
configPathFlagIndex = commandLine.indexOf('--source')
43+
const configPath = command.getFlagValue('source')
6444

65-
if (configPathFlagIndex === -1)
66-
configPathFlagIndex = commandLine.indexOf('-s')
45+
if (!configPath) {
46+
const message = `'--source' flag is missing (eg '${commandExample}')`
6747

68-
if (configPathFlagIndex === -1) {
69-
console.log(
70-
chalk.redBright(`'--source' flag is missing (eg '${commandExample}')`)
71-
)
48+
console.log(chalk.redBright(message))
7249

7350
return
74-
}
51+
} else if (!validateConfigPath(configPath)) {
52+
const message = `Provide a path to context config file (eg '${commandExample}')`
7553

76-
configPath = commandLine[configPathFlagIndex + 1]
77-
78-
if (!configPath || !validateConfigPath(configPath)) {
79-
console.log(
80-
chalk.redBright(
81-
`Provide a path to context config file (eg '${commandExample}')`
82-
)
83-
)
54+
console.log(chalk.redBright(message))
8455

8556
return
8657
}
8758

8859
return await readFile(configPath)
8960
}
9061

91-
const getContextName = (upToSourceFlag = false) => {
92-
let contextName = ''
62+
const getContextName = () => {
63+
let contextName = command.values.join(' ')
9364

94-
if (targetNameFlagIndex === -1) {
95-
contextName = commandLine.slice(2).join(' ')
96-
} else {
97-
contextName = commandLine
98-
.slice(2, upToSourceFlag ? configPathFlagIndex : targetNameFlagIndex)
99-
.join(' ')
100-
}
65+
if (!contextName) {
66+
const message = `Provide a context name (eg 'sasjs context <command> contextName')`
10167

102-
if (!contextName && !upToSourceFlag) {
103-
console.log(
104-
chalk.redBright(
105-
`Provide a context name (eg 'sasjs context <command> contextName')`
106-
)
107-
)
68+
console.log(chalk.redBright(message))
10869

10970
return null
11071
}
@@ -122,10 +83,12 @@ export async function processContext(commandLine) {
12283
displayResult(err)
12384
})
12485

86+
let config
87+
let parsedConfig
12588
let output
12689

127-
switch (command) {
128-
case commands.create:
90+
switch (subCommand) {
91+
case subCommands.create:
12992
config = await getConfig()
13093

13194
if (!config) break
@@ -135,7 +98,7 @@ export async function processContext(commandLine) {
13598
output = await create(parsedConfig, sasjs, accessToken)
13699

137100
break
138-
case commands.edit: {
101+
case subCommands.edit: {
139102
config = await getConfig()
140103

141104
const contextName = getContextName(true)
@@ -146,7 +109,7 @@ export async function processContext(commandLine) {
146109

147110
break
148111
}
149-
case commands.delete: {
112+
case subCommands.delete: {
150113
const contextName = getContextName()
151114

152115
if (contextName) {
@@ -155,11 +118,11 @@ export async function processContext(commandLine) {
155118

156119
break
157120
}
158-
case commands.list:
121+
case subCommands.list:
159122
output = list(target, sasjs, accessToken)
160123

161124
break
162-
case commands.export: {
125+
case subCommands.export: {
163126
const contextName = getContextName()
164127

165128
if (contextName) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chalk from 'chalk'
22
import ora from 'ora'
3-
import { displayResult } from '../utils/displayResult'
3+
import { displayResult } from '../../utils/displayResult'
44

55
/**
66
* Lists all accessible and inaccessible compute contexts.

0 commit comments

Comments
 (0)