| import fs from 'fs' |
| import path from 'path' |
| import crypto from 'crypto' |
| import { fileURLToPath } from 'url' |
|
|
| const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
|
|
| const LAST_HASH_FILEPATH = path.join(__dirname, './last-hash.txt') |
|
|
| |
| const COMPILE_DEPENDENCIES = [ |
| path.join(__dirname, './index.js'), |
| path.join(__dirname, '../compile.js'), |
| path.join(__dirname, '../src/plausible.js') |
| ] |
|
|
| function currentHash() { |
| const combinedHash = crypto.createHash('sha256') |
|
|
| for (const filePath of COMPILE_DEPENDENCIES) { |
| try { |
| const fileContent = fs.readFileSync(filePath) |
| const fileHash = crypto.createHash('sha256').update(fileContent).digest() |
| combinedHash.update(fileHash) |
| } catch (error) { |
| throw new Error(`Failed to read or hash ${filePath}: ${error.message}`) |
| } |
| } |
|
|
| return combinedHash.digest('hex') |
| } |
|
|
| function lastHash() { |
| if (fs.existsSync(LAST_HASH_FILEPATH)) { |
| return fs.readFileSync(LAST_HASH_FILEPATH).toString() |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function canSkipCompile() { |
| const current = currentHash() |
| const last = lastHash() |
|
|
| if (current === last) { |
| return true |
| } else { |
| fs.writeFileSync(LAST_HASH_FILEPATH, current) |
| return false |
| } |
| } |
|
|