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

@codemod-utils/files

v2.0.1

Published

Utilities for handling files

Downloads

1,171

Readme

This project uses GitHub Actions for continuous integration.

@codemod-utils/files

Utilities for handling files

What is it?

@codemod-utils/files wraps the synchronous methods from node:fs, node:path, and glob to help you update many files in the same way.

API

Many of the methods make use of the file path, a string that represents the location of a file. Therefore, I recommend learning findFiles first, as it returns the paths of all files that match the search criteria.

copyFiles

Copies files from one directory (source) to another (destination). Creates the destination directory if it doesn't exist.

Copy LICENSE.md and README.md from the project root to the folder ember-container-query.

import { copyFiles } from '@codemod-utils/files';

const filePathMap = new Map([
  ['LICENSE.md', 'ember-container-query/LICENSE.md'],
  ['README.md', 'ember-container-query/README.md'],
]);

copyFiles(filePathMap, {
  projectRoot,
});

createDirectory

Creates the directories specified in the file path, if they don't exist yet.

⚠️ Likely, you won't need this method but createFiles instead.

Create the folder ember-container-query if it doesn't exist.

import { createDirectory } from '@codemod-utils/files';

const newFilePath = 'ember-container-query/LICENSE.md';
const destination = join(projectRoot, newFilePath);

createDirectory(destination);

createFiles

Create files. Creates the destination directory if it doesn't exist.

Create LICENSE.md and README.md in the project root.

import { createFiles } from '@codemod-utils/files';

const fileMap = new Map([
  ['LICENSE.md', 'The MIT License (MIT)'],
  ['README.md', '# ember-container-query'],
]);

createFiles(fileMap, {
  projectRoot,
});

findFiles

Often, you will want a codemod step to apply to select files. findFiles provides the paths of all files that match your search criteria (i.e. glob pattern, ignore list, and project root). The paths are sorted in alphabetical order.

Find all component templates in an Ember app.

import { findfiles } from '@codemod-utils/files';

const filePaths = findFiles('app/components/**/*.hbs', {
  projectRoot,
});

You can provide ignoreList, an array of file paths or glob patterns, to exclude files.

Find all component classes in an Ember app.

import { findfiles } from '@codemod-utils/files';

const filePaths = findFiles('app/components/**/*.{js,ts}', {
  ignoreList: ['**/*.d.ts'],
  projectRoot,
});

To look for multiple types of files, you can pass an array of glob patterns (pattern A or pattern B or ...).

import { findfiles } from '@codemod-utils/files';

const filePaths = findFiles([
  'LICENSE.md',
  'README.md',
], {
  projectRoot,
});
import { findfiles } from '@codemod-utils/files';

const filePaths = findFiles([
  'app/components/**/*.hbs',
  'tests/integration/components/**/*-test.{js,ts}',
], {
  projectRoot,
});

mapFilePaths

Creates a mapping of file paths, which can then be passed to copyFiles or moveFiles.

Map LICENSE.md to ember-container-query/LICENSE.md (and similarly for README.md).

import { mapFilePaths } from '@codemod-utils/files';

const filePaths = ['LICENSE.md', 'README.md'];

const filePathMap = mapFilePaths(filePaths, {
  from: '',
  to: 'ember-container-query',
});

moveFiles

Moves files from one directory (source) to another (destination). Creates the destination directory if it doesn't exist. Removes the source directory if it is empty.

Move LICENSE.md and README.md from the project root to a folder named ember-container-query.

import { moveFiles } from '@codemod-utils/files';

const filePathMap = new Map([
  ['LICENSE.md', 'ember-container-query/LICENSE.md'],
  ['README.md', 'ember-container-query/README.md'],
]);

moveFiles(filePathMap, {
  projectRoot,
});

parseFilePath

Parses a file path, similarly to parse() from node:path, but correctly handles file extensions with more than one ., e.g. .d.ts and .css.d.ts.

import { parseFilePath } from '@codemod-utils/files';

const filePath = 'src/components/navigation-menu.d.ts';
const { base, dir, ext, name } = parseFilePath(filePath);

// base -> 'navigation-menu.d.ts'
// dir  -> 'src/components'
// ext  -> '.d.ts'
// name -> 'navigation-menu'

removeDirectoryIfEmpty

Removes the directories specified in the file path, if they are empty.

⚠️ Likely, you won't need this method but removeFiles instead.

Remove the folder ember-container-query if it is empty.

import { removeDirectoryIfEmpty } from '@codemod-utils/files';

const filePath = 'ember-container-query/LICENSE.md';

removeDirectoryIfEmpty(filePath, {
  projectRoot,
});

removeFiles

Removes files. Removes the source directory if it is empty.

Remove LICENSE.md and README.md from the project root.

import { removeFiles } from '@codemod-utils/files';

const filePaths = ['LICENSE.md', 'README.md'];

removeFiles(filePaths, {
  projectRoot,
});

renamePathByDirectory

Forms a new file path by altering the path's directory.

Prepare to move components from addon to ember-container-query/src.

import { renamePathByDirectory } from '@codemod-utils/files';

const oldFilePath = 'addon/components/container-query.hbs';

const newFilePath = renamePathByDirectory(oldFilePath, {
  from: 'addon',
  to: 'ember-container-query/src',
});

// newFilePath -> 'ember-container-query/src/components/container-query.hbs'

Compatibility

  • Node.js v18 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.