smartenv/ts/smartenv.classes.smartenv.ts

118 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-06-17 06:46:28 +00:00
import * as plugins from './smartenv.plugins';
import * as interfaces from './interfaces';
2017-05-17 13:59:10 +00:00
// interfaces
export interface IEnvObject {
2019-06-17 06:46:28 +00:00
name: string;
value: string;
2017-05-17 13:59:10 +00:00
}
2019-06-17 06:46:28 +00:00
/**
* Smartenv class that makes it easy
*/
2017-05-17 13:59:10 +00:00
export class Smartenv {
2020-06-25 23:00:32 +00:00
public getSafeNodeModule(moduleNameArg: string) {
// tslint:disable-next-line: function-constructor
return new Function(
'exports',
'require',
'module',
'__filename',
'__dirname',
`return require('${moduleNameArg}')`
)(exports,require,module,__filename,__dirname);
}
2019-08-21 22:20:10 +00:00
public get runtimeEnv() {
if (typeof window !== 'undefined') {
2019-06-17 06:46:28 +00:00
return 'browser';
} else if (typeof process !== 'undefined') {
2019-06-17 06:46:28 +00:00
return 'node';
}
}
2019-08-21 22:20:10 +00:00
public get isBrowser(): boolean {
2019-06-17 06:46:28 +00:00
return !this.isNode;
}
2019-08-21 22:20:10 +00:00
public get userAgent(): string {
2019-06-17 06:46:28 +00:00
if (this.isBrowser) {
// make sure we are in Browser
return navigator.userAgent;
} else {
2019-06-17 06:46:28 +00:00
return 'undefined';
}
}
2019-08-21 22:20:10 +00:00
public get isNode(): boolean {
2019-06-17 06:46:28 +00:00
return this.runtimeEnv === 'node';
}
2019-08-21 22:20:10 +00:00
public get nodeVersion(): string {
2019-06-17 06:46:28 +00:00
return process.version;
}
2019-06-17 06:46:28 +00:00
2019-08-21 22:20:10 +00:00
public get isCI(): boolean {
if (this.isNode) {
if (process.env.CI) {
2019-06-17 06:46:28 +00:00
return true;
} else {
2019-06-17 06:46:28 +00:00
return false;
}
} else {
2019-06-17 06:46:28 +00:00
return false;
}
}
2019-08-21 22:20:10 +00:00
public async isMacAsync(): Promise<boolean> {
2019-06-17 06:46:28 +00:00
if (this.isNode) {
2020-06-25 23:00:32 +00:00
const os = this.getSafeNodeModule('os');
2019-06-17 06:46:28 +00:00
return os.platform() === 'darwin';
} else {
2019-06-17 06:46:28 +00:00
return false;
}
}
2019-06-17 06:46:28 +00:00
2019-08-21 22:20:10 +00:00
public async isWindowsAsync(): Promise<boolean> {
2019-06-17 06:46:28 +00:00
if (this.isNode) {
2020-06-25 23:00:32 +00:00
const os = this.getSafeNodeModule('os');
2019-06-17 06:46:28 +00:00
return os.platform() === 'win32';
} else {
2019-06-17 06:46:28 +00:00
return false;
}
}
2019-08-21 22:20:10 +00:00
public async isLinuxAsync(): Promise<boolean> {
2019-06-17 06:46:28 +00:00
if (this.isNode) {
2020-06-25 23:00:32 +00:00
const os = this.getSafeNodeModule('os');
2019-06-17 06:46:28 +00:00
return os.platform() === 'linux';
} else {
2019-06-17 06:46:28 +00:00
return false;
}
}
2017-05-17 13:59:10 +00:00
/**
* get environment variables that fit the description
*/
// get envVars (regexArg: RegExp) {
// let EnvironmentArray = []
// // TODO: plugins.smartparam.forEachMinimatch()
// }
2017-05-17 13:59:10 +00:00
/**
* prints the environment to console
*/
2019-08-21 22:20:10 +00:00
public async printEnv() {
2017-05-17 13:59:10 +00:00
if (this.isNode) {
2019-06-17 06:46:28 +00:00
console.log('running on NODE');
2019-08-21 22:20:10 +00:00
const smartenvVersion = require('../package.json').version;
2019-06-17 06:46:28 +00:00
console.log(
'node version is ' + this.nodeVersion + ' and smartenv version is ' + smartenvVersion
);
2017-05-17 13:59:10 +00:00
} else {
2019-06-17 06:46:28 +00:00
console.log('running on BROWSER');
console.log('browser is ' + this.userAgent);
2017-05-17 13:59:10 +00:00
}
}
}