UNPKG

1.35 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4var path = require('path');
5
6module.exports = {
7 getCompiledType: getCompiledType,
8 getType: getType,
9 stripIgnoredBasePath: stripIgnoredBasePath,
10 changeExt: changeExt
11};
12
13function getCompiledType(type) {
14 // Accept an object with a 'type' property, or else assume a string
15 type = _.get(type, 'type') || type;
16
17 var typeMap = {
18 css: ['sass', 'less', 'stylus'],
19 js: ['coffee']
20 };
21 return _.findKey(typeMap, function (types) {
22 return _.includes(types, type);
23 });
24}
25
26function getType(ext) {
27 var typeMap = {
28 js: 'js',
29 css: 'css',
30 scss: 'sass',
31 less: 'less',
32 styl: 'stylus',
33 coffee: 'coffee'
34 };
35 return typeMap[_.trimStart(ext, '.')];
36}
37
38function stripIgnoredBasePath(_path, basePaths) {
39 // Remove dots and slashes to ensure we never write above root.
40 var trimmedPath = _.trimStart(_path, './\\');
41
42 var ignored = basePaths.find(function (base) {
43 return _.startsWith(trimmedPath, _.trimEnd(base, './\\'));
44 });
45 var ignoredLength = ignored ? (ignored + path.sep).length : 0;
46 return trimmedPath.substring(ignoredLength);
47}
48
49function changeExt(file, toExt, fromExt) {
50 var _path$parse = path.parse(file),
51 dir = _path$parse.dir,
52 name = _path$parse.name;
53
54 return path.join(dir, (fromExt ? path.basename(file, fromExt) : name) + toExt);
55}
\No newline at end of file