-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathnode_gallery_helper.js
More file actions
132 lines (107 loc) · 2.45 KB
/
Copy pathnode_gallery_helper.js
File metadata and controls
132 lines (107 loc) · 2.45 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import fs from "node:fs"
const gallery = process.argv[2]
const title = process.argv[3]
const notice = process.argv[4]
if (!gallery || notice === undefined) {
console.error("ERROR, MISSING GALLERY OR NOTICE ARGUMENT")
process.exit()
} else {
console.log(`CREATING A GALLERY FILE FOR GALLERY ${gallery} WITH NOTICE ${notice}`)
}
/*
interfaces from the vue project
interface galleryItemInterface {
name: string,
text: string
}
interface galleryInterface {
path: string,
type: string,
title: string,
id: string,
content: galleryItemInterface[],
notice: string | null
}
*/
class GalleryItem {
name = null
text = null
constructor(name) {
this.name = name
}
buildForJson() {
return {
"name": this.name,
"text": this.text
}
}
}
class Gallery {
path = null
type = "large"
id = null
title = null
content = []
notice = null
constructor(galleryName, notice, title) {
this.path = `${galleryName}/`
this.id = galleryName
this.notice = notice
this.setTitle(title)
}
// add a new gallery item entry
addItem(name, text) {
this.content.push(new GalleryItem(name))
}
// reorder the list, always put in first the "Event" files
reorderContent() {
this.content.sort((a,b) => {
if (a.name.startsWith("Event") && !b.name.startsWith("Event")) return -1
if (!a.name.startsWith("Event") && b.name.startsWith("Event")) return 1
return [a,b].sort()
})
this.content.forEach((f, i) => {
f.text = this.title + " " + (i + 1)
})
}
buildForJson() {
const filesJson = []
this.content.forEach((c) => {
filesJson.push(c.buildForJson())
})
const json = {
"path": this.path,
"type": this.type,
"id": this.id,
"title": this.title,
"notice": this.notice,
"content" : filesJson
}
if (notice === "") {
delete json.notice
}
return json
}
setTitle(title) {
if (title) {
this.title = title
} else {
this.title = this.id.toUpperCase()
}
}
}
const folder = `images/gallery/${gallery}`
fs.readdir(folder, (err, files) => {
if (err) {
console.log(err)
process.exit()
}
const fullGallery = new Gallery(gallery, notice, title)
files.forEach((f, i) => {
fullGallery.addItem(f, i+1)
})
fullGallery.reorderContent()
fs.writeFile("./gallery_output.json", JSON.stringify(fullGallery.buildForJson()), {}, (err) => {
if (err) console.log(err)
})
})