Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement #133

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/app/forcersync/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ const ForceRsync = BaseController.extend<ForceRsyncInterface>({
},
index: function (props) {
let cliService = this.returnCliService();
// if (cliService.hasStartupCommand('forcersync')) {
// let _devRsyncPushService = this.returnDevRsyncPushService(cliService,props);
// return;
// }
// Ini Di panggil dari menu dan dari devsync nested prompt
if (cliService.hasStartupCommand('singlesync') || props.action == "single_sync_nested_prompt") {
let _singleSyncService = this.returnSingleSyncService(cliService, props);
return;
Expand Down
86 changes: 3 additions & 83 deletions src/app/forcersync/compute/SingleSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const SingleSync = SyncPush.extend<SingleSyncInterface>({
},
submitPushSelective: function (props) {
try {
// console.log('_source',_source,props);

// console.log('_source',_source,props);
let extraWatch: Array<{
path: string
ignores: Array<string>
Expand All @@ -52,92 +52,12 @@ const SingleSync = SyncPush.extend<SingleSyncInterface>({

switch (props.option.toLowerCase()) {
case 'upload':
return this._recursiveRsync(extraWatch,0);
return this._recursiveRsync(extraWatch, 0);
case 'download':
this._syncPull.setOnListener(this._onListener);
return this._syncPull._recursiveRsync(extraWatch,0);
}

return;

let _filterPatternRules = this._filterPatternRule();
// console.log('_filterPatternRules',_filterPatternRules);
let config = this._config;

let _source = (() => {
switch (props.option.toLowerCase()) {
case 'upload':
let _local_path = props.single_sync_list;
return upath.normalize(_local_path);
case 'download':
return config.username + '@' + config.host + ':' + upath.normalize(config.base_path + '/' + props.single_sync_list);
}
})()

let _destination = (() => {
switch (props.option.toLowerCase()) {
case 'upload':
return config.username + '@' + config.host + ':' + upath.normalize(config.base_path + '/' + props.single_sync_list);
case 'download':
let _local_path = upath.normalize(props.single_sync_list);
return upath.normalize(_local_path);
}
})()

var rsync = Rsync.build({
/* Support multiple source too */
source: _source,
destination: _destination,
/* Include First */
// include : _filterPatternRules.pass,
/* Exclude after include */
// exclude: _filterPatternRules.ignores,
// flags : '-vt',
flags: 'avzL',
set: '--usermap=*:' + this._config.username + ' --groupmap=*:' + this._config.username + ' --chmod=D2775,F775 --size-only --checksum ' + (config.mode == "hard" ? '--delete' : ''),
shell: 'ssh -i ' + config.privateKeyPath + ' -p ' + config.port
});
console.log('_source', _source)
console.log('rsync command -> ', rsync.command());

switch (props.option.toLocaleLowerCase()) {
case 'upload':
this.submitPush()
break;
case 'download':
break;
return this._syncPull._recursiveRsync(extraWatch, 0);
}

var child = child_process.spawn(rsync.command(), [''], {
env: { IS_PROCESS: "single_sync" },
stdio: 'inherit',//['pipe', process.stdout, process.stderr]
shell: true
});

child.on('exit', (e, code) => {
this._onListener({
action: "exit",
return: {
e, code
}
})
});

/** 27/Jun/2021
* Use rsync by library
* But now still study with this.
* Only use to get the result command */
// rsync.execute(
// function (error: any, code: any, cmd: any) {
// // we're done
// }, function (data: any) {
// console.log(data.toString());
// // do things like parse progress
// }, function (data: any) {
// console.log('error', data.toString());
// // do things like parse error output
// }
// );
} catch (ex: any) {
console.log('submitPush - ex ', ex);
process.exit(1);
Expand Down
212 changes: 108 additions & 104 deletions src/app/forcersync/compute/SyncPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
let config = this._config;
let _local_path = config.local_path;
let _is_file = false;

if (extraWatchs[index] != null) {

// Convert absolute path to relative
let _remote_path = upath.normalizeSafe(config.base_path + '/' + extraWatchs[index].path);
if (isFile == true) {
Expand All @@ -46,17 +48,10 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
}

process.stdout.write(chalk.green('Rsync Download | ') + _local_path + ' << ' + _remote_path + '\n');
// if (extraWatchs[index + 1] != null) {
// this._recursiveRsync(extraWatchs, index + 1);
// } else {
// this._onListener({
// action: "exit",
// return: {}
// })
// })

let _delete_mode_active = config.mode == "hard" ? true : false;
_delete_mode_active = extraWatchs[index].includes.length > 0 ? false : _delete_mode_active

var rsync = Rsync.build({
/* Support multiple source too */
source: config.username + '@' + config.host + ':' + _remote_path,
Expand All @@ -75,10 +70,10 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
shell: 'ssh -i ' + config.privateKeyPath + ' -p ' + config.port
});


process.stdout.write(chalk.green("Rsync Download | ") + 'rsync command -> ' + rsync.command() + '\n');

var shell = os.platform() === 'win32' ? "C:\\Program Files\\Git\\bin\\bash.exe" : 'bash';
// Remember command exit is called on initPtyProcess, check it
var ptyProcess = this.iniPtyProcess(shell, []);
ptyProcess.write(rsync.command() + '\r');
let firstString = null;
Expand All @@ -103,20 +98,6 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
}
});

// ptyProcess.write('pwd\n')
// var _readLine = this.initReadLine();
// var theCallback = (key: any, data: any) => {
// // console.log(data);
// if (data.sequence == "\u0003") {
// ptyProcess.write('\u0003');
// _readLine = this.initReadLine();
// process.stdin.off('keypress', theCallback);
// recursive();
// return;
// }
// ptyProcess.write(data.sequence);
// }

ptyProcess.on('exit', (exitCode: any, signal: any) => {
// process.stdin.off('keypress', theCallback);
ptyProcess.kill();
Expand All @@ -125,13 +106,19 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
if (_is_file == true) {
this._recursiveRsync(extraWatchs, index, _is_file);
} else {
// Cache it to temp
this._cacheToTemp(extraWatchs, index, isFile);
// And next recursive
this._recursiveRsync(extraWatchs, index + 1);
}
} else {
if (_is_file == true) {
this._recursiveRsync(extraWatchs, index, _is_file);
return;
}
// Cache it to temp
this._cacheToTemp(extraWatchs, index, isFile);
// ANd exit
this._onListener({
action: "exit",
return: {
Expand All @@ -141,9 +128,104 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
}
});

// var recursive = () => {
// process.stdin.on('keypress', theCallback);
// }
}
} catch (ex) {
console.log('_recursiveRsync - ex ', ex);
}
},
_cacheToTemp(extraWatchs, index = 0, isFile = false) {
try {
let config = this._config;
let _local_path = config.local_path;
let _is_file = false;
if (extraWatchs[index] != null) {

_local_path = path.relative(upath.normalizeSafe(path.resolve("")), upath.normalizeSafe(_local_path + '/' + extraWatchs[index].path));
let _remote_path = extraWatchs[index].path;
if (isFile == true) {
/* Remove file path to be dirname only */
_local_path = path.relative(upath.normalizeSafe(path.resolve("")), upath.normalizeSafe(_local_path + '/' + dirname(extraWatchs[index].path)));
_local_path = upath.normalizeSafe('./' + _local_path);
_remote_path = dirname(_remote_path);
} else {
_local_path = upath.normalizeSafe('./' + _local_path + '/')
}

process.stdout.write(chalk.yellow('Rsync Download Cache | ') + _local_path + ' >> ' + upath.normalize(this.tempFolder+"/"+_remote_path) + '\n');

let _delete_mode_active = config.mode == "hard" ? true : false;
_delete_mode_active = extraWatchs[index].includes.length > 0 ? false : _delete_mode_active
var rsync = Rsync.build({
/* Support multiple source too */
source: _local_path,
// source : upath.normalize(_local_path+'/'),
destination: upath.normalize(this.tempFolder + "/" + _remote_path),
/* Include First */
include: extraWatchs[index].includes,
/* Exclude after include */
exclude: extraWatchs[index].ignores,
// set: '--usermap=*:' + this._config.username + ' --groupmap=*:' + this._config.username + ' --chmod=D2775,F775 --size-only --checksum ' + (_delete_mode_active == true ? '--force --delete' : ''),
// flags : '-vt',
flags: '-avzLm',
// shell: 'ssh -i ' + config.privateKeyPath + ' -p ' + config.port
});

// console.log("rsync commandnya :: ", rsync.command());
// process.stdout.write(chalk.green('Rsync Upload Cache | ') + 'rsync command -> ' + rsync.command() + '\n');

var shell = os.platform() === 'win32' ? "C:\\Program Files\\Git\\bin\\bash.exe" : 'bash';
var ptyProcess = this.iniPtyProcess(shell, []);
if (_is_file == false) {
ptyProcess.write('ls ' + _local_path + ' ' + '\r');
}
setTimeout(() => {
if (ptyProcess != null) {
ptyProcess.write(rsync.command() + '\r');
}
}, 2000);

ptyProcess.on('data', (data: any) => {
// console.log(data)
// let _text = this._stripAnsi(data.toString());
let _split = data.split(/\n/);// this._stripAnsi(data.toString());
if (_split != "") {
for (var a = 0; a < _split.length; a++) {
switch (_split[a]) {
case '':
case '\r':
case '\u001b[32m\r':
break;
default:
// process.stdout.write(chalk.green('Rsync Upload Cache | '));
// process.stdout.write(this._stripAnsi(_split[a]).replace('X', '') + '\n');
break;
}
}
}
if (data.includes('Not a directory')) {
_is_file = true;
ptyProcess.write('exit' + '\r');
}
});

ptyProcess.on('exit', (exitCode: any, signal: any) => {
// process.stdin.off('keypress', theCallback);
ptyProcess.kill();
ptyProcess = null;
if (extraWatchs[index + 1] != null) {
if (_is_file == true) {
this._cacheToTemp(extraWatchs, index, _is_file);
} else {
// You dont need it
// this._cacheToTemp(extraWatchs, index + 1);
}
} else {
if (_is_file == true) {
this._cacheToTemp(extraWatchs, index, _is_file);
return;
}
}
});

// recursive();
}
Expand Down Expand Up @@ -183,85 +265,7 @@ const SyncPull = SyncPush.extend<Omit<SynPullInterface, 'model'>>({
}
}
}

this._recursiveRsync(extraWatch, 0);
return;

let config = this._config;
let _local_path = config.local_path;

// Convert absolute path to relative
_local_path = path.relative(upath.normalizeSafe(path.resolve("")), upath.normalizeSafe(_local_path));

var rsync = Rsync.build({
/* Support multiple source too */
source: config.username + '@' + config.host + ':' + config.base_path + '/',
// source : upath.normalize(_local_path+'/'),
destination: upath.normalizeSafe('./' + _local_path + '/'),
/* Include First */
include: _filterPatternRules.pass,
/* Exclude after include */
exclude: _filterPatternRules.ignores,
// flags : '-vt',
flags: 'avzL',
set: '--size-only --checksum',
// set : '--no-perms --no-owner --no-group',
// set : '--chmod=D777,F777',
// set : '--perms --chmod=u=rwx,g=rwx,o=,Dg+s',
shell: 'ssh -i ' + config.privateKeyPath + ' -p ' + config.port
});


console.log('rsync command -> ', rsync.command());

var shell = os.platform() === 'win32' ? "C:\\Program Files\\Git\\bin\\bash.exe" : 'bash';
var ptyProcess = this.iniPtyProcess(shell, []);
ptyProcess.write(rsync.command() + '\r');
ptyProcess.on('exit', (exitCode: any, signal: any) => {
this._onListener({
action: "exit",
return: {
exitCode, signal
}
})
});

// ptyProcess.write('pwd\n')
var _readLine = this.initReadLine();
var theCallback = (key: any, data: any) => {
// console.log(data);
if (data.sequence == "\u0003") {
ptyProcess.write('\u0003');
_readLine = this.initReadLine();
process.stdin.off('keypress', theCallback);
recursive();
return;
}
ptyProcess.write(data.sequence);
}

var recursive = () => {
process.stdin.on('keypress', theCallback);
}

recursive();
return;

/* Old code style */
var child = child_process.spawn(rsync.command(), [''], {
env: { IS_PROCESS: "sync_pull" },
stdio: 'inherit',//['pipe', process.stdout, process.stderr]
shell: true
});

child.on('exit', (e, code) => {
this._onListener({
action: "exit",
return: {
e, code
}
})
});

} catch (ex: any) {
console.log('submitPush - ex ', ex);
Expand Down
Loading