Skip to content

Commit

Permalink
feat: Support global .env at project level
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhas committed Nov 23, 2022
1 parent 90fce98 commit ff76624
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export const authenticateTarget = async (
)) as any

if (isLocal) {
const envFileContent = `CLIENT=${authConfig.client}\nSECRET=${authConfig.secret}\nACCESS_TOKEN=${authConfig.access_token}\nREFRESH_TOKEN=${authConfig.refresh_token}\n`
const envFileContent = `CLIENT=${clientId}\nSECRET=${clientSecret}\nACCESS_TOKEN=${authConfig.access_token}\nREFRESH_TOKEN=${authConfig.refresh_token}\n`
const envFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${targetJson.name}`
Expand Down
67 changes: 51 additions & 16 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,32 @@ export const getAuthConfig = async (
const isLocal = extConfig.get('isLocal') as boolean

if ((await isSasjsProject()) && isLocal) {
const authConfig = (await getAuthConfigFromEnvFile(
target.name,
const targetEnvFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${target.name}`
)

const authConfigFromTargetEnv = (await getAuthConfigFromEnvFile(
targetEnvFilePath,
target.serverType
)) as AuthConfig
if (authConfig) {
return authConfig

if (authConfigFromTargetEnv) {
return authConfigFromTargetEnv
}

const envFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${target.name}`
)

const authConfigFromEnv = (await getAuthConfigFromEnvFile(
envFilePath,
target.serverType
)) as AuthConfig

if (authConfigFromEnv) {
return authConfigFromEnv
}
}

Expand Down Expand Up @@ -224,12 +244,32 @@ export const getAuthConfigSas9 = async (
const isLocal = extConfig.get('isLocal') as boolean

if ((await isSasjsProject()) && isLocal) {
const authConfigSas9 = (await getAuthConfigFromEnvFile(
target.name,
const targetEnvFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${target.name}`
)

const authConfigFromTargetEnv = (await getAuthConfigFromEnvFile(
targetEnvFilePath,
target.serverType
)) as AuthConfigSas9
if (authConfigSas9) {
return authConfigSas9

if (authConfigFromTargetEnv) {
return authConfigFromTargetEnv
}

const envFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${target.name}`
)

const authConfigFromEnv = (await getAuthConfigFromEnvFile(
envFilePath,
target.serverType
)) as AuthConfigSas9

if (authConfigFromEnv) {
return authConfigFromEnv
}
}

Expand All @@ -245,16 +285,11 @@ export const getAuthConfigSas9 = async (
}

const getAuthConfigFromEnvFile = async (
targetName: string,
envFilePath: string,
serverType: ServerType
) => {
const targetEnvFilePath = path.join(
workspace.workspaceFolders![0].uri.fsPath,
`.env.${targetName}`
)

if (await fileExists(targetEnvFilePath)) {
const targetEnvFileContent = await readFile(targetEnvFilePath)
if (await fileExists(envFilePath)) {
const targetEnvFileContent = await readFile(envFilePath)
const targetEnvConfig = dotenv.parse(targetEnvFileContent)

if (serverType === ServerType.Sas9) {
Expand Down

0 comments on commit ff76624

Please sign in to comment.