/
env.js
100 lines (84 loc) 路 2.15 KB
/
env.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
import path from 'path'
import AppPaths, { ensureAppDirectoriesSync } from './paths'
let envId = 0
const patchEnvPath = () => {
if (process.platform === 'darwin') {
process.env.PATH += (process.env.PATH.endsWith(path.delimiter) ? '' : path.delimiter) + '/Library/TeX/texbin'
}
}
export class AppEnvironment {
constructor (options) {
this._id = envId++
this._appPaths = new AppPaths(options.userDataPath)
this._debug = !!options.debug
this._isDevMode = !!options.isDevMode
this._verbose = !!options.verbose
this._safeMode = !!options.safeMode
}
/**
* Returns an unique identifier that can be used with IPC to identify messages from this environment.
*
* @returns {number} Returns an unique identifier.
*/
get id () {
return this._id
}
/**
* @returns {AppPaths}
*/
get paths () {
return this._appPaths
}
/**
* @returns {boolean}
*/
get debug () {
return this._debug
}
/**
* @returns {boolean}
*/
get isDevMode () {
return this._isDevMode
}
/**
* @returns {boolean}
*/
get verbose () {
return this._verbose
}
/**
* @returns {boolean}
*/
get safeMode () {
return this._safeMode
}
}
/**
* Create a (global) application environment instance and bootstraps the application.
*
* @param {arg.Result} args The parsed application arguments.
* @returns {AppEnvironment} The current (global) environment.
*/
const setupEnvironment = args => {
patchEnvPath()
const isDevMode = process.env.NODE_ENV !== 'production'
const debug = args['--debug'] || !!process.env.MARKTEXT_DEBUG || process.env.NODE_ENV !== 'production'
const verbose = args['--verbose'] || 0
const safeMode = args['--safe']
const userDataPath = args['--user-data-dir'] // or null (= default user data path)
const appEnvironment = new AppEnvironment({
debug,
isDevMode,
verbose,
safeMode,
userDataPath
})
ensureAppDirectoriesSync(appEnvironment.paths)
// Keep this for easier access.
global.MARKTEXT_DEBUG = debug
global.MARKTEXT_DEBUG_VERBOSE = verbose
global.MARKTEXT_SAFE_MODE = safeMode
return appEnvironment
}
export default setupEnvironment