-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncr.ts
More file actions
72 lines (70 loc) · 2.3 KB
/
syncr.ts
File metadata and controls
72 lines (70 loc) · 2.3 KB
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
import * as fs from 'fs';
import * as path from 'path';
const scp = require('scp2').Client;
// if (!process.argv[2]) {
// throw 'remote path required';
// }
const remotePath = process.argv[3] || 'zpin';
const localPath = process.argv[4] || './';
const address = process.argv[2] || '192.168.2.45';
const username = 'pi' || process.argv[5];
const password = 'pass' || process.argv[6];
const toCopy: string[] = [];
const client = new scp({
port: 22,
host: address,
username,
password,
readyTimeout: 0,
});
/*console.log("starting...");
copyFile('./**').then(() =>*/ {
console.log('watching, waiting');
fs.watch(localPath, {
recursive: true,
}, async (eventType, filename) => {
if (filename?.startsWith('.')) {
/*console.log('skip dot');*/ return;
}
if (filename && fs.statSync(path.resolve(localPath, filename)).isDirectory()) {
// console.log('skip dir');
return;
}
console.log(new Date().getHours() + ':' + new Date().getMinutes(), eventType, filename);
if (filename && !toCopy.includes(filename)) {
toCopy.push(filename);
// console.info('queue ', filename, toCopy);
}
});
}/*)
.catch(err => console.error('fatal error', err));*/
setTimeout(sync, 50);
async function sync() {
while (toCopy.length) {
const filename = toCopy.shift()!;
try {
// console.info('start ', filename);
if (!toCopy.length)
await new Promise(r => setTimeout(r, 10));
await copyFile(filename);
console.info('updated %s, %i remaining', filename, toCopy.length);
} catch (e) {
console.error('error ', filename, e);
}
}
setTimeout(sync, 50);
}
function copyFile(filename: string): Promise<void> {
const remote = path.join(remotePath, filename);
return new Promise((resolve, reject) => {
// scp(filename, `${username}:${password}@${address}:${remote}`, (err: Error) => {
// if (err) reject(err);
// else resolve();
// });
console.log('copy %s to %s', path.join(localPath, filename), remote);
client.upload(path.join(localPath, filename), remote, (err: Error) => {
if (err) reject(err);
else resolve();
});
});
}