Open History to see the last commit
1#!/usr/bin/env node
2
3import { createHash } from 'node:crypto'
4import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs'
5import { join, relative } from 'node:path'
6import { fileURLToPath } from 'node:url'
7
8const __dirname = fileURLToPath(new URL('.', import.meta.url))
9
10/**
11 * Recursively gets all files in a directory
12 * @param {string} dir - Directory to scan
13 * @param {string[]} fileList - Accumulator for file paths
14 * @returns {string[]} Array of file paths
15 */
16function getAllFiles(dir, fileList = []) {
17 try {
18 const files = readdirSync(dir)
19
20 files.forEach((file) => {
21 const filePath = join(dir, file)
22 try {
23 const stat = statSync(filePath)
24
25 if (stat.isDirectory()) {
26 // Skip node_modules and dist directories
27 if (file !== 'node_modules' && file !== 'dist') {
28 getAllFiles(filePath, fileList)
29 }
30 } else {
31 fileList.push(filePath)
32 }
33 } catch (err) {
34 console.warn(`Warning: Could not read ${filePath}: ${err.message}`)
35 }
36 })
37 } catch (err) {
38 console.warn(`Warning: Could not read directory ${dir}: ${err.message}`)
39 }
40
41 return fileList
42}
43
44/**
45 * Generates a checksum for all files in specified directories
46 * @param {string} baseDir - Base directory path
47 * @param {string[]} directories - Array of directory names to checksum
48 * @returns {string} Hex checksum
49 */
50function generateChecksum(baseDir, directories) {
51 const hash = createHash('sha256')
52 const allFiles = []
53
54 // Collect all files from specified directories
55 directories.forEach((dir) => {
56 const dirPath = join(baseDir, dir)
57 const files = getAllFiles(dirPath)
58 allFiles.push(...files)
59 })
60
61 // Sort files to ensure consistent ordering
62 allFiles.sort()
63
64 // Hash each file's content
65 allFiles.forEach((filePath) => {
66 try {
67 const content = readFileSync(filePath)
68 const relativePath = relative(baseDir, filePath)
69 // Include the file path in the hash for uniqueness
70 hash.update(relativePath)
71 hash.update(content)
72 } catch (err) {
73 console.warn(`Warning: Could not read ${filePath}: ${err.message}`)
74 }
75 })
76
77 return hash.digest('hex')
78}
79
80/**
81 * Generates checksum.ts file for a framework
82 * @param {string} frameworkPath - Path to the framework directory
83 * @param {string} frameworkName - Name of the framework for logging
84 */
85function generateChecksumFile(frameworkPath, frameworkName) {
86 const directories = ['add-ons', 'examples', 'hosts', 'project', 'toolchains']
87 const checksum = generateChecksum(frameworkPath, directories)
88
89 const checksumContent = `// This file is auto-generated. Do not edit manually.
90// Generated from add-ons, examples, hosts, project, and toolchains directories
91export const contentChecksum = '${checksum}'
92`
93
94 const checksumPath = join(frameworkPath, 'src', 'checksum.ts')
95 writeFileSync(checksumPath, checksumContent, 'utf8')
96
97 console.log(`✓ Generated checksum for ${frameworkName}: ${checksum}`)
98}
99
100// Main execution
101const rootDir = join(__dirname, '..')
102const reactCraPath = join(rootDir, 'frameworks', 'react-cra')
103const solidPath = join(rootDir, 'frameworks', 'solid')
104
105console.log('Generating checksums...')
106generateChecksumFile(reactCraPath, 'react-cra')
107generateChecksumFile(solidPath, 'solid')
108console.log('✓ Checksums generated successfully')