Skip to content

Commit

Permalink
Reapply improved performance change
Browse files Browse the repository at this point in the history
  • Loading branch information
ROSeaboyer committed Feb 18, 2025
1 parent 9b19b1d commit a310cae
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 347 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
runs-on: self-hosted

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '22'

- name: Install Yarn
run: npm install --global yarn
Expand Down
34 changes: 11 additions & 23 deletions createJsonOutput.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
const fs = require('fs')

function getDirContents(p) {
return fs.readdirSync(p, function (err, files) {
var retArr = []
files.forEach(f => retArr.push(f))
return retArr
})
}
import { readdirSync, existsSync, mkdirSync, writeFileSync } from 'fs'

const keyArr = new Set(...[
[
...getDirContents('device'),
...getDirContents('device-lowres')
[
...readdirSync('device'),
...readdirSync('device-lowres')
].filter(x => x != '.DS_Store')
.map(x => x.includes('.') ? x.split('.').slice(0,-1).join('.') : x) // Remove file extension
])
Expand All @@ -23,12 +15,12 @@ for (const key of keyArr) {
let pngExists = false
let folderExists = false

if (fs.existsSync(`device-lowres/${key}.png`)) lowresExists = true
if (fs.existsSync(`device/${key}.png`)) pngExists = true
if (fs.existsSync(`device/${key}/0.png`)) folderExists = true
if (existsSync(`device-lowres/${key}.png`)) lowresExists = true
if (existsSync(`device/${key}.png`)) pngExists = true
if (existsSync(`device/${key}/0.png`)) folderExists = true

if (folderExists) {
const folderContents = getDirContents(`device/${key}`)
const folderContents = readdirSync(`device/${key}`)
let images = []

for (let i = 0;; i++) {
Expand All @@ -42,7 +34,7 @@ for (const key of keyArr) {
retArr.push({
key: key,
count: images.length,
dark: images.map(x => x.dark).filter(x => x).length > 0,
dark: images.filter(x => x.dark).length > 0,
index: images
})

Expand All @@ -59,12 +51,8 @@ for (const key of keyArr) {
dark: false
}]
})

continue
}
}

if (!fs.existsSync('out')) fs.mkdirSync('out')
fs.writeFile('out/main.json', JSON.stringify(retArr), (err) => {
if (err) console.log(err)
})
if (!existsSync('out')) mkdirSync('out')
writeFileSync('out/main.json', JSON.stringify(retArr))
94 changes: 36 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,43 @@
const path = require('path')
const fs = require('fs')
const sharp = require("sharp")
const { create } = require('domain')
import { resolve, join, basename } from 'path'
import { readdirSync, statSync, mkdirSync } from 'fs'
import sharp from "sharp"

const resizeArr = [32,64,128,256,512,1024,'main','preview']

const imgPath = path.resolve(__dirname, 'out')
const deviceDirPath = path.resolve(__dirname, "device")
const lowResDirPath = path.resolve(__dirname, "device-lowres")
const imageDirPath = path.resolve(__dirname, "images")
const __dirname = import.meta.dirname

const argDevices = process.argv.slice(2)
const imgPath = resolve(__dirname, 'out')
const deviceDirPath = resolve(__dirname, "device")
const lowResDirPath = resolve(__dirname, "device-lowres")
const imageDirPath = resolve(__dirname, "images")

function getDirContents(p) {
return fs.readdirSync(p, function (err, files) {
var retArr = []
files.forEach(f => retArr.push(f))
return retArr
})
}
const argDevices = process.argv.slice(2)

function getPngs(p, dirPath) {
let key = undefined
const dirArr = getDirContents(p).map(x => {
const recursePath = path.join(dirPath, x)
return readdirSync(p).map(x => {
const recursePath = join(dirPath, x)

if (x.endsWith('.png')) return {
key: path.basename(x,'.png'),
key: basename(x,'.png'),
imageArr: false
}
else if (fs.statSync(recursePath).isDirectory()) return {
else if (statSync(recursePath).isDirectory()) return {
key: x,
imageArr: getDirContents(recursePath).filter(x => x.endsWith('.png'))
imageArr: readdirSync(recursePath).filter(x => x.endsWith('.png'))
}
}).filter(x => x).filter(x => {
}).filter(x => {
if (!x) return false
if (argDevices.length) return argDevices.includes(x.key)
return true
})
return dirArr
}

function mkDir(p) { if (!fs.existsSync(p)) fs.mkdirSync(p) }

const deviceDirArr = getPngs(deviceDirPath, deviceDirPath)
const lowResDirArr = getPngs(lowResDirPath, deviceDirPath).filter(x => !deviceDirArr.map(y => y.key).includes(x.key))
const imageDirArr = getPngs(imageDirPath, imageDirPath)

async function createImg(img, res, dir, outputFormat, outputDir) {
try {
const outDirArr = [imgPath, `${outputDir}@${res}`, img.key]
for (const o in outDirArr) mkDir(
path.join(...
outDirArr.filter((x, index) => {
return index <= o
})
)
)

let options = {
width: res,
height: res,
Expand All @@ -66,41 +47,38 @@ async function createImg(img, res, dir, outputFormat, outputDir) {
if (res == 'preview') options = { height: 256 }
else if (res == 'main') options = { height: 352 }

const outDir = path.join(...outDirArr)

function outputSharpImage(inputPath, options, outputPath, outputFormat) {
let img = sharp(inputPath).resize(options)
const outDir = join(...[imgPath, `${outputDir}@${res}`, img.key])
mkdirSync(outDir, {recursive: true})

if (outputFormat == 'png') img.png()
else if (outputFormat == 'webp') img.webp()
else if (outputFormat == 'avif') img.avif({
quality: 70
})

img.toFile(outputPath, (err,) => {
if (err) console.log(err)
})
async function outputSharpImage(inputPath, options, outputPath, outputFormat) {
let conversionOptions = {}
if (outputFormat == 'avif') conversionOptions['quality'] = 70
await sharp(inputPath).resize(options)[outputFormat](conversionOptions).toFile(outputPath)
}

let promises = []
if (img.imageArr) for (const i of img.imageArr) {
const inputPath = path.join(dir, img.key, i)
const outputPath = path.join(outDir, path.basename(inputPath, '.png') + '.' + outputFormat)
const inputPath = join(dir, img.key, i)
const outputPath = join(outDir, basename(inputPath, '.png') + '.' + outputFormat)

outputSharpImage(inputPath, options, outputPath, outputFormat)
promises.push(outputSharpImage(inputPath, options, outputPath, outputFormat))
} else {
const inputPath = path.join(dir, img.key + '.png')
const outputPath = path.join(outDir, '0.' + outputFormat)
const inputPath = join(dir, img.key + '.png')
const outputPath = join(outDir, '0.' + outputFormat)

outputSharpImage(inputPath, options, outputPath, outputFormat)
promises.push(outputSharpImage(inputPath, options, outputPath, outputFormat))
}
await Promise.all(promises)
} catch (err) {
console.log(img, err)
process.exit()
}
}

let parentPromises = []
for (const res of resizeArr) for (const imgType of ['png','webp','avif']) {
for (const img of deviceDirArr) createImg(img, res, deviceDirPath, imgType, 'device')
for (const img of lowResDirArr) createImg(img, res, lowResDirPath, imgType, 'device')
for (const img of imageDirArr) createImg(img, res, imageDirPath, imgType, 'images')
}
for (const img of deviceDirArr) parentPromises.push(createImg(img, res, deviceDirPath, imgType, 'device'))
for (const img of lowResDirArr) parentPromises.push(createImg(img, res, lowResDirPath, imgType, 'device'))
for (const img of imageDirArr) parentPromises.push(createImg(img, res, imageDirPath, imgType, 'images'))
}
await Promise.all(parentPromises)
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"repository": "https://github.com/emiyl/apple-device-images",
"author": "emiyl <me@emiyl.com>",
"license": "MIT",
"type": "module",
"dependencies": {
"sharp": "^0.30.7"
}
}
"sharp": "^0.33.5"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Loading

0 comments on commit a310cae

Please sign in to comment.