-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwatch.js
134 lines (120 loc) · 3.85 KB
/
watch.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
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
const fs = require('fs');
const path = require('path');
const readdirp = require('readdirp');
function readdir(root, opts, handler) {
if (typeof opts === 'function') {
handler = opts;
opts = {};
}
opts = Object.assign(opts, {root});
return new Promise((resolve, reject) => {
let actions = [];
readdirp(opts).on('data', (file) => {
actions.push(handler(file));
}).on('error', (reason) => {
reject(reason);
}).on('end', () => {
resolve(Promise.all(actions));
});
})
}
async function watch(mod) {
const modDir = getModDirectory();
const info = JSON.parse(fs.readFileSync(path.join(mod, 'info.json'), 'utf8'));
const destination = path.join(modDir, `${info.name}_${info.version}`);
await promisify(fs.mkdir, destination).catch(() => {});
console.log('Clearing old files...');
await clearDirectory(destination);
console.log('Copying new files...');
await copyDirectory(mod, destination);
console.log(`Watching ${mod}`);
// Watch main directory
watchDirectory(mod, mod, destination);
// Watch sub directories (fs.watch is buggy in this)
readdirp({root: mod, entryType: 'directories'}).on('data', async (file) => {
watchDirectory(file.fullPath, mod, destination);
}).on('error', (reason) => {
reject(reason);
}).on('end', () => {
console.log(`Now watching all files in ${mod}`);
});
}
function watchDirectory(dir, source, destination) {
fs.watch(dir, (type, file) => {
console.log('watcher', type, file);
let relativeDir = path.relative(source, dir);
let from = path.resolve(dir, file);
let to = path.resolve(destination, relativeDir, file);
console.log('-', from);
console.log('-', to);
fs.createReadStream(from).pipe(fs.createWriteStream(to));
});
}
function copyDirectory(from, to) {
return readdir(from, {entryType: 'both'}, (file) => {
let dest = path.resolve(to, path.relative(from, file.fullPath));
if (file.stat.isDirectory()) {
return promisify(fs.mkdir, dest).catch(() => {});
}
fs.createReadStream(file.fullPath).pipe(fs.createWriteStream(dest));
});
}
function clearDirectory(directory) {
return new Promise((resolve, reject) => {
let files = [];
let folders = [];
readdirp({root: directory, entryType: 'both'}).on('data', async (file) => {
if (file.stat.isDirectory()) {
folders.push(file.fullPath);
} else {
files.push(file.fullPath);
}
}).on('error', (reason) => {
reject(reason);
}).on('end', () => {
folders.sort((a, b) => b.length - a.length);
resolve(Promise.all(files.map(path => promisify(fs.unlink, path))).then(() => {
return Promise.all(folders.map(path => promisify(fs.rmdir, path)));
}));
});
});
}
function main() {
if (!getModDirectory()) {
return console.log('Failed to find mod directory');
}
if (!process.argv[2]) {
return console.log('Please supply a mod directory name as argument');
}
let mod = path.join(__dirname, process.argv[2]);
fs.stat(mod, (err, x) => {
if (err) return console.log(`The directory ${mod} does not exist`);
if (!x.isDirectory()) return console.log(`${mod} is not a directory`);
watch(mod).catch(reason => {
console.log(reason);
});
});
}
function getModDirectory() {
if (process.argv[3]) {
let dir = path.resolve(process.argv[3]);
let stats = fs.statSync(dir);
if (stats && stats.isDirectory()) return dir;
else return null;
}
if (process.platform == 'win32' && process.env.APPDATA) {
return path.resolve(process.env.APPDATA, 'Factorio/mods');
} else if (process.env.HOME) {
return path.resolve(process.env.HOME, '.factorio/mods');
}
return null;
}
function promisify(fn, ...args) {
return new Promise((resolve, reject) => {
fn(...args, (err, ...result) => {
if (err) reject(err);
else resolve(...result);
});
});
}
main();