-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom-paths.js
70 lines (63 loc) · 1.93 KB
/
atom-paths.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
const fs = require('fs-plus');
const path = require('path');
const hasWriteAccess = dir => {
const testFilePath = path.join(dir, 'write.test');
try {
fs.writeFileSync(testFilePath, new Date().toISOString(), { flag: 'w+' });
fs.unlinkSync(testFilePath);
return true;
} catch (err) {
return false;
}
};
const getAppDirectory = () => {
switch (process.platform) {
case 'darwin':
return process.execPath.substring(
0,
process.execPath.indexOf('.app') + 4
);
case 'linux':
case 'win32':
return path.join(process.execPath, '..');
}
};
module.exports = {
setAtomHome: homePath => {
// When a read-writeable .atom folder exists above app use that
const portableHomePath = path.join(getAppDirectory(), '..', '.atom');
if (fs.existsSync(portableHomePath)) {
if (hasWriteAccess(portableHomePath)) {
process.env.ATOM_HOME = portableHomePath;
} else {
// A path exists so it was intended to be used but we didn't have rights, so warn.
console.log(
`Insufficient permission to portable Atom home "${portableHomePath}".`
);
}
}
// Check ATOM_HOME environment variable next
if (process.env.ATOM_HOME !== undefined) {
return;
}
// Fall back to default .atom folder in users home folder
process.env.ATOM_HOME = path.join(homePath, '.atom');
},
setUserData: app => {
const electronUserDataPath = path.join(
process.env.ATOM_HOME,
'electronUserData'
);
if (fs.existsSync(electronUserDataPath)) {
if (hasWriteAccess(electronUserDataPath)) {
app.setPath('userData', electronUserDataPath);
} else {
// A path exists so it was intended to be used but we didn't have rights, so warn.
console.log(
`Insufficient permission to Electron user data "${electronUserDataPath}".`
);
}
}
},
getAppDirectory: getAppDirectory
};