forked from littlebyteorg/appledb-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateImages.js
111 lines (93 loc) · 2.94 KB
/
generateImages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const path = require('path')
const fs = require('fs')
const sharp = require("sharp")
const { create } = require('domain')
const resizeArr = [256,512,1024]
const imgPath = path.resolve(__dirname, "docs/.vuepress/public/assets/images/")
const dirPath = path.join(imgPath, 'device')
const lowResDirPath = path.join(imgPath, 'device-lowres')
function getDirContents(p) {
return fs.readdirSync(p, function (err, files) {
var retArr = []
files.forEach(f => retArr.push(f))
return retArr
})
}
function getPngs(p) {
let identifier = undefined
const dirArr = getDirContents(p).map(x => {
const recursePath = path.join(dirPath, x)
if (x.endsWith('.png')) return {
identifier: x.split('.')[0],
imageArr: false
}
else if (fs.statSync(recursePath).isDirectory()) return {
identifier: x,
imageArr: getDirContents(recursePath).filter(x => x.endsWith('.png'))
}
}).filter(x => x)
return dirArr
}
/*function getPngs(p, recursive = false, identifier = undefined) {
const dirArr = fs.readdirSync(p, function (err, files) {
var retArr = []
files.forEach(f => retArr.push(f))
return retArr
}).map(x => {
if (x.endsWith('png')) {
identifier = (identifier && recursive) ? identifier : x.split('.')[0]
return {
identifier: identifier,
image: x
}
}
const recursePath = path.join(dirPath, x)
if (fs.statSync(recursePath).isDirectory()) return getPngs(recursePath, true, x)
return undefined
}).filter(x => x)
var retArr = {}
console.log(dirArr)
/*for (const d of dirArr) {
if (retArr.hasOwnProperty(d.identifier)) retArr[d.identifier].imageArr.push(d.image)
else retArr[d.identifier] = {
imageArr: [d.image]
}
}*/
/*return retArr
}*/
function mkDir(p) { if (!fs.existsSync(p)) fs.mkdirSync(p) }
const dirArr = getPngs(dirPath)
const lowResDirArr = getPngs(lowResDirPath).filter(x => !dirArr.map(y => y.identifier).includes(x.identifier))
async function createPng(img, res, dir) {
try {
const outDirArr = [imgPath, `device@${res}`, img.identifier]
for (const o in outDirArr) mkDir(
path.join(...
outDirArr.filter((x, index) => {
return index <= o
})
)
)
const outDir = path.join(...outDirArr)
if (img.imageArr) for (const i of img.imageArr) {
const inputPath = path.join(dir, img.identifier, i)
const fileName = path.basename(inputPath)
await sharp(inputPath)
.resize(res)
.toFile(path.join(outDir, fileName))
} else {
const inputPath = path.join(dir, img.identifier + '.png')
const fileName = '0.png'
await sharp(inputPath)
.resize(res)
.toFile(path.join(outDir, fileName))
}
} catch (err) {
console.log(img, err)
process.exit()
}
}
for (const res of resizeArr) {
for (const img of dirArr) createPng(img, res, dirPath)
for (const img of lowResDirArr) createPng(img, res, lowResDirPath)
}