├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── is.d.ts ├── is.js ├── package.json └── test ├── assertions.js └── headless-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | .nyc_output 17 | 18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 19 | .grunt 20 | 21 | # node-waf configuration 22 | .lock-wscript 23 | 24 | # Compiled binary addons (http://nodejs.org/api/addons.html) 25 | build/Release 26 | 27 | # Dependency directory 28 | node_modules 29 | 30 | # Optional npm cache directory 31 | .npm 32 | 33 | # Optional REPL history 34 | .node_repl_history 35 | 36 | # mac files 37 | .DS_Store 38 | 39 | # vim swap files 40 | *.swp 41 | 42 | # locks 43 | package-lock.json 44 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | env: 4 | - NODE=6 5 | 6 | os: 7 | - linux 8 | - osx 9 | 10 | addons: 11 | apt: 12 | packages: 13 | - xvfb 14 | 15 | install: 16 | - rm -rf ~/.nvm 17 | - git clone https://github.com/creationix/nvm.git ~/.nvm 18 | - source ~/.nvm/nvm.sh 19 | - nvm install $NODE 20 | - nvm --version 21 | - node --version 22 | - npm --version 23 | - npm install 24 | 25 | script: 26 | - npm test 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Tomas Della Vedova 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # electron-is 2 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/delvedor/electron-is.svg?branch=master)](https://travis-ci.org/delvedor/electron-is) 3 | 4 | An 'is' utility for Electron. 5 | `electron-is` provides a set of isomorphic 'is' APIs, that you can use it both in main and renderer process. 6 | See usage for more information. 7 | 8 | ## Install 9 | ``` 10 | $ npm install electron-is --save 11 | ``` 12 | 13 | ## API 14 | 15 | - **is.renderer()** 16 | Returns `true` if you are calling the function from the renderer process. 17 | 18 | - **is.main()** 19 | Returns `true` if you are calling the function from the main process. 20 | 21 | - **is.macOS()** *aliases* **is.osx()** 22 | Returns `true` if your app is running under Mac OS. 23 | 24 | - **is.windows()** 25 | Returns `true` if your app is running under Windows OS. 26 | 27 | - **is.linux()** 28 | Returns `true` if your app is running under Linux OS. 29 | 30 | - **is.x86()** 31 | Returns `true` if you the architecture of the processor is `ia32`. 32 | 33 | - **is.x64()** 34 | Returns `true` if you the architecture of the processor is `x64`. 35 | 36 | - **is.production()** 37 | Returns `true` if you are running the app in a `production` environment. 38 | 39 | - **is.dev()** 40 | Returns `true` if you are running the app in a `dev` environment. 41 | 42 | - **is.sandbox()** *only* ***macOS*** 43 | Returns `true` if you are running the app in a `sandbox` environment under macOS. 44 | 45 | - **is.mas()** 46 | Returns `true` if the app is running as a Mac App Store build. 47 | 48 | - **is.windowsStore()** 49 | Returns `true` if the app is running as a Windows Store (appx) build. 50 | 51 | - **is.all(args)** 52 | Returns `true` if all the 'is functions' passed as argument are true. 53 | example: `is.all(is.osx, is.x64)` 54 | 55 | - **is.none(args)** 56 | Returns `true` if all the 'is functions' passed as argument are false. 57 | example: `is.none(is.windows, is.x86, is.main)` 58 | 59 | - **is.one(args)** 60 | Returns `true` if one of the 'is functions' passed as argument is true. 61 | example: `is.one(is.osx, is.linux)` 62 | 63 | - **is.release(args)** 64 | Checks the if the given release is the same of the OS (\*) 65 | example: `is.release('10.0.10586')` 66 | 67 | - **is.gtRelease(args)** 68 | Checks if the given release is greater than the current OS release (\*) 69 | example: `is.gtRelease('10.9.5')` 70 | 71 | - **is.ltRelease(args)** 72 | Checks if the given release is less than the current OS release (\*) 73 | example: `is.ltRelease('6.3')` 74 | 75 | The [Mac](https://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history) versions are mapped as `osx: darwin`, you must pass the *9.x.y* or *10.x.y* OSX version as argument and not the darwin version. 76 | If you are testing a [Windows](https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions) release you must pass the NT release, it can be *x.y* or *x.y.build* . 77 | 78 | \* *Not implemented for Linux yet* 79 | 80 | 81 | ## Usage 82 | - In Main process: 83 | ```javascript 84 | // es6 85 | import is from 'electron-is' 86 | // es5 87 | const is = require('electron-is') 88 | console.log(is.main()) 89 | ``` 90 | - In Renderer process: 91 | ```html 92 | 96 | ``` 97 | ## Acknowledgements 98 | `electron-is` makes use of [electron-is-dev](https://github.com/sindresorhus/electron-is-dev) package from [@sindresorhus](https://github.com/sindresorhus). 99 | 100 | ## Contributing 101 | If you feel you can help in any way, be it with examples, extra testing, or new features please open a pull request or open an issue. 102 | 103 | The code follows the Standard code style. 104 | [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) 105 | ______________________________________________________________________________________________________________________ 106 | ## License 107 | **[MIT](https://github.com/delvedor/electron-is/blob/master/LICENSE)** 108 | 109 | *The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.* 110 | 111 | Copyright © 2016 Tomas Della Vedova 112 | -------------------------------------------------------------------------------- /is.d.ts: -------------------------------------------------------------------------------- 1 | declare namespace is { 2 | export function renderer(): boolean; 3 | export function main(): boolean; 4 | export function osx(): boolean; 5 | export function macOS(): boolean; 6 | export function windows(): boolean; 7 | export function linux(): boolean; 8 | export function x86(): boolean; 9 | export function x64(): boolean; 10 | export function production(): boolean; 11 | export function dev(): boolean; 12 | export function sandbox(): boolean; 13 | export function mas(): boolean; 14 | export function windowsStore(): boolean; 15 | export function all(): boolean; 16 | export function none(): boolean; 17 | export function one(): boolean; 18 | export function release(requested: string): boolean; 19 | export function gtRelease(requested: string): boolean; 20 | export function ltRelease(requested: string): boolean; 21 | } 22 | declare function is (): any; 23 | export = is; 24 | -------------------------------------------------------------------------------- /is.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Project: electron-is 3 | * Version: 3.0.0 4 | * Author: delvedor 5 | * Twitter: @delvedor 6 | * License: MIT 7 | * GitHub: https://github.com/delvedor/electron-is 8 | */ 9 | 'use strict' 10 | 11 | const semver = require('semver') 12 | const gt = semver.gt 13 | const lt = semver.lt 14 | const release = require('os').release 15 | const isDev = require('electron-is-dev') 16 | 17 | module.exports = { 18 | // Checks if we are in renderer process 19 | renderer: function () { 20 | return process.type === 'renderer' 21 | }, 22 | // Checks if we are in main process 23 | main: function () { 24 | return process.type === 'browser' 25 | }, 26 | // Checks if we are under Mac OS 27 | osx: function () { 28 | return process.platform === 'darwin' 29 | }, 30 | // Checks if we are under Mac OS 31 | macOS: function () { 32 | return this.osx() 33 | }, 34 | // Checks if we are under Windows OS 35 | windows: function () { 36 | return process.platform === 'win32' 37 | }, 38 | // Checks if we are under Linux OS 39 | linux: function () { 40 | return process.platform === 'linux' 41 | }, 42 | // Checks if we are the processor's arch is x86 43 | x86: function () { 44 | return process.arch === 'ia32' 45 | }, 46 | // Checks if we are the processor's arch is x64 47 | x64: function () { 48 | return process.arch === 'x64' 49 | }, 50 | // Checks if the env is setted to 'production' 51 | production: function () { 52 | return !isDev 53 | }, 54 | // Checks if the env is setted to 'dev' 55 | dev: function () { 56 | return isDev 57 | }, 58 | // Checks if the app is running in a sandbox on macOS 59 | sandbox: function () { 60 | return 'APP_SANDBOX_CONTAINER_ID' in process.env 61 | }, 62 | // Checks if the app is running as a Mac App Store build 63 | mas: function () { 64 | return process.mas === true 65 | }, 66 | // Checks if the app is running as a Windows Store (appx) build 67 | windowsStore: function () { 68 | return process.windowsStore === true 69 | }, 70 | // checks if all the 'is functions' passed as arguments are true 71 | all: function () { 72 | const isFunctions = new Array(arguments.length) 73 | for (var i = 0; i < isFunctions.length; i++) { 74 | isFunctions[i] = arguments[i] 75 | } 76 | if (!isFunctions.length) return 77 | for (i = 0; i < isFunctions.length; i++) { 78 | if (!isFunctions[i]()) return false 79 | } 80 | return true 81 | }, 82 | // checks if all the 'is functions' passed as arguments are false 83 | none: function () { 84 | const isFunctions = new Array(arguments.length) 85 | for (var i = 0; i < isFunctions.length; i++) { 86 | isFunctions[i] = arguments[i] 87 | } 88 | if (!isFunctions.length) return 89 | for (i = 0; i < isFunctions.length; i++) { 90 | if (isFunctions[i]()) return false 91 | } 92 | return true 93 | }, 94 | // returns true if one of the 'is functions' passed as argument is true 95 | one: function () { 96 | const isFunctions = new Array(arguments.length) 97 | for (var i = 0; i < isFunctions.length; i++) { 98 | isFunctions[i] = arguments[i] 99 | } 100 | if (!isFunctions.length) return 101 | for (i = 0; i < isFunctions.length; i++) { 102 | if (isFunctions[i]()) return true 103 | } 104 | return false 105 | }, 106 | // checks the if the given release is the same of the OS 107 | release: function (requested) { 108 | if (this.osx()) { 109 | return requested === osxRelease() 110 | } else if (this.windows()) { 111 | requested = requested.split('.') 112 | const actual = release().split('.') 113 | if (requested.length === 2) { 114 | return `${actual[0]}.${actual[1]}` === `${requested[0]}.${requested[1]}` 115 | } 116 | return `${actual[0]}.${actual[1]}.${actual[2]}` === `${requested[0]}.${requested[1]}.${requested[2]}` 117 | } else { 118 | // Not implemented for Linux yet 119 | return null 120 | } 121 | }, 122 | // checks if the given release is greater than the current OS release 123 | gtRelease: function (requested) { 124 | if (this.osx()) { 125 | return gt(requested, osxRelease()) 126 | } else if (this.windows()) { 127 | requested = requested.split('.') 128 | const actual = release().split('.') 129 | if (requested.length === 2) { 130 | return gt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`) 131 | } 132 | return gt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`) 133 | } else { 134 | // Not implemented for Linux yet 135 | return null 136 | } 137 | }, 138 | // checks if the given release is less than the current OS release 139 | ltRelease: function (requested) { 140 | if (this.osx()) { 141 | return lt(requested, osxRelease()) 142 | } else if (this.windows()) { 143 | requested = requested.split('.') 144 | const actual = release().split('.') 145 | if (requested.length === 2) { 146 | return lt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`) 147 | } 148 | return lt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`) 149 | } else { 150 | // Not implemented for Linux yet 151 | return null 152 | } 153 | } 154 | } 155 | 156 | // returns the current osx release 157 | function osxRelease () { 158 | const actual = release().split('.') 159 | return `10.${actual[0] - 4}.${actual[1]}` 160 | } 161 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-is", 3 | "version": "3.0.0", 4 | "description": "An 'is' utility for Electron which provides a set of handy functions, with a self-descriptive name.", 5 | "main": "is.js", 6 | "scripts": { 7 | "pretest": "standard", 8 | "test": "node test/headless-test.js" 9 | }, 10 | "keywords": [ 11 | "platform", 12 | "type", 13 | "process", 14 | "arch", 15 | "env", 16 | "electron", 17 | "os", 18 | "release", 19 | "is", 20 | "main", 21 | "renderer" 22 | ], 23 | "author": "Tomas Della Vedova - @delvedor (http://delved.org)", 24 | "contributors": [ 25 | "Masoud Ghorbani (http://msud.ir)" 26 | ], 27 | "license": "MIT", 28 | "repository": { 29 | "type": "git", 30 | "url": "git+https://github.com/delvedor/electron-is.git" 31 | }, 32 | "bugs": { 33 | "url": "https://github.com/delvedor/electron-is/issues" 34 | }, 35 | "homepage": "https://github.com/delvedor/electron-is#readme", 36 | "devDependencies": { 37 | "electron-eval": "^0.9.10", 38 | "standard": "^11.0.1" 39 | }, 40 | "dependencies": { 41 | "electron-is-dev": "^0.3.0", 42 | "semver": "^5.5.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/assertions.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const assert = require('assert') 4 | const release = require('os').release 5 | const path = require('path') 6 | const is = require(path.join(__dirname, '..', '..', '..', 'is')) 7 | 8 | function assertions () { 9 | assert.equal(is.main(), process.type === 'browser', 'is.main() not ok!') 10 | assert.equal(is.renderer(), process.type === 'renderer', 'is.renderer() not ok!') 11 | 12 | assert.equal(is.osx(), process.platform === 'darwin', 'is.osx() not ok!') 13 | assert.equal(is.macOS(), process.platform === 'darwin', 'is.macOS() not ok!') 14 | assert.equal(is.windows(), process.platform === 'win32', 'is.windows() not ok!') 15 | assert.equal(is.linux(), process.platform === 'linux', 'is.linux() not ok!') 16 | 17 | assert.equal(is.x86(), process.arch === 'ia32', 'is.x86() not ok!') 18 | assert.equal(is.x64(), process.arch === 'x64', 'is.x64() not ok!') 19 | 20 | assert.equal(is.production(), (process.env.NODE_ENV || 'dev') === 'production', 'is.production() not ok!') 21 | assert.equal(is.dev(), (process.env.NODE_ENV || 'dev') === 'dev', 'is.dev() not ok!') 22 | 23 | assert.equal(is.sandbox(), ('APP_SANDBOX_CONTAINER_ID' in process.env), 'is.sandbox() not ok!') 24 | assert.equal(is.mas(), process.mas === true, 'is.mas() not ok!') 25 | assert.equal(is.windowsStore(), process.windowsStore === true, 'is.windowsStore() not ok!') 26 | 27 | assert.equal(is.all(is.osx, is.x64), is.osx() && is.x64(), 'is.all() 1 not ok!') 28 | assert.equal(is.all(is.osx, is.x86), is.osx() && is.x86(), 'is.all() 2 not ok!') 29 | assert.equal(is.none(is.windows, is.x86), !is.windows() && !is.x86(), 'is.none() 1 not ok!') 30 | assert.equal(is.none(is.windows, is.x64), !is.windows() && !is.x64(), 'is.none() 2 not ok!') 31 | assert.equal(is.one(is.windows, is.osx), is.windows() || is.osx(), 'is.one() 1 not ok!') 32 | assert.equal(is.one(is.windows, is.linux), is.windows() || is.linux(), 'is.one() 2 not ok!') 33 | 34 | if (is.osx()) { 35 | const osx = osxRelease() 36 | // mac el capitan 37 | assert.equal(is.release(osx), true, 'is.release() not ok!') 38 | 39 | assert.equal(is.gtRelease(osx), false, 'is.gtRelease() 1 not ok!') 40 | assert.equal(is.gtRelease('100.100.100'), true, 'is.gtRelease() 2 not ok!') 41 | assert.equal(is.gtRelease('1.0.0'), false, 'is.gtRelease() 3 not ok!') 42 | 43 | assert.equal(is.ltRelease(osx), false, 'is.ltRelease() 1 not ok!') 44 | assert.equal(is.ltRelease('100.100.100'), false, 'is.ltRelease() 2 not ok!') 45 | assert.equal(is.ltRelease('1.0.0'), true, 'is.ltRelease() 3 not ok!') 46 | } else if (is.windows()) { 47 | // tests Windows 10 AU 48 | assert.equal(is.release('10.0'), true, 'is.release() not ok!') 49 | assert.equal(is.release('10.0.14393'), true, 'is.release() not ok!') 50 | } else { 51 | assert.equal(is.release('1.2.3'), null, 'is.release() not ok!') 52 | } 53 | 54 | return true 55 | } 56 | 57 | // returns the current osx release 58 | function osxRelease () { 59 | const actual = release().split('.') 60 | return `10.${actual[0] - 4}.${actual[1]}` 61 | } 62 | 63 | assertions() 64 | -------------------------------------------------------------------------------- /test/headless-test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const electronEval = require('electron-eval') 5 | 6 | const assertions = fs.readFileSync('./test/assertions.js', 'utf8') 7 | const daemon = electronEval() 8 | 9 | ;(function test (options) { 10 | daemon.eval(assertions, { mainProcess: options.main }, (err, res) => { 11 | if (err) { 12 | console.log(err.message) 13 | process.exit(1) 14 | } 15 | if (options.main) { 16 | test({ main: false }) 17 | } else { 18 | daemon.close() 19 | console.log('All test passed!') 20 | process.exit(0) 21 | } 22 | }) 23 | })({ main: true }) 24 | --------------------------------------------------------------------------------