npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

normalized-tmpdir

v1.0.1

Published

Normalize os.tmpdir() to eliminate OS-specific symlinks and naming variations

Downloads

177

Readme

normalized-tmpdir

Normalize the value returned from os.tmpdir() to eliminate OS-specific symlinks and naming variations. This is mainly useful in tests that rely on path comparisons.

Currently-handled issues

Mac

os.tmpdir() may return /var/... which is a symlink to /private/var/.... This can be fixed with fs.realpathSync().

Windows

On Windows, os.tmpdir() uses the value of the TEMP or TMP environment variables. In some cases (including on GitHub Actions runners), if the user directory name is more than 8 characters, these variables may include a short (8.3) name for the user directory.

Node doesn't have an API to convert between short and long names. There are various possible workarounds, including:

  • Use powershell: this is an official API but much more expensive.
  • There's no cmd built-in or one-liner for this. However, attrib.exe appears to always expand the last path segment into a long path.
  • If the only short segment is the user directory name, replace it with the path from os.homedir() (as long as it's the same directory and not a short name).

This package currently tries the os.homedir() workaround first (since it doesn't require spawning a process), and tries iterating through path segments and calling attrib.exe as a fallback.

API

normalizedTmpdir(options?): string

Return a normalized version of os.tmpdir().

import { normalizedTmpdir } from 'normalized-tmpdir';

const tmpdir = normalizedTmpdir();

The function accepts an options object:

  • console?: boolean | { warn: typeof console['warn'] }: By default, the library does not log warnings if normalization fails. If this option is true, log warnings to the default console. If a console object is provided, use it for logging.

To avoid potentially doing the Windows normalization on every call, the calculated full path is cached.

Example: Using with Jest console mocks

If you have Jest tests that mock the console methods, but you want errors from normalizedTmpdir to display anyway (for easier debugging), you can do the following:

import { normalizedTmpdir } from 'normalized-tmpdir';
import realConsole from 'console';

const tmpdir = normalizedTmpdir({ console: realConsole });

expandShortPath(shortPath: string): string | false

On Windows only: expand an absolute path with short (8.3) segments to a long path. If the user name is the only short segment, and shortPath is under os.homedir() (which must not include any short segments), uses os.homedir() as a replacement for the short part. Otherwise, expands each short segment of the path using attrib.exe.

Returns the expanded path, or false if not on Windows, it's an unsupported type of path, or there's an error expanding any of the segments.

import os from 'os';
import { expandShortPath } from 'normalized-tmpdir';

if (os.platform() === 'win32') {
  // Example: expand a short path to C:\Users\VeryLongName
  const longPath = expandShortPath('C:\\Users\\VERYLO~1');
}