Skip to content

[675] Add Shared Constants Module - #164

Merged
Anton-88 merged 5 commits into
mainfrom
675-feature/shared-constants
Jun 9, 2025
Merged

[675] Add Shared Constants Module#164
Anton-88 merged 5 commits into
mainfrom
675-feature/shared-constants

Conversation

@VitalyyP

@VitalyyP VitalyyP commented Jun 8, 2025

Copy link
Copy Markdown
Contributor
  • Added new shared constants module for centralized constant management
  • Implemented constants generation scripts
  • Updated build and development scripts to include constants generation

@VitalyyP
VitalyyP requested a review from killev as a code owner June 8, 2025 11:54
@coderabbitai

coderabbitai Bot commented Jun 8, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

"""

Walkthrough

A new shared constants module is introduced for ApostropheCMS, providing standard form field name constants accessible on both server and client. Supporting scripts automate the generation and synchronization of these constants. Package scripts are updated to ensure the constants file is generated before app execution, and a test suite validates the generation process.

Changes

Files / Groups Change Summary
website/app.js Registers the new 'shared-constants' module in the ApostropheCMS configuration.
website/modules/@apostrophecms/shared-constants/index.js Adds a server-side module exporting standard form field name constants and exposes them to the client.
website/modules/@apostrophecms/shared-constants/ui/src/index.js Adds an auto-generated client-side module exporting the shared constants for browser use.
website/package.json Updates scripts to run generate_constants.js before start, dev, and build; adds standalone generate_constants.
website/scripts/generate_constants.js Adds a script to generate the client-side constants file from the server-side source.
website/scripts/generate_constants.test.js Adds Jest tests for the constants generation script, covering success and error paths.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant ServerModule as shared-constants (server)
    participant Script as generate_constants.js
    participant ClientModule as shared-constants (client)

    Developer->>Script: Run generate_constants.js (via npm script)
    Script->>ServerModule: Import and initialize to access constants
    ServerModule-->>Script: Provide STANDARD_FORM_FIELD_NAMES
    Script->>ClientModule: Write auto-generated JS file exporting constants
    Note right of ClientModule: Browser imports constants for use
Loading

Suggested reviewers

  • killev
  • yuramax
  • Anton-88
    """

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 667db0c and c1de79f.

📒 Files selected for processing (1)
  • website/modules/@apostrophecms/shared-constants/ui/src/index.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • website/modules/@apostrophecms/shared-constants/ui/src/index.js
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: lint
  • GitHub Check: unit-tests
  • GitHub Check: e2e-tests
  • GitHub Check: security-scan
  • GitHub Check: Analyze (javascript-typescript)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🔭 Outside diff range comments (1)
website/modules/@apostrophecms/shared-constants/ui/src/index.js (1)

1-20: 💡 Verification agent

🧩 Analysis chain

Consider adding this file to .gitignore.

Since this is an auto-generated file, consider whether it should be tracked in version control or added to .gitignore and generated during CI/CD builds.


🏁 Script executed:

#!/bin/bash
# Check if there's already a .gitignore and what it contains
echo "Current .gitignore contents:"
cat website/.gitignore 2>/dev/null || echo "No .gitignore found in website directory"

echo "Checking root .gitignore:"
cat .gitignore 2>/dev/null || echo "No .gitignore found in root"

Length of output: 2561


Add the auto-generated shared constants file to .gitignore

Since website/modules/@apostrophecms/shared-constants/ui/src/index.js is generated from the server-side constants, it shouldn’t be checked in. Please update the root .gitignore to exclude it:

• File needing update:

  • .gitignore (root)

• Patch to apply:

 # DynamoDB Local files
 .dynamodb/

+# Auto-generated shared constants
+website/modules/@apostrophecms/shared-constants/ui/src/index.js
🧹 Nitpick comments (2)
website/package.json (1)

7-9: Consider adding error handling for constants generation.

The integration of constants generation into the build pipeline is well-designed. However, if the constants generation fails, it will prevent the application from starting.

Consider wrapping the constants generation in conditional logic or adding fallback handling to improve robustness:

-    "start": "node scripts/generate-constants.js && node app @apostrophecms/asset:build && node app.js",
+    "start": "(node scripts/generate-constants.js || echo 'Warning: Constants generation failed') && node app @apostrophecms/asset:build && node app.js",
website/scripts/generate_constants.js (1)

51-51: Consider using asynchronous file operations.

Using synchronous file operations can block the event loop. Consider using async operations for better performance.

+const { promisify } = require('util');
+const writeFile = promisify(fs.writeFile);

-  // Write the client-side constants file
-  fs.writeFileSync(clientConstantsPath, clientFileContent);
+  // Write the client-side constants file
+  await writeFile(clientConstantsPath, clientFileContent);

If you choose to keep synchronous operations, that's also acceptable for build scripts where blocking behavior is often desired.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 371ca76 and 4c63666.

📒 Files selected for processing (6)
  • website/app.js (1 hunks)
  • website/modules/@apostrophecms/shared-constants/index.js (1 hunks)
  • website/modules/@apostrophecms/shared-constants/ui/src/index.js (1 hunks)
  • website/package.json (1 hunks)
  • website/scripts/generate_constants.js (1 hunks)
  • website/scripts/generate_constants.test.js (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
website/modules/@apostrophecms/shared-constants/index.js (2)
website/modules/@apostrophecms/shared-constants/ui/src/index.js (2)
  • STANDARD_FORM_FIELD_NAMES (8-12)
  • STANDARD_FORM_FIELD_NAMES (8-12)
website/scripts/generate_constants.js (2)
  • self (26-26)
  • self (30-30)
website/modules/@apostrophecms/shared-constants/ui/src/index.js (1)
website/modules/@apostrophecms/shared-constants/index.js (1)
  • STANDARD_FORM_FIELD_NAMES (6-10)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: security-scan
  • GitHub Check: lint
  • GitHub Check: e2e-tests
  • GitHub Check: unit-tests
  • GitHub Check: Analyze (actions)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (9)
website/app.js (1)

40-42: LGTM! Standard module registration.

The shared-constants module is properly registered in the ApostropheCMS configuration following standard conventions.

website/package.json (1)

17-17: LGTM! Useful standalone script.

The standalone generate-constants script provides a convenient way to regenerate constants independently.

website/scripts/generate_constants.js (1)

25-27:

Details

✅ Verification successful

Validate the module initialization pattern.

The init(self) pattern seems unusual for ApostropheCMS modules. Verify this is the correct way to access module constants.


🏁 Script executed:

#!/bin/bash
# Search for similar init patterns in the codebase
echo "Searching for similar module initialization patterns:"
rg "\.init\(" website/modules --type js -A 2 -B 2

echo "Checking the server constants module implementation:"
cat website/modules/@apostrophecms/shared-constants/index.js

Length of output: 1518


Module initialization pattern validated. The init(self) method is defined in website/modules/@apostrophecms/shared-constants/index.js and is intended to populate self.STANDARD_FORM_FIELD_NAMES. The generate_constants.js script correctly leverages this pattern to extract constants.

website/modules/@apostrophecms/shared-constants/ui/src/index.js (3)

1-5: LGTM! Clear auto-generated file warning.

The header effectively communicates that this is a generated file and should not be edited directly.


8-12: Constants match server-side definition correctly.

The generated constants match the server-side definition from website/modules/@apostrophecms/shared-constants/index.js lines 6-10, ensuring consistency between client and server.


15-19: Good backwards compatibility approach.

The default export function maintains compatibility for consumers that might expect a function-based import pattern.

website/modules/@apostrophecms/shared-constants/index.js (2)

1-10: LGTM! Well-structured constants definition.

The constants are clearly defined with descriptive names and follow a consistent naming convention. The comment clearly indicates this is the single source of truth.


12-28: LGTM! Proper ApostropheCMS module structure.

The module follows ApostropheCMS conventions correctly:

  • Proper alias configuration for easy access (sharedConstants)
  • init method properly attaches constants to the module instance
  • getBrowserData method correctly exposes constants to the client-side

This establishes a clean separation between server and client while maintaining a single source of truth.

website/scripts/generate_constants.test.js (1)

33-78: Test structure and error handling look good.

The test suite properly covers both success and error scenarios, with appropriate mocking setup and cleanup. The error handling test ensures the script fails gracefully with descriptive error messages.

Comment thread website/scripts/generate_constants.js
Comment thread website/scripts/generate_constants.js
Comment thread website/scripts/generate_constants.test.js
Comment thread website/scripts/generate_constants.test.js
Comment thread website/scripts/generate_constants.test.js Outdated
Comment thread website/scripts/generate_constants.test.js
@github-actions

github-actions Bot commented Jun 8, 2025

Copy link
Copy Markdown

🔍 Vulnerabilities of apostrophe-cms:test

📦 Image Reference apostrophe-cms:test
digestsha256:78cab8696d4fe9eb2288ef5e430df37415e48b85ef373dea686c117107e0d705
vulnerabilitiescritical: 0 high: 3 medium: 0 low: 0
platformlinux/amd64
size289 MB
packages971
📦 Base Image node:23-alpine
also known as
  • 23-alpine3.22
  • 23.11-alpine
  • 23.11-alpine3.22
  • 23.11.1-alpine
  • 23.11.1-alpine3.22
digestsha256:b9d38d589853406ff0d4364f21969840c3e0397087643aef8eede40edbb6c7cd
vulnerabilitiescritical: 0 high: 0 medium: 0 low: 0
critical: 0 high: 1 medium: 0 low: 0 connect-multiparty 2.2.0 (npm)

pkg:npm/connect-multiparty@2.2.0

high 7.8: CVE--2022--29623 Unrestricted Upload of File with Dangerous Type

Affected range<=2.2.0
Fixed versionNot Fixed
CVSS Score7.8
CVSS VectorCVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
EPSS Score0.334%
EPSS Percentile56th percentile
Description

An arbitrary file upload vulnerability in the file upload module of Express Connect-Multiparty 2.2.0 allows attackers to execute arbitrary code via a crafted PDF file. NOTE: the Supplier has not verified this vulnerability report.

critical: 0 high: 1 medium: 0 low: 0 async 0.9.2 (npm)

pkg:npm/async@0.9.2

high 7.8: CVE--2021--43138 OWASP Top Ten 2017 Category A9 - Using Components with Known Vulnerabilities

Affected range<2.6.4
Fixed version2.6.4, 3.2.2
CVSS Score7.8
CVSS VectorCVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
EPSS Score1.061%
EPSS Percentile77th percentile
Description

A vulnerability exists in Async through 3.2.1 (fixed in 3.2.2), which could let a malicious user obtain privileges via the mapValues() method.

critical: 0 high: 1 medium: 0 low: 0 async 1.5.2 (npm)

pkg:npm/async@1.5.2

high 7.8: CVE--2021--43138 OWASP Top Ten 2017 Category A9 - Using Components with Known Vulnerabilities

Affected range<2.6.4
Fixed version2.6.4, 3.2.2
CVSS Score7.8
CVSS VectorCVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
EPSS Score1.061%
EPSS Percentile77th percentile
Description

A vulnerability exists in Async through 3.2.1 (fixed in 3.2.2), which could let a malicious user obtain privileges via the mapValues() method.

…ate paths, add validation, ensure output dir exists, improve tests
@VitalyyP VitalyyP self-assigned this Jun 8, 2025
@VitalyyP VitalyyP changed the title Add Shared Constants Module [675] Add Shared Constants Module Jun 8, 2025
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jun 8, 2025

@yuramax yuramax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VitalyyP, during the testing and PR review, I found two issues:

  1. there is a mismatch in file naming
  2. the script is looking for files in the /app/ folder, but we are using the /website/ folder instead

I’ve attached screenshots for reference.

Screenshot 2025-06-08 at 21 34 26 Screenshot 2025-06-08 at 21 35 34

- Update constants to use double quotes for consistency\n- Rename generate-constants.js to generate_constants.js\n- Remove redundant generate-constants script from package.json
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jun 9, 2025
@VitalyyP

VitalyyP commented Jun 9, 2025

Copy link
Copy Markdown
Contributor Author

@yuramax Thanks. I've fixed it.

- Use single quotes for strings\n- Add trailing commas\n- Remove unnecessary quotes around object keys
@Anton-88
Anton-88 requested a review from yuramax June 9, 2025 11:33

@yuramax yuramax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 👍

@Anton-88 Anton-88 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

@killev killev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@sonarqubecloud

sonarqubecloud Bot commented Jun 9, 2025

Copy link
Copy Markdown

@Anton-88
Anton-88 merged commit 9876de9 into main Jun 9, 2025
@Anton-88
Anton-88 deleted the 675-feature/shared-constants branch June 9, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants