Skip to content

Commit

Permalink
fix: replace the usage of getLocalConfig with process.sasjsConfig (#1335
Browse files Browse the repository at this point in the history
)

* fix: invoke setConstants from getTargetInfo method of TargetCommand class

* chore: adjust the position of functional params

* chore: mock setConstants in specs

* chore: quick fix

* fix: get from same source in whole life cycle of a command execution

if target is picked from .sasjsrc file then configuration should be picked from same file in whole commands execution
and if target is picked from local sasjsconfig.json then configuration should be picked from it too

* chore: quick fix

* fix: logic for function getProgramFolders

* chore: quick fix

* fix: get config from local sasjsconfig or global sasjsrc file based on target selection

* fix: store sasjs config in process variable to reduce redundant io operations

* chore: reset changes in package.json

* chore: specs fix

* fix: use sasjs config from process variable instead of reading config file every time

* fix: replace the usage of getLocalConfig with process.sasjsConfig

* chore: organize imports

* chore: refactor saveTokens function
  • Loading branch information
sabhas authored Mar 13, 2023
1 parent a6e32c9 commit 475113d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 97 deletions.
44 changes: 20 additions & 24 deletions src/commands/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import path from 'path'
import {
Target,
ServerType,
chunk,
getDependencyPaths,
readFile,
asyncForEach,
base64EncodeFile,
listSubFoldersInFolder,
listFilesInFolder,
chunk,
createFile,
folderExists,
asyncForEach,
removeHeader
getDependencyPaths,
listFilesInFolder,
listSubFoldersInFolder,
readFile,
removeHeader,
ServerType,
Target
} from '@sasjs/utils'
import { ServerTypeError } from '@sasjs/utils/error'
import path from 'path'

import { isSasFile } from '../../utils/file'
import {
getLocalConfig,
getMacroFolders,
getStreamConfig
} from '../../utils/config'
import { compile } from '../compile/compile'
import { getBuildInit, getBuildTerm } from './internal/config'
import { getLaunchPageCode } from './internal/getLaunchPageCode'
import {
FileTree,
FolderMember,
MemberType,
ServicePackSASjs,
SASJsFileType
SASJsFileType,
ServicePackSASjs
} from '@sasjs/utils/types'
import { compressAndSave } from '../../utils/compressAndSave'
import { getMacroFolders, getStreamConfig } from '../../utils/config'
import { isSasFile } from '../../utils/file'
import { compile } from '../compile/compile'
import {
loadDependencies,
getCompileTree
getCompileTree,
loadDependencies
} from '../compile/internal/loadDependencies'
import { getBuildInit, getBuildTerm } from './internal/config'
import { getLaunchPageCode } from './internal/getLaunchPageCode'

export async function build(target: Target) {
if (
Expand Down Expand Up @@ -488,7 +484,7 @@ function getLines(text: string): string[] {

export async function getBuildVars(target: Target) {
const targetBuildVars = target?.buildConfig?.macroVars ?? {}
const configuration = await getLocalConfig()
const configuration = process.sasjsConfig
const commonBuildVars = configuration?.buildConfig?.macroVars ?? {}

return convertVarsToSasFormat({ ...commonBuildVars, ...targetBuildVars })
Expand Down
51 changes: 22 additions & 29 deletions src/commands/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,42 @@
import path from 'path'
import {
getProgramFolders,
getMacroFolders,
getTestSetUp,
getTestTearDown,
getLocalConfig,
getGlobalRcFile
} from '../../utils/config'
import {
listSubFoldersInFolder,
listFilesInFolder,
fileExists,
deleteFolder,
asyncForEach,
CompileTree,
copy,
createFolder,
deleteFolder,
fileExists,
folderExists,
copy,
asyncForEach,
listFilesAndSubFoldersInFolder,
isTestFile,
CompileTree
listFilesAndSubFoldersInFolder,
listFilesInFolder,
listSubFoldersInFolder
} from '@sasjs/utils'
import { createWebAppServices } from '../web/web'
import { isSasFile } from '../../utils/file'
import { SASJsFileType, StreamConfig, Target } from '@sasjs/utils/types'
import path from 'path'
import {
Target,
StreamConfig,
SASJsFileType,
Configuration
} from '@sasjs/utils/types'
import { checkCompileStatus } from './internal/checkCompileStatus'
getMacroFolders,
getProgramFolders,
getTestSetUp,
getTestTearDown
} from '../../utils/config'
import { isSasFile } from '../../utils/file'
import { createWebAppServices } from '../web/web'
import * as compileModule from './compile'
import { getAllFolders, SasFileType } from './internal/getAllFolders'
import { checkCompileStatus } from './internal/checkCompileStatus'
import { compileFile } from './internal/compileFile'
import { getAllFolders, SasFileType } from './internal/getAllFolders'

import {
compileTestFile,
compileTestFlow,
copyTestMacroFiles
} from './internal/compileTestFile'
import { copySyncFolder } from './internal/copySyncFolder'
import {
getDestinationServicePath,
getDestinationJobPath
getDestinationJobPath,
getDestinationServicePath
} from './internal/getDestinationPath'
import { getCompileTree } from './internal/loadDependencies'
import { copySyncFolder } from './internal/copySyncFolder'

export async function compile(target: Target, forceCompile = false) {
const result = await checkCompileStatus(target, ['tests'])
Expand Down
37 changes: 14 additions & 23 deletions src/commands/compile/internal/compileTestFile.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import { TestFlow, Coverage, CoverageType, CoverageState } from '../../../types'
import path from 'path'
import {
createFile,
copy,
listFilesInFolder,
fileExists,
createFolder,
Target,
asyncForEach,
moveFile,
CompileTree,
Configuration,
copy,
createFile,
deleteFile,
fileExists,
folderExists,
deleteFolder,
getAbsolutePath,
isTestFile,
listFilesAndSubFoldersInFolder,
pathSepEscaped,
SASJsFileType,
getAbsolutePath,
isTestFile,
testFileRegExp,
Configuration,
CompileTree
Target,
testFileRegExp
} from '@sasjs/utils'
import { loadDependencies } from './loadDependencies'
import { sasFileRegExp } from '../../../utils/file'
import chalk from 'chalk'
import {
getProgramFolders,
getMacroFolders,
getLocalConfig,
getGlobalRcFile
} from '../../../utils/config'
import path from 'path'
import { Coverage, CoverageState, CoverageType, TestFlow } from '../../../types'
import { getMacroFolders, getProgramFolders } from '../../../utils/config'
import { sasFileRegExp } from '../../../utils/file'
import { loadDependencies } from './loadDependencies'

const getFileName = (filePath: string) => path.parse(filePath).base

Expand Down
10 changes: 7 additions & 3 deletions src/commands/compile/internal/getAllFolders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Target, getAbsolutePath, Configuration } from '@sasjs/utils'
import { getGlobalRcFile, getLocalConfig } from '../../../utils/config'
import { ServiceConfig, JobConfig } from '@sasjs/utils'
import {
Configuration,
getAbsolutePath,
JobConfig,
ServiceConfig,
Target
} from '@sasjs/utils'

export enum SasFileType {
Service = 'service',
Expand Down
3 changes: 1 addition & 2 deletions src/commands/compile/internal/getServerType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Target } from '@sasjs/utils'
import { Configuration, ServerType } from '@sasjs/utils/types'
import { getGlobalRcFile, getLocalConfig } from '../../../utils/config'
import { ServerType } from '@sasjs/utils/types'

/**
* Returns server type for 'compile' step.
Expand Down
17 changes: 6 additions & 11 deletions src/commands/compile/internal/loadDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
Target,
readFile,
SASJsFileType,
loadDependenciesFile,
DependencyHeader,
CompileTree,
DependencyHeader,
loadDependenciesFile,
readFile,
removeHeader,
Configuration
SASJsFileType,
Target
} from '@sasjs/utils'
import {
getBinaryFolders,
getLocalConfig,
getGlobalRcFile
} from '../../../utils/config'
import path from 'path'
import { getBinaryFolders } from '../../../utils/config'

export async function loadDependencies(
target: Target,
Expand Down
5 changes: 2 additions & 3 deletions src/commands/docs/docsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { generateDocs, generateDot, initDocs } from '..'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { getLocalConfig } from '../../utils'

enum DocSubCommand {
Init = 'init',
Expand Down Expand Up @@ -63,7 +62,7 @@ export class DocsCommand extends TargetCommand {

async executeGenerateDocs() {
const { target } = await this.getTargetInfo()
const config = await getLocalConfig()
const config = process.sasjsConfig
const returnCode = await generateDocs(
target,
config,
Expand All @@ -85,7 +84,7 @@ export class DocsCommand extends TargetCommand {

async executeGenerateDot() {
const { target } = await this.getTargetInfo()
const config = await getLocalConfig()
const config = process.sasjsConfig
const returnCode = await generateDot(
target,
config,
Expand Down
13 changes: 11 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,17 @@ export const saveTokens = async (
access_token: string,
refresh_token: string
): Promise<void> => {
const localConfig = await getLocalConfig()
const isLocalTarget = localConfig.targets?.some((t) => t.name === targetName)
const isLocalTarget = await getLocalConfig()
.then((localConfig) =>
localConfig.targets?.some((t) => t.name === targetName)
)
.catch(() => {
process.logger?.info(
'Local sasjsconfig.json file could not be found! Now finding target in global .sasjsrc file'
)
return false
})

if (isLocalTarget) {
const envFileContent = `CLIENT=${client}\nSECRET=${secret}\nACCESS_TOKEN=${access_token}\nREFRESH_TOKEN=${refresh_token}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)
Expand Down

0 comments on commit 475113d

Please sign in to comment.