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

restructor

v0.0.3

Published

Restructure your project files in a structured way

Downloads

6

Readme

RE-STRUCTOR

It could:

  • make all file names camelCase
  • make all file names snake-case
  • rename all React sources to jsx
  • and name them from Capital letter, as long they export Class
  • or name from lower, if not. Why not?
  • add aliases to code base
  • remove aliases from code base
  • move a file to a new position

And it this same time:

  • it will use git mv to rename the file. Maintaining history.
  • it will update all the usages from just renamed file.

It will help you RESTRUCTURE your codebase, rewiring all imports, requires, and even proxyquire

Schemes

There are 4 pre-created schemes

  • import toReactCamelCase from 'restructor/scheme/React-Camel-case - to restructure to React+CamelCase
  • import toSnakeCase from 'restructor/scheme/snake-case - to restructure to snake case
  • import rewireAlises from 'restructor/scheme/change-aliases - to change aliases
  • import rename from 'restructor/scheme/rename - just to rename anything

All accepts root as the first argument, and aliases as the second.

const aliases =  {
  common: path.resolve('src/common'),
  components: path.resolve('src/components'),  
};

await toReactCamelCase(root, aliases)
await toKebabCase(root, aliases)
await rewireAlises(root, oldAliases, newAliases)
await rename(root, renameCallback, aliases)

All will return the set of changes, you have to apply. For example

const changes = await toKebabCase(root, aliases);
await writeContent(change);
await gitRenameAsync(change);

API

// the main
import restructor from 'restructor';
import {toGit} from 'restructor/versionControl';
import {isJS} from 'restructor/types';
import {isReact, toJSX, isHOC} from "restructor/React";
import {startsFromLower, startsFromCapital, toSnakeCase, toCamelCase} from "restructor/letterCase";
import {isClassDefaultExported} from "restructor/exports";
import {applyAliases, toRelative, renameImports, resolveImports, rewireImports} from 'restructor/imports'

index

  • structure = await restructor(root) - generates the structure
  • structure = rename(structure, callback) - renames the files by the rule
  • async writeContent(structure) - writes content back to files
  • where structure is array of file.

versionControl

  • const structure = await gitRenameAsync(structure) - flushes the commands into GIT

React

  • isJS(file), isCSS(file) - returns a file type. (.ts is also js)
  • isReact(file) - true, if file contain JSX.
  • isHOC(file, additionalSignatures=[]) - true, if HOC is exported as default export.
  • toJSX(fileName) - rename file to jsx(or tsx).

letterCase

  • startsFromLower(fileName)/startsFromCapital - changes file signature.
  • toSnakeCase(fileName)/toCamelCase - changes file signature.

exports

  • isClassDefaultExported(file) - true, if class is exported as default export.
  • nameAsExport(file) - use default exported Component name as name.

imports

resolveImports(structure, aliases) - main module function, fills the imports information. rewireImports(structure) - main module function, flushes the changes back renameImports(structure) - applies module renames (already done in resolveImports)

toRelative(structure) - converts all paths to relative applyAliases(structure, aliases) - applies aliases where possible.

Example

This is ReactCamelCase scheme

const structure = await restructure(root);
  const aliases = {
    '/': root,
    ...initialAliases
  };

  const renamed = rename(structure,
    file => {
      let fileName = file.file;
      if(!isTest(file)) {
        if (isJS(file)) {
          if (isReact(file)) {
            if (isClassDefaultExported(file)) {
              fileName = nameAsExport(file)
            }
            fileName = toJSX(fileName);
          }

          fileName = toCamelCase(fileName);
          if (isClassDefaultExported(file) || isHOC(file)) {
            fileName = startsFromCapital(fileName);
          } else {
            fileName = startsFromLower(fileName);
          }
          fileName = keepIndex(fileName)
          return fileName;
        }
      }
      return null;
    }
  );

  const imported = resolveImports(renamed, {
    '/': root,
    ...aliases
  });

  const result = rewireImports(applyAliases(toRelative(renameImports(imported)), aliases, true))
    .filter(({file, rename, newContent, context}) => (
      (rename && file !== rename) || (newContent && newContent !== context)
    ));

  return result;

Licence

MIT