forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-installer.js
116 lines (102 loc) · 3.14 KB
/
command-installer.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
const path = require('path');
const fs = require('fs-plus');
module.exports = class CommandInstaller {
constructor(applicationDelegate) {
this.applicationDelegate = applicationDelegate;
}
initialize(appVersion) {
this.appVersion = appVersion;
}
getInstallDirectory() {
return '/usr/local/bin';
}
getResourcesDirectory() {
return process.resourcesPath;
}
installShellCommandsInteractively() {
const showErrorDialog = error => {
this.applicationDelegate.confirm(
{
message: 'Failed to install shell commands',
detail: error.message
},
() => {}
);
};
this.installAtomCommand(true, (error, atomCommandName) => {
if (error) return showErrorDialog(error);
this.installApmCommand(true, (error, apmCommandName) => {
if (error) return showErrorDialog(error);
this.applicationDelegate.confirm(
{
message: 'Commands installed.',
detail: `The shell commands \`${atomCommandName}\` and \`${apmCommandName}\` are installed.`
},
() => {}
);
});
});
}
getCommandNameForChannel(commandName) {
let channelMatch = this.appVersion.match(/beta|nightly/);
let channel = channelMatch ? channelMatch[0] : '';
switch (channel) {
case 'beta':
return `${commandName}-beta`;
case 'nightly':
return `${commandName}-nightly`;
default:
return commandName;
}
}
installAtomCommand(askForPrivilege, callback) {
this.installCommand(
path.join(this.getResourcesDirectory(), 'app', 'atom.sh'),
this.getCommandNameForChannel('atom'),
askForPrivilege,
callback
);
}
installApmCommand(askForPrivilege, callback) {
this.installCommand(
path.join(
this.getResourcesDirectory(),
'app',
'apm',
'node_modules',
'.bin',
'apm'
),
this.getCommandNameForChannel('apm'),
askForPrivilege,
callback
);
}
installCommand(commandPath, commandName, askForPrivilege, callback) {
if (process.platform !== 'darwin') return callback();
const destinationPath = path.join(this.getInstallDirectory(), commandName);
fs.readlink(destinationPath, (error, realpath) => {
if (error && error.code !== 'ENOENT') return callback(error);
if (realpath === commandPath) return callback(null, commandName);
this.createSymlink(fs, commandPath, destinationPath, error => {
if (error && error.code === 'EACCES' && askForPrivilege) {
const fsAdmin = require('fs-admin');
this.createSymlink(fsAdmin, commandPath, destinationPath, error => {
callback(error, commandName);
});
} else {
callback(error);
}
});
});
}
createSymlink(fs, sourcePath, destinationPath, callback) {
fs.unlink(destinationPath, error => {
if (error && error.code !== 'ENOENT') return callback(error);
fs.makeTree(path.dirname(destinationPath), error => {
if (error) return callback(error);
fs.symlink(sourcePath, destinationPath, callback);
});
});
}
};