forked from akanix42/meteor-css-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-output-path.js
36 lines (31 loc) · 1.02 KB
/
get-output-path.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
33
34
35
36
/* globals Npm */
import R from 'ramda';
import path from 'path';
import format from 'string-template';
export default function getOutputPath(filePath, outputPathTemplates) {
const template = getTemplate(filePath, outputPathTemplates);
const extname = path.extname(filePath);
return format(template, {
dirname: path.dirname(filePath),
basename: path.basename(filePath, extname),
extname
});
}
function getTemplate(filePath, outputPathTemplates) {
if (R.type(outputPathTemplates) === 'String') {
return outputPathTemplates;
}
const keys = Object.keys(outputPathTemplates);
for (let index = 0; index < keys.length; index++) {
const key = keys[index];
if (key === 'default') continue;
let val = outputPathTemplates[key];
if (R.type(val) === 'String') {
val = outputPathTemplates[key] = { template: val, regex: new RegExp(key) };
}
if (val.regex.test(filePath)) {
return val.template;
}
}
return outputPathTemplates['default'] || '{dirname}/{basename}{extname}';
}