This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.js
119 lines (105 loc) · 3.65 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*global services, plugin*/
({
name: 'c9Scripts',
git: {
getClone: function(self) {
return '/home/ubuntu/c9-scripts';
},
getRemote: function(self) {
return 'git@github.com:fnkr/c9-scripts.git';
}
},
console: {
getPrefix: function(self) {
return '[' + self.name + ']';
},
log: function(self, args) {
console.log.apply(console, [self.console.getPrefix(self)].concat(args));
},
error: function(self, args) {
console.error.apply(console, [self.console.getPrefix(self)].concat(args));
}
},
notification: {
error: (self, err) => services['dialog.error'].show(err)
},
onError: function(self, err) {
self.console.error(self, err);
self.notification.error(self, self.name + ' failed: ' + err.message);
},
init: function() {
this.doInstallOrUpgradeIfHostedWorkspace(this);
},
doInstallOrUpgradeIfHostedWorkspace: function(self) {
self.console.log(self, 'doInstallOrUpgradeIfHostedWorkspace');
services.proc.execFile('sh', {
args: ['-c', 'echo -n "$C9_HOSTNAME"'], cwd: '/'
}, (err, stdout, stderr) => self.doInstallOrUpgradeIfHostedWorkspaceCallback(self, err, stdout, stderr));
},
doInstallOrUpgradeIfHostedWorkspaceCallback: function(self, err, stdout, stderr) {
self.console.log(self, 'doInstallOrUpgradeIfHostedWorkspaceCallback');
if (err) {
return self.onError(self, err);
}
if (stdout.endsWith('.c9users.io')) {
self.doInstallOrUpgrade(self);
}
},
doInstallOrUpgrade: function(self) {
self.console.log(self, 'doInstallOrUpgrade');
services.proc.execFile('test', {
args: ['-d', self.git.getClone()], cwd: '/'
}, (err, stdout, stderr) => self.doInstallOrUpgradeCallback(self, err, stdout, stderr));
},
doInstallOrUpgradeCallback: function(self, err, stdout, stderr) {
self.console.log(self, 'doInstallOrUpgradeCallback');
if (err && (err.code == 1)) {
// not installed
self.doClone(self);
} else if (err) {
// error
self.onError(self, err);
} else {
// installed
self.doUpgrade(self);
}
},
doClone: function(self, callback) {
self.console.log(self, 'doClone');
services.proc.execFile('git', {
args: ['clone', self.git.getRemote(), self.git.getClone()], cwd: '/'
}, (err) => self.doCloneCallback(self, err));
},
doCloneCallback: function(self, err) {
self.console.log(self, 'doCloneCallback');
if (err) {
return self.onError(self, err);
}
self.doInstall(self);
},
doUpgrade: function(self) {
self.console.log(self, 'doUpgrade');
services.proc.execFile('git', {
args: ['pull'], cwd: self.git.getClone()
}, (err) => self.doUpgradeCallback(self, err));
},
doUpgradeCallback: function(self, err) {
self.console.log(self, 'doUpgradeCallback');
if (err) {
return self.onError(self, err);
}
self.doInstall(self);
},
doInstall: function(self) {
self.console.log(self, 'doInstall');
services.proc.execFile('bash', {
args: ['./install'], cwd: self.git.getClone()
}, (err) => self.doInstallCallback(self, err));
},
doInstallCallback: function(self, err) {
self.console.log(self, 'doInstallCallback');
if (err) {
return self.onError(self, err);
}
}
}).init();