-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.ts
95 lines (77 loc) · 2.95 KB
/
watch.ts
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
import chokidar from 'chokidar';
import path from 'path'
import upload from 'greybel-js/out/upload.js'
import { logger as greybelLogger } from 'greybel-js/out/helper/logger.js'
import fs from 'fs'
const __dirname = import.meta.dirname
const rootDir = path.join(__dirname, "root")
// Greybel currently works differently if we are uploading a dir or a file.
const getTargetDir = (filepath: string): string => {
const isUploadingDir = fs.lstatSync(filepath).isDirectory()
if (isUploadingDir) return '/'
const parsedPath = path.parse(filepath);
const filepathParent = parsedPath.dir
const relativePath = path.relative(__dirname, filepathParent)
const relativePathUnix = relativePath.replace(/\\/g, '/'); // If using windows, convert to unix path.
return relativePathUnix
}
const uploadToGame = async (filepath: string) => {
const isUploadingDir = fs.lstatSync(filepath).isDirectory()
const success = await upload(filepath, {
ingameDirectory: getTargetDir(filepath),
});
const dirOrFileStr = isUploadingDir ? "dir" : "file"
if (!success) console.error(`Failed to upload the ${dirOrFileStr} ${filepath}.`)
else console.log(`Successfully uploaded the ${dirOrFileStr} ${filepath}.`)
}
const uploadQueue: Array<string> = []
let currentlyUploading: string | undefined = undefined
const updateQueue = () => {
if (currentlyUploading) return
currentlyUploading = uploadQueue.shift()
if (currentlyUploading) {
uploadToGame(currentlyUploading).then(() => {
currentlyUploading = undefined
updateQueue()
})
}
}
const addUploadToQueue = (path: string) => {
if (!uploadQueue.includes(path)) {
uploadQueue.push(path)
updateQueue()
}
}
const main = async (): Promise<void> => {
console.clear()
const originalConsoleLog = console.log
console.log = (...data) => {
if (typeof data[0] === 'string' && data[0].startsWith('Trying to connect to'))
return
else originalConsoleLog(...data)
}
greybelLogger.setLogLevel('warn') // Hide most of greybel's logs
const pathToWatch = rootDir
console.log("Uploading all the files...")
addUploadToQueue(pathToWatch)
// Initialize watcher.
const watcher = chokidar.watch(pathToWatch, {
ignoreInitial: true, // Don't individually call for each file on start.
// ignored: (path) => !path.endsWith('.src'), // only watch js files
persistent: true
});
// Add event listeners.
watcher
.on('add', path => {
console.log(`File ${path} has been added. Uploading...`)
addUploadToQueue(path)
})
.on('change', path => {
console.log(`File ${path} has been changed. Uploading...`)
addUploadToQueue(path);
})
// .on('unlink', path => console.log(`File ${path} has been removed`));
console.log(`Watching the path ${pathToWatch}.`)
return new Promise(() => null)
}
await main()