Skip to content

Commit

Permalink
Merge pull request #200 from sasjs/issue-139
Browse files Browse the repository at this point in the history
refactor: improve sasjs-commands related logic
  • Loading branch information
YuryShkoda authored Nov 16, 2020
2 parents d782376 + 8c3b720 commit fbb5dee
Show file tree
Hide file tree
Showing 51 changed files with 459 additions and 655 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@sasjs/adapter": "^1.17.0",
"@sasjs/core": "^1.7.3",
"@sasjs/core": "^1.7.4",
"base64-img": "^1.0.4",
"btoa": "^1.2.1",
"chalk": "^4.1.0",
Expand Down
38 changes: 7 additions & 31 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ function parseCommand(rawArgs) {

if (args.length) {
const name = getUnaliasedCommand(args[0])
let parameters

switch (name) {
case 'run':
parameters = processRunParameters(args.slice(1))
return { name, parameters }
case 'request':
parameters = processRequestParameters(args.slice(1))
return { name, parameters }
}

return { name, parameters: args }
}
Expand Down Expand Up @@ -170,25 +160,19 @@ export async function cli(args) {
}
switch (command.name) {
case 'create': {
const { projectName, appType } = processCreateParameters(
command.parameters
)
await createFileStructure(projectName, appType)
await createFileStructure(command.parameters)
break
}
case 'compile': {
await compileServices(command.parameters[1])
break
}
case 'build': {
await buildServices(command.parameters[1])
await buildServices(command.parameters)
break
}
case 'deploy': {
await deployServices(
command.parameters[1],
command.parameters[2] === '-f'
)
await deployServices(command.parameters)
break
}
case 'servicepack': {
Expand Down Expand Up @@ -216,27 +200,19 @@ export async function cli(args) {
break
}
case 'web': {
await buildWebApp(command.parameters[1])
await buildWebApp(command.parameters)
break
}
case 'add': {
await add(command.parameters[1])
await add(command.parameters)
break
}
case 'run': {
const { filePath, targetName } = command.parameters
await run(filePath, targetName)
await run(command.parameters)
break
}
case 'request': {
const {
sasJobLocation,
dataFilePath,
configFilePath,
targetName
} = command.parameters

await runRequest(sasJobLocation, dataFilePath, configFilePath, targetName)
await runRequest(command.parameters)

break
}
Expand Down
2 changes: 1 addition & 1 deletion src/sasjs-add/index.js → src/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { create } from '../sasjs-create'
import { create } from './create'
import { getAndValidateField } from '../utils/input-utils'
import chalk from 'chalk'
import path from 'path'
Expand Down
6 changes: 3 additions & 3 deletions src/sasjs-build/index.js → src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as thisModule from './index'
import * as thisModule from './build'

import find from 'find'
import path from 'path'
import chalk from 'chalk'
import uniqBy from 'lodash.uniqby'
import groupBy from 'lodash.groupby'
import { deploy } from '../sasjs-deploy'
import { createWebAppServices } from '../sasjs-web'
import { deploy } from './deploy'
import { createWebAppServices } from './web'
import {
readFile,
getSubFoldersInFolder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'

/**
* Creates compute context using provided config.
Expand Down
2 changes: 1 addition & 1 deletion src/sasjs-context/edit.js → src/commands/context/edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'

/**
* Edits existing compute context.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'
import path from 'path'
import { createFile, sanitizeFileName } from '../utils/file-utils'
import { createFile, sanitizeFileName } from '../../utils/file-utils'

/**
* Export compute context to json file in current folder.
Expand Down
101 changes: 32 additions & 69 deletions src/sasjs-context/index.js → src/commands/context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,68 @@ import { edit } from './edit'
import { remove } from './remove'
import { list } from './list'
import { exportContext } from './export'
import { fileExists, readFile } from '../utils/file-utils'
import { getBuildTarget, getAccessToken } from '../utils/config-utils'
import { displayResult } from '../utils/displayResult'
import { fileExists, readFile } from '../../utils/file-utils'
import { getBuildTarget, getAccessToken } from '../../utils/config-utils'
import { displayResult } from '../../utils/displayResult'
import { Command } from '../../utils/command'
import SASjs from '@sasjs/adapter/node'

export async function processContext(commandLine) {
const command = commandLine[1]
const commands = {
const command = new Command(commandLine)
const subCommand = command.getSubCommand()
const subCommands = {
create: 'create',
edit: 'edit',
delete: 'delete',
list: 'list',
export: 'export'
}

if (!commands.hasOwnProperty(command)) {
if (!subCommands.hasOwnProperty(subCommand)) {
console.log(
chalk.redBright(
`Not supported context command. Supported commands are:\n${Object.keys(
commands
subCommands
).join('\n')}`
)
)

return
}

const commandExample =
'sasjs context <command> --source ../contextConfig.json --target targetName'

let targetName = []
let targetNameFlagIndex = commandLine.indexOf('--target')

if (targetNameFlagIndex === -1)
targetNameFlagIndex = commandLine.indexOf('-t')

if (targetNameFlagIndex !== -1) {
for (let i = targetNameFlagIndex + 1; i < commandLine.length; i++) {
if (commandLine[i] === '--source' || commandLine[i] === '-s') {
throw `Target name has to be provided as the last argument (eg ${commandExample})`
}

targetName.push(commandLine[i])
}
}

targetName = targetName.join(' ')

const targetName = command.getFlagValue('target')
const target = await getBuildTarget(targetName)

let configPath
let config
let parsedConfig
let configPathFlagIndex
const commandExample =
'sasjs context <command> --source ../contextConfig.json --target targetName'

const getConfig = async () => {
configPathFlagIndex = commandLine.indexOf('--source')
const configPath = command.getFlagValue('source')

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

if (configPathFlagIndex === -1) {
console.log(
chalk.redBright(`'--source' flag is missing (eg '${commandExample}')`)
)
console.log(chalk.redBright(message))

return
}
} else if (!validateConfigPath(configPath)) {
const message = `Provide a path to context config file (eg '${commandExample}')`

configPath = commandLine[configPathFlagIndex + 1]

if (!configPath || !validateConfigPath(configPath)) {
console.log(
chalk.redBright(
`Provide a path to context config file (eg '${commandExample}')`
)
)
console.log(chalk.redBright(message))

return
}

return await readFile(configPath)
}

const getContextName = (upToSourceFlag = false) => {
let contextName = ''
const getContextName = () => {
let contextName = command.values.join(' ')

if (targetNameFlagIndex === -1) {
contextName = commandLine.slice(2).join(' ')
} else {
contextName = commandLine
.slice(2, upToSourceFlag ? configPathFlagIndex : targetNameFlagIndex)
.join(' ')
}
if (!contextName) {
const message = `Provide a context name (eg 'sasjs context <command> contextName')`

if (!contextName && !upToSourceFlag) {
console.log(
chalk.redBright(
`Provide a context name (eg 'sasjs context <command> contextName')`
)
)
console.log(chalk.redBright(message))

return null
}
Expand All @@ -122,10 +83,12 @@ export async function processContext(commandLine) {
displayResult(err)
})

let config
let parsedConfig
let output

switch (command) {
case commands.create:
switch (subCommand) {
case subCommands.create:
config = await getConfig()

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

break
case commands.edit: {
case subCommands.edit: {
config = await getConfig()

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

break
}
case commands.delete: {
case subCommands.delete: {
const contextName = getContextName()

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

break
}
case commands.list:
case subCommands.list:
output = list(target, sasjs, accessToken)

break
case commands.export: {
case subCommands.export: {
const contextName = getContextName()

if (contextName) {
Expand Down
2 changes: 1 addition & 1 deletion src/sasjs-context/list.js → src/commands/context/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import ora from 'ora'
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'

/**
* Lists all accessible and inaccessible compute contexts.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'

/**
* Removes compute context.
Expand Down
2 changes: 1 addition & 1 deletion src/sasjs-create/index.js → src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../utils/file-utils'
import chalk from 'chalk'

export async function create(parentFolderName = '.', appType = '') {
export async function create(parentFolderName, appType) {
const configPath =
appType === 'sasonly' ? '../config-sasonly.json' : '../config.json'
const config = await getConfiguration(path.join(__dirname, configPath))
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk'
import { displayResult } from '../utils/displayResult'
import { displayResult } from '../../utils/displayResult'

/**
* Creates folder.
Expand Down
Loading

0 comments on commit fbb5dee

Please sign in to comment.