This document provides an overview of the various scripts used throughout the CraftSpace project, with a focus on build tools, data processing, and automation.
CraftSpace uses a collection of scripts to manage:
- Content Processing - Downloading and transforming Internet Archive content
- Build Automation - Compiling and packaging application components
- Deployment - Publishing content to various platforms
- Development Utilities - Tools that simplify the development workflow
The project uses TypeScript for all scripting to ensure type safety and maintainability. The core build configuration is located in SvelteKit/BackSpace/scripts/tsconfig.json.
The TypeScript configuration (tsconfig.json) is set up for modern ES modules:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"outDir": "../dist/scripts",
"sourceMap": true,
"declaration": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["./**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}The script build process is coordinated by build-scripts.ts, which handles:
- Compilation of TypeScript files to JavaScript
- Management of dependencies
- Integration with the main SvelteKit build process
Located in SvelteKit/BackSpace/scripts/, these handle all interaction with Internet Archive:
- ia-collections-registry.js: Manages collection registration and discovery
- ia-collection-downloader.js: Downloads content from Internet Archive
- ia-process-collections.js: Processes downloaded collections
- ia-unity-export.js: Prepares collections for Unity integration
Scripts that transform raw content into optimized formats:
- process-epub.js: Extracts metadata and content from EPUB files
- process-pdf.js: Handles PDF content extraction and thumbnail generation
- generate-atlases.js: Creates texture atlases for Unity visualization
- generate-icons.js: Generates low-resolution icons for distant viewing
Scripts for building and deploying various components:
- build-unity.js: Builds and prepares Unity WebGL output
- build-sveltekit.js: Builds the SvelteKit application
- deploy-cdn.js: Uploads processed collections to CDN
- deploy-app.js: Deploys the complete application
Common utilities used across multiple scripts:
- download-manager.js: Manages concurrent downloads with rate limiting
- file-processor.js: Common file operations and transformations
- logger.js: Consistent logging system
- cache-manager.js: Handles caching of downloaded and processed content
The project defines convenience commands in package.json for common operations:
# Internet Archive Collection Management
npm run ia:list # List all registered collections
npm run ia:get <prefix> # Get details for a specific collection
npm run ia:register <...args> # Register a new collection
npm run ia:unregister <prefix> # Unregister a collection
npm run ia:scan # Scan collections directory
# Content Processing
npm run ia:download <...args> # Download a collection from Internet Archive
npm run ia:process # Process collections incrementally
npm run ia:process-full # Process all collections from scratch
npm run ia:process-unity # Process and export to Unity
# Build Commands
npm run build:scripts # Build TypeScript scripts
npm run build:unity # Build Unity WebGL project
npm run build:all # Build everythingThe scripts are organized within the SvelteKit/BackSpace/scripts/ directory:
scripts/
├── ia/ # Internet Archive integration scripts
│ ├── collections-registry.ts
│ ├── collection-downloader.ts
│ └── process-collections.ts
├── processing/ # Content processing scripts
│ ├── process-epub.ts
│ ├── process-pdf.ts
│ └── generate-atlases.ts
├── utils/ # Utility functions and helpers
│ ├── download-manager.ts
│ ├── file-processor.ts
│ └── logger.ts
├── build/ # Build automation scripts
│ ├── build-unity.ts
│ └── build-sveltekit.ts
└── deploy/ # Deployment scripts
├── deploy-cdn.ts
└── deploy-app.ts
# Register collection
npm run ia:register scifi "Science Fiction" "subject:science fiction" --include-in-unity
# Download and process
npm run ia:process -- --collection=scifi
# Export to Unity
npm run ia:process -- --collection=scifi --unity-export# Update all collections incrementally
npm run ia:process
# Force refresh of specific collection
npm run ia:process -- --collection=poetry --force-refresh
# Update Unity export
npm run sync-unity-collections# Full content pipeline
npm run ia:process-full
# Build all application components
npm run build:allWhen developing or modifying scripts:
- Use TypeScript: All new scripts should be written in TypeScript
- ES Modules: Use ESM syntax (
import/export) rather than CommonJS - Async/Await: Prefer async/await over callbacks or promise chains
- Error Handling: Implement proper error handling with graceful fallbacks
- Logging Levels: Use appropriate logging levels (info, warn, error, debug)
- Configuration: Accept configuration via both CLI flags and config files
- Testing: Add test coverage for critical functionality
Most CLI scripts follow this pattern:
// Define command structure
const commands = {
list: { description: "List all collections", handler: listCollections },
get: { description: "Get collection details", handler: getCollection },
// ...
};
// Process command line arguments
const args = process.argv.slice(2);
const command = args[0];
if (commands[command]) {
commands[command].handler(args.slice(1));
} else {
showHelp();
}Scripts typically load configuration from multiple sources:
// Load configuration with precedence:
// 1. Command line arguments
// 2. Environment variables
// 3. Configuration files
// 4. Default values
const config = {
...defaultConfig,
...loadConfigFromFile(configPath),
...parseEnvironmentVars(),
...parseCommandLineArgs(args)
};For operations on multiple items:
// Process items with concurrency control
async function processItems(items, concurrency = 5) {
const queue = new Queue(concurrency);
for (const item of items) {
await queue.add(() => processItem(item));
}
await queue.onIdle();
}For troubleshooting script issues:
-
Enable Verbose Logging:
npm run ia:process -- --verbose
-
Run in Test Mode:
npm run ia:process -- --dry-run
-
Examine Logs: Log files are stored in the
logs/directory with timestamps -
Check Environment Variables: Verify values in
.envfile or using:npm run env-check
-
Single-Step Processing: Process a single item for testing:
npm run ia:process -- --collection=scifi --item=item123 --debug
These scripts integrate with GitHub Actions workflows for automated processing:
- update-collections.yml: Scheduled collection updates
- build-deploy.yml: Build and deployment process
Complete reference for CI/CD is available in .github/README.md.
When running scripts on large collections:
-
Adjust Concurrency: Set appropriate concurrency levels based on available resources:
npm run ia:process -- --concurrent-items=3 --concurrent-downloads=5
-
Batch Processing: Process collections in smaller batches:
npm run ia:process -- --batch-size=50 --offset=100
-
Resource Monitoring: Use the monitoring tools to check resource usage:
npm run monitor:resources
Future improvements to the script system include:
- Workflow Orchestration: Better coordination between script steps
- Incremental Processing: More granular change detection
- Progress Reporting: Enhanced status updates and progress bars
- Recovery Mechanisms: Better handling of interruptions and restarts
The script system is designed to be modular and extensible, allowing for continuous improvement and adaptation to evolving requirements.