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

gulp-subdir-rename

v1.1.0

Published

Dynamically rename subdirectories on a per-file-basis.

Downloads

3

Readme

gulp-subdir-rename

Dynamically rename subdirectories on a per-file-basis.

Purpose

The need for this plugin arose within a project where we had to pull automatically generated modules, which were stored under ugly guid-named folders, and put them into our IDE. What we wanted was to have the actual modules name as folder name.

Why don't you use gulp-rename?

One Simple reason: We only know the folders name for a file during execution time, so we can not use static configuration. And also, the wanted name of the sub-directory is actually found in a file WITHIN this exact sub-directory. What would work with a rather complex function to gulp-rename() is done easily with this plugin.

Installation

npm install gulp-subdir-rename

Usage

Suppose you have several modules in a folder structure where the directory names are weird GUIDs, the modules names are actually stored within a module.json under each module-subdirectory, like this:

app
|
├── 9ABD4D9D80C702AF85C822A8
|        ├── module.json
|        └── ... some other files
└── CJF4D4L0D80C702AF85CJFA9
         ├──module.json
         └── ... some other files and subdirs

What you actually want is this:

app
|
├── module1
|        ├──module.json
|        └── ... some other files
└── module2
         ├──module.json
         └── ... some other files and subdirs

With gulp-subdir-rename, it is as easy as this:

// gulpfile.js

var gulp = require('gulp'),
    subdirRename = require('gulp-subdir-rename');


gulp.task('default', function () {
    gulp.src('./source/**/**.*')
        .pipe(subdirRename({
            baseFile : './module.json',
            renameTo : function (baseFileData) {
                var moduleJSON = JSON.parse(baseFileData);
                return moduleJSON.moduleName;
            }
        }))
        .pipe(gulp.dest('./target'));
});

Also see the '/examples'-Folder for a work example, just execute gulp.

API

gulp-subdir-rename renames the first subfolder after the base-path of what you gulp.src(). The base-path is usually where the glob starts, so for example for a gulp.src('/base/path/**') it would be /base/path.

require('gulp-subdir-rename') returns a function that accepts an options-object with two parameters:

  • baseFile - string : Path to the file you wanna pull the subdirectories name from. The Path must be relative to the subdirectory that's to be renamed (so the first directory after the base-path). It's content will be given as a paramter to renameTo().
  • renameTo - function(baseFileContent, oldSubDirName) : The functions return value will be used as the new subdirectories name. It's first parameter are the contents of the specified baseFile (as a string) and second parameter is old name of directory that will be changed. If ""or undefined or null is returned from the function then no rename is done and folder is copied as it is.
  • isOptional - boolean (default: false) : set this to true if you have folders which do not contain the baseFile. Per default, those folders would cause an error. If set to true, those folders will then just be copied as they are with their original dir-name.

Limitations

There are some limitations to the functionality of this plugin, as it is suffice as it is to my own requirements right now and i tried to keep it as simple as possible. If you wish for enhancements, just let me know and I'll be happy to work them in. You can also put a pull-request, of course.

Right now the known limitations are:

  • As stated several times, the only directory that can be renamed is the first one after the base-path
  • The renamteTo()-Function can only handle synchronous functions