mapcomplete/scripts/ScriptUtils.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-10 03:18:32 +02:00
import {lstatSync, readdirSync} from "fs";
import * as https from "https";
2021-04-10 03:18:32 +02:00
export default class ScriptUtils {
public static readDirRecSync(path): string[] {
const result = []
for (const entry of readdirSync(path)) {
const fullEntry = path + "/" + entry
const stats = lstatSync(fullEntry)
if (stats.isDirectory()) {
// Subdirectory
// @ts-ignore
result.push(...ScriptUtils.readDirRecSync(fullEntry))
} else {
result.push(fullEntry)
}
}
return result;
}
public static DownloadJSON(url, continuation : (parts : string []) => void){
https.get(url, (res) => {
console.log("Got response!")
const parts : string[] = []
res.setEncoding('utf8');
res.on('data', function (chunk) {
// @ts-ignore
parts.push(chunk)
});
res.addListener('end', function () {
continuation(parts)
});
})
}
public static sleep(ms) {
return new Promise((resolve) => {
console.debug("Sleeping for", ms)
setTimeout(resolve, ms);
});
}
2021-04-10 03:18:32 +02:00
}