Skip to content

Commit 9db01aa

Browse files
committed
Rename script for clarity
1 parent e4c56aa commit 9db01aa

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed
Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*/
88

99
/**
10-
* Simple script to update the CSP directives in ssr.js by merging in the new configuration
10+
* Script to add new CSP directive values to existing configuration in ssr.js
11+
*
12+
* This script only adds new values - it cannot remove or modify existing directives.
13+
* It preserves existing comments and prevents duplicate values.
1114
*/
1215

1316
const fs = require('fs')
@@ -79,9 +82,9 @@ function getCurrentCSPConfig(ssrFilePath = DEFAULT_SSR_FILE_PATH) {
7982
}
8083

8184
/**
82-
* Merge new CSP configuration with existing configuration
85+
* Add new CSP directive values to existing configuration
8386
*/
84-
function mergeCSPConfig(existingConfig, newConfig) {
87+
function addCSPDirectives(existingConfig, newConfig) {
8588
const mergedConfig = {...existingConfig}
8689

8790
Object.entries(newConfig).forEach(([directiveName, newEntries]) => {
@@ -179,7 +182,7 @@ function parseInputFromStdin() {
179182
}
180183

181184
/**
182-
* Update ssr.js with new CSP configuration
185+
* Update ssr.js file with the enhanced CSP configuration
183186
*/
184187
function updateSSRFile(config, ssrFilePath = DEFAULT_SSR_FILE_PATH) {
185188
const content = fs.readFileSync(ssrFilePath, 'utf8')
@@ -191,7 +194,7 @@ function updateSSRFile(config, ssrFilePath = DEFAULT_SSR_FILE_PATH) {
191194
)
192195

193196
fs.writeFileSync(ssrFilePath, newContent, 'utf8')
194-
console.log('✅ Successfully updated CSP directives in ssr.js')
197+
console.log('✅ Successfully added CSP directives to ssr.js')
195198
}
196199

197200
/**
@@ -207,25 +210,31 @@ async function main() {
207210
if (filteredArgs.length === 0 && process.stdin.isTTY) {
208211
console.log(`
209212
Usage:
210-
node scripts/update-csp-directives-simple.js <config.json>
211-
echo '{"img-src": ["*.example.com"]}' | node scripts/update-csp-directives-simple.js
213+
node scripts/add-csp-directives.js <config.json>
214+
echo '{"img-src": ["*.example.com"]}' | node scripts/add-csp-directives.js
215+
216+
Description:
217+
Adds new CSP directive values to existing configuration in ssr.js
218+
- Preserves existing comments and values
219+
- Prevents duplicate values
220+
- Cannot remove or modify existing directives
212221
213222
Options:
214223
--ssr-path <path> - Custom path to ssr.js file (default: packages/template-retail-react-app/app/ssr.js)
215224
216225
Examples:
217-
# Merge CSP from configuration file
218-
node scripts/update-csp-directives-simple.js csp-config.json
226+
# Add CSP directives from configuration file
227+
node scripts/add-csp-directives.js csp-config.json
219228
220-
# Merge CSP from JSON via stdin/pipe
221-
echo '{"img-src": ["*.example.com"]}' | node scripts/update-csp-directives-simple.js
222-
cat csp-config.json | node scripts/update-csp-directives-simple.js
229+
# Add CSP directives from JSON via stdin/pipe
230+
echo '{"img-src": ["*.example.com"]}' | node scripts/add-csp-directives.js
231+
cat csp-config.json | node scripts/add-csp-directives.js
223232
224233
# Use custom ssr.js path
225-
node scripts/update-csp-directives-simple.js --ssr-path custom/path/ssr.js csp-config.json
226-
echo '{"script-src": ["cdn.example.com"]}' | node scripts/update-csp-directives-simple.js --ssr-path custom/path/ssr.js
234+
node scripts/add-csp-directives.js --ssr-path custom/path/ssr.js csp-config.json
235+
echo '{"script-src": ["cdn.example.com"]}' | node scripts/add-csp-directives.js --ssr-path custom/path/ssr.js
227236
228-
Config JSON Format:
237+
Config JSON Format (simplified - just arrays of strings):
229238
{
230239
"img-src": [
231240
"*.commercecloud.salesforce.com",
@@ -268,12 +277,14 @@ Config JSON Format:
268277
})
269278
})
270279

271-
// Get current configuration and merge with new configuration
280+
// Get current configuration and add new directives to it
272281
const currentConfig = getCurrentCSPConfig(ssrFilePath)
273-
const mergedConfig = mergeCSPConfig(currentConfig, newConfig)
282+
const mergedConfig = addCSPDirectives(currentConfig, newConfig)
274283

275284
updateSSRFile(mergedConfig, ssrFilePath)
276-
console.log(`✅ Merged CSP directives successfully`)
285+
console.log(`✅ Added CSP directives successfully`)
286+
287+
// TODO: Fix any linting errors by `npm run lint:fix` from the folder containing the ssr.js file
277288
} catch (error) {
278289
console.error('❌ Error:', error.message)
279290
process.exit(1)
@@ -290,7 +301,7 @@ if (require.main === module) {
290301

291302
module.exports = {
292303
getCurrentCSPConfig,
293-
mergeCSPConfig,
304+
addCSPDirectives,
294305
generateCSPDirectives,
295306
updateSSRFile,
296307
getSSRFilePath,

0 commit comments

Comments
 (0)