-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.ts
114 lines (94 loc) · 2.56 KB
/
fs.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import fs from "fs";
import {
FRAME_DIR,
FRAME_LIST,
MIN_FRAME,
MAX_FRAME,
TOTAL_FRAME,
} from "./const";
// DONE: FIX WEIRD INDEXING NONSENSE W TOTAL_FRAME
// CHECKED: Working
export const getArr = (): number[] => {
let data = fs.readFileSync(`${FRAME_LIST}`, "utf-8");
let arr = data.split(",").map((num) => Number(num));
return arr;
};
//CHECKED: Working
export const getBorderIndex = (arr: number[]) => {
const count = arr[TOTAL_FRAME + 1];
const index = TOTAL_FRAME - count + 1;
// console.log(`count: ${count}, index: ${index}`);
return index;
};
//CHECKED: Working
export const removeFrame = (frameIndex: number, arr: number[]): number => {
arr[TOTAL_FRAME + 1]++;
const index = getBorderIndex(arr);
//TODO: fix weird cases but necessary for now
if (index < frameIndex || index < 0)
throw new Error(
`Logic error in frame selection. Undefined behavior could occur.\n frameIndex: ${frameIndex}\n index: ${index}`
);
let temp = arr[frameIndex];
arr[frameIndex] = arr[index];
arr[index] = temp;
// Write the CSV string to a file
let csv = arr.join(",") + "\n";
fs.writeFileSync(`${FRAME_LIST}`, csv);
return arr[index];
};
//CHECKED: Working
export const getRemainingUsedFrames = (
arr: number[]
): { rem: number[]; use: number[] } => {
const index = getBorderIndex(arr);
let remaining: number[] = arr.slice(0, index);
let used: number[] = arr.slice(index, TOTAL_FRAME + 1);
// console.log({ rem: remaining, use: used });
return { rem: remaining, use: used };
};
//CHECKED: Working
const seed = () => {
const arr: number[] = [];
if (!fs.existsSync(FRAME_DIR)) {
fs.mkdirSync(FRAME_DIR);
}
for (let i = MIN_FRAME; i <= MAX_FRAME; i++) {
arr.push(i);
}
// Write the CSV string to a file
arr.push(0); //NOTE: may need to be 1 just test
let csv = arr.join(",") + "\n";
fs.writeFileSync(`${FRAME_LIST}`, csv);
};
//CHECKED: Working
// TEST ~30ms
const timer = () => {
const start = Date.now();
seed();
const end = Date.now();
console.log(`Execution time: ${end - start} ms`);
rmdir();
};
//CHECKED: Working
const rmdir = () => {
fs.rm(FRAME_DIR, { recursive: true }, (e) => {
if (e) console.error(e);
else console.log(`successfully deleted ${FRAME_DIR}`);
});
};
// Command Line Arguments:
if (process.argv[2] === "seed") {
seed();
} else if (process.argv[2] === "delete") {
rmdir();
} else if (process.argv[2] === "time") {
timer();
} else {
console.error(
`seed.ts ${process.argv.slice(
2,
3
)} is invalid! I hope you know what you're doing!`
);
}