Files
ycode/scripts/rea/recover-artifact-link-styles.mjs
T

35 lines
2.3 KiB
JavaScript

import fs from 'node:fs/promises'
import path from 'node:path'
import crypto from 'node:crypto'
import postcss from 'postcss'
const directory = '.tmp/rea-css-recovered'
const manifest = JSON.parse(await fs.readFile(path.join(directory, 'manifest.json'), 'utf8'))
if (manifest.sourceEvidenceId !== 'ev_377c8790468b3c67dad8577aabb327f98d3140659f0fffdbabfcff5a40cc9e94') throw Error('Unexpected source Evidence')
const result = postcss.root(), sources = []
const hash = value => crypto.createHash('sha256').update(value).digest('hex')
for (const file of manifest.files) {
const source = await fs.readFile(path.join(directory, file.file), 'utf8')
if (hash(source) !== file.sha256) throw Error('Source CSS SHA mismatch')
const selected = postcss.root()
const project = node => {
if (node.type === 'rule') {
const selectors = node.selectors.filter(selector => ['.conversation-finished-footer', '.cr-cancelled-indicator'].includes(selector) || /^\.artifact-slot-panel(?:\s*$|__(?:actions|action-btn|action-chevron)(?:[\s:>.#[]|$))/.test(selector))
return selectors.length ? node.clone({ selector: selectors.join(',') }) : null
}
if (node.type === 'atrule' && node.nodes && !/keyframes$/.test(node.name)) {
const nodes = node.nodes.map(project).filter(Boolean)
return nodes.length ? node.clone({ nodes }) : null
}
return null
}
for (const node of postcss.parse(source).nodes) { const part = project(node); if (part) selected.append(part) }
if (selected.nodes.length) { sources.push({ file: file.file, sha256: file.sha256 }); result.append(selected.nodes) }
}
if (!sources.length || !result.toString().includes('.artifact-slot-panel__action-btn')) throw Error('Missing original artifact link rules')
const text = `/* REA ${manifest.sourceEvidenceId}; WorkBuddy 5.8.2. Original rule bodies/order/conditions.
* ${sources.map(source => source.file + ' sha256=' + source.sha256).join('\n * ')} */\n` + result.toString() + '\n'
const output = 'src/renderer/src/components/source-artifact-link.css'
if (process.argv.includes('--check')) { if (await fs.readFile(output, 'utf8') !== text) throw Error('Artifact link CSS changed') }
else await fs.writeFile(output, text)
console.log(JSON.stringify({ sources, output, sha256: hash(text), check: process.argv.includes('--check') }))