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

alternate-file

v0.2.6

Published

Find and create spec files for any framework

Downloads

10

Readme

alternate-file

npm package CircleCI codecov David Dependency Status

In Vim, you can often go to the "alternate file" for the active buffer - usually the spec file for an implementation, or vice versa - by pressing :A. This extension adds exposes core functionality to build that behaviour into other editors, and also includes a CLI tool for broader integration.

alternate-file reads a config file based on the .projections.json file from Tim Pope's Projectionist. This lets you specify where the spec files for different types of files in your project are set up. One you have a .projections.json in your file tree, you can use this library to find or create a spec for an implementation file, or vice versa.

.projections.json

To describe your project's layout, create a .projections.json in the root of your project.

Each line should have the pattern for an implementation file as the key, and an object with the pattern for the alternate file (usually the spec file, but it can be whatever you want). Use a * in the main pattern and a {} in the alternate pattern to note the part of the path that should be the same between the two files. A * can stand in for an arbitrarily deep path.

Split paths

If your test paths have an extra directly in the middle of them, like with app/some/path/__test__/file.test.js with Jest, you can use {dirname} for the directory path and {basename} for the filename. You can do the same thing on the implementation side with the standard glob syntax: ** to represent the directory path, and * to represent the filename, like app/**/something/*.js.

If your paths have more than two variable parts, that can work too! You can use multiple sets of **/{dirname} pairs, which allows you to do something like:

"apps/**/lib/**/*.ex": {
  "alternate": "apps/{dirname}/test/{dirname}/{basename}_test.exs"
}

Multiple alternates

If your project is inconsistent about where specs go (it happens to the best of us), you can also pass an array to alternate. The extension will look for a file matching the first alternate, then the second, and so on. When you create an alternate file, it will always follow the first pattern.

Note that this isn't part of the original projectionist spec, but it's sometimes handy.

Examples

{
  // Basic
  // app/foo/bar/file.js => app/foo/bar/file.spec.js
  "app/*.js": { "alternate": "app/{}.spec.js" },
  // Dirname/Basename
  // app/foo/bar/file.js => app/foo/bar/__test__/file.test.js
  "*.js": { "alternate": "{dirname}/__test__/{basename}.test.js" },
  // Globbed implementation:
  // app/foo/bar/js/file.js => test/foo/bar/file_test.js
  "app/**/js/*.js": { "alternate": "test/{}/_test.js" },
  // Multiple alternatives
  // app/foo/bar/file.jsx =>
  //   app/foo/bar/file.spec.jsx OR app/foo/bar/file.spec.js OR
  //   spec/js/foo/bar/file_spec.js
  "app/*.jsx": { "alternate": ["app/{}.spec.jsx", "app/{}.spec.js", "spec/js/{}_spec.js"] }
}

For .projections.json files for popular frameworks, see the sample-projections. If your framework isn't in there, PRs for new sample-projections are welcome!

Contributing

Setup

Clone the repository, then

yarn install

Run Unit Tests

yarn test

Roadmap

  • Support templates for auto-populating new files.
  • Automatically create default .projection.json files
  • Support all the transformations from Projectionist, not just dirname and basename.
  • Support the "type" attribute in .projections.json, and allow for lookup by filetype, like for "controller/view/template".