Skip to content

Commit dba90e3

Browse files
committed
Merge pull request #14 from rovjuvano/feat/conforming-behavior
Feat/conforming behavior
2 parents 88a9730 + fbe1420 commit dba90e3

File tree

7 files changed

+371
-184
lines changed

7 files changed

+371
-184
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,41 @@ gulp-rename provides simple file renaming methods.
1010
var rename = require("gulp-rename");
1111

1212
// rename via string
13-
gulp.src("./src/hello.txt")
14-
.pipe(rename("goodbye.txt"))
15-
.pipe(gulp.dest("./dist"));
13+
gulp.src("./src/main/text/hello.txt")
14+
.pipe(rename("main/text/ciao/goodbye.md"))
15+
.pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/goodbye.md
1616

1717
// rename via function
18-
gulp.src("./src/hello.txt")
19-
.pipe(rename(function (dir, base, ext) {
20-
return base + "-goodbye" + ext;
18+
gulp.src("./src/**/hello.txt")
19+
.pipe(rename(function (path) {
20+
path.dirname += "/ciao";
21+
path.basename += "-goodbye";
22+
path.extname = ".md"
2123
}))
22-
.pipe(gulp.dest("./dist")); // ./dist/hello-goodbye.txt
24+
.pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md
2325

2426
// rename via hash
25-
gulp.src("./src/hello.txt")
27+
gulp.src("./src/main/text/hello.txt", { base: process.cwd() })
2628
.pipe(rename({
29+
dirname: "main/text/ciao",
30+
basename: "aloha",
2731
prefix: "bonjour-",
2832
suffix: "-hola",
29-
ext: ".md"
33+
extname: ".md"
3034
}))
31-
.pipe(gulp.dest("./dist")); // ./dist/bonjour-hello-hola.md
35+
.pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/bonjour-aloha-hola.md
3236
```
3337

38+
**See test/rename.spec.js for more examples and test/path-parsing.spec.js for hairy details.**
39+
3440
## Notes
3541

36-
`ext` follows the node convention in that it includes the period.
42+
* `dirname` is the relative path from the base directory set by `gulp.src` to the filename.
43+
* `gulp.src()` uses glob-stream which sets the base to the parent of the first directory glob (`*`, `**`, [], or extglob). `dirname` is the remaining directories or `./` if none. glob-stream versions >= 3.1.0 (used by gulp >= 3.2.2) accept a `base` option, which can be used to explicitly set the base.
44+
* `gulp.dest()` renames the directories between `process.cwd()` and `dirname` (i.e. the base relative to CWD). Use `dirname` to rename the directories matched by the glob or descendents of the base of option.
45+
* KNOWN ISSUE: The base set when using brace expansion may not be what you expect (See wearefractal/glob2base#1). Use the `base` option described above.
46+
* `basename` is the filename without the extension like path.basename(filename, path.extname(filename)).
47+
* `extname` is the file extension including the '.' like path.extname(filename).
3748

3849
## License
3950

index.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
11
var map = require("map-stream"),
2-
path = require("path");
2+
Path = require("path");
33

44
module.exports = function (obj) {
55
"use strict";
66

7+
function parsePath(path) {
8+
var extname = Path.extname(path);
9+
return {
10+
dirname: Path.dirname(path),
11+
basename: Path.basename(path, extname),
12+
extname: extname
13+
};
14+
}
15+
716
function rename(file, callback) {
817

9-
if (!obj) {
10-
callback(new Error("No renaming parameter supplied"), undefined);
11-
}
18+
var parsedPath = parsePath(file.relative);
19+
var path;
1220

13-
// helper variables
14-
var relativePath = path.relative(file.cwd, file.path),
15-
dir = path.dirname(relativePath),
16-
firstname = file.path.substr(file.path.indexOf(".", 1)),
17-
ext = file.path.substr(file.path.lastIndexOf(".")),
18-
base = path.basename(file.path, ext),
19-
finalName = "";
21+
var type = typeof obj;
2022

21-
if (typeof obj === "string") {
23+
if (type === "string" && obj !== "") {
2224

23-
finalName = obj;
25+
path = obj;
2426

25-
} else if (typeof obj === "function") {
27+
} else if (type === "function") {
2628

27-
finalName = obj(dir, base, ext);
29+
var result = obj(parsedPath) || parsedPath;
30+
path = Path.join(result.dirname, result.basename + result.extname);
2831

29-
} else if (typeof obj === "object") {
32+
} else if (type === "object" && obj !== undefined && obj !== null) {
3033

31-
var prefix = obj.prefix || "",
34+
var dirname = obj.dirname || parsedPath.dirname,
35+
prefix = obj.prefix || "",
3236
suffix = obj.suffix || "",
33-
extension = obj.ext || ext;
37+
basename = obj.basename || parsedPath.basename,
38+
extname = obj.extname || parsedPath.extname;
3439

35-
finalName = prefix + path.basename(file.path, firstname) + suffix + extension;
40+
path = Path.join(dirname, prefix + basename + suffix + extname);
3641

3742
} else {
3843
callback(new Error("Unsupported renaming parameter type supplied"), undefined);
44+
return;
3945
}
4046

41-
file.path = path.join(dir, finalName);
47+
file.path = Path.join(file.base, path);
48+
4249
callback(null, file);
4350
}
4451

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"url": "git://github.com/hparra/gulp-rename.git"
1919
},
2020
"scripts": {
21-
"test": "mocha"
21+
"test": "mocha test/*.spec.js"
2222
},
2323
"dependencies": {
2424
"map-stream": "~0.0.4"

test/main.js

Lines changed: 0 additions & 152 deletions
This file was deleted.

test/path-parsing.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*global describe, context, it, helper */
2+
"use strict";
3+
4+
require("./spec-helper");
5+
var Path = require("path");
6+
7+
describe("gulp-rename path parsing", function () {
8+
describe("dirname", function () {
9+
context("when src pattern contains no globs", function () {
10+
it("dirname is '.'", function (done) {
11+
var srcPattern = "test/fixtures/hello.txt";
12+
var obj = function (path) {
13+
path.dirname.should.equal(".");
14+
};
15+
helper(srcPattern, obj, null, done);
16+
});
17+
});
18+
19+
context("when src pattern contains filename glob", function () {
20+
it("dirname is '.'", function (done) {
21+
var srcPattern = "test/fixtures/*.min.txt";
22+
var obj = function (path) {
23+
path.dirname.should.equal(".");
24+
};
25+
helper(srcPattern, obj, null, done);
26+
});
27+
});
28+
29+
var dirname_helper = function (srcPattern, expectedPath) {
30+
it("dirname is path from directory glob to file", function (done) {
31+
var obj = function (path) {
32+
path.dirname.should.match(/^fixtures[0-9]?$/);
33+
};
34+
helper(srcPattern, obj, null, done);
35+
});
36+
}
37+
38+
context("when src pattern matches a directory with *", function () {
39+
dirname_helper("test/*/*.min.txt");
40+
});
41+
42+
context("when src pattern matches a directory with **", function () {
43+
dirname_helper("test/**/*.min.txt");
44+
});
45+
46+
context("when src pattern matches a directory with [...]", function () {
47+
dirname_helper("test/fixt[a-z]res/*.min.txt");
48+
});
49+
50+
/* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */
51+
context.skip("when src pattern matches a directory with {...,...}", function () {
52+
dirname_helper("test/f{ri,ixtur}es/*.min.txt");
53+
});
54+
55+
/* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */
56+
context.skip("when src pattern matches a directory with {#..#}", function () {
57+
dirname_helper("test/fixtures{0..9}/*.min.txt");
58+
});
59+
60+
context("when src pattern matches a directory with an extglob", function () {
61+
dirname_helper("test/f+(ri|ixtur)es/*.min.txt");
62+
});
63+
64+
/* requires glob-stream >= 3.1.0 */
65+
context.skip("when src pattern includes `base` option", function () {
66+
it("dirname is path from given directory to file", function (done) {
67+
var srcPattern = "test/**/*.min.txt";
68+
var srcOptions = {base: process.cwd()};
69+
var obj = function (path) {
70+
path.dirname.should.equal("test/fixtures");
71+
};
72+
helper({pattern: srcPattern, options: srcOptions}, obj, null, done);
73+
});
74+
});
75+
});
76+
77+
describe("basename", function () {
78+
it("strips extension like Path.basename(path, ext)", function (done) {
79+
var srcPattern = "test/fixtures/hello.min.txt";
80+
var obj = function (path) {
81+
path.basename.should.equal("hello.min");
82+
path.basename.should.equal(Path.basename(srcPattern, Path.extname(srcPattern)));
83+
};
84+
helper(srcPattern, obj, null, done);
85+
});
86+
});
87+
88+
describe("extname", function () {
89+
it("includes '.' like Path.extname", function (done) {
90+
var srcPattern = "test/fixtures/hello.txt";
91+
var obj = function (path) {
92+
path.extname.should.equal(".txt");
93+
path.extname.should.equal(Path.extname(srcPattern));
94+
};
95+
helper(srcPattern, obj, null, done);
96+
});
97+
98+
it("excludes multiple extensions like Path.extname", function (done) {
99+
var srcPattern = "test/fixtures/hello.min.txt";
100+
var obj = function (path) {
101+
path.extname.should.equal(".txt");
102+
path.extname.should.equal(Path.extname(srcPattern));
103+
};
104+
helper(srcPattern, obj, null, done);
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)