Skip to content

Commit

Permalink
TypeScript 1.7 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
serkanyersen committed Dec 20, 2015
1 parent 90c3981 commit 64ca4c9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,6 +7,8 @@ lib-cov
*.pid
*.gz

.idea

pids
logs
results
Expand Down
2 changes: 1 addition & 1 deletion bin/syncjs
@@ -1,5 +1,5 @@
#!/usr/bin/env node
var path = require('path');
var Sync = require(path.resolve(__dirname, '../dist/index.js')).default;
var Sync = require(path.resolve(__dirname, '../dist/src/index.js')).default;

new Sync();
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "sync.js",
"version": "2.0.0",
"main": "dist/main.js",
"main": "dist/src/index.js",
"preferGlobal": true,
"scripts": {
"postinstall": "./node_modules/.bin/tsc"
Expand All @@ -18,7 +18,7 @@
"moment": "latest",
"readline-sync": "^1.2.21",
"scp2": "^0.2.2",
"typescript": "1.6.2",
"typescript": "^1.7.*",
"upath": "^0.1.6"
},
"description": "sync.js let's you keep your remote files in sync with your local copy. Whenever you make a change on your local project, sync.js uploads the changed files to remote server using `scp` command.",
Expand Down
82 changes: 69 additions & 13 deletions src/classes/Watcher.ts
Expand Up @@ -5,8 +5,15 @@ import Uploader from "./Uploader";
import Config from "./Config";
import CLI from "./CLI";

interface PausedChanges {
event: string;
path: string;
}

export default class Watcher {
files: FSWatcher;
private paused = false;
private changes: PausedChanges[] = [];

constructor(
private uploader: Uploader,
Expand All @@ -24,7 +31,7 @@ export default class Watcher {

// Attach events
["all", "add", "change", "unlink", "unlinkDir"].forEach(method => {
this.files.on(method, this[method]);
this.files.on(method, this.pauseHandler(method));
});
}

Expand All @@ -34,23 +41,72 @@ export default class Watcher {
});
}

all = (event: string, path: string) => {
pause() {
this.paused = true;
}

let eventToWord = {
add: chalk.green("ADDED"),
change: chalk.green("CHANGED"),
unlink: chalk.red("DELETED"),
unlinkDir: chalk.red("DELETED")
resume(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.cli.write(`${ this.changes.length } files had changed while paused.`);
this.changes.forEach((change) => {
this.cli.write(`${ this.eventToWord[change.event] }: ${ change.path } `);
this.cli.read('Do you want to make these uploads? [Y/N] ').then((answer) => {
if (answer[0].toUpperCase() === 'Y') {
this.cli.write('Uploading.');
resolve();
} else {
reject();
this.cli.write('Resuming.');
}
// Set pause false the last thing. Even if changes happen
// during this prompt is on screen. They are still caught
this.paused = false;
});
});
});
}

eventToWord = {
add: chalk.green("ADDED"),
change: chalk.green("CHANGED"),
unlink: chalk.red("DELETED"),
unlinkDir: chalk.red("DELETED")
};


private pauseHandler(method: string): Function {
return (...args: string[]) => {
let path: string,
event = method;

// Handle argument difference
if (method === 'all') {
path = args[1];
event = args[0]
} else {
path = args[0];
}

// If paused store the values
if (this.paused ) {
this.changes.push({ event, path });
} else {
// If not, continue as ususal
this[method](...args);
}
}
}


if (event in eventToWord) {
private all = (event:string, path:string) => {
if (event in this.eventToWord) {
this.cli.workspace();
this.cli.write(`\n${eventToWord[event]} ${path}`);
this.cli.write(`\n${ this.eventToWord[event]} ${path}`);
this.cli.startProgress();
}
};

add = (path: string) => {
private add = (path: string) => {
this.uploader.uploadFile(path).then(remote => {
this.cli.stopProgress();
this.cli.write(`\nSAVED ${remote}`);
Expand All @@ -60,7 +116,7 @@ export default class Watcher {
});
};

change = (path: string) => {
private change = (path: string) => {
this.uploader.uploadFile(path).then(remote => {
this.cli.stopProgress();
this.cli.write(`\nSAVED ${remote}`);
Expand All @@ -70,7 +126,7 @@ export default class Watcher {
});
};

unlink = (path: string) => {
private unlink = (path: string) => {
this.uploader.unlinkFile(path).then(remote => {
this.cli.stopProgress();
this.cli.write(`\nSAVED ${remote}`);
Expand All @@ -80,7 +136,7 @@ export default class Watcher {
});
};

unlinkDir = (path: string) => {
private unlinkDir = (path: string) => {
this.uploader.unlinkFolder(path).then(remote => {
this.cli.stopProgress();
this.cli.write(`\nSAVED ${remote}`);
Expand Down

0 comments on commit 64ca4c9

Please sign in to comment.