CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-std-env

Runtime agnostic JavaScript utility library for environment detection, platform information, CI/CD provider detection, and runtime identification.

Pending
Overview
Eval results
Files

platform-detection.mddocs/

Platform Detection

Operating system and platform identification with boolean flags for common platforms and Node.js version detection.

Capabilities

Platform String

Raw platform string from the underlying process or runtime.

/**
 * Value of process.platform or empty string if not available
 * Common values: 'win32', 'darwin', 'linux', 'freebsd', etc.
 */
const platform: string;

Usage Examples:

import { platform } from "std-env";

console.log(`Running on platform: ${platform}`);

// Switch based on platform
switch (platform) {
  case "win32":
    console.log("Windows platform");
    break;
  case "darwin":
    console.log("macOS platform");
    break;
  case "linux":
    console.log("Linux platform");
    break;
  default:
    console.log("Unknown platform");
}

Windows Detection

Detects Windows operating system based on platform string.

/**
 * Detects if running on Windows platform
 * Based on platform string matching /^win/i pattern
 */
const isWindows: boolean;

Usage Examples:

import { isWindows } from "std-env";

if (isWindows) {
  // Use Windows-specific paths
  const configPath = path.join(process.env.APPDATA, "myapp");
  // Handle Windows line endings
  const content = data.replace(/\n/g, "\r\n");
}

Linux Detection

Detects Linux operating system based on platform string.

/**
 * Detects if running on Linux platform
 * Based on platform string matching /^linux/i pattern
 */
const isLinux: boolean;

Usage Examples:

import { isLinux } from "std-env";

if (isLinux) {
  // Use Linux-specific system calls
  // Check for systemd
  // Use XDG directories
}

macOS Detection

Detects macOS (Darwin kernel) operating system based on platform string.

/**
 * Detects if running on macOS (Darwin kernel)
 * Based on platform string matching /^darwin/i pattern
 */
const isMacOS: boolean;

Usage Examples:

import { isMacOS } from "std-env";

if (isMacOS) {
  // Use macOS-specific paths
  const configPath = path.join(os.homedir(), "Library", "Application Support");
  // Handle macOS keychain
  // Use Cocoa APIs through Node.js bindings
}

Node.js Version Detection

Node.js version information with parsed major version number.

/**
 * Node.js version string without 'v' prefix, or null if not Node.js
 * Example: "18.17.0" for Node.js v18.17.0
 */
const nodeVersion: string | null;

/**
 * Node.js major version number, or null if not Node.js
 * Example: 18 for Node.js v18.17.0
 */
const nodeMajorVersion: number | null;

Usage Examples:

import { nodeVersion, nodeMajorVersion } from "std-env";

if (nodeVersion) {
  console.log(`Node.js version: ${nodeVersion}`);
  
  if (nodeMajorVersion && nodeMajorVersion >= 18) {
    // Use Node.js 18+ features
    // Native fetch API available
  } else if (nodeMajorVersion && nodeMajorVersion >= 16) {
    // Use Node.js 16+ features
    // Stable timers/promises
  }
}

// Version-specific feature detection
if (nodeMajorVersion && nodeMajorVersion >= 20) {
  // Use Node.js 20+ features like test runner
} else {
  // Fallback to external test framework
}

Platform-Specific Usage Patterns

Cross-Platform Path Handling

import { isWindows, platform } from "std-env";
import path from "path";

// Platform-aware path construction
const configDir = isWindows 
  ? path.join(process.env.APPDATA || "", "myapp")
  : path.join(process.env.HOME || "", ".config", "myapp");

// Platform-specific executable extensions
const executableName = isWindows ? "myapp.exe" : "myapp";

Platform-Specific Features

import { isLinux, isMacOS, isWindows } from "std-env";

// Platform-specific notifications
if (isLinux) {
  // Use libnotify
} else if (isMacOS) {
  // Use macOS notification center
} else if (isWindows) {
  // Use Windows toast notifications
}

Node.js Version Compatibility

import { nodeMajorVersion } from "std-env";

// Feature detection based on Node.js version
const features = {
  fetch: nodeMajorVersion && nodeMajorVersion >= 18,
  testRunner: nodeMajorVersion && nodeMajorVersion >= 20,
  asyncIterators: nodeMajorVersion && nodeMajorVersion >= 10,
};

if (features.fetch) {
  // Use native fetch
  const response = await fetch(url);
} else {
  // Use polyfill or alternative
  const response = await require('node-fetch')(url);
}

Install with Tessl CLI

npx tessl i tessl/npm-std-env

docs

environment-detection.md

environment-variables.md

index.md

platform-detection.md

provider-detection.md

runtime-detection.md

tile.json