forked from plentymarkets/plentyshop-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetching pwa settings (plentymarkets#544)
* WIP * refactor: use async config method * fix: resolve data * Fetching PWA settings before build. * Applying code review changes. * Creating build directory for fetching script. + Trying to fix pipeline. * Fix import. * Fixing pipelines. * Fixing pipelines. * Fixing pipelines. * Removing console logs. * Changelog edit * refactor: remove duplicate env setup * chore: revert eol change * refactor: remove unnecessary variable * refactor: fetch script * Fixing lint error + code adjustment. --------- Co-authored-by: clincu-plenty <[email protected]> Co-authored-by: Maximilian Röll <[email protected]> Co-authored-by: ksted <[email protected]>
- Loading branch information
1 parent
24d2df6
commit dd9fab6
Showing
8 changed files
with
104 additions
and
6 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
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,86 @@ | ||
import axios from 'axios'; | ||
import https from 'node:https'; | ||
import fs from 'node:fs'; | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
|
||
const environmentFilePath = path.resolve(__dirname, '../.env'); | ||
const environmentTemporaryFilePath = path.resolve(__dirname, '../.env.tmp'); | ||
|
||
const environmentMap = { | ||
DISABLE_SETTINGS_FETCH: process.env.DISABLE_SETTINGS_FETCH, | ||
API_ENDPOINT: process.env.API_ENDPOINT, | ||
API_SECURITY_TOKEN: process.env.API_SECURITY_TOKEN, | ||
CONFIG_ID: process.env.CONFIG_ID, | ||
}; | ||
|
||
const setupTemporaryEnvironment = () => { | ||
let requiredEnvironmentData = ''; | ||
|
||
for (const [key, value] of Object.entries(environmentMap)) { | ||
if (key === 'DISABLE_SETTINGS_FETCH') continue; | ||
if (!value) { | ||
console.error(`Missing or invalid required environment variable: ${key}`); | ||
return; | ||
} | ||
|
||
requiredEnvironmentData += `${key}=${value}\n`; | ||
} | ||
|
||
fs.writeFile(environmentTemporaryFilePath, requiredEnvironmentData, () => {}); | ||
}; | ||
|
||
const writeConfigurationToTemporaryEnvironment = (data: Array<Array<{ [key: string]: string }>>) => { | ||
const environmentVariables = fs.readFileSync(environmentTemporaryFilePath, 'utf8').split(os.EOL); | ||
|
||
for (const category in data) { | ||
if (Array.isArray(data[category])) { | ||
data[category].forEach((item: { [key: string]: string }) => { | ||
environmentVariables.push(`${item.key.toUpperCase()}="${item.value}"`); | ||
}); | ||
} | ||
} | ||
|
||
fs.writeFileSync(environmentTemporaryFilePath, environmentVariables.join(os.EOL)); | ||
}; | ||
|
||
const fetchAndWriteRemoteConfiguration = async () => { | ||
const instance = axios.create({ | ||
withCredentials: true, | ||
baseURL: environmentMap.API_ENDPOINT, | ||
headers: { | ||
'X-Security-Token': environmentMap.API_SECURITY_TOKEN, | ||
}, | ||
httpsAgent: new https.Agent({ | ||
rejectUnauthorized: false, | ||
}), | ||
}); | ||
|
||
try { | ||
const { data } = await instance.get(`/storefront/settings/${environmentMap.CONFIG_ID}`); | ||
writeConfigurationToTemporaryEnvironment(data); | ||
} catch (error: any) { | ||
if (axios.isAxiosError(error)) { | ||
console.error('PWA settings error:', error.response?.data.error); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
const convertTemporaryToPermanentEnvironment = () => { | ||
fs.copyFile(environmentTemporaryFilePath, environmentFilePath, () => {}); | ||
fs.unlink(environmentTemporaryFilePath, () => {}); | ||
}; | ||
|
||
const fetchConfiguration = async () => { | ||
if (environmentMap.DISABLE_SETTINGS_FETCH === undefined || environmentMap.DISABLE_SETTINGS_FETCH === '1') { | ||
console.warn(`Fetching PWA settings is disabled! Check DISABLE_SETTINGS_FETCH in .env file.`); | ||
return; | ||
} | ||
|
||
setupTemporaryEnvironment(); | ||
await fetchAndWriteRemoteConfiguration(); | ||
convertTemporaryToPermanentEnvironment(); | ||
}; | ||
|
||
export default fetchConfiguration; |
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