2022-03-16 15:09:24 +00:00
|
|
|
import * as plugins from './smartenv.plugins.js';
|
|
|
|
import * as interfaces from './interfaces/index.js';
|
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-09-29 18:38:21 +00:00
|
|
|
public async getEnvAwareModule(optionsArg: {
|
|
|
|
nodeModuleName: string;
|
|
|
|
webUrlArg: string;
|
|
|
|
getFunction: () => any;
|
|
|
|
}) {
|
|
|
|
if (this.isNode) {
|
2022-12-28 18:43:48 +00:00
|
|
|
const moduleResult = await this.getSafeNodeModule(optionsArg.nodeModuleName);
|
2020-09-29 18:38:21 +00:00
|
|
|
return moduleResult;
|
|
|
|
} else if (this.isBrowser) {
|
2022-12-28 18:43:48 +00:00
|
|
|
const moduleResult = await this.getSafeWebModule(
|
|
|
|
optionsArg.webUrlArg,
|
|
|
|
optionsArg.getFunction
|
|
|
|
);
|
|
|
|
return moduleResult;
|
2020-09-29 18:38:21 +00:00
|
|
|
} else {
|
|
|
|
console.error('platform for loading not supported by smartenv');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 15:09:24 +00:00
|
|
|
public async getSafeNodeModule<T = any>(moduleNameArg: string): Promise<T> {
|
2020-09-29 17:49:33 +00:00
|
|
|
if (!this.isNode) {
|
2022-08-06 23:17:45 +00:00
|
|
|
console.error(`You tried to load a node module in a wrong context: ${moduleNameArg}`);
|
2020-09-29 17:49:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-25 23:00:32 +00:00
|
|
|
// tslint:disable-next-line: function-constructor
|
2022-12-28 18:43:48 +00:00
|
|
|
return new Function(`return import('${moduleNameArg}')`)() as Promise<T>;
|
2020-06-25 23:00:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 17:49:33 +00:00
|
|
|
public loadedScripts: string[] = [];
|
2020-09-29 18:38:21 +00:00
|
|
|
public async getSafeWebModule(urlArg: string, getFunctionArg: () => any) {
|
2020-09-29 17:49:33 +00:00
|
|
|
if (!this.isBrowser) {
|
|
|
|
console.error('You tried to load a web module in a wrong context');
|
|
|
|
return;
|
|
|
|
}
|
2022-12-28 18:43:48 +00:00
|
|
|
|
2020-09-29 17:49:33 +00:00
|
|
|
if (this.loadedScripts.includes(urlArg)) {
|
2020-10-13 20:09:15 +00:00
|
|
|
return getFunctionArg();
|
2020-09-29 17:49:33 +00:00
|
|
|
} else {
|
|
|
|
this.loadedScripts.push(urlArg);
|
|
|
|
}
|
2022-12-28 18:43:48 +00:00
|
|
|
|
2020-09-29 17:49:33 +00:00
|
|
|
const done = plugins.smartpromise.defer();
|
|
|
|
if (globalThis.importScripts) {
|
|
|
|
globalThis.importScripts(urlArg);
|
|
|
|
done.resolve();
|
|
|
|
} else {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.onload = () => {
|
|
|
|
done.resolve();
|
|
|
|
};
|
|
|
|
script.src = urlArg;
|
|
|
|
document.head.appendChild(script);
|
|
|
|
}
|
|
|
|
await done.promise;
|
2020-09-29 18:38:21 +00:00
|
|
|
return getFunctionArg();
|
2020-09-29 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public get runtimeEnv() {
|
2022-05-28 21:22:31 +00:00
|
|
|
if (typeof process !== 'undefined') {
|
2019-06-17 06:46:28 +00:00
|
|
|
return 'node';
|
2022-05-28 21:22:31 +00:00
|
|
|
} else {
|
|
|
|
return 'browser';
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public get isBrowser(): boolean {
|
2019-06-17 06:46:28 +00:00
|
|
|
return !this.isNode;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return 'undefined';
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public get isNode(): boolean {
|
2019-06-17 06:46:28 +00:00
|
|
|
return this.runtimeEnv === 'node';
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public get nodeVersion(): string {
|
2019-06-17 06:46:28 +00:00
|
|
|
return process.version;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
2019-06-17 06:46:28 +00:00
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public get isCI(): boolean {
|
2018-02-13 23:12:21 +00:00
|
|
|
if (this.isNode) {
|
|
|
|
if (process.env.CI) {
|
2019-06-17 06:46:28 +00:00
|
|
|
return true;
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return false;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public async isMacAsync(): Promise<boolean> {
|
2019-06-17 06:46:28 +00:00
|
|
|
if (this.isNode) {
|
2022-03-16 15:09:24 +00:00
|
|
|
const os = await this.getSafeNodeModule('os');
|
2019-06-17 06:46:28 +00:00
|
|
|
return os.platform() === 'darwin';
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return false;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2022-03-16 15:09:24 +00:00
|
|
|
const os = await this.getSafeNodeModule('os');
|
2019-06-17 06:46:28 +00:00
|
|
|
return os.platform() === 'win32';
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return false;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:20:10 +00:00
|
|
|
public async isLinuxAsync(): Promise<boolean> {
|
2019-06-17 06:46:28 +00:00
|
|
|
if (this.isNode) {
|
2022-03-16 15:09:24 +00:00
|
|
|
const os = await this.getSafeNodeModule('os');
|
2019-06-17 06:46:28 +00:00
|
|
|
return os.platform() === 'linux';
|
2018-02-13 23:12:21 +00:00
|
|
|
} else {
|
2019-06-17 06:46:28 +00:00
|
|
|
return false;
|
2018-02-13 23:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
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');
|
2022-12-28 18:43:48 +00:00
|
|
|
console.log('node version is ' + this.nodeVersion);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|