Skip to content

Instantly share code, notes, and snippets.

@getanwar
Created March 11, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getanwar/3101f1e624ebbb07cf4568bcd8d9d811 to your computer and use it in GitHub Desktop.
Save getanwar/3101f1e624ebbb07cf4568bcd8d9d811 to your computer and use it in GitHub Desktop.
Sync files and folders with server
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