-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateShortcut.js
169 lines (146 loc) · 5.23 KB
/
createShortcut.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const path = require("path")
const fs = require("fs")
const { execSync } = require("child_process")
const os = require("os")
const createShortcut = () => {
// Get the name of the main directory
let cwd = __dirname
let dirs = cwd.split(path.sep)
let name = dirs[dirs.length - 2]
// Windows Shortcuts
if (os.platform() == "win32") {
// Paths and Icon for Windows shortcuts
let target = path.join( __dirname, "launch-windows.bat")
let icon = path.join( __dirname, "superalgos.ico")
let shortcutPaths = [
path.join(os.homedir(), "Desktop", `${name}.lnk`),
path.join(os.homedir(),
"AppData",
"Roaming",
"Microsoft",
"Windows",
"Start Menu",
"Programs",
`${name}.lnk`)
]
// Place Shortcuts using powershell
for (let dir of shortcutPaths) {
let command = `$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut("${dir}"); $S.TargetPath = "${target}"; $S.IconLocation = "${icon}"; $S.Save()`
execSync(command,
{
shell: "powershell.exe",
execArgv: "-ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -Command"
},
(error, stdout) => {
if (error) {
console.log('')
console.log("There was an error installing a shortcut: ")
console.log('')
console.log(error)
return false
} else {
console.log('')
console.log("Shortcut added successfully!")
console.log('')
console.log(stdout)
}
})
}
return "Shortcuts created for windows system"
// Linux Shortcuts
} else if (os.platform() == "linux") {
// Check for Ubuntu
let version = os.version()
if (version.includes("Ubuntu")) {
// Paths and Icon for Ubuntu shortcuts
let icon = path.join( __dirname,"..", "/Projects/Foundations/Icons/superalgos.png")
// Create .desktop shortcut file
fs.writeFileSync( `${name}.desktop`,
`[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=${name}
Comment=Launch Shortcut for Superalgos
Path=${__dirname}
Exec=${__dirname}/launch-linux-mac.sh
Terminal=true
Icon=${icon}
Categories=Application;`,
)
// Set shortcut as executable
fs.chmodSync( `${name}.desktop`, "775" )
// Place shortcut in proper folders
let command = `cp ${name}.desktop ~/Desktop/${name}.desktop & cp ${name}.desktop ~/.local/share/applications/${name}.desktop`
execSync(command,
(error) => {
if (error) {
console.log('')
console.log("There was an error installing a shortcut: ")
console.log('')
console.log( error )
return false
} else {
console.log('')
console.log("Shortcuts added successfully!")
}
// Remove temporary .desktop file
fs.unlinkSync( `${name}.desktop` )
})
return "Shortcuts created for Ubuntu"
} else {
console.log( "Automatic shortcut creation is not yet supported on your flavor of linux. If you would like to see this feature add, message @harrellbm on telegram or discord to ask how you can help!")
return 'Linux shortcuts supported for Ubuntu only'
}
// Mac Shortcuts
} else if (os.platform() == "darwin") {
path.join( __dirname,"/superalgos.ico")
const createShortcutCommand = `chmod +x ${name}.command & cp ${name}.command ~/Desktop/${name}.command`
const installFileIconcommand = `npm install -g fileicon`
const changeIconCommand = `./node_modules/fileicon/bin/fileicon set ~/Desktop/${name}.command ./Launch-Scripts/superalgos.ico`
const unInstallFileIconcommand = `npm uninstall -g fileicon`
try {
// Create .desktop shortcut file
fs.writeFileSync( `${name}.command`,
`#!/bin/sh
cd ${__dirname}
cd ..
node run
"$SHELL"`,
)
// Place shortcut in proper folders
execSync( createShortcutCommand,{
timeout: 30000
})
// Remove temporary .command file
fs.unlinkSync( `${name}.command` )
//Install fileicon utility
execSync(installFileIconcommand, {
stdio: 'inherit',
timeout: 30000
})
//change Icon
execSync(changeIconCommand, {
stdio: 'inherit',
timeout: 30000
})
//Un-Install fileicon utility
execSync(unInstallFileIconcommand, {
stdio: 'inherit',
timeout: 30000
})
} catch (error) {
console.log('')
console.log("There was an error installing a shortcut: ")
console.log('')
console.log( error )
return false
}
console.log("Shortcuts added successfully!")
return 'Shortcuts created for Mac'
// Misc Operating System
} else {
console.log("Automatic shortcut creation is not currently supported on your operating system. If you would like to see your operating system added please reach out on discord or telegram to let the devs know.")
return 'Shortcuts not supported on your system'
}
}
module.exports = createShortcut