forked from sagold/handlebars-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTargetFilepath.js
32 lines (26 loc) · 1.04 KB
/
getTargetFilepath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const path = require("path");
const sanitizePath = require("./sanitizePath");
/**
* Returns the target filePath of a handlebars template
* @param {String} filePath - input filePath
* @param {String} [outputTemplate] - template for output filename. If ommited, the same filename stripped of its
* extension will be used
* @param {String} [rootPath] - input rootPath
* @return {String} target filePath
*/
module.exports = function getTargetFilepath(filePath, outputTemplate, rootPath) {
filePath = sanitizePath(filePath);
rootPath = rootPath ? sanitizePath(rootPath) : path.dirname(filePath);
if (outputTemplate == null) {
return filePath.replace(path.extname(filePath), "");
}
const folderPath = path
.dirname(filePath)
.split(rootPath)[1];
const fileName = path
.basename(filePath)
.replace(path.extname(filePath), "");
return outputTemplate
.replace("[path]", folderPath)
.replace("[name]", fileName);
};