Skip to content

Commit 2b2497d

Browse files
Merge pull request #158 from sasjs/157-global-targets
fix(targets): use a single function to fetch target from configuration
2 parents 45e4538 + 3b18487 commit 2b2497d

File tree

5 files changed

+39
-62
lines changed

5 files changed

+39
-62
lines changed

src/sasjs-build/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { asyncForEach, removeComments, chunk, diff } from '../utils/utils'
1717
import {
1818
getSourcePaths,
1919
getConfiguration,
20-
getTargetToBuild,
20+
findTargetInConfiguration,
2121
getTargetSpecificFile,
2222
getMacroCorePath
2323
} from '../utils/config-utils'
@@ -38,7 +38,9 @@ export async function build(
3838
buildSourceFolder = CONSTANTS.buildSourceFolder
3939
buildDestinationFolder = CONSTANTS.buildDestinationFolder
4040
buildDestinationServ = CONSTANTS.buildDestinationServ
41-
targetToBuild = await getTargetToBuild(targetName)
41+
const { target } = await findTargetInConfiguration(targetName)
42+
targetToBuild = target
43+
4244
if (compileBuildDeployOnly) {
4345
await compile()
4446
await createFinalSasFiles()

src/sasjs-deploy/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22
import SASjs from '@sasjs/adapter/node'
33
import chalk from 'chalk'
4-
import { getTargetToBuild } from '../utils/config-utils'
4+
import { findTargetInConfiguration } from '../utils/config-utils'
55
import { asyncForEach, executeShellScript, getVariable } from '../utils/utils'
66
import {
77
isSasFile,
@@ -25,7 +25,10 @@ export async function deploy(
2525
isForced = false
2626
) {
2727
if (preTargetToBuild) targetToBuild = preTargetToBuild
28-
else targetToBuild = await getTargetToBuild(targetName)
28+
else {
29+
const { target } = await findTargetInConfiguration(targetName)
30+
targetToBuild = target
31+
}
2932

3033
if (targetToBuild.serverType === 'SASVIYA' && !targetToBuild.authInfo) {
3134
console.log(

src/sasjs-web/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTargetToBuild } from '../utils/config-utils'
1+
import { findTargetInConfiguration } from '../utils/config-utils'
22
import { asyncForEach, chunk } from '../utils/utils'
33
import {
44
readFile,
@@ -31,25 +31,33 @@ export async function createWebAppServices(
3131
buildDestinationFolder = CONSTANTS.buildDestinationFolder
3232
console.log(chalk.greenBright('Building web app services...'))
3333
await createBuildDestinationFolder()
34-
let target = null
35-
if (preTargetToBuild) target = preTargetToBuild
36-
else target = await getTargetToBuild(targetName)
34+
let targetToBuild = null
35+
if (preTargetToBuild) targetToBuild = preTargetToBuild
36+
else {
37+
const { target } = await findTargetInConfiguration(targetName)
38+
targetToBuild = target
39+
}
3740

38-
if (target) {
41+
if (targetToBuild) {
3942
console.log(
40-
chalk.greenBright(`Building for target ${chalk.cyanBright(target.name)}`)
43+
chalk.greenBright(
44+
`Building for target ${chalk.cyanBright(targetToBuild.name)}`
45+
)
4146
)
4247

43-
const webAppSourcePath = target.webSourcePath
48+
const webAppSourcePath = targetToBuild.webSourcePath
4449
const destinationPath = path.join(
4550
buildDestinationFolder,
4651
'services',
47-
target.streamWebFolder
52+
targetToBuild.streamWebFolder
4853
)
4954
await createTargetDestinationFolder(destinationPath)
5055

5156
if (webAppSourcePath) {
52-
const assetPathMap = await createAssetServices(target, destinationPath)
57+
const assetPathMap = await createAssetServices(
58+
targetToBuild,
59+
destinationPath
60+
)
5361
const hasIndexHtml = await fileExists(
5462
path.join(process.projectDir, webAppSourcePath, 'index.html')
5563
)
@@ -64,7 +72,7 @@ export async function createWebAppServices(
6472
tag,
6573
webAppSourcePath,
6674
destinationPath,
67-
target,
75+
targetToBuild,
6876
assetPathMap
6977
)
7078
})
@@ -74,7 +82,7 @@ export async function createWebAppServices(
7482
linkTag,
7583
webAppSourcePath,
7684
destinationPath,
77-
target
85+
targetToBuild
7886
)
7987
})
8088

@@ -85,7 +93,7 @@ export async function createWebAppServices(
8593
faviconTag,
8694
webAppSourcePath,
8795
destinationPath,
88-
target
96+
targetToBuild
8997
)
9098
})
9199

src/utils/config-utils.js

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export async function getConfiguration(pathToFile) {
1818
return Promise.resolve(null)
1919
}
2020

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

252-
export async function getTargetToBuild(targetName) {
253-
const { buildSourceFolder } = require('../constants')
254-
const buildTargets = await getBuildTargets(buildSourceFolder)
255-
256-
if (buildTargets.length) {
257-
let targetToBuild = buildTargets.find((t) => t.name === targetName)
258-
259-
if (!targetToBuild) {
260-
targetToBuild = buildTargets[0]
261-
262-
console.log(
263-
chalk.yellowBright(
264-
`No build target specified. Using ${chalk.cyanBright(
265-
targetToBuild.name
266-
)} by default.`
267-
)
268-
)
269-
}
270-
271-
if (targetToBuild.appLoc)
272-
targetToBuild.appLoc = sanitizeAppLoc(targetToBuild.appLoc)
273-
274-
return Promise.resolve(targetToBuild)
275-
} else {
276-
// Use default target to build. For cases when build target was not found.
277-
const defaultTargetToBuild = {
278-
buildOutputFileName: 'build.sas',
279-
serverType: 'SASVIYA'
280-
}
281-
282-
console.log(
283-
chalk.yellowBright(
284-
`No build target found. Using default target:\n${JSON.stringify(
285-
defaultTargetToBuild,
286-
null,
287-
2
288-
)}`
289-
)
290-
)
291-
292-
return Promise.resolve(defaultTargetToBuild)
293-
}
294-
}
295-
296260
/**
297261
* Sanitizes app location string.
298262
* @param {string} appLoc - app location

test/commands/servicepack/sasjs-servicepack.deploy.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import path from 'path'
44
import { processServicepack } from '../../../src/sasjs-servicepack/index'
55

66
describe('sasjs servicepack', () => {
7-
beforeAll(() => {
8-
saveGlobalRcFile(
7+
beforeAll(async () => {
8+
await saveGlobalRcFile(
99
JSON.stringify({
1010
targets: [
1111
{

0 commit comments

Comments
 (0)