Created
March 11, 2021 07:46
-
-
Save getanwar/3101f1e624ebbb07cf4568bcd8d9d811 to your computer and use it in GitHub Desktop.
Sync files and folders with server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chokidar = require('chokidar'); | |
const { exec } = require('child_process'); | |
const watcher = chokidar.watch('.', { | |
ignored: ['**/node_modules/**/*', '**/.git/**/*'], | |
ignoreInitial: true, | |
interval: 1500, | |
binaryInterval: 1500, | |
}); | |
watcher.on('change', path => { | |
const arr = path.split('/'); | |
let folder; | |
if (arr.length > 1) { | |
// in side a folder | |
folder = arr.slice(0, -1).join('/'); | |
} | |
function handleError(err) { | |
if (err.message.includes('(code 12)')) { | |
console.log(folder); | |
// just sync the folder | |
return exec( | |
`rsync -r ./${folder} USER@HOST:PROJECT_ROOT/${folder}`, | |
e => { | |
if (e) { | |
return handleError(e); | |
} | |
console.log('UPLOADED', path); | |
} | |
); | |
} | |
throw err; | |
} | |
exec( | |
`rsync -r ./${path} USER@HOST:PROJECT_ROOT/${path}`, | |
err => { | |
if (err) { | |
return handleError(err); | |
} | |
console.log('UPLOADED', path); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment