forked from delvedor/electron-is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is.js
173 lines (150 loc) · 4.59 KB
/
is.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Project: electron-is
* Version: 2.3.0
* Author: delvedor
* Twitter: @delvedor
* License: MIT
* GitHub: https://github.com/delvedor/electron-is
*/
'use strict'
const { gt, lt } = require('semver')
const { release } = require('os')
const isDev = require('electron-is-dev')
// Constructor
function IsApi () {}
// Checks if we are in renderer process
IsApi.prototype.renderer = function () {
return process.type === 'renderer'
}
// Checks if we are in main process
IsApi.prototype.main = function () {
return process.type === 'browser'
}
IsApi.prototype.standardBrowser = function () {
return (typeof process === 'undefined')
}
// Checks if we are under Mac OS
IsApi.prototype.osx = IsApi.prototype.macOS = function () {
return process.platform === 'darwin'
}
// Checks if we are under Windows OS
IsApi.prototype.windows = function () {
return process.platform === 'win32'
}
// Checks if we are under Linux OS
IsApi.prototype.linux = function () {
return process.platform === 'linux'
}
// Checks if we are the processor's arch is x86
IsApi.prototype.x86 = function () {
return process.arch === 'ia32'
}
// Checks if we are the processor's arch is x64
IsApi.prototype.x64 = function () {
return process.arch === 'x64'
}
// Checks if the env is setted to 'production'
IsApi.prototype.production = function () {
return !isDev
}
// Checks if the env is setted to 'dev'
IsApi.prototype.dev = function () {
return isDev
}
// Checks if the app is running in a sandbox on macOS
IsApi.prototype.sandbox = function () {
return 'APP_SANDBOX_CONTAINER_ID' in process.env
}
// Checks if the app is running as a Mac App Store build
IsApi.prototype.mas = function () {
return process.mas === true
}
// Checks if the app is running as a Windows Store (appx) build
IsApi.prototype.windowsStore = function () {
return process.windowsStore === true
}
// checks if all the 'is functions' passed as arguments are true
IsApi.prototype.all = function (...isFunctions) {
if (!isFunctions.length) return
for (let i = 0; i < isFunctions.length; i++) {
if (!isFunctions[i]()) return false
}
return true
}
// checks if all the 'is functions' passed as arguments are false
IsApi.prototype.none = function (...isFunctions) {
if (!isFunctions.length) return
for (let i = 0; i < isFunctions.length; i++) {
if (isFunctions[i]()) return false
}
return true
}
// returns true if one of the 'is functions' passed as argument is true
IsApi.prototype.one = function (...isFunctions) {
if (!isFunctions.length) return
for (let i = 0; i < isFunctions.length; i++) {
if (isFunctions[i]()) return true
}
return false
}
// checks the if the given release is the same of the OS
IsApi.prototype.release = function (requested) {
if (this.osx()) {
return requested === osxRelease()
} else if (this.windows()) {
requested = requested.split('.')
let actual = release().split('.')
if (requested.length === 2) {
return `${actual[0]}.${actual[1]}` === `${requested[0]}.${requested[1]}`
}
return `${actual[0]}.${actual[1]}.${actual[2]}` === `${requested[0]}.${requested[1]}.${requested[2]}`
} else {
// Not implemented for Linux yet
return null
}
}
// checks if the given release is greater than the current OS release
IsApi.prototype.gtRelease = function (requested) {
if (this.osx()) {
return gt(requested, osxRelease())
} else if (this.windows()) {
let requested = requested.split('.')
let actual = release().split('.')
if (requested.length === 2) {
return gt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
}
return gt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
} else {
// Not implemented for Linux yet
return null
}
}
// checks if the given release is less than the current OS release
IsApi.prototype.ltRelease = function (requested) {
if (this.osx()) {
return lt(requested, osxRelease())
} else if (this.windows()) {
let requested = requested.split('.')
let actual = release().split('.')
if (requested.length === 2) {
return lt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
}
return lt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
} else {
// Not implemented for Linux yet
return null
}
}
// returns the current osx release
function osxRelease () {
let actual = release().split('.')
return `10.${actual[0] - 4}.${actual[1]}`
}
// new instace
function isBuilder () {
if (!(this instanceof IsApi)) {
return new IsApi()
}
}
// exports
module.exports = isBuilder()