forked from atom/atom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-installer.js
More file actions
88 lines (76 loc) · 2.55 KB
/
command-installer.js
File metadata and controls
88 lines (76 loc) · 2.55 KB
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
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 => {
if (error) return showErrorDialog(error)
this.installApmCommand(true, error => {
if (error) return showErrorDialog(error)
this.applicationDelegate.confirm({
message: 'Commands installed.',
detail: 'The shell commands `atom` and `apm` are installed.'
}, () => {})
})
})
}
installAtomCommand (askForPrivilege, callback) {
this.installCommand(
path.join(this.getResourcesDirectory(), 'app', 'atom.sh'),
this.appVersion.includes('beta') ? 'atom-beta' : 'atom',
askForPrivilege,
callback
)
}
installApmCommand (askForPrivilege, callback) {
this.installCommand(
path.join(this.getResourcesDirectory(), 'app', 'apm', 'node_modules', '.bin', 'apm'),
this.appVersion.includes('beta') ? 'apm-beta' : '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()
this.createSymlink(fs, commandPath, destinationPath, error => {
if (error && error.code === 'EACCES' && askForPrivilege) {
const fsAdmin = require('fs-admin')
this.createSymlink(fsAdmin, commandPath, destinationPath, callback)
} 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)
})
})
}
}