diff --git a/package-lock.json b/package-lock.json index 2b1174680..29af8d264 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1899,9 +1899,9 @@ } }, "@sasjs/core": { - "version": "1.7.3", - "resolved": "https://registry.npmjs.org/@sasjs/core/-/core-1.7.3.tgz", - "integrity": "sha512-iXlFQoYiJUIMISftOL03VLCW9LRWG2dmflA0xMWVnW03Oy0eldjnc5Uhf7nYYfrIz682pWG613Fz+ZDbP2XV/w==" + "version": "1.7.4", + "resolved": "https://registry.npmjs.org/@sasjs/core/-/core-1.7.4.tgz", + "integrity": "sha512-biOOxz7+glYE7a1ZtkmONshyEjwixPVxYw92mD+psFOcNu7KJzfC18pevFzhQl52hveTWzLKVJJHs/rLglZguA==" }, "@semantic-release/commit-analyzer": { "version": "8.0.1", diff --git a/package.json b/package.json index b7a102bbc..e8454d04f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cli.js b/src/cli.js index 851f2dd13..336fe37ad 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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 } } @@ -170,10 +160,7 @@ 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': { @@ -181,14 +168,11 @@ export async function cli(args) { 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': { @@ -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 } diff --git a/src/sasjs-add/index.js b/src/commands/add.js similarity index 99% rename from src/sasjs-add/index.js rename to src/commands/add.js index 21016c983..9f7f49573 100644 --- a/src/sasjs-add/index.js +++ b/src/commands/add.js @@ -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' diff --git a/src/sasjs-build/index.js b/src/commands/build.js similarity index 99% rename from src/sasjs-build/index.js rename to src/commands/build.js index b7424e591..2538888f7 100644 --- a/src/sasjs-build/index.js +++ b/src/commands/build.js @@ -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, diff --git a/src/sasjs-context/create.js b/src/commands/context/create.js similarity index 94% rename from src/sasjs-context/create.js rename to src/commands/context/create.js index bbd2fffbd..8c59986f5 100644 --- a/src/sasjs-context/create.js +++ b/src/commands/context/create.js @@ -1,4 +1,4 @@ -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Creates compute context using provided config. diff --git a/src/sasjs-context/edit.js b/src/commands/context/edit.js similarity index 93% rename from src/sasjs-context/edit.js rename to src/commands/context/edit.js index 0c9401b4a..a9ba1ea50 100644 --- a/src/sasjs-context/edit.js +++ b/src/commands/context/edit.js @@ -1,4 +1,4 @@ -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Edits existing compute context. diff --git a/src/sasjs-context/export.js b/src/commands/context/export.js similarity index 91% rename from src/sasjs-context/export.js rename to src/commands/context/export.js index 5842d2399..4c1e2e368 100644 --- a/src/sasjs-context/export.js +++ b/src/commands/context/export.js @@ -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. diff --git a/src/sasjs-context/index.js b/src/commands/context/index.js similarity index 53% rename from src/sasjs-context/index.js rename to src/commands/context/index.js index 01e5209b9..406f043ce 100644 --- a/src/sasjs-context/index.js +++ b/src/commands/context/index.js @@ -4,14 +4,16 @@ 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', @@ -19,11 +21,11 @@ export async function processContext(commandLine) { 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')}` ) ) @@ -31,56 +33,25 @@ export async function processContext(commandLine) { return } - const commandExample = - 'sasjs context --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 --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 } @@ -88,23 +59,13 @@ export async function processContext(commandLine) { 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 contextName')` - if (!contextName && !upToSourceFlag) { - console.log( - chalk.redBright( - `Provide a context name (eg 'sasjs context contextName')` - ) - ) + console.log(chalk.redBright(message)) return null } @@ -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 @@ -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) @@ -146,7 +109,7 @@ export async function processContext(commandLine) { break } - case commands.delete: { + case subCommands.delete: { const contextName = getContextName() if (contextName) { @@ -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) { diff --git a/src/sasjs-context/list.js b/src/commands/context/list.js similarity index 97% rename from src/sasjs-context/list.js rename to src/commands/context/list.js index 72213488d..2c46dbaa4 100644 --- a/src/sasjs-context/list.js +++ b/src/commands/context/list.js @@ -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. diff --git a/src/sasjs-context/remove.js b/src/commands/context/remove.js similarity index 92% rename from src/sasjs-context/remove.js rename to src/commands/context/remove.js index 2e9534694..88e879a6e 100644 --- a/src/sasjs-context/remove.js +++ b/src/commands/context/remove.js @@ -1,4 +1,4 @@ -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Removes compute context. diff --git a/src/sasjs-create/index.js b/src/commands/create.js similarity index 96% rename from src/sasjs-create/index.js rename to src/commands/create.js index 6b5058c64..c5dfb8fc9 100644 --- a/src/sasjs-create/index.js +++ b/src/commands/create.js @@ -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)) diff --git a/src/sasjs-db/index.js b/src/commands/db.js similarity index 100% rename from src/sasjs-db/index.js rename to src/commands/db.js diff --git a/src/sasjs-deploy/index.js b/src/commands/deploy.js similarity index 100% rename from src/sasjs-deploy/index.js rename to src/commands/deploy.js diff --git a/src/sasjs-folder/create.js b/src/commands/folder/create.js similarity index 95% rename from src/sasjs-folder/create.js rename to src/commands/folder/create.js index c856860c5..59761fda2 100644 --- a/src/sasjs-folder/create.js +++ b/src/commands/folder/create.js @@ -1,5 +1,5 @@ import chalk from 'chalk' -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Creates folder. diff --git a/src/commands/folder/index.js b/src/commands/folder/index.js new file mode 100644 index 000000000..cb69076ae --- /dev/null +++ b/src/commands/folder/index.js @@ -0,0 +1,72 @@ +import SASjs from '@sasjs/adapter/node' +import chalk from 'chalk' +import { getBuildTarget, getAccessToken } from '../../utils/config-utils' +import { displayResult } from '../../utils/displayResult' +import { create } from './create' +import { move } from './move' +import { remove } from './remove' +import { Command } from '../../utils/command' + +export async function folder(commandLine) { + const command = new Command(commandLine) + const subCommand = command.getSubCommand() + + const subCommands = { + create: 'create', + move: 'move', + delete: 'delete' + } + + if (!subCommands.hasOwnProperty(subCommand)) { + const message = `Not supported folder command. Supported commands are:\n${Object.keys( + subCommands + ).join('\n')}` + + console.log(chalk.redBright(message)) + + return + } + + const forceFlag = command.getFlag('force') + const targetName = command.getFlagValue('target') + let folderPath = command.values.join(' ') + + const target = await getBuildTarget(targetName) + + if (!folderPath) { + console.log( + chalk.redBright( + `Please provide folder path (eg 'sasjs folder /Public/folder').` + ) + ) + + return + } + + folderPath = command.prefixAppLoc(target.appLoc, folderPath) + + const sasjs = new SASjs({ + serverUrl: target.serverUrl, + serverType: target.serverType + }) + + const accessToken = await getAccessToken(target).catch((err) => + displayResult(err) + ) + + switch (subCommand) { + case subCommands.create: + return await create( + folderPath, + sasjs, + accessToken, + forceFlag !== undefined + ) + case subCommands.delete: + return await remove(folderPath, sasjs, accessToken) + case subCommands.move: + return await move(folderPath, sasjs, accessToken) + default: + break + } +} diff --git a/src/sasjs-folder/move.js b/src/commands/folder/move.js similarity index 94% rename from src/sasjs-folder/move.js rename to src/commands/folder/move.js index f9ba94573..2a3d7f0b9 100644 --- a/src/sasjs-folder/move.js +++ b/src/commands/folder/move.js @@ -1,4 +1,4 @@ -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Moves folder to a new location. diff --git a/src/sasjs-folder/remove.js b/src/commands/folder/remove.js similarity index 91% rename from src/sasjs-folder/remove.js rename to src/commands/folder/remove.js index da03e6e57..cac321feb 100644 --- a/src/sasjs-folder/remove.js +++ b/src/commands/folder/remove.js @@ -1,4 +1,4 @@ -import { displayResult } from '../utils/displayResult' +import { displayResult } from '../../utils/displayResult' /** * Deletes folder. diff --git a/src/sasjs-help/index.js b/src/commands/help.js similarity index 100% rename from src/sasjs-help/index.js rename to src/commands/help.js diff --git a/src/commands/index.js b/src/commands/index.js new file mode 100644 index 000000000..e52004d3a --- /dev/null +++ b/src/commands/index.js @@ -0,0 +1,38 @@ +export { addTarget } from './add' + +export { + build, + loadDependencies, + getServiceVars, + getBuildVars, + getProgramList, + validateProgramsList, + validateFileRef, + getProgramDependencies, + getDependencyPaths, + prioritiseDependencyOverrides +} from './build' + +export { processContext } from './context' + +export { create } from './create' + +export { buildDB } from './db' + +export { deploy } from './deploy' + +export { folder } from './folder' + +export { printHelpText } from './help' + +export { processJob } from './job' + +export { runSasJob } from './request' + +export { runSasCode } from './run' + +export { processServicepack } from './servicepack' + +export { printVersion } from './version' + +export { createWebAppServices } from './web' diff --git a/src/sasjs-job/execute.js b/src/commands/job/execute.js similarity index 96% rename from src/sasjs-job/execute.js rename to src/commands/job/execute.js index 7e991ddda..53aaeeb0a 100644 --- a/src/sasjs-job/execute.js +++ b/src/commands/job/execute.js @@ -1,7 +1,7 @@ import chalk from 'chalk' import ora from 'ora' -import { displayResult } from '../utils/displayResult' -import { createFile, createFolder, folderExists } from '../utils/file-utils' +import { displayResult } from '../../utils/displayResult' +import { createFile, createFolder, folderExists } from '../../utils/file-utils' import path from 'path' /** @@ -69,7 +69,7 @@ export async function execute( : `Job session`) + ` can be found at ${target.serverUrl + sessionLink}` ) - if (output || log) { + if (output !== undefined || log) { try { const outputJson = JSON.stringify(submittedJob, null, 2) diff --git a/src/sasjs-job/index.js b/src/commands/job/index.js similarity index 56% rename from src/sasjs-job/index.js rename to src/commands/job/index.js index 171269992..81e864fd2 100644 --- a/src/sasjs-job/index.js +++ b/src/commands/job/index.js @@ -3,16 +3,16 @@ import { getBuildTarget, getAccessToken, sanitizeAppLoc -} from '../utils/config-utils' -import { displayResult } from '../utils/displayResult' +} from '../../utils/config-utils' +import { displayResult } from '../../utils/displayResult' import SASjs from '@sasjs/adapter/node' import { execute } from './execute' -import { Command } from '../utils/command' +import { Command } from '../../utils/command' export async function processJob(commandLine) { const command = new Command(commandLine) - const subCommand = command.values.shift() + const subCommand = command.getSubCommand() const subCommands = { execute: 'execute' } @@ -29,29 +29,14 @@ export async function processJob(commandLine) { return } - let targetName = command.flags.find((flag) => flag.name === 'target') - targetName = targetName ? targetName.value : '' - - const waitForJob = - command.flags.find((flag) => flag.name === 'wait') !== undefined - - let output = command.flags.find((flag) => flag.name === 'output') - output = output ? (output.value ? output.value : true) : null - - let log = command.flags.find((flag) => flag.name === 'log') - log = log ? (log.value ? log.value : true) : null + const targetName = command.getFlagValue('target') + const waitForJob = command.getFlagValue('wait') + const output = command.getFlagValue('output') + const log = command.getFlagValue('log') const target = await getBuildTarget(targetName) - let jobPath = command.values.join('') - - if (!/^\//.test(jobPath)) { - const { appLoc } = target - - jobPath = (/\/$/.test(appLoc) ? appLoc : appLoc + '/') + jobPath - } - - jobPath = sanitizeAppLoc(jobPath) + const jobPath = command.prefixAppLoc(target.appLoc, command.values) const sasjs = new SASjs({ serverUrl: target.serverUrl, diff --git a/src/sasjs-request/index.js b/src/commands/request.js similarity index 88% rename from src/sasjs-request/index.js rename to src/commands/request.js index 0144ea989..99681739a 100644 --- a/src/sasjs-request/index.js +++ b/src/commands/request.js @@ -9,13 +9,15 @@ import { } from '../utils/file-utils' import { getAccessToken } from '../utils/config-utils' import { displayResult } from '../utils/displayResult' +import { Command } from '../utils/command' + +export async function runSasJob(commandLine) { + const command = new Command(commandLine) + const sasJobLocation = command.values.shift() + const dataFilePath = command.getFlagValue('datafile') + const configFilePath = command.getFlagValue('configfile') + const targetName = command.getFlagValue('target') -export async function runSasJob( - sasJobLocation, - dataFilePath, - configFilePath, - targetName -) { const { target, isLocal } = await findTargetInConfiguration(targetName, true) if (!target) { throw new Error('Target not found! Please try again with another target.') diff --git a/src/sasjs-run/index.js b/src/commands/run.js similarity index 95% rename from src/sasjs-run/index.js rename to src/commands/run.js index d5fb6b5cb..e3af1b204 100644 --- a/src/sasjs-run/index.js +++ b/src/commands/run.js @@ -7,13 +7,18 @@ import { } from '../utils/config-utils' import { readFile, createFile } from '../utils/file-utils' import { getVariable, generateTimestamp } from '../utils/utils' +import { Command } from '../utils/command' /** * Runs SAS code from a given file on the specified target. * @param {string} filePath - the path to the file containing SAS code. * @param {string} targetName - the name of the target to run the SAS code on. */ -export async function runSasCode(filePath, targetName) { +export async function runSasCode(commandLine) { + const command = new Command(commandLine) + const filePath = command.values.shift() + const targetName = command.getFlagValue('target') + if (!/\.sas$/i.test(filePath)) { throw new Error(`'sasjs run' command supports only *.sas files.`) } diff --git a/src/sasjs-servicepack/deploy.js b/src/commands/servicepack/deploy.js similarity index 92% rename from src/sasjs-servicepack/deploy.js rename to src/commands/servicepack/deploy.js index 7ed63e815..a750c7633 100644 --- a/src/sasjs-servicepack/deploy.js +++ b/src/commands/servicepack/deploy.js @@ -1,12 +1,12 @@ import path from 'path' import SASjs from '@sasjs/adapter/node' import chalk from 'chalk' -import { readFile } from '../utils/file-utils' -import { displayResult } from '../utils/displayResult' +import { readFile } from '../../utils/file-utils' +import { displayResult } from '../../utils/displayResult' import { getAccessToken, findTargetInConfiguration -} from '../utils/config-utils' +} from '../../utils/config-utils' export async function servicePackDeploy( jsonFilePath = null, @@ -67,7 +67,7 @@ async function deployToSasViyaWithServicePack( serverType: buildTarget.serverType }) - const CONSTANTS = require('../constants') + const CONSTANTS = require('../../constants') const buildDestinationFolder = CONSTANTS.buildDestinationFolder const finalFilePathJSON = path.join( diff --git a/src/commands/servicepack/index.js b/src/commands/servicepack/index.js new file mode 100644 index 000000000..b92755f71 --- /dev/null +++ b/src/commands/servicepack/index.js @@ -0,0 +1,39 @@ +import { servicePackDeploy } from './deploy' +import { Command } from '../../utils/command' + +import chalk from 'chalk' + +export async function processServicepack(commandLine) { + const command = new Command(commandLine) + const subCommand = command.getSubCommand() + const subCommands = { + deploy: 'deploy' + } + + if (!subCommands.hasOwnProperty(subCommand)) { + console.log( + chalk.redBright( + `Not supported servicepack command. Supported commands are:\n${Object.keys( + subCommands + ).join('\n')}` + ) + ) + + return + } + + const targetName = command.getFlagValue('target') + const jsonFilePath = command.getFlagValue('source') + const isForced = command.getFlagValue('force') + + let output + + switch (subCommand) { + case subCommands.deploy: + output = await servicePackDeploy(jsonFilePath, targetName, isForced) + + break + } + + return output +} diff --git a/src/sasjs-version/index.js b/src/commands/version.js similarity index 100% rename from src/sasjs-version/index.js rename to src/commands/version.js diff --git a/src/sasjs-web/index.js b/src/commands/web/index.js similarity index 96% rename from src/sasjs-web/index.js rename to src/commands/web/index.js index 423661044..d9df11ea4 100644 --- a/src/sasjs-web/index.js +++ b/src/commands/web/index.js @@ -1,5 +1,5 @@ -import { findTargetInConfiguration } from '../utils/config-utils' -import { asyncForEach, chunk } from '../utils/utils' +import { findTargetInConfiguration } from '../../utils/config-utils' +import { asyncForEach, chunk } from '../../utils/utils' import { readFile, base64EncodeFile, @@ -9,13 +9,14 @@ import { createFile, deleteFolder, getFilesInFolder -} from '../utils/file-utils' +} from '../../utils/file-utils' import path from 'path' import chalk from 'chalk' import jsdom from 'jsdom' import base64img from 'base64-img' import { sasjsout } from './sasjsout' import btoa from 'btoa' +import { Command } from '../../utils/command' let buildDestinationFolder = '' const permittedServerTypes = { @@ -24,14 +25,23 @@ const permittedServerTypes = { } export async function createWebAppServices( - targetName = null, + commandLine = null, preTargetToBuild = null ) { - const CONSTANTS = require('../constants') + const command = new Command(commandLine) + + let targetName = command.getFlagValue('target') + targetName = targetName ? targetName : null + + const CONSTANTS = require('../../constants') buildDestinationFolder = CONSTANTS.buildDestinationFolder + console.log(chalk.greenBright('Building web app services...')) + await createBuildDestinationFolder() + let targetToBuild = null + if (preTargetToBuild) targetToBuild = preTargetToBuild else { const { target } = await findTargetInConfiguration(targetName) diff --git a/src/sasjs-web/sasjsout.js b/src/commands/web/sasjsout.js similarity index 100% rename from src/sasjs-web/sasjsout.js rename to src/commands/web/sasjsout.js diff --git a/src/main.js b/src/main.js index 8729197ca..5b8ce4877 100644 --- a/src/main.js +++ b/src/main.js @@ -1,25 +1,34 @@ -import { build } from './sasjs-build' -import { deploy } from './sasjs-deploy' -import { processServicepack } from './sasjs-servicepack' -import { buildDB } from './sasjs-db' -import { create } from './sasjs-create' -import { printHelpText } from './sasjs-help' -import { printVersion } from './sasjs-version' -import { createWebAppServices } from './sasjs-web' -import { addTarget } from './sasjs-add' -import { runSasCode } from './sasjs-run' -import { runSasJob } from './sasjs-request' -import { processContext } from './sasjs-context' -import { folder } from './sasjs-folder' -import { processJob } from './sasjs-job' +import { + addTarget, + build, + processContext, + create, + buildDB, + deploy, + folder, + printHelpText, + processJob, + runSasJob, + runSasCode, + processServicepack, + printVersion, + createWebAppServices +} from './commands' import chalk from 'chalk' import { displayResult } from './utils/displayResult' +import { Command } from './utils/command' + +export async function createFileStructure(commandLine) { + const command = new Command(commandLine) + const template = command.getFlagValue('template') + const parentFolderName = command.values.shift() -export async function createFileStructure(parentFolderName, appType) { let result - await create(parentFolderName, appType) + + await create(parentFolderName || '.', template) .then(() => { result = true + console.log( chalk.greenBright.bold.italic( `Project ${ @@ -30,6 +39,7 @@ export async function createFileStructure(parentFolderName, appType) { }) .catch((err) => { result = err + console.log( chalk.redBright( 'An error has occurred whilst creating your project.', @@ -37,6 +47,7 @@ export async function createFileStructure(parentFolderName, appType) { ) ) }) + return result } @@ -48,7 +59,14 @@ export async function showVersion() { await printVersion() } -export async function buildServices(targetName) { +export async function buildServices(commandLine) { + const command = new Command(commandLine) + let targetName = command.getFlagValue('target') + + if (!targetName) { + targetName = command.getTargetWithoutFlag() + } + await build(targetName) .then(() => console.log( @@ -110,7 +128,15 @@ export async function compileServices(targetName) { }) } -export async function deployServices(targetName, isForced) { +export async function deployServices(commandLine) { + const command = new Command(commandLine) + let targetName = command.getFlagValue('target') + const isForced = command.getFlagValue('force') + + if (!targetName) { + targetName = command.getTargetWithoutFlag() + } + await deploy(targetName, null, isForced) .then(() => console.log( @@ -247,8 +273,8 @@ export async function buildDBs() { return result } -export async function buildWebApp(targetName) { - await createWebAppServices(targetName) +export async function buildWebApp(commandLine) { + await createWebAppServices(commandLine) .then(() => console.log( chalk.greenBright.bold.italic( @@ -268,9 +294,11 @@ export async function buildWebApp(targetName) { }) } -export async function add(resourceType = 'target') { +export async function add(commandLine) { + const command = new Command(commandLine) let result = false - if (resourceType === 'target') { + + if (command && command.name === 'add') { await addTarget() .then(() => { console.log(chalk.greenBright('Target successfully added!')) @@ -281,25 +309,22 @@ export async function add(resourceType = 'target') { result = err }) } + return result } -export async function run(filePath, targetName) { - await runSasCode(filePath, targetName).catch((err) => { +export async function run(commandLine) { + await runSasCode(commandLine).catch((err) => { console.log( chalk.redBright('An error has occurred when running your SAS code.', err) ) }) } -export async function runRequest( - sasJobLocation, - dataFilePath, - configFilePath, - targetName -) { +export async function runRequest(commandLine) { let result = false - await runSasJob(sasJobLocation, dataFilePath, configFilePath, targetName) + + await runSasJob(commandLine) .then((res) => (result = res)) .catch((err) => { result = err @@ -307,6 +332,7 @@ export async function runRequest( chalk.redBright('An error has occurred when running your SAS job', err) ) }) + return result } diff --git a/src/sasjs-folder/index.js b/src/sasjs-folder/index.js deleted file mode 100644 index d6f89f87d..000000000 --- a/src/sasjs-folder/index.js +++ /dev/null @@ -1,99 +0,0 @@ -import SASjs from '@sasjs/adapter/node' -import chalk from 'chalk' -import { getBuildTarget, getAccessToken } from '../utils/config-utils' -import { displayResult } from '../utils/displayResult' -import { create } from './create' -import { move } from './move' -import { remove } from './remove' - -export async function folder(commandLine) { - const command = commandLine[1] - const commands = { - create: 'create', - move: 'move', - delete: 'delete' - } - - if (!commands.hasOwnProperty(command)) { - console.log( - chalk.redBright( - `Not supported folder command. Supported commands are:\n${Object.keys( - commands - ).join('\n')}` - ) - ) - - return - } - - let forceFlagIndex = commandLine.indexOf('-f') - - if (forceFlagIndex === -1) forceFlagIndex = commandLine.indexOf('--force') - - let targetName = [] - let targetFlagIndex = commandLine.indexOf('-t') - - if (targetFlagIndex === -1) targetFlagIndex = commandLine.indexOf('--target') - - if (targetFlagIndex !== -1) { - for (let i = targetFlagIndex + 1; i < commandLine.length; i++) { - if (i === forceFlagIndex) break - - targetName.push(commandLine[i]) - } - } - - targetName = targetName.join(' ') - - const target = await getBuildTarget(targetName) - - let folderPath = '' - - if (targetFlagIndex === -1 && forceFlagIndex === -1) { - folderPath = commandLine.slice(2).join(' ') - } else if (targetFlagIndex === -1) { - folderPath = commandLine.slice(2, forceFlagIndex).join(' ') - } else if (forceFlagIndex === -1) { - folderPath = commandLine.slice(2, targetFlagIndex).join(' ') - } else { - folderPath = commandLine - .slice( - 2, - targetFlagIndex > forceFlagIndex ? forceFlagIndex : targetFlagIndex - ) - .join(' ') - } - - if (!folderPath) { - console.log( - chalk.redBright( - `Please provide folder path (eg 'sasjs folder /Public/folder').` - ) - ) - - return - } - - const sasjs = new SASjs({ - serverUrl: target.serverUrl, - serverType: target.serverType - }) - - // Folder paths not starting with a slash are prefixed with the appLoc - if (!/^\//.test(folderPath)) folderPath = `${target.appLoc}/${folderPath}` - - const accessToken = await getAccessToken(target).catch((err) => - displayResult(err) - ) - - switch (command) { - case commands.create: - return await create(folderPath, sasjs, accessToken, forceFlagIndex !== -1) - case commands.delete: - return await remove(folderPath, sasjs, accessToken) - case commands.move: - return await move(folderPath, sasjs, accessToken) - default: - break - } -} diff --git a/src/sasjs-servicepack/index.js b/src/sasjs-servicepack/index.js deleted file mode 100644 index 826a2cfd4..000000000 --- a/src/sasjs-servicepack/index.js +++ /dev/null @@ -1,54 +0,0 @@ -import { servicePackDeploy } from './deploy' -import { - getCommandParameter, - getCommandParameterLastMultiWord, - isFlagPresent -} from '../utils/command-utils' - -import chalk from 'chalk' - -export async function processServicepack(commandLine) { - const command = commandLine[1] - const commands = { - deploy: 'deploy' - } - - if (!commands.hasOwnProperty(command)) { - console.log( - chalk.redBright( - `Not supported servicepack command. Supported commands are:\n${Object.keys( - commands - ).join('\n')}` - ) - ) - - return - } - - const commandExample = - 'sasjs servicepack --source ../viyadeploy.json --target targetName' - - let output - - switch (command) { - case commands.deploy: - const targetName = getCommandParameterLastMultiWord( - '-t', - '--target', - commandLine, - commandExample - ) - const jsonFilePath = getCommandParameter( - '-s', - '--source', - commandLine, - commandExample - ) - const isForced = isFlagPresent('-f', commandLine) - - output = await servicePackDeploy(jsonFilePath, targetName, isForced) - break - } - - return output -} diff --git a/src/utils/command-utils.js b/src/utils/command-utils.js deleted file mode 100644 index a0ddb8bf0..000000000 --- a/src/utils/command-utils.js +++ /dev/null @@ -1,64 +0,0 @@ -import chalk from 'chalk' - -export function isFlagPresent(flag, commandLine) { - return commandLine.indexOf(flag) > -1 -} - -export function getCommandParameter( - commandFlag, - commandFlagLong, - commandLine, - commandExample = '' -) { - let parameterValueFlagIndex = commandLine.indexOf(commandFlagLong) - - if (parameterValueFlagIndex === -1) - parameterValueFlagIndex = commandLine.indexOf(commandFlag) - - if (parameterValueFlagIndex === -1) { - const errorMessage = chalk.redBright( - `'${commandFlag || commandFlagLong}' flag is missing. ${ - commandExample ? "(eg '" + commandExample + "')" : '' - }` - ) - throw new Error(errorMessage) - - return - } - - let parameterValue = commandLine[parameterValueFlagIndex + 1] - - return parameterValue -} - -export function getCommandParameterLastMultiWord( - commandFlag, - commandFlagLong, - commandLine, - commandExample = '' -) { - let parameterValue = [] - let parameterFlagIndex = commandLine.indexOf(commandFlagLong) - - if (parameterFlagIndex === -1) - parameterFlagIndex = commandLine.indexOf(commandFlag) - - if (parameterFlagIndex !== -1) { - for (let i = parameterFlagIndex + 1; i < commandLine.length; i++) { - if (commandLine[i].startsWith('-')) { - const errorMessage = `Parameter '${ - commandFlagLong || commandFlag - }' has to be provided as the last argument ${ - commandExample ? "(eg '" + commandExample + "')" : '' - }` - throw new Error(errorMessage) - } - - parameterValue.push(commandLine[i]) - } - } - - parameterValue = parameterValue.join(' ') - - return parameterValue -} diff --git a/src/utils/command.js b/src/utils/command.js index 75c8d1f9f..dc42cd7c9 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -1,4 +1,6 @@ import { displayResult } from './displayResult' +import { arrToObj } from './utils' +import chalk from 'chalk' const showInvalidCommandMessage = () => { displayResult( @@ -6,7 +8,17 @@ const showInvalidCommandMessage = () => { `Invalid command. Run 'sasjs help' to get the list of valid commands.` ) } -const arrToObj = (arr) => arr.reduce((o, key) => ({ ...o, [key]: key }), {}) + +const showInvalidFlagMessage = (flagMessage, supportedFlags) => { + displayResult( + {}, + `${flagMessage}${ + supportedFlags + ? ` Supported flags are:\n${supportedFlags.join('\n')}` + : '' + }` + ) +} const initialCommands = arrToObj([ ...new Set([ @@ -78,7 +90,7 @@ const commandFlags = [ }, { command: initialCommands.servicepack, - flags: [initialFlags.target, initialFlags.source] + flags: [initialFlags.target, initialFlags.source, initialFlags.force] }, { command: initialCommands.run, flags: [initialFlags.target] }, { @@ -141,9 +153,11 @@ export class Command { this.aliases = initialAliases.find((alias) => alias.name === this.name) this.aliases = this.aliases ? this.aliases.aliases : null - this.supportedFlags = commandFlags.filter( - (commandFlag) => commandFlag.command === this.name - )[0].flags + const supportedFlags = commandFlags.filter( + (flag) => flag.command === this.name + )[0] + + if (supportedFlags) this.supportedFlags = supportedFlags.flags for (let i = 0; i < commandLine.length; i++) { if (/^-/.test(commandLine[i]) && this.supportedFlags) { @@ -154,33 +168,84 @@ export class Command { .filter((f) => regExp.test(f)) .filter((f) => this.supportedFlags.includes(f))[0] - flag = new Flag(flag) + try { + flag = new Flag(flag) - this.flags.push(flag) + this.flags.push(flag) - if (flag.withValue) { - if (/^-/.test(commandLine[i + 1])) continue + if (flag.withValue) { + if (/^-/.test(commandLine[i + 1])) continue - i++ + i++ - const value = commandLine[i] + const value = commandLine[i] - if (value) { - this.flags.find((f) => f.name === flag.name).setValue(value) + if (value) { + this.flags.find((f) => f.name === flag.name).setValue(value) + } } + } catch (error) { + showInvalidFlagMessage(error, this.supportedFlags) } } else { this.values.push(commandLine[i]) } } } + + getSubCommand() { + return this.values.shift() + } + + getFlag(flagName) { + return this.flags.find((flag) => flag.name === flagName) + } + + getFlagValue(flagName) { + const flag = this.getFlag(flagName) + + if (!flag) return undefined + if (!flag.withValue) return true + + return flag.value + } + + prefixAppLoc(appLoc = '', path = '') { + if (!path) return null + + if (!/^\//.test(appLoc)) appLoc = '/' + appLoc + + if (Array.isArray(path)) path = path.join(' ') + + return path + .split(' ') + .map((p) => (/^\//.test(p) ? path : `${appLoc}/${p}`)) + .join(' ') + } + + getTargetWithoutFlag() { + const deprecationDate = new Date(2021, 10, 2) + const today = new Date() + + if (today < deprecationDate && this.values.length) { + console.log( + chalk.yellowBright( + `WARNING: use --target or -t flag to specify the target name. Specifying the target name without a flag will not be supported starting from November 1, 2021.` + ) + ) + + return this.values.shift() + } + + return undefined + } } class Flag { value = null constructor(name) { - if (!name || typeof name !== 'string') throw 'Not valid flag name!' + if (!name || typeof name !== 'string') throw `Not valid flag name!` this.name = name this.longSyntax = '--' + name diff --git a/src/utils/config-utils.js b/src/utils/config-utils.js index b887344bd..08e4688e6 100644 --- a/src/utils/config-utils.js +++ b/src/utils/config-utils.js @@ -10,11 +10,15 @@ import path from 'path' import chalk from 'chalk' export async function getConfiguration(pathToFile) { - const config = await readFile(pathToFile, false, true).catch(() => null) + const config = await readFile(pathToFile, false, true).catch((err) => + console.log(err) + ) + if (config) { const configJson = JSON.parse(config) return Promise.resolve(configJson.config ? configJson.config : configJson) } + return Promise.resolve(null) } diff --git a/src/utils/displayResult.js b/src/utils/displayResult.js index 9cb8aad55..8bd002125 100644 --- a/src/utils/displayResult.js +++ b/src/utils/displayResult.js @@ -19,7 +19,9 @@ export function displayResult(err, failureMessage, successMessage) { console.log(chalk.redBright(failureMessage, err.body)) } } else { - console.log(chalk.redBright(failureMessage, err)) + console.log( + chalk.redBright(failureMessage, Object.keys(err).length ? err : '') + ) } } diff --git a/src/utils/utils.js b/src/utils/utils.js index 701bf6aad..7399a4096 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -231,3 +231,6 @@ export function generateTimestamp() { return timestamp } + +export const arrToObj = (arr) => + arr.reduce((o, key) => ({ ...o, [key]: key }), {}) diff --git a/test/command-utils.spec.js b/test/command-utils.spec.js deleted file mode 100644 index a3443b197..000000000 --- a/test/command-utils.spec.js +++ /dev/null @@ -1,135 +0,0 @@ -import { - isFlagPresent, - getCommandParameter, - getCommandParameterLastMultiWord -} from '../src/utils/command-utils' - -const mockCommandLine = [ - 'test', - '-f', - '-t2', - 'testParam', - '--test3', - 'testLongParam', - '-l', - 'multi', - 'word' -] - -describe('isFlagPresent', () => { - test('passed existing flag', () => { - let flagPresent = isFlagPresent('-f', mockCommandLine) - - expect(flagPresent).toEqual(true) - }) - - test('passed non existing flag', () => { - let flagPresent = isFlagPresent('-a', mockCommandLine) - - expect(flagPresent).toEqual(false) - }) - - test('passed empty string', () => { - let flagPresent = isFlagPresent('', mockCommandLine) - - expect(flagPresent).toEqual(false) - }) - - test('passed null', () => { - let flagPresent = isFlagPresent(null, mockCommandLine) - - expect(flagPresent).toEqual(false) - }) - - test('passed undefined', () => { - let flagPresent = isFlagPresent(undefined, mockCommandLine) - - expect(flagPresent).toEqual(false) - }) -}) - -describe('getCommandParameter', () => { - test('passed short flag and long flag', () => { - let parameter = getCommandParameter('-t', '--test3', mockCommandLine) - - expect(parameter).toEqual('testLongParam') - }) - - test('passed short flag, without long flag', () => { - let parameter = getCommandParameter('-t2', null, mockCommandLine) - - expect(parameter).toEqual('testParam') - }) - - test('passed long flag, without short flag', () => { - let parameter = getCommandParameter(null, '--test3', mockCommandLine) - - expect(parameter).toEqual('testLongParam') - }) - - test('should throw and error when passed non-existing flags', () => { - expect(() => getCommandParameter('-n', '--non', mockCommandLine)).toThrow() - }) -}) - -describe('getCommandParameterLastMultiWord', () => { - test('passed short flag and long flag', () => { - let parameter = getCommandParameterLastMultiWord( - '-l', - '--last', - mockCommandLine - ) - - expect(parameter).toEqual('multi word') - }) - - test('passed short flag without long flag', () => { - let parameter = getCommandParameterLastMultiWord( - '-l', - null, - mockCommandLine - ) - - expect(parameter).toEqual('multi word') - }) - - test('passed long flag without short flag', () => { - let mockCommandLineCustom = [ - 'test', - '-f', - '-t2', - 'testParam', - '--test3', - 'testLongParam', - '--last', - 'multi', - 'word' - ] - - let parameter = getCommandParameterLastMultiWord( - null, - '--last', - mockCommandLineCustom - ) - - expect(parameter).toEqual('multi word') - }) - - test('throws an error when passed non-existing flags', () => { - let parameter = getCommandParameterLastMultiWord( - '-n', - '--non', - mockCommandLine - ) - - expect(parameter).toEqual('') - }) - - test('should throw and error when argument is not the last one', () => { - const input = ['-t', 'target', '-s', 'source'] - - expect(() => - getCommandParameterLastMultiWord('-t', '--target', input) - ).toThrow() - }) -}) diff --git a/test/commands/add/sasjs-add.spec.js b/test/commands/add.spec.js similarity index 90% rename from test/commands/add/sasjs-add.spec.js rename to test/commands/add.spec.js index 93c82157e..aa52c113a 100644 --- a/test/commands/add/sasjs-add.spec.js +++ b/test/commands/add.spec.js @@ -1,12 +1,9 @@ import dotenv from 'dotenv' import path from 'path' -import { add } from '../../../src/main' -import { - getConfiguration, - getGlobalRcFile -} from '../../../src/utils/config-utils' -import { deleteFolder } from '../../../src/utils/file-utils' -import { generateTimestamp } from '../../../src/utils/utils' +import { add } from '../../src/main' +import { getConfiguration, getGlobalRcFile } from '../../src/utils/config-utils' +import { deleteFolder } from '../../src/utils/file-utils' +import { generateTimestamp } from '../../src/utils/utils' describe('sasjs add', () => { const testingAppFolder = 'cli-tests-add' @@ -51,9 +48,9 @@ describe('sasjs add', () => { stdin.send([`1\r`]) }, 1000) - await expect(add()).resolves.toEqual(true) + await expect(add('add')).resolves.toEqual(true) - const buildSourceFolder = require('../../../src/constants') + const buildSourceFolder = require('../../src/constants') .buildSourceFolder const config = await getConfiguration( path.join(buildSourceFolder, 'sasjsconfig.json') @@ -105,7 +102,7 @@ describe('sasjs add', () => { stdin.send([`1\r`]) }, 1000) - await expect(add()).resolves.toEqual(true) + await expect(add('add')).resolves.toEqual(true) const config = await getGlobalRcFile() diff --git a/test/commands/cbd/sasjs-cbd.spec.js b/test/commands/cbd/cbd.spec.js similarity index 100% rename from test/commands/cbd/sasjs-cbd.spec.js rename to test/commands/cbd/cbd.spec.js diff --git a/test/commands/compile/getDependencyPaths.spec.js b/test/commands/compile/getDependencyPaths.spec.js index 88e0bff26..467cf46ac 100644 --- a/test/commands/compile/getDependencyPaths.spec.js +++ b/test/commands/compile/getDependencyPaths.spec.js @@ -3,7 +3,7 @@ import { readFile } from '../../../src/utils/file-utils' import { getDependencyPaths, prioritiseDependencyOverrides -} from '../../../src/sasjs-build/index' +} from '../../../src/commands/build' process.projectDir = path.join(process.cwd()) describe('getDependencyPaths', () => { diff --git a/test/commands/compile/getProgramDependencies.spec.js b/test/commands/compile/getProgramDependencies.spec.js index a8c9b712b..2f875fe13 100644 --- a/test/commands/compile/getProgramDependencies.spec.js +++ b/test/commands/compile/getProgramDependencies.spec.js @@ -5,7 +5,7 @@ import { getProgramList, validateFileRef, validateProgramsList -} from '../../../src/sasjs-build/index' +} from '../../../src/commands/build' process.projectDir = path.join(process.cwd()) diff --git a/test/commands/compile/loadDependencies.spec.js b/test/commands/compile/loadDependencies.spec.js index 810d508ab..c7384d817 100644 --- a/test/commands/compile/loadDependencies.spec.js +++ b/test/commands/compile/loadDependencies.spec.js @@ -1,5 +1,5 @@ import path from 'path' -import * as compileModule from '../../../src/sasjs-build/index' +import * as compileModule from '../../../src/commands/build' const fakeServiceInit = `/** @file serviceinit.sas diff --git a/test/commands/context/sasjs-context.spec.js b/test/commands/context.spec.js similarity index 76% rename from test/commands/context/sasjs-context.spec.js rename to test/commands/context.spec.js index 78752aac9..78ff7e8be 100644 --- a/test/commands/context/sasjs-context.spec.js +++ b/test/commands/context.spec.js @@ -1,11 +1,11 @@ import dotenv from 'dotenv' import path from 'path' -import { processContext } from '../../../src/sasjs-context/index' +import { processContext } from '../../src/commands' import { sanitizeFileName, readFile, createFile -} from '../../../src/utils/file-utils' +} from '../../src/utils/file-utils' let contexts let testContextConfig @@ -37,9 +37,7 @@ describe('sasjs context', () => { it( 'should list accessible compute contexts', async () => { - const command = ['context', 'list', '-t', targetName] - - contexts = await processContext(command) + contexts = await processContext(`context list -t ${targetName}`) expect(contexts.length).toBeGreaterThan(0) }, @@ -52,7 +50,7 @@ describe('sasjs context', () => { 'should exports compute context', async () => { const contextName = contexts[0].name - const command = ['context', 'export', contextName, '-t', targetName] + const command = `context export ${contextName} -t ${targetName}` await expect(processContext(command)).resolves.toEqual(true) }, @@ -80,14 +78,7 @@ describe('sasjs context', () => { await createFile(testContextConfigPath, contextConfig) - const command = [ - 'context', - 'create', - '-s', - testContextConfigFile, - '-t', - targetName - ] + const command = `context create -s ${testContextConfigFile} -t targetName` await expect(processContext(command)).resolves.toEqual(true) }, @@ -105,15 +96,7 @@ describe('sasjs context', () => { await createFile(testContextConfigPath, contextConfig) - const command = [ - 'context', - 'edit', - ...testContextConfig.name.split(' '), - '-s', - testContextConfigFile, - '-t', - targetName - ] + const command = `context edit ${testContextConfig.name} -s ${testContextConfigFile} -t ${targetName}` await expect(processContext(command)).resolves.toEqual(true) }, @@ -125,13 +108,7 @@ describe('sasjs context', () => { it( 'should delete compute context', async () => { - const command = [ - 'context', - 'delete', - ...testContextConfig.name.split(' '), - '-t', - targetName - ] + const command = `context delete ${testContextConfig.name} -t targetName` await expect(processContext(command)).resolves.toEqual(true) }, diff --git a/test/commands/create/sasjs-create.spec.js b/test/commands/create.spec.js similarity index 83% rename from test/commands/create/sasjs-create.spec.js rename to test/commands/create.spec.js index 9d3f4abd6..8eb5ce4d7 100644 --- a/test/commands/create/sasjs-create.spec.js +++ b/test/commands/create.spec.js @@ -1,9 +1,9 @@ import dotenv from 'dotenv' import path from 'path' import rimraf from 'rimraf' -import { createFileStructure } from '../../../src/main' -import { createFolder } from '../../../src/utils/file-utils' -import { generateTimestamp } from '../../../src/utils/utils' +import { createFileStructure } from '../../src/main' +import { createFolder } from '../../src/utils/file-utils' +import { generateTimestamp } from '../../src/utils/utils' describe('sasjs create', () => { beforeAll(() => { @@ -24,9 +24,7 @@ describe('sasjs create', () => { await createFolder(process.projectDir) - await expect( - createFileStructure(undefined, undefined) - ).resolves.toEqual(true) + await expect(createFileStructure('sasjs create')).resolves.toEqual(true) await verifyCreate({ parentFolderName: '.' }) }, @@ -46,7 +44,9 @@ describe('sasjs create', () => { await createFolder(process.projectDir) - await expect(createFileStructure('.', undefined)).resolves.toEqual(true) + await expect(createFileStructure('sasjs create .')).resolves.toEqual( + true + ) await verifyCreate({ parentFolderName: '.' }) }, @@ -66,7 +66,9 @@ describe('sasjs create', () => { await createFolder(process.projectDir) - await expect(createFileStructure('.', 'sasonly')).resolves.toEqual(true) + await expect( + createFileStructure('sasjs create . -t sasonly') + ).resolves.toEqual(true) await verifyCreate({ parentFolderName: '.', sasonly: true }) }, @@ -86,7 +88,7 @@ describe('sasjs create', () => { await createFolder(process.projectDir) - await expect(createFileStructure(undefined, 'react')).resolves.toEqual( + await expect(createFileStructure('create -t react')).resolves.toEqual( true ) @@ -111,7 +113,7 @@ describe('sasjs create', () => { await createFolder(process.projectDir) await expect( - createFileStructure(`${parentFolderNameTimeStamped}`, undefined) + createFileStructure(`create ${parentFolderNameTimeStamped}`) ).resolves.toEqual(true) await verifyCreate({ parentFolderName: parentFolderNameTimeStamped }) @@ -133,7 +135,9 @@ describe('sasjs create', () => { await createFolder(process.projectDir) await expect( - createFileStructure(`${parentFolderNameTimeStamped}`, 'minimal') + createFileStructure( + `create ${parentFolderNameTimeStamped} --template minimal` + ) ).resolves.toEqual(true) await verifyCreateWeb({ @@ -158,7 +162,9 @@ describe('sasjs create', () => { await createFolder(process.projectDir) await expect( - createFileStructure(`${parentFolderNameTimeStamped}`, 'angular') + createFileStructure( + `create ${parentFolderNameTimeStamped} -t angular` + ) ).resolves.toEqual(true) await verifyCreateWeb({ diff --git a/test/commands/db/sasjs-db.spec.js b/test/commands/db.spec.js similarity index 89% rename from test/commands/db/sasjs-db.spec.js rename to test/commands/db.spec.js index efbbbfead..a26367085 100644 --- a/test/commands/db/sasjs-db.spec.js +++ b/test/commands/db.spec.js @@ -1,7 +1,7 @@ import dotenv from 'dotenv' import path from 'path' -import { createFileStructure, buildDBs } from '../../../src/main' -import { createFolder, deleteFolder } from '../../../src/utils/file-utils' +import { createFileStructure, buildDBs } from '../../src/main' +import { createFolder, deleteFolder } from '../../src/utils/file-utils' describe('sasjs db', () => { const testingAppFolder = 'cli-tests-db' diff --git a/test/commands/folder/delete.spec.js b/test/commands/folder/delete.spec.js index 08b1bc7fa..aa0a1d7a9 100644 --- a/test/commands/folder/delete.spec.js +++ b/test/commands/folder/delete.spec.js @@ -1,6 +1,6 @@ import dotenv from 'dotenv' -import { folder } from '../../../src/sasjs-folder/index' -import * as removeModule from '../../../src/sasjs-folder/remove' +import { folder } from '../../../src/commands/folder/index' +import * as removeModule from '../../../src/commands/folder/remove' import { generateTimestamp } from '../../../src/utils/utils' const createConfig = (targetName, timestamp) => ({ @@ -25,7 +25,7 @@ const createConfig = (targetName, timestamp) => ({ tgtDeployScripts: [] }) -jest.mock('../../../src/sasjs-folder/remove') +jest.mock('../../../src/commands/folder/remove') describe('sasjs folder delete', () => { let config diff --git a/test/commands/job/sasjs-job.spec.js b/test/commands/job.spec.js similarity index 91% rename from test/commands/job/sasjs-job.spec.js rename to test/commands/job.spec.js index a277805f6..ad030dc7c 100644 --- a/test/commands/job/sasjs-job.spec.js +++ b/test/commands/job.spec.js @@ -1,8 +1,8 @@ import dotenv from 'dotenv' import path from 'path' -import { processJob } from '../../../src/sasjs-job/index' -import { processContext } from '../../../src/sasjs-context/index' -import { folderExists, fileExists } from '../../../src/utils/file-utils' +import { processJob } from '../../src/commands' +import { processContext } from '../../src/commands' +import { folderExists, fileExists } from '../../src/utils/file-utils' describe('sasjs job', () => { const targetName = 'cli-tests-job' @@ -101,7 +101,7 @@ describe('sasjs job', () => { it( 'should submit a job and create a file with job log', async () => { - const command = `job execute /Public/app/cli-tests/testJob -t ${targetName} -l testLog` + const command = `job execute testJob -t ${targetName} -l testLog` const folderPath = path.join(process.cwd(), 'testLog') const filePath = path.join(process.cwd(), 'testLog/testJob-log.json') diff --git a/test/commands/request/sasjs-request.spec.js b/test/commands/request/request.spec.js similarity index 87% rename from test/commands/request/sasjs-request.spec.js rename to test/commands/request/request.spec.js index 4db1d6322..ba38e5cd2 100644 --- a/test/commands/request/sasjs-request.spec.js +++ b/test/commands/request/request.spec.js @@ -9,7 +9,7 @@ import { deleteFolder, createFile } from '../../../src/utils/file-utils' -import { remove } from '../../../src/sasjs-folder/remove' +import { remove } from '../../../src/commands/folder/remove' import SASjs from '@sasjs/adapter/node' describe('sasjs request', () => { @@ -93,10 +93,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - `/Public/app/cli-tests-${timestamp}/runRequest/sendArr`, - dataPathRel, - 'default', - targetName + `request /Public/app/cli-tests-${timestamp}/runRequest/sendArr -d ${dataPathRel} -c default -t ${targetName}` ) ).resolves.toEqual(true) const rawData = await readFile(`${process.projectDir}/output.json`) @@ -114,10 +111,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - `/Public/app/cli-tests-${timestamp}/runRequest/sendObj`, - dataPathRel, - 'default', - targetName + `request /Public/app/cli-tests-${timestamp}/runRequest/sendObj -d ${dataPathRel} -c default -t ${targetName}` ) ).resolves.toEqual(true) const rawData = await readFile(`${process.projectDir}/output.json`) @@ -136,7 +130,9 @@ describe('sasjs request', () => { `should execute service 'sendArr'`, async () => { await expect( - runRequest('runRequest/sendArr', dataPathRel, 'default', targetName) + runRequest( + `request runRequest/sendArr -d ${dataPathRel} -c default -t ${targetName}` + ) ).resolves.toEqual(true) const rawData = await readFile(`${process.projectDir}/output.json`) @@ -153,7 +149,9 @@ describe('sasjs request', () => { `should execute service sendObj`, async () => { await expect( - runRequest('runRequest/sendObj', dataPathRel, 'default', targetName) + runRequest( + `request runRequest/sendObj -d ${dataPathRel} -c default -t ${targetName}` + ) ).resolves.toEqual(true) const rawData = await readFile(`${process.projectDir}/output.json`) @@ -175,10 +173,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - `/Public/app/cli-tests-${timestamp}/runRequest/sendArr`, - dataPathRel, - configPathRel, - targetName + `request /Public/app/cli-tests-${timestamp}/runRequest/sendArr -d ${dataPathRel} -c ${configPathRel} -t ${targetName}` ) ).resolves.toEqual(true) @@ -197,10 +192,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - `/Public/app/cli-tests-${timestamp}/runRequest/sendObj`, - dataPathRel, - configPathRel, - targetName + `request /Public/app/cli-tests-${timestamp}/runRequest/sendObj -d ${dataPathRel} -c ${configPathRel} -t ${targetName}` ) ).resolves.toEqual(true) @@ -221,10 +213,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - 'runRequest/sendArr', - dataPathRel, - configPathRel, - targetName + `request runRequest/sendArr -d ${dataPathRel} -c ${configPathRel} -t ${targetName}` ) ).resolves.toEqual(true) @@ -243,10 +232,7 @@ describe('sasjs request', () => { async () => { await expect( runRequest( - 'runRequest/sendObj', - dataPathRel, - configPathRel, - targetName + `request runRequest/sendObj -d ${dataPathRel} -c ${configPathRel} -t ${targetName}` ) ).resolves.toEqual(true) diff --git a/test/sasjs-run.spec.js b/test/commands/run.spec.js similarity index 50% rename from test/sasjs-run.spec.js rename to test/commands/run.spec.js index 923f5f04e..ae0a9ffdd 100644 --- a/test/sasjs-run.spec.js +++ b/test/commands/run.spec.js @@ -1,12 +1,12 @@ -import { runSasCode } from '../src/sasjs-run/index' +import { runSasCode } from '../../src/commands' describe('sasjs run', () => { describe('runSasCode', () => { - it('should throw if file type is not *.sas', async () => { + it('should throw an error if file type is not *.sas', async () => { const file = 'test.sas.txt' const error = new Error(`'sasjs run' command supports only *.sas files.`) - await expect(runSasCode(file, '')).rejects.toEqual(error) + await expect(runSasCode(`run ${file}`)).rejects.toEqual(error) }) }) }) diff --git a/test/commands/servicepack/sasjs-servicepack.spec.js b/test/commands/servicepack/servicepack.spec.js similarity index 95% rename from test/commands/servicepack/sasjs-servicepack.spec.js rename to test/commands/servicepack/servicepack.spec.js index 9c4349a22..6cae7c0c9 100644 --- a/test/commands/servicepack/sasjs-servicepack.spec.js +++ b/test/commands/servicepack/servicepack.spec.js @@ -1,6 +1,6 @@ import dotenv from 'dotenv' import path from 'path' -import { processServicepack } from '../../../src/sasjs-servicepack/index' +import { processServicepack } from '../../../src/commands/servicepack' describe('sasjs servicepack', () => { const targetName = 'cli-tests-servicepack'