@pushrocks/smartenv
Version:
store things about your environment and let them travel across modules
235 lines (231 loc) • 6.93 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// node_modules/.pnpm/@pushrocks+smartpromise@3.1.7/node_modules/@pushrocks/smartpromise/dist_ts/index.js
var require_dist_ts = __commonJS({
"node_modules/.pnpm/@pushrocks+smartpromise@3.1.7/node_modules/@pushrocks/smartpromise/dist_ts/index.js"(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFirstTrueOrFalse = exports.timeoutAndContinue = exports.timeoutWrap = exports.map = exports.rejectedPromise = exports.resolvedPromise = exports.defer = exports.Deferred = void 0;
var Deferred = class {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = (valueArg) => {
this.status = "fulfilled";
this.stoppedAt = Date.now();
resolve(valueArg);
};
this.reject = (reason) => {
this.status = "rejected";
this.stoppedAt = Date.now();
reject(reason);
};
this.startedAt = Date.now();
this.status = "pending";
});
}
get duration() {
if (this.stoppedAt) {
return this.stoppedAt - this.startedAt;
} else {
return Date.now() - this.startedAt;
}
}
};
exports.Deferred = Deferred;
exports.defer = () => {
return new Deferred();
};
exports.resolvedPromise = (value) => {
return Promise.resolve(value);
};
exports.rejectedPromise = (err) => {
return Promise.reject(err);
};
exports.map = async (inputArg, functionArg) => {
const promiseArray = [];
const resultArray = [];
for (const item of inputArg) {
const promise = functionArg(item);
promiseArray.push(promise);
promise.then((x) => {
resultArray.push(x);
});
}
await Promise.all(promiseArray);
return resultArray;
};
exports.timeoutWrap = async (promiseArg, timeoutInMsArg, rejectArg = true) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (rejectArg) {
reject(new Error("timeout"));
} else {
resolve(null);
}
}, timeoutInMsArg);
promiseArg.then(resolve, reject);
});
};
exports.timeoutAndContinue = async (promiseArg, timeoutInMsArg = 6e4) => {
return exports.timeoutWrap(promiseArg, timeoutInMsArg, false);
};
exports.getFirstTrueOrFalse = async (promisesArg) => {
const done = exports.defer();
for (const promiseArg of promisesArg) {
promiseArg.then((resultArg) => {
if (resultArg === true) {
done.resolve(true);
}
});
}
Promise.all(promisesArg).then(() => {
done.resolve(false);
});
return done.promise;
};
}
});
// ts/smartenv.plugins.ts
var smartpromise = __toESM(require_dist_ts(), 1);
// ts/smartenv.classes.smartenv.ts
var Smartenv = class {
constructor() {
this.loadedScripts = [];
}
async getEnvAwareModule(optionsArg) {
if (this.isNode) {
const moduleResult = await this.getSafeNodeModule(optionsArg.nodeModuleName);
return moduleResult;
} else if (this.isBrowser) {
const moduleResult = await this.getSafeWebModule(
optionsArg.webUrlArg,
optionsArg.getFunction
);
return moduleResult;
} else {
console.error("platform for loading not supported by smartenv");
}
}
async getSafeNodeModule(moduleNameArg) {
if (!this.isNode) {
console.error(`You tried to load a node module in a wrong context: ${moduleNameArg}`);
return;
}
return new Function(`return import('${moduleNameArg}')`)();
}
async getSafeWebModule(urlArg, getFunctionArg) {
if (!this.isBrowser) {
console.error("You tried to load a web module in a wrong context");
return;
}
if (this.loadedScripts.includes(urlArg)) {
return getFunctionArg();
} else {
this.loadedScripts.push(urlArg);
}
const done = 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;
return getFunctionArg();
}
get runtimeEnv() {
if (typeof process !== "undefined") {
return "node";
} else {
return "browser";
}
}
get isBrowser() {
return !this.isNode;
}
get userAgent() {
if (this.isBrowser) {
return navigator.userAgent;
} else {
return "undefined";
}
}
get isNode() {
return this.runtimeEnv === "node";
}
get nodeVersion() {
return process.version;
}
get isCI() {
if (this.isNode) {
if (process.env.CI) {
return true;
} else {
return false;
}
} else {
return false;
}
}
async isMacAsync() {
if (this.isNode) {
const os = await this.getSafeNodeModule("os");
return os.platform() === "darwin";
} else {
return false;
}
}
async isWindowsAsync() {
if (this.isNode) {
const os = await this.getSafeNodeModule("os");
return os.platform() === "win32";
} else {
return false;
}
}
async isLinuxAsync() {
if (this.isNode) {
const os = await this.getSafeNodeModule("os");
return os.platform() === "linux";
} else {
return false;
}
}
async printEnv() {
if (this.isNode) {
console.log("running on NODE");
console.log("node version is " + this.nodeVersion);
} else {
console.log("running on BROWSER");
console.log("browser is " + this.userAgent);
}
}
};
export {
Smartenv
};
//# sourceMappingURL=bundle.js.map