/ src / atom-paths.js
atom-paths.js
 1  const fs = require('fs-plus');
 2  const path = require('path');
 3  
 4  const hasWriteAccess = dir => {
 5    const testFilePath = path.join(dir, 'write.test');
 6    try {
 7      fs.writeFileSync(testFilePath, new Date().toISOString(), { flag: 'w+' });
 8      fs.unlinkSync(testFilePath);
 9      return true;
10    } catch (err) {
11      return false;
12    }
13  };
14  
15  const getAppDirectory = () => {
16    switch (process.platform) {
17      case 'darwin':
18        return process.execPath.substring(
19          0,
20          process.execPath.indexOf('.app') + 4
21        );
22      case 'linux':
23      case 'win32':
24        return path.join(process.execPath, '..');
25    }
26  };
27  
28  module.exports = {
29    setAtomHome: homePath => {
30      // When a read-writeable .atom folder exists above app use that
31      const portableHomePath = path.join(getAppDirectory(), '..', '.atom');
32      if (fs.existsSync(portableHomePath)) {
33        if (hasWriteAccess(portableHomePath)) {
34          process.env.ATOM_HOME = portableHomePath;
35        } else {
36          // A path exists so it was intended to be used but we didn't have rights, so warn.
37          console.log(
38            `Insufficient permission to portable Atom home "${portableHomePath}".`
39          );
40        }
41      }
42  
43      // Check ATOM_HOME environment variable next
44      if (process.env.ATOM_HOME !== undefined) {
45        return;
46      }
47  
48      // Fall back to default .atom folder in users home folder
49      process.env.ATOM_HOME = path.join(homePath, '.atom');
50    },
51  
52    setUserData: app => {
53      const electronUserDataPath = path.join(
54        process.env.ATOM_HOME,
55        'electronUserData'
56      );
57      if (fs.existsSync(electronUserDataPath)) {
58        if (hasWriteAccess(electronUserDataPath)) {
59          app.setPath('userData', electronUserDataPath);
60        } else {
61          // A path exists so it was intended to be used but we didn't have rights, so warn.
62          console.log(
63            `Insufficient permission to Electron user data "${electronUserDataPath}".`
64          );
65        }
66      }
67    },
68  
69    getAppDirectory: getAppDirectory
70  };