1
|
|
|
import Config from "./Config"; |
2
|
|
|
import CLI from "./CLI"; |
3
|
|
|
import Watcher from "./Watcher"; |
4
|
|
|
import Uploader from "./Uploader"; |
5
|
|
|
import InitConfig from './InitConfig'; |
6
|
|
|
import * as upath from "upath"; |
7
|
|
|
import * as fs from "fs"; |
8
|
|
|
import * as process from 'process'; |
9
|
|
|
|
10
|
|
|
const observatory = require("observatory"); |
11
|
|
|
|
12
|
|
|
export default class Sync { |
13
|
|
|
config: Config; |
14
|
|
|
watch: Watcher; |
15
|
|
|
cli: CLI; |
16
|
|
|
uploader: Uploader; |
17
|
|
|
task: any; |
18
|
|
|
|
19
|
|
|
constructor() { |
20
|
|
|
this.cli = new CLI(); |
21
|
|
|
|
22
|
|
|
this.task = observatory.add("Initializing..."); |
23
|
|
|
|
24
|
|
|
if (this.cli.hasStartupCommand("init")) { |
25
|
|
|
new InitConfig(); |
26
|
|
|
} else { |
27
|
|
|
// Get config |
28
|
|
|
this.config = new Config(this.cli); |
29
|
|
|
this.task.status("reading config"); |
30
|
|
|
|
31
|
|
|
this.config.ready().then(() => { |
32
|
|
|
// Get Command line interface |
33
|
|
|
//this.cli.write("Connecting"); |
34
|
|
|
//this.cli.startProgress(); |
35
|
|
|
this.task.status("watching files"); |
36
|
|
|
|
37
|
|
|
var ori = this.config.localPath; |
38
|
|
|
var root = process.cwd(); |
39
|
|
|
if (upath.isAbsolute(this.config.localPath)) { |
40
|
|
|
var mod = upath.normalizeSafe(this.config.localPath.replace(root, '')); |
41
|
|
|
if (mod == ori) { |
42
|
|
|
throw new Error("Cannot find absolute path"); |
43
|
|
|
} else { |
44
|
|
|
this.config.localPath = '.' + mod; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
this.config.localPath = upath.normalizeSafe(this.config.localPath); |
49
|
|
|
fs.exists(this.config.localPath, function (es) { |
50
|
|
|
if (!es) { |
51
|
|
|
throw new Error("Local path not exists"); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
// Setup the uploader |
56
|
|
|
this.uploader = new Uploader(this.config, this.cli); |
57
|
|
|
|
58
|
|
|
// Initiate file watch |
59
|
|
|
this.watch = new Watcher(this.uploader, this.config, this.cli); |
60
|
|
|
|
61
|
|
|
// When files are found start connection |
62
|
|
|
|
63
|
|
|
return this.watch.ready(); |
64
|
|
|
}).then(() => { |
65
|
|
|
this.task.status("connecting server"); |
66
|
|
|
return this.uploader.connect(); |
67
|
|
|
}).then(() => { |
68
|
|
|
// All done, stop indicator and show workspace |
69
|
|
|
// this.cli.stopProgress(); |
70
|
|
|
this.task.done("Connected").details(this.config.host); |
71
|
|
|
this.cli.workspace(); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |