This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
platform.service.js
152 lines (134 loc) · 4.97 KB
/
platform.service.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
// @flow
import * as childProcess from 'child_process';
import * as os from 'os';
import * as path from 'path';
import fs from 'fs';
import { remote } from 'electron';
import { PACKAGE_MANAGER } from '../config/app';
export const isWin = /^win/.test(os.platform());
export const isMac = /darwin/.test(os.platform());
// Returns path to the user's `Documents` directory
// For Windows Support
// Documents folder is much better place for project
// folders (Most programs use it as a default save location)
// Since there is a chance of being moved or users language
// might be different we are reading the value from Registry
// There might be a better solution but this seems ok so far
let winDocPath = '';
if (isWin) {
const winDocumentsRegRecord = childProcess.execSync(
'REG QUERY "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders" /v Personal',
{
encoding: 'utf8',
}
);
const winDocPathArray = winDocumentsRegRecord.split(' ');
winDocPath = winDocPathArray[winDocPathArray.length - 1]
.replace('%USERPROFILE%\\', '')
.replace(/\s/g, '');
}
export const windowsHomeDir = isWin ? path.join(os.homedir(), winDocPath) : '';
// Returns formatted command for Windows
export const formatCommandForPlatform = (command: string): string =>
isWin ? `${command}.cmd` : command;
export const PACKAGE_MANAGER_CMD = path
.join(
remote.app.getAppPath(),
'node_modules/yarn/bin',
formatCommandForPlatform(PACKAGE_MANAGER)
)
.replace('app.asar', 'app.asar.unpacked');
// Forward the host env, and append the
// project's .bin directory to PATH to allow
// package scripts to function properly.
/**
* For running tasks in a cross-platform manner, this helper does a few things:
* - Forward the host environment
* - Append the project's .bin directory to PATH to allow package scripts to
* function properly.
* - Add `FORCE_COLOR: true`, so that terminal output includes color codes.
*/
export const getBaseProjectEnvironment = (
projectPath: string,
currentEnvironment: Object = window.process.env
) => {
const pathKey = isWin ? 'Path' : 'PATH';
return {
...currentEnvironment,
// NOTE: `FORCE_COLOR` adds control characters to the output.
// If at some point we need "raw" output with no control characters, we
// should move this out into a "wrapping" function, and update current
// callsites to use it.
FORCE_COLOR: true,
[pathKey]:
currentEnvironment[pathKey] +
path.join(projectPath, 'node_modules', '.bin', path.delimiter),
};
};
// HACK: With electron-builder, we're having some issues on mac finding Node.
// This is because for some reason, the PATH is not updated properly :(
// 'fix-path' is supposed to do this for us, but it doesn't work, for unknown
// reasons.
export const initializePath = () => {
return new Promise<void>(resolve => {
if (!isMac) {
return resolve();
}
// Check if we need to fix the Path (Mac only)
childProcess.exec(
'which node',
{ env: window.process.env },
(_, nodePath) => {
if (nodePath) {
// Node found
return resolve();
}
// Get login username
const username = os.userInfo().username;
childProcess.exec(
`dscl . -read /Users/${username} UserShell`,
(error, stdout) => {
if (error) {
resolve(error);
}
// Check which shell is in use e.g. /bin/bash or /bin/zsh
// Get the name of the shell after the last instant of '/'
const shell = stdout
.substring(stdout.lastIndexOf('/') + 1, stdout.length)
.trim();
// Format the config file using correct shell
let sourceFile =
shell === 'fish' ? '.config/fish/config.fish' : `.${shell}rc`;
// Check the file actually exists first
const fileExists = fs.existsSync(`${os.homedir()}/${sourceFile}`);
// If file doesn't exist then source .bash_profile instead
if (!fileExists) {
sourceFile = '.bash_profile';
}
// For users with a standard Node installation, node will be in
// /usr/local/bin
// For users using NVM, the path to Node will be added to the shell config file.
// Add both to the PATH.
try {
childProcess.exec(
`source ~/${sourceFile} ; echo $PATH`,
(err, updatedPath) => {
if (updatedPath) {
window.process.env.PATH = `/usr/local/bin:${updatedPath}`;
}
resolve();
}
);
} catch (e) {
resolve(e);
}
}
);
}
);
});
};
export const getCopyForOpeningFolder = () =>
// For Mac users, use the more-common term 'Finder'.
// For Windows and Linux users, 'folder' should be meaningful enough.
isMac ? 'Open in Finder' : 'Open folder';