Skip to content

Commit

Permalink
Merge pull request #158 from sasjs/157-global-targets
Browse files Browse the repository at this point in the history
fix(targets): use a single function to fetch target from configuration
  • Loading branch information
krishna-acondy authored Oct 15, 2020
2 parents 45e4538 + 3b18487 commit 2b2497d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 62 deletions.
6 changes: 4 additions & 2 deletions src/sasjs-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { asyncForEach, removeComments, chunk, diff } from '../utils/utils'
import {
getSourcePaths,
getConfiguration,
getTargetToBuild,
findTargetInConfiguration,
getTargetSpecificFile,
getMacroCorePath
} from '../utils/config-utils'
Expand All @@ -38,7 +38,9 @@ export async function build(
buildSourceFolder = CONSTANTS.buildSourceFolder
buildDestinationFolder = CONSTANTS.buildDestinationFolder
buildDestinationServ = CONSTANTS.buildDestinationServ
targetToBuild = await getTargetToBuild(targetName)
const { target } = await findTargetInConfiguration(targetName)
targetToBuild = target

if (compileBuildDeployOnly) {
await compile()
await createFinalSasFiles()
Expand Down
7 changes: 5 additions & 2 deletions src/sasjs-deploy/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import SASjs from '@sasjs/adapter/node'
import chalk from 'chalk'
import { getTargetToBuild } from '../utils/config-utils'
import { findTargetInConfiguration } from '../utils/config-utils'
import { asyncForEach, executeShellScript, getVariable } from '../utils/utils'
import {
isSasFile,
Expand All @@ -25,7 +25,10 @@ export async function deploy(
isForced = false
) {
if (preTargetToBuild) targetToBuild = preTargetToBuild
else targetToBuild = await getTargetToBuild(targetName)
else {
const { target } = await findTargetInConfiguration(targetName)
targetToBuild = target
}

if (targetToBuild.serverType === 'SASVIYA' && !targetToBuild.authInfo) {
console.log(
Expand Down
32 changes: 20 additions & 12 deletions src/sasjs-web/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTargetToBuild } from '../utils/config-utils'
import { findTargetInConfiguration } from '../utils/config-utils'
import { asyncForEach, chunk } from '../utils/utils'
import {
readFile,
Expand Down Expand Up @@ -31,25 +31,33 @@ export async function createWebAppServices(
buildDestinationFolder = CONSTANTS.buildDestinationFolder
console.log(chalk.greenBright('Building web app services...'))
await createBuildDestinationFolder()
let target = null
if (preTargetToBuild) target = preTargetToBuild
else target = await getTargetToBuild(targetName)
let targetToBuild = null
if (preTargetToBuild) targetToBuild = preTargetToBuild
else {
const { target } = await findTargetInConfiguration(targetName)
targetToBuild = target
}

if (target) {
if (targetToBuild) {
console.log(
chalk.greenBright(`Building for target ${chalk.cyanBright(target.name)}`)
chalk.greenBright(
`Building for target ${chalk.cyanBright(targetToBuild.name)}`
)
)

const webAppSourcePath = target.webSourcePath
const webAppSourcePath = targetToBuild.webSourcePath
const destinationPath = path.join(
buildDestinationFolder,
'services',
target.streamWebFolder
targetToBuild.streamWebFolder
)
await createTargetDestinationFolder(destinationPath)

if (webAppSourcePath) {
const assetPathMap = await createAssetServices(target, destinationPath)
const assetPathMap = await createAssetServices(
targetToBuild,
destinationPath
)
const hasIndexHtml = await fileExists(
path.join(process.projectDir, webAppSourcePath, 'index.html')
)
Expand All @@ -64,7 +72,7 @@ export async function createWebAppServices(
tag,
webAppSourcePath,
destinationPath,
target,
targetToBuild,
assetPathMap
)
})
Expand All @@ -74,7 +82,7 @@ export async function createWebAppServices(
linkTag,
webAppSourcePath,
destinationPath,
target
targetToBuild
)
})

Expand All @@ -85,7 +93,7 @@ export async function createWebAppServices(
faviconTag,
webAppSourcePath,
destinationPath,
target
targetToBuild
)
})

Expand Down
52 changes: 8 additions & 44 deletions src/utils/config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export async function getConfiguration(pathToFile) {
return Promise.resolve(null)
}

/**
* Returns the target with the given name.
* If the target is not found in the local configuration,
* this function then looks in the global configuration.
* If it is still unable to find it, it throws an error.
* @param {string} targetName - the name of the target in question.
* @param {boolean} viyaSpecific - will fall back to the first target of type SASVIYA.
*/
export async function findTargetInConfiguration(
targetName,
viyaSpecific = false
Expand Down Expand Up @@ -249,50 +257,6 @@ export function getMacroCorePath() {
return path.join(process.projectDir, 'node_modules', '@sasjs/core')
}

export async function getTargetToBuild(targetName) {
const { buildSourceFolder } = require('../constants')
const buildTargets = await getBuildTargets(buildSourceFolder)

if (buildTargets.length) {
let targetToBuild = buildTargets.find((t) => t.name === targetName)

if (!targetToBuild) {
targetToBuild = buildTargets[0]

console.log(
chalk.yellowBright(
`No build target specified. Using ${chalk.cyanBright(
targetToBuild.name
)} by default.`
)
)
}

if (targetToBuild.appLoc)
targetToBuild.appLoc = sanitizeAppLoc(targetToBuild.appLoc)

return Promise.resolve(targetToBuild)
} else {
// Use default target to build. For cases when build target was not found.
const defaultTargetToBuild = {
buildOutputFileName: 'build.sas',
serverType: 'SASVIYA'
}

console.log(
chalk.yellowBright(
`No build target found. Using default target:\n${JSON.stringify(
defaultTargetToBuild,
null,
2
)}`
)
)

return Promise.resolve(defaultTargetToBuild)
}
}

/**
* Sanitizes app location string.
* @param {string} appLoc - app location
Expand Down
4 changes: 2 additions & 2 deletions test/commands/servicepack/sasjs-servicepack.deploy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import path from 'path'
import { processServicepack } from '../../../src/sasjs-servicepack/index'

describe('sasjs servicepack', () => {
beforeAll(() => {
saveGlobalRcFile(
beforeAll(async () => {
await saveGlobalRcFile(
JSON.stringify({
targets: [
{
Expand Down

0 comments on commit 2b2497d

Please sign in to comment.