@nasriya/atomix
Version:
Composable helper functions for building reliable systems
178 lines (177 loc) • 6.33 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
class Runtime {
/**
* Gets the name of the project.
* @returns The name of the project.
* @since v1.0.0
*/
getProjectName() {
// Read package.json file
const packageJson = fs_1.default.readFileSync('package.json', 'utf8');
// Parse package.json as JSON
const packageData = JSON.parse(packageJson);
// Extract project name
return packageData.name;
}
/**
* Determines the module system being used.
*
* @returns 'commonjs' or 'module'.
* @since v1.0.0
*/
getModuleSystem() {
return typeof module !== 'undefined' && module.exports ? 'commonjs' : 'module';
}
/**
* Asynchronously loads a module by name, either as a file or a package.
*
* @param name - The name or path of the module to load.
* @param options - Optional settings for loading the module.
* @param options.isFile - If true, treats the name as a file path. Defaults to false.
* @returns A promise that resolves to the loaded module.
* @throws Error with an appropriate message if the module cannot be loaded.
* @since v1.0.0
*/
async loadModule(name, options) {
return new Promise((resolve, reject) => {
try {
const isFile = options?.isFile ?? false;
const nodeEnv = this.getModuleSystem();
if (nodeEnv === 'commonjs') {
const mod = require(name);
resolve('default' in mod ? mod.default : mod);
}
else {
// @ts-ignore
Promise.resolve(`${isFile ? `file://${name}` : name}`).then(s => __importStar(require(s))).then(mod => resolve('default' in mod ? mod.default : mod));
}
}
catch (error) {
if (error instanceof Error) {
error.message = `Unable to load module (${name}): ${error.message}`;
}
reject(error);
}
});
}
/**
* Asynchronously loads a module from a specified file path.
*
* @param filePath - The path to the file to load as a module.
* @returns A promise that resolves to the loaded module.
* @throws Error with an appropriate message if the module cannot be loaded.
* @since v1.0.0
*/
async loadFileModule(filePath) {
return this.loadModule(filePath, { isFile: true });
}
platform = {
/**
* Determines if the current platform is Windows.
*
* @returns True if the current platform is Windows, false otherwise.
* @since v1.0.0
*/
isWindows: () => {
return process.platform === 'win32';
},
/**
* Determines if the current platform is Linux.
* @returns True if the current platform is Linux, false otherwise.
* @since v1.0.0
*/
isLinux: () => {
return process.platform === 'linux';
},
/**
* Determines if the current platform is macOS.
* @returns True if the current platform is macOS, false otherwise.
* @since v1.0.0
*/
isMac: () => {
return process.platform === 'darwin';
}
};
/**
* Checks if the current environment is Node.js.
* @returns true if the environment is Node.js, false otherwise.
* @since v1.0.0
*/
isNode() {
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
}
/**
* Checks if the current environment is Bun.
* @returns true if the environment is Bun, false otherwise.
* @since v1.0.0
*/
isBun() {
return typeof process !== 'undefined' && process.versions != null && process.versions.bun != null;
}
/**
* Checks if the current environment is Deno.
* @returns true if the environment is Deno, false otherwise.
* @since v1.0.0
*/
isDeno() {
// @ts-ignore
return typeof Deno !== 'undefined' && typeof Deno.version === 'object';
}
/**
* Gets the name of the runtime engine (Node.js, Bun, Deno, etc.) that is currently being used.
* @returns The name of the runtime engine, or 'unknown' if the engine could not be determined.
* @since v1.0.0
*/
getRuntimeEngine() {
if (this.isNode()) {
return 'node';
}
if (this.isBun()) {
return 'bun';
}
if (this.isDeno()) {
return 'deno';
}
return 'unknown';
}
}
const runtime = new Runtime;
exports.default = runtime;