forked from Fllorent0D/MacNetworkDriveJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (94 loc) · 3.08 KB
/
index.js
File metadata and controls
113 lines (94 loc) · 3.08 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
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
"use strict"
const exec = require('child-process-promise').exec
const MAX_BUFFER_SIZE = 2000 * 1024;
const applescript = require('applescript')
const macNetworkDrive = {
list: function list() {
let fetchDisks = exec('/bin/df', {
maxBuffer: MAX_BUFFER_SIZE
});
let completePromise = new Promise((resolve, reject) => {
fetchDisks.then(result => {
if (typeof result.stderr === 'string' && result.stderr.length !== 0) {
reject(stderr)
}
let pathList = result.stdout.split(/\s*[\n\r]+/g);
let drivePath = {}
pathList.splice(0, 1)
let regex = /^\/\/\S*@(\S*)\s*\S*\s*\S*\s*\S*\s*\S*\s*\S*\s*\S*\s*\S*\s*(\S*)$/
let re = new RegExp(regex, 'i')
for (let line of pathList) {
let matches = line.match(re);
if (matches) {
drivePath[matches[1].replace(/\\/g, '/')] = matches[2];
}
}
resolve(drivePath)
}).catch(err => {
reject(err)
})
})
return completePromise;
},
find: function find(drivePath) {
let completePromise = new Promise((resolve, reject) => {
if ('string' !== typeof drivePath || 0 === drivePath.length) {
reject('Invalid path');
}
macNetworkDrive.list().then(networkDrives => {
for (let currentDrivePath in networkDrives) {
if (!networkDrives.hasOwnProperty(currentDrivePath)) {
continue;
}
if (currentDrivePath.toUpperCase() === drivePath.toUpperCase()) {
resolve(networkDrives[currentDrivePath]);
}
}
resolve(undefined);
})
})
return completePromise;
},
mount: function mount(drivePath, localPath = null, username = null, password = null) {
/* parametetr localPath is unused -> allow to have the same signature */
let completePromise = new Promise((resolve, reject) => {
macNetworkDrive.find(drivePath).then(result => {
if (result !== undefined) {
resolve(result)
} else {
let connectPath = ''
let serverPath = drivePath
if (username != null && password != null) {
connectPath = `${username}:${password}@`
}
let pathDrive = `smb://${connectPath}${serverPath}`
let mountScript = `
tell application "Finder"
with timeout of 2 seconds
try
mount volume "${pathDrive}"
end try
end timeout
end tell`
applescript.execString(mountScript, (err, result) => {
if (err || result === undefined) {
if (err === undefined)
reject('Unable to connect')
reject(err)
}
macNetworkDrive.find(drivePath).then(result => {
resolve(result)
})
})
}
})
})
return completePromise;
},
unmount: function unmount() {
}
}
exports.list = macNetworkDrive.list
exports.find = macNetworkDrive.find
exports.mount = macNetworkDrive.mount
exports.unmount = macNetworkDrive.unmount