update smartenv to support os information on nodejs

This commit is contained in:
2018-02-14 00:12:21 +01:00
parent 5edb62c134
commit 50d610e5df
12 changed files with 418 additions and 540 deletions

View File

@ -1,5 +1,4 @@
import * as plugins from './smartenv.plugins'
import helpers = require('./smartenv.envhelpers')
// interfaces
export interface IEnvObject {
@ -8,28 +7,81 @@ export interface IEnvObject {
}
export class Smartenv {
runtimeEnv: string
isBrowser: boolean
userAgent: string
isNode: boolean
nodeVersion: string
isCI: boolean
constructor() {
this.runtimeEnv = helpers.getEnvString()
this.isBrowser = helpers.isBrowser()
this.userAgent = helpers.getUserAgentString()
this.isNode = helpers.isNode()
this.nodeVersion = helpers.getNodeVersion()
this.isCI = helpers.isCI()
};
get runtimeEnv () {
if (typeof window !== 'undefined') {
return 'browser'
} else if (typeof process !== 'undefined') {
return 'node'
}
}
get isBrowser (): boolean {
return !this.isNode
}
get userAgent (): string {
if (this.isBrowser) { // make sure we are in Browser
return navigator.userAgent
} else {
return 'undefined'
}
}
get isNode ():boolean {
return this.runtimeEnv === 'node'
}
get nodeVersion ():string {
return process.version
}
get isCI (): boolean {
if (this.isNode) {
if (process.env.CI) {
return true
} else {
return false
};
} else {
return false
}
}
async isMacAsync (): Promise<boolean> {
if(this.isNode) {
let os = await import('os')
return os.platform() === 'darwin'
} else {
return false
}
}
async isWindowsAsync (): Promise<boolean> {
if(this.isNode) {
let os = await import('os')
return os.platform() === 'win32'
} else {
return false
}
}
async isLinuxAsync (): Promise<boolean> {
if(this.isNode) {
let os = await import('os')
return os.platform() === 'linux'
} else {
return false
}
}
/**
* get environment variables that fit the description
*/
async getEnvVars (regexArg: RegExp) {
let EnvironmentArray = []
// TODO: plugins.smartparam.forEachMinimatch()
}
// get envVars (regexArg: RegExp) {
// let EnvironmentArray = []
// // TODO: plugins.smartparam.forEachMinimatch()
// }
/**
* prints the environment to console

View File

@ -1,39 +0,0 @@
import * as plugins from './smartenv.plugins'
export let getEnvString = function (): string {
if (typeof window !== 'undefined') {
return 'browser'
} else if (typeof process !== 'undefined') {
return 'node'
}
}
/**
* gets the user agent string in a browser
*/
export let getUserAgentString = function (): string {
if (isBrowser()) { // make sure we are in Browser
return navigator.userAgent
} else {
return 'undefined'
}
}
export let isNode = function (): boolean {
return getEnvString() === 'node'
}
export let getNodeVersion = function (): string {
return process.version
}
export let isBrowser = function (): boolean {
return !isNode()
}
export let isCI = function () {
if (process.env.CI) {
return true
} else {
return false
};
}

View File

@ -1,4 +1,3 @@
import 'typings-global'
import * as smartparam from 'smartparam'
import * as smartq from 'smartq'
import * as lodash from 'lodash'