-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
194 lines (171 loc) · 5.42 KB
/
Copy pathindex.js
File metadata and controls
194 lines (171 loc) · 5.42 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require('fs')
const path = require('path')
const { spawn } = require('child_process')
const prompts = require('prompts')
const lastFolderFileName = 'last.txt'
main().catch((error) => {
console.error(error)
setTimeout(() => {
}, 10000)
})
async function main() {
const folder = await promptFolder()
await checkIfOBSFolder(folder)
fs.promises.writeFile(lastFolderFileName, folder, 'utf-8')
const files = await fs.promises.readdir(folder, {
recursive: true,
withFileTypes: true
})
const plugins = [
'aja', 'aja-output-ui', 'decklink', 'decklink-captions', 'decklink-output-ui',
'frontend-tools', 'text-freetype2', 'vlc-video',
]
const handlers = [
allOfType('.pdb'),
allOfType('.pak').except('en-US', 'resources', 'chrome_100_percent', 'chrome_200_percent'),
allOfType('.ini').except('en-US', 'global', 'locale', 'basic'),
someOfType('.dll', ...plugins),
someOfType('.ovt', 'Yami_Acri', 'Yami_Grey', 'Yami_Light', 'Yami_Rachni'),
someOfType('.obt', 'System'),
allOnPath('plugin_config/obs-browser'),
allOnPath('config/obs-studio/profiler_data'),
allOnPath('config/obs-studio/logs'),
allOnPath('config/obs-studio/crashes'),
allOnPath(...['Acri', 'Light', 'Rachni'].map(t => `data/obs-studio/themes/${t}`)),
allOnPath(...plugins.map(p => `data/obs-plugins/${p}`))
]
let total = files.length
const toRemove = files.filter(file => {
if (!file.isFile()) return false
return handlers.some(h => h(file))
})
if (toRemove.length > 5000) {
throw new Error('This script should not be deleting more than 5000 files.')
}
toRemove.forEach(file => {
const fullPath = path.join(file.path, file.name)
return fs.unlinkSync(fullPath)
console.log('Removed', fullPath)
})
console.log('Removed', toRemove.length, 'out of', total, 'files')
}
async function promptFolder() {
if (process.argv[2]) return process.argv.slice(2).join(' ')
const choices = await createPromptChoices()
const { handler } = await prompts({
type: 'select',
name: 'handler',
message: 'Select OBS folder',
choices
})
return await handler()
}
async function createPromptChoices() {
const choices = []
try {
const lastDir = await fs.promises.readFile(lastFolderFileName, 'utf-8')
choices.push({
title: `Use last: ${lastDir}`,
value: async () => {
return lastDir
}
})
} catch (error) {
console.log('No last directory found')
}
choices.push({
title: 'Choose from system',
value: async () => {
return await selectFolderFromSystem()
}
})
return choices
}
async function checkIfOBSFolder(folder) {
const topLevelFiles = await fs.promises.readdir(folder)
const topLevelFilesSet = new Set(topLevelFiles)
const expectedTopLevelFiles = ['obs-plugins', 'data', 'bin']
const validOBS = expectedTopLevelFiles.every(s => topLevelFilesSet.has(s))
if (!validOBS) {
throw new Error('Select a valid OBS folder path containing', expectedTopLevelFiles.join(', '))
}
}
function runCommand(command, args, options) {
return new Promise((resolve, reject) => {
runCommand0(command, args, options, resolve, reject)
})
}
function runCommand0(command, args, options, resolve, reject) {
const process = spawn(command, args, options)
const stdoutArray = setupBuffer(process.stdout)
const stderrArray = setupBuffer(process.stderr)
process.on('close', () => {
const stdout = stdoutArray.join('')
const stderr = stderrArray.join('')
resolve({ stdout, stderr })
})
process.on('error', (error) => {
reject(error)
})
}
function setupBuffer(stream) {
const buffer = []
stream.on('data', (data) => {
const str = data.toString()
console.log(str)
buffer.push(str)
})
return buffer
}
async function selectFolderFromSystem() {
// https://stackoverflow.com/a/51658369
const command = `Function Select-FolderDialog
{
param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.SelectedPath
}
Else
{
Write-Error "Operation cancelled by user."
}
}
$folder = Select-FolderDialog # the variable contains user folder selection
write-host $folder
`
const { stdout, stderr } = await runCommand('powershell.exe', [command])
return stdout.toString().replaceAll('\n', '')
}
function someOfType(type, ...args) {
return (file) => {
const nameWithoutExtension = file.name.split('.').at(-2)
return file.name.endsWith(type) && args.some(arg => nameWithoutExtension === arg)
}
}
function allOfType(type) {
const handler = (file) => {
return file.name.endsWith(type)
}
handler.except = (...args) => {
return (file) => {
const nameWithoutExtension = file.name.split('.').at(-2)
//console.log(file, nameWithoutExtension, ...args)
return handler(file) && args.every(arg => nameWithoutExtension !== arg)
}
}
return handler
}
function allOnPath(...deletePaths) {
return (file) => {
return deletePaths.some(deletePath => {
return deletePath.split('/').every(p => file.path.includes(p))
})
}
}