forked from ThemezerNX/Layouts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayoutUpdater.ts
More file actions
179 lines (149 loc) · 4.38 KB
/
layoutUpdater.ts
File metadata and controls
179 lines (149 loc) · 4.38 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import 'source-map-support/register'
const editJsonFile = require('edit-json-file')
import { uuid } from 'uuidv4'
import { accessSync, constants, readdirSync, statSync, readFileSync } from 'fs'
import { pgp, db } from './db'
const exist = (dir) => {
try {
accessSync(dir, constants.F_OK | constants.R_OK | constants.W_OK)
return true
} catch (e) {
return false
}
}
async function run() {
const dbLayouts = await db.any(`
SELECT *
FROM layouts
`)
const targetFolders = readdirSync('./').filter(
(lF) =>
!lF.startsWith('@') && !lF.startsWith('.') && !lF.startsWith('node_modules') && statSync(lF).isDirectory()
)
const layoutFolders = []
targetFolders.forEach((m) => {
readdirSync(m).forEach((lF) => {
layoutFolders.push(`${m}/${lF}`)
})
})
const layouts = layoutFolders.map((lF) => {
const detailsFile = editJsonFile(`${lF}/details.json`, { autosave: true })
if (!detailsFile.get('uuid')) {
detailsFile.set('uuid', uuid())
}
const details = detailsFile.toObject()
const jsonFile = editJsonFile(`${lF}/layout.json`, { autosave: true })
if (details.creator_id !== '0') {
jsonFile.unset('Ready8X')
}
let commonlayout = null
try {
commonlayout = readFileSync(`${lF}/common.json`, 'utf-8')
} catch (e) {}
const pieces = []
if (exist(`${lF}/pieces`)) {
const options = readdirSync(`${lF}/pieces`)
options.forEach((option) => {
const split = option.split('_')
if (split.length > 1) split.shift()
const optionName = split.join()
const values = readdirSync(`${lF}/pieces/${option}`)
const jsons = values.filter((v) => v.endsWith('.json'))
const valueJsons = []
jsons.forEach((j) => {
const valueName = j.replace('.json', '')
const valueFile = editJsonFile(`${lF}/pieces/${option}/${valueName}.json`, {autosave: true})
if (!valueFile.get('uuid')) {
valueFile.set('uuid', uuid())
}
const value = valueFile.toObject()
const value_uuid = value.uuid
delete value.uuid
valueJsons.push({
value: jsons.length > 1 ? valueName : true,
uuid: value_uuid,
image: values.includes(`${valueName}.png`) ? `${valueName}.png` : null,
json: JSON.stringify(value),
})
})
pieces.push({
name: optionName,
values: valueJsons,
})
})
}
const layout_str = JSON.stringify(jsonFile.toObject())
let resJson: any = {
uuid: details.uuid,
details,
baselayout: layout_str !== '{}' ? layout_str : null,
target: jsonFile.get('TargetName')?.replace(/.szs/i, '') || lF.split('/')[0],
last_updated: new Date(),
pieces,
commonlayout,
creator_id: details.creator_id,
}
return resJson
})
const newLayouts = layouts.filter((l) => !dbLayouts.some((dL) => dL.uuid === l.uuid)),
deletedLayouts = dbLayouts.filter((dL) => !layouts.some((l) => l.uuid === dL.uuid)),
existingLayouts = dbLayouts
.filter((l) =>
layouts.find(
(dL) =>
l.uuid === dL.uuid &&
// Check if any of the fields changed
(JSON.stringify(dL.details) !== JSON.stringify(l.details) ||
dL.baselayout !== l.baselayout ||
JSON.stringify(dL.pieces) !== JSON.stringify(l.pieces) ||
dL.commonlayout !== l.commonlayout ||
dL.creator_id !== l.creator_id)
)
)
.map((dL) => layouts.find((l) => l.uuid === dL.uuid))
let nL,
dL = [],
oL
const cs = new pgp.helpers.ColumnSet(
[
{ name: 'uuid', cast: 'uuid' },
{ name: 'details', cast: 'json' },
'baselayout',
'target',
{ name: 'last_updated', cast: 'timestamp without time zone' },
{ name: 'pieces', cast: 'json[]' },
'commonlayout',
'creator_id',
],
{
table: 'layouts',
}
)
if (newLayouts.length > 0) {
console.log('\n---- newLayouts:')
console.log(newLayouts.map((l) => l.details.name).join('\n'))
const query = () => pgp.helpers.insert(newLayouts, cs)
nL = db.none(query)
}
if (deletedLayouts.length > 0) {
console.log('\n---- deletedLayouts:')
dL = deletedLayouts.map((l) => {
console.log(`${l.details.name}\n`)
return db.none(
`
DELETE FROM layouts
WHERE uuid = $1
`,
[l.uuid]
)
})
}
if (existingLayouts.length > 0) {
console.log('\n---- existingLayouts:')
console.log(existingLayouts.map((l) => l.details.name).join('\n'))
const query = () => pgp.helpers.update(existingLayouts, cs) + ' where v.uuid = t.uuid'
oL = db.none(query)
}
Promise.all([nL, ...dL, oL]).then(() => db.$pool.end())
}
run()