-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
50 lines (40 loc) · 1.18 KB
/
run.js
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
/* G-Code editor and for Niall @ Pioneer Valley Frameworks
*******Set code Name********/
var codeName = '805.i';
//Header imports and variables
var fs = require('fs');
var data;
var index;
var currentIndex = 65535;
var currentToReplace;
var replaceWith;
var currentIndex;
//Read in Gcode file
data = fs.readFileSync('codes/' + codeName, 'utf8');
fs.rename('codes/' + codeName, 'codes/original-' + codeName);
data = data.replace(/(?:\r\n|\r|\n)/g, " ");
var arr = data.split(' ');
//Loop over array and replace codes
for (var i = 10; i >= 10; i = i + 5) {
currentCode = 'N' + currentIndex.toString();
index = arr.indexOf(currentCode);
if (index != -1) {
replaceWith = 'N' + i.toString();
arr[index] = replaceWith;
currentIndex = currentIndex + 5;
console.log('Replacing: ' + currentCode + ' with ' + replaceWith);
} else {
console.log('Finished!');
i = 0;
}
}
//Revert data to string and write file
data = arr.join(' ');
var completedFile = data.replace(/ N/g, '\nN');
fs.writeFile('codes/' + codeName, completedFile, 'utf8', function (err) {
if (err) {
console.log('File could not be saved!');
} else {
console.log('File has been saved!');
}
});