|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { createHash } from 'crypto'; |
| 3 | +import { existsSync, mkdirSync, writeFileSync } from 'fs'; |
| 4 | +import { resolve } from 'path'; |
| 5 | +import { rimrafSync } from 'rimraf'; |
| 6 | +import presets from './presets.json' with { type: 'json' }; |
| 7 | + |
| 8 | +const CACHE_DIR = resolve(new URL('.', import.meta.url).pathname, 'cache'); |
| 9 | + |
| 10 | +function hashLibraryList(libs) { |
| 11 | + const list = Array.from(new Set(libs)).sort().join('\n'); |
| 12 | + return createHash('sha256').update(list).digest('hex'); |
| 13 | +} |
| 14 | + |
| 15 | +if (existsSync(CACHE_DIR)) { |
| 16 | + rimrafSync(CACHE_DIR); |
| 17 | +} |
| 18 | + |
| 19 | +mkdirSync(CACHE_DIR, { recursive: true }); |
| 20 | +let index = []; |
| 21 | +for (const libs of presets) { |
| 22 | + const hash = hashLibraryList(libs); |
| 23 | + console.log(hash); |
| 24 | + for (const lib of libs) { |
| 25 | + console.log(` -> ${lib}`); |
| 26 | + } |
| 27 | + |
| 28 | + const cache = resolve(CACHE_DIR, `libraries-${hash}.tar.zst`); |
| 29 | + const dir = `tmp/${hash}`; |
| 30 | + const absDir = resolve(dir); |
| 31 | + mkdirSync(dir, { recursive: true }); |
| 32 | + const libVars = libs.map((_, index) => `"$L_${index}"`); |
| 33 | + const libCmd = `arduino-cli lib install -- ${libVars.join(' ')}`; |
| 34 | + try { |
| 35 | + execSync(libCmd, { |
| 36 | + cwd: dir, |
| 37 | + env: { |
| 38 | + HOME: process.env.HOME ?? '', |
| 39 | + PATH: process.env.PATH ?? '', |
| 40 | + ARDUINO_SKETCHBOOK_DIR: absDir, |
| 41 | + ...Object.fromEntries(libs.map((libName, index) => [`L_${index}`, libName])), |
| 42 | + } |
| 43 | + }); |
| 44 | + writeFileSync(`${absDir}/libraries/index.txt`, libs.sort().join('\n')); |
| 45 | + writeFileSync(`${absDir}/libraries/libraries_hash.txt`, hash); |
| 46 | + execSync(`tar -cf - -C ${absDir}/libraries . | zstd -19 -T0 - > ${cache}`); |
| 47 | + } finally { |
| 48 | + rimrafSync(absDir); |
| 49 | + } |
| 50 | + index.push(hash); |
| 51 | +} |
| 52 | + |
| 53 | +writeFileSync(resolve(CACHE_DIR, 'index.json'), JSON.stringify(index, null, 2)); |
| 54 | +writeFileSync(resolve(CACHE_DIR, 'index.txt'), index.join('\n') + '\n'); |
0 commit comments