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

parse-workspace-path

v0.1.1

Published

> Parses arbitrary path string considering the presence of packages and workspaces

Downloads

18

Readme

parse-workspace-path

Parses arbitrary path string considering the presence of packages and workspaces

It returns similar object like NodeJS path.parse but adds additional if finds yarn workspaces and (or) npm packages (it should be existing path)

Install

npm i parse-workspace-path --save

Usage

const parseWspPath = require('parse-workspace-path');

const absolutePath = '.../yarn-workspace-folder/packages/package-A/src/...';

parseWspPath(absolutePath)
  .then(result => console.log(result))

The returned object will have the following properties:

  • dir <string>
  • root <string>
  • workspaceName <string>
  • packageDir <string>
  • packageName <string>
  • packageVersion <string>
  • localDir <string>
  • base <string>
  • name <string>
  • ext <string>
┌─────────────────────┬───────────┬─────────────────────┬──────────────┬────────────┐
│         .dir        │.workspace │   .packageDir       │  .localDir   │   .base    │
│                     │   name    │                     │              │            │
├──────┐              ├───────────┼─────────────────────┼──────────────┼──────┬─────┤
│.root │              │           │                     │              │.name │.ext │
│' /    home/user/dir / monorepo  / lib/addons/packageA / src/__test__ / file  .txt'│
│      │              │           │                   ↳'/ package.json'│      │     │
│      │              │        ↳ '/ package.json'       │              │      │     │
└──────┴──────────────┴───────────┴─────────────────────┴──────────────┴──────┴─────┘
                                       ⇧⇧⇧                   ⇧⇧⇧
                               ╭──── workspace ───╮  ╭──── package ─────╮
                               │ "private": true  │  │  (.packageName)  │
                               │ "workspaces": [] │  │ (.packageVersion)│
                               ╰──────────────────╯  ╰──────────────────╯

Notice

  • base, name and ext will be empty strings if absolutePath points to a dir
  • packageDir is a relative path between workspace folder and a package folder. It will be an empty string if one of them is absent
  • dir path to the folder:
    1. that contain a workspace folder (not including workspace folder itself)
    2. of a package if no workspace is found
    3. that absolutePath points if neither a package nor a workspace were found. Never includes a file name.
  • localDir is a remaining part of path between package folder (or workspace folder) and a end point specified by absolutePath. Never includes a file name.
  • A workspace folder to be detected as a "workspace" should contain fields "private": true and "workspaces" and fit at least one of the conditions:
    1. The rest part of the path don't contain package.json files (a single workspace case)
    2. The rest part of the path contain package.json and it matches one of "workspaces": [] glob patterns

You can rely on that dir + workspace + packageDir + localDir + base is always equal to absolutePath. See examples for details.

Example

const absolutePath = '/home/usulpro/WebProjects/read-pkg-workspace/workspaces/unicorns/celestabelleabethabelle/src/stories/unicorn.story.js';

parseWspPath(absolutePath)
  .then(result => console.log(result))

will output:

{
  root: '/',
  dir: '/home/usulpro/WebProjects/read-pkg-workspace',
  localDir: 'src/stories',
  base: 'unicorn.story.js',
  ext: '.js',
  name: 'unicorn.story',
  workspaceName: 'workspaces',
  packageDir: 'unicorns/celestabelleabethabelle',
  packageName: 'celestabelleabethabelle',
  packageVersion: '1.0.0'
}

Take a look on some other visual examples:

color output

API

parseWspPath(absolutePath)

Returns a Promise for the result object.

absolutePath

Directory or a file to start analyzing

See also

  • read-pkg-up - Read the closest package.json file
  • read-pkg-workspace - Reads the closest two package.json files and checks if they are organized in a workspace

Credits

@UsulPro @UsulPro

MIT 2018