Developer Scripts
Relevant source files
The following files were used as context for generating this wiki page:The CyberPath project utilizes a set of scripts defined in package.json to automate common development and build tasks. These scripts, executed via pnpm, handle everything from starting the local development server to creating a production-ready build, including the automated generation of a screenshot asset.
The primary scripts facilitate local development, building the static site, and previewing the production build. The build script is particularly notable for its multi-step process that concludes with running a custom Node.js script to capture a screenshot of the application's database page.
Sources: package.json:5-10, README.md:39-53
Available Scripts
The project's core developer tasks are managed through npm scripts. These are defined in the scripts section of the package.json file.
Sources: package.json:5-10
| Script | Command | Description - |
|---|---|---|
dev | astro dev | Starts the Astro development server. The application can be accessed at localhost:4321. - |
start|astro dev| An alias for thedevscript. - |
|build|astro check && astro build && astro preferences disable devToolbar && npx puppeteer browsers install chrome && node ./scripts/screenshot.js| Executes a multi-step production build process. It includes type checking, building the site, disabling the dev toolbar, installing the Chrome browser for Puppeteer, and running thescreenshot.jsscript. |
|preview|astro preview| Starts a local server to preview the production build. - |
|astro|astro| Provides direct access to the Astro CLI for other commands. - |
Usage
To run any of the scripts, pnpm is required. The general command format is:
pnpm run <script_name>
For example, to start the development server:
pnpm run dev
Sources: README.md:35-37, 49-53, package.json:5-10
Build Process
The build script is the most complex, chaining several commands to produce a complete production artifact.
Sources: package.json:8
This diagram illustrates the sequence of operations executed by the pnpm run build command.
astro check: Performs type checking across the project using TypeScript.astro build: Compiles the Astro project into static files. The output asset directory is configured asassetsinastro.config.ts.astro preferences disable devToolbar: Disables the Astro Dev Toolbar in the production build.npx puppeteer browsers install chrome: Ensures the correct version of Chrome for Puppeteer is downloaded. This is a dependency for the screenshot script.node ./scripts/screenshot.js: Executes the custom script to generate a screenshot of the application.
Sources: package.json:8, astro.config.ts:25-27
Screenshot Script (screenshot.js)
This Node.js script is a critical part of the build process. It programmatically launches a headless browser, navigates to the application's database page, and captures a screenshot which is then optimized and saved as a WebP image in the build's asset directory.
Sources: scripts/screenshot.js:1-7, 161-164
Dependencies
The script relies on two key devDependencies:
puppeteer: To control a headless Chrome browser for navigation and screenshotting.sharp: For high-performance image processing to crop and optimize the screenshot into a WebP format.
Sources: package.json:40, 41, scripts/screenshot.js:12, 13
Execution Flow
The following sequence diagram details the internal process of the screenshot.js script.
Sources: scripts/screenshot.js
Key Functions
The script is organized into several asynchronous functions:
| Function | Description - |
|---|---|
startServer() | Spawns the npm run dev process to start the Astro development server. It waits 10 seconds to allow the server to initialize before proceeding. - |
initPuppeteer() | Launches a headless Puppeteer instance, creates a new page, and sets the viewport to 1920x800 pixels. - |
focusOnTarget() | Navigates the Puppeteer page to http://localhost:4321/database/. It then executes client-side JavaScript to remove the Termly cookie banner and scroll down, before waiting another 10 seconds for React to render the data table. |
getTargetFilename() | Reads the ./dist/assets directory to find the generated WebP file for the database screenshot. This is necessary because Astro adds a content hash to asset filenames. - |
main() | The main entry point that orchestrates the entire process: starting the server, initializing Puppeteer, taking the screenshot, processing the image with Sharp, and finally killing the server process. - |
Sources: scripts/screenshot.js:22-45, 51-64, 70-87, 93-105, 107-159