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

directory-helpers

v0.1.8

Published

A collection of helper methods for working with directories

Downloads

169

Readme

directory-helpers

Build Status

A collection of helper methods for working with directories

Installing

directory-helpers is available as an npm package.

Usage

directory-helpers is best used in an ES.next environment with async/await enabled.

import Directory from 'directory-helpers';

async main() {
  const directory = new Directory('./project');
  await directory.writeFiles({
    'package.json': {
      name: 'project',
      version: '0.0.1'
    },
    'index.js': `
      process.stdout.write('Hello World!');
    `
  });
  const output = await project.exec('node', ['index.js'])
}
main();

Methods

Constructor

import Directory from 'directory-helpers';

const directory = new Directory(basePath)

Initializes a directory adapter pointing to the given basePath. The given basePath will be passed through path.resolve. Note that the directory is not created by the constructor.

Contains

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('/tmp/project');
  const fileInDirectory = await directory.contains('/tmp/project/package.json');
}

Determines whether the given path is contained within the basePath of the directory. Note that #contains does not determine whether a file actually exists at the given path.

Create

import Directory from 'directory-helpers';

async function main() {
  const basePath = './project';
  const directory = new Directory(basePath);
  await directory.create();
}

Creates the directory at the basePath given to the constructor if it does not already exist.

Exec

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.create();
  const output = await directory.exec('ls', ['-la']);
}

Executes the given shell command from basePath, returning the contents of stdout after the process exits. If the process exits unsuccessfully, the contents of stderr are thrown as an Error.

ExecJs

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.create();
  const output = await directory.execJs(`
    import * as path from path;
    console.log(path.resolve());
  `);
}

Executes the given JavaScript (ES.next supported) code from basePath, returning the output of stdout. If execution fails, an error is thrown with the contents of stderr.

Glob

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.create();
  const files = await directory.glob('**/*.js');
}

Evaluates a glob using glob, exposing the same interface. Note that options.cwd is set to the basePath of the directory.

Path

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  directory.path('src/lib/helpers.js');
}

Resolves paths relative to the basePath of the directory.

Read

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.write({
    'code.js': `
      console.log('Hello World!')
    `,
    'data.json': {
      hello: 'world'
    }
  });
  const code = await directory.read('file.js');
  const data = await directory.read('data.json');
}

Reads the contents of a file at the path relative to the basePath of the directory. The file contents are parsed using JSON.parse() when possible.

Remove

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.write({
    foo: '',
    bar: '',
    baz: ''
  });
  await directory.remove('foo');
  await directory.remove();
}

Deletes a file at path relative to the basePath of the directory. If no file is given, deletes the directory at basePath if it exists.

Resolve

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.resolve('npm-package');
}

Resolves the absolute path to a Node.js module using the require.resolve algorithm.

Spawn

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.write({
    'package.json': {
      name: 'project'
    },
    'server.js': `
      setTimeout(() => {
        console.log('Loading...');
        setTimeout(() => {
          console.log('Ready');
          setTimeout(() => {
            console.log('Result');
          }, 100);
        }, 100);
      }, 100);
    `
  });
  const server = directory.spawn('npm', ['start']);
  await server.filter((output) => output.match(/Listening/));
  server.process.kill();
}

Spawns a child process from basePath and returns an Observable of text produced by stdout and stderr. The ChildProcess instance can be accessed from the process attribute. The command is spawned in its own process group using setsid so that when ChildProcess#kill is called, it kills the spawned process and all of its subprocesses.

Start

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.write({
    'package.json': {
      name: 'project',
      scripts: {
        start: 'serve'
      }
    }
  });
  await directory.start(/Listening/);
  // ...
  await directory.stop();
}

Spawns npm start with basePath as the working directory. If a regular expression is given, #start waits and resolves after reading a matching line from STDOUT. The process is spawned in its own process group, making it easier to kill the process and all of its descendent processes.

Stat

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  const stats = await directory.stat('package.json');
}

Reads the file status for the file at the given path relative to basePath, returning an instance of fs.Stats.

Stop

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.write({
    'package.json': {
      name: 'project',
      scripts: {
        start: 'serve'
      }
    }
  });
  await directory.start(/Listening/);
  // ...
  await directory.stop();
}

Stops the process group spawned by #start.

Symlink

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.symlink('../sourcePath', 'destinationPath');
}

Creates a symbolic link at destinationPath pointing to the sourcePath. Both paths are relative to the basePath. If any of the directories in the destinationPath do not exist, they are created.

Write

import Directory from 'directory-helpers';

async function main() {
  const directory = new Directory('./project');
  await directory.create();
  await directory.write({
    'package.json': {
      name: 'project',
      version: '0.0.1'
    },
    'src/index.js': `
      import * as path from 'path';
      console.log(path.resolve());
    `
  });
}

Creates or overwrites files at paths relative to the basePath of the directory. If the file contents are an object or array, it will be converted to a string using JSON.stringify(contents, null, 2). If the file contents are a string, that string will be re-indented so that there is no leading space on the first line. Any missing directories will be created.

Development

Getting Started

The application requires the following external dependencies:

  • Node.js

The rest of the dependencies are handled through:

npm install

Run tests with:

npm test