Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sasjs-add): fix tests and restructure code
- Loading branch information
1 parent
69b175a
commit 890c922
Showing
8 changed files
with
232 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Logger, LogLevel } from '@sasjs/utils/logger' | ||
import { Target, ServerType } from '@sasjs/utils/types' | ||
|
||
import path from 'path' | ||
|
||
import { | ||
findTargetInConfiguration, | ||
saveToGlobalConfig | ||
} from '../../utils/config-utils' | ||
import { createFile } from '../../utils/file-utils' | ||
import { TargetScope } from '../../types/TargetScope' | ||
import { | ||
getCommonFields, | ||
getAndValidateSasViyaFields, | ||
getAndValidateSas9Fields | ||
} from './input' | ||
import { getLocalConfig } from './config' | ||
|
||
export async function addTarget(): Promise<boolean> { | ||
const logLevel = (process.env.LOG_LEVEL || LogLevel.Error) as LogLevel | ||
const logger = new Logger(logLevel) | ||
const { scope, serverType, name, appLoc, serverUrl } = await getCommonFields() | ||
|
||
let target: Partial<Target> | Target = { | ||
name, | ||
serverType: serverType, | ||
serverUrl, | ||
appLoc | ||
} | ||
|
||
let filePath = await saveConfig(scope, target as Target) | ||
logger.info(`Target configuration has been saved to ${filePath}.`) | ||
|
||
if (serverType === ServerType.Sas9) { | ||
const sas9FieldValues = await getAndValidateSas9Fields() | ||
target = { | ||
...target, | ||
tgtBuildVars: sas9FieldValues, | ||
tgtDeployVars: sas9FieldValues | ||
} | ||
} else { | ||
const { contextName } = await getAndValidateSasViyaFields( | ||
name, | ||
scope, | ||
serverUrl, | ||
logger | ||
) | ||
|
||
target = { | ||
...target, | ||
tgtBuildVars: { contextName }, | ||
tgtDeployVars: { contextName }, | ||
deployServicePack: true, | ||
tgtDeployScripts: [] | ||
} | ||
|
||
const { target: currentTarget } = await findTargetInConfiguration(name) | ||
target = { ...currentTarget, ...target } | ||
} | ||
|
||
filePath = await saveConfig(scope, target as Target) | ||
logger.info(`Target configuration has been saved to ${filePath}.`) | ||
return true | ||
} | ||
|
||
async function saveConfig(scope: TargetScope, target: Target) { | ||
let filePath = '' | ||
if (scope === TargetScope.Local) { | ||
filePath = await saveToLocalConfig(target as Target) | ||
} else if (scope === TargetScope.Global) { | ||
filePath = await saveToGlobalConfig(target as Target) | ||
} | ||
|
||
return filePath | ||
} | ||
|
||
async function saveToLocalConfig(buildTarget: Target) { | ||
const buildSourceFolder = require('../../constants').get().buildSourceFolder | ||
let config = await getLocalConfig() | ||
if (config) { | ||
if (config.targets && config.targets.length) { | ||
const existingTargetIndex = config.targets.findIndex( | ||
(t: Target) => t.name === buildTarget.name | ||
) | ||
if (existingTargetIndex > -1) { | ||
config.targets[existingTargetIndex] = buildTarget | ||
} else { | ||
config.targets.push(buildTarget) | ||
} | ||
} else { | ||
config.targets = [buildTarget] | ||
} | ||
} else { | ||
config = { targets: [buildTarget] } | ||
} | ||
|
||
const configPath = path.join(buildSourceFolder, 'sasjsconfig.json') | ||
|
||
await createFile(configPath, JSON.stringify(config, null, 2)) | ||
|
||
return configPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { create } from '../create' | ||
import path from 'path' | ||
import { getConfiguration } from '../../utils/config-utils' | ||
|
||
export async function getLocalConfig() { | ||
const buildSourceFolder = require('../../constants').get().buildSourceFolder | ||
const config = await getConfiguration( | ||
path.join(buildSourceFolder, 'sasjsconfig.json') | ||
) | ||
if (!config) await create('.', 'sasonly') | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum TargetScope { | ||
Global = 'Global', | ||
Local = 'Local' | ||
} |
Oops, something went wrong.