Skip to content

Commit f462bdb

Browse files
committed
fix: tilde expansion
1 parent 4d2856d commit f462bdb

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/RollingFileWriteStream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class RollingFileWriteStream extends Writable {
4343
throw new Error(`Invalid filename: ${filePath}`);
4444
} else if (filePath.endsWith(path.sep)) {
4545
throw new Error(`Filename is a directory: ${filePath}`);
46-
} else {
46+
} else if (filePath.indexOf(`~${path.sep}`) === 0) {
4747
// handle ~ expansion: https://github.com/nodejs/node/issues/684
4848
// exclude ~ and ~filename as these can be valid files
49-
filePath = filePath.replace(new RegExp(`^~(?=${path.sep}.+)`), os.homedir());
49+
filePath = filePath.replace("~", os.homedir());
5050
}
5151
super(options);
5252
this.options = this._parseOption(options);

test/RollingFileWriteStream-test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ describe("RollingFileWriteStream", () => {
8989
});
9090
});
9191

92+
describe("with tilde expansion in filename", () => {
93+
let s;
94+
const fileName = "tmpTilde.log";
95+
const expandedPath = path.join(__dirname, fileName)
96+
97+
before(() => {
98+
const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
99+
"./now": mockNow,
100+
"os": {
101+
homedir() {
102+
return path.join(__dirname);
103+
}
104+
}
105+
});
106+
s = new RollingFileWriteStream(path.join("~", fileName));
107+
});
108+
109+
after(() => {
110+
s.end(() => fs.removeSync(expandedPath));
111+
});
112+
113+
it("should take a filename and options, return Writable", async () => {
114+
s.currentFileStream.path.should.eql(expandedPath);
115+
fs.existsSync(expandedPath).should.be.true();
116+
});
117+
});
118+
92119
describe("with default arguments", () => {
93120
const fileObj = generateTestFile();
94121
let s;
@@ -341,7 +368,7 @@ describe("RollingFileWriteStream", () => {
341368
s.end(() => {
342369
fs.removeSync(fileObj.dir);
343370
done();
344-
})
371+
});
345372
});
346373

347374
it("should rotate files within the day, and when the day changes", () => {

0 commit comments

Comments
 (0)