Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rename

popomore40.2kMIT1.0.4TypeScript support: definitely-typed

Rename files using some transformers.

readme

rename

Rename files using some transformers.


NPM version Build Status Build Status NPM downloads

NOTE: Rename < 0.2.0 is not the same lib, you can see renamer. 1.0.0 have a big change.

Install

$ npm install rename -g

Usage

Rename file using another filepath.

rename('a.js', 'b.js');
// => b.js

Rename file using a transform object that contain some property.

{
  dirname: "replace dirname",
  prefix: "add prefix before basename",
  basename: "replace dirname",
  suffix: "add prefix after basename",
  extname: "replace extname"
}

Add -debug suffix

rename('a.js', {suffix: '-debug'});
// => a-debug.js

Also can use a transform function that could return a transform object.

rename('a.js', function() {
  return {suffix: '-debug'};
});
// => a-debug.js

API

rename(filepath, transformer)

filepath

Filepath can be a string or object. Parse the filepath to file object if it's a string.

File object should contain property dirname, basename and extname.

rename({
  basename: 'a',
  extname: '.js'
}, 'b.js');
// => b.js

transformer

Transformer can be a function, string or object.

Simple example about transform function using custom property.

function transformer(fileObj) {
  return {
    suffix: fileObj.hash || '-debug'
  };
}

rename({
  basename: 'a',
  extname: '.js'
}, transformer);
// => a-debug.js

rename({
  basename: 'a',
  extname: '.js',
  hash: '-123'
}, transformer);
// => a-123.js

The value of transform object can be template that parsed from file object.

rename({
  basename: 'c',
  extname: '.js',
  hash: '111'
}, {
  suffix: '-${hash}',
}).should.eql('c-111.js');

rename.parse

Generate a file object from a string or object.

rename.stringify

Generate a filepath from file object.

LISENCE

Copyright (c) 2017 popomore. Licensed under the MIT license.

changelog

1.0.4 / 2016-12-27

  • style: use eslint
  • deps upgrade debug
  • chore: use egg-bin for test

1.0.3 / 2015-02-15

  • fix: return self if transformer is empty

1.0.2 / 2015-02-15

  • fix: should clone transformer object

1.0.1 / 2015-02-13

  • fix: should use transformed object after function call

1.0.0

Big change!

  • new API rename(filepath, transformer)
  • split parse and stringify methods
  • support template in transform object

0.2.3

fix ../ path

0.2.2

fix relative path when dirname is empty

0.2.1

  • empty string will not throw
  • support relative path

0.2.0

First commit