11import crypto from "node:crypto" ;
2+ import { execFile } from "node:child_process" ;
23import { promises as fs } from "node:fs" ;
34import path from "node:path" ;
5+ import { promisify } from "node:util" ;
46import sharp from "sharp" ;
57
68const root = process . cwd ( ) ;
79const sourceRoot = path . join ( root , "assets/images-source" ) ;
810const outputRoot = path . join ( root , "public/images" ) ;
911const manifestPath = path . join ( root , "assets/images-manifest.json" ) ;
1012const settings = { format : "webp" , quality : 85 , alphaQuality : 100 } ;
13+ const execFileAsync = promisify ( execFile ) ;
1114
1215async function filesIn ( dir , extension ) {
1316 const entries = await fs . readdir ( dir , { withFileTypes : true } ) . catch ( ( ) => [ ] ) ;
@@ -20,6 +23,20 @@ async function filesIn(dir, extension) {
2023 return files ;
2124}
2225
26+ async function sourceFiles ( ) {
27+ const discovered = await filesIn ( sourceRoot , ".png" ) ;
28+ try {
29+ const { stdout } = await execFileAsync ( "git" , [ "ls-files" , "-z" , "--" , "assets/images-source" ] ) ;
30+ const tracked = stdout
31+ . split ( "\0" )
32+ . filter ( ( file ) => file . toLowerCase ( ) . endsWith ( ".png" ) )
33+ . map ( ( file ) => path . join ( root , file ) ) ;
34+ return [ ...new Set ( [ ...discovered , ...tracked ] ) ] ;
35+ } catch {
36+ return discovered ;
37+ }
38+ }
39+
2340async function hashFile ( file ) {
2441 return crypto . createHash ( "sha256" ) . update ( await fs . readFile ( file ) ) . digest ( "hex" ) ;
2542}
@@ -34,7 +51,7 @@ async function readManifest() {
3451
3552async function convert ( ) {
3653 const manifest = await readManifest ( ) ;
37- const sources = ( await filesIn ( sourceRoot , ".png" ) ) . sort ( ) ;
54+ const sources = ( await sourceFiles ( ) ) . sort ( ) ;
3855 const nextFiles = { } ;
3956 let converted = 0 ;
4057 let skipped = 0 ;
@@ -67,7 +84,7 @@ async function convert() {
6784
6885async function check ( ) {
6986 const manifest = await readManifest ( ) ;
70- const sources = ( await filesIn ( sourceRoot , ".png" ) ) . sort ( ) ;
87+ const sources = ( await sourceFiles ( ) ) . sort ( ) ;
7188 const problems = [ ] ;
7289 if ( manifest . version !== 1 || JSON . stringify ( manifest . settings ) !== JSON . stringify ( settings ) ) problems . push ( "manifest version/settings mismatch" ) ;
7390 const sourceSet = new Set ( ) ;
0 commit comments