-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.js
More file actions
117 lines (98 loc) · 3.96 KB
/
Copy pathutilities.js
File metadata and controls
117 lines (98 loc) · 3.96 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function getDirectoryContent(path) {
try{
const fs = require('fs');
return fs.readdirSync(path);
}
catch{
return [];
}
}
function generateExportedFilePath(filePath, theme) {
if (theme === '_default.scss')
return `${filePath}.scss`;
const themeName = theme.slice(1,-5).replace(/_/g, '-');
return `${filePath}-${themeName}.scss`;
}
function getFileContents(filepath) {
const fs = require('fs');
return fs.readFileSync(filepath, 'utf-8');
}
module.exports.getBootlaterusFiles = function getBootlaterusFiles(path, outpath, themesDirectoryName, mainFileName) {
const directories = getDirectoryContent(path);
const themesDirectory = directories.find(directory => directory === themesDirectoryName);
const bootlaterusDirectories = directories.filter(directory => directory !== themesDirectoryName);
if (!themesDirectory)
throw 'themes directory not found';
if (!bootlaterusDirectories)
throw 'bootlaterus directories not found';
const themeFiles = getDirectoryContent(`${path}/${themesDirectory}`);
const bootlaterusFiles = {};
bootlaterusDirectories.forEach(bootlaterusDirectory => {
const exportedFilePathBase = `${outpath}/${bootlaterusDirectory}/${bootlaterusDirectory}`;
const mainFilePath = `${path}/${bootlaterusDirectory}/${mainFileName}`;
themeFiles.forEach(themeFile => {
const themeFilePath = `${path}/${themesDirectoryName}/${themeFile}`;
const exportedFilePath = generateExportedFilePath(exportedFilePathBase, themeFile);
bootlaterusFiles[exportedFilePath] = [themeFilePath, mainFilePath];
});
});
return bootlaterusFiles;
}
module.exports.getBootlaterusDistFiles = function getBootlaterusDistFiles(outpath, bootlaterusFiles) {
const prebuildFiles = Object.keys(bootlaterusFiles);
const distFiles = {};
prebuildFiles.forEach(file => {
const fileNameBeginPosition = file.lastIndexOf('/');
const fileNameEndPosition = file.lastIndexOf('.');
const fileName = file.substring(fileNameBeginPosition+1, fileNameEndPosition);
distFiles[`${outpath}/${fileName}.css`] = file;
});
return distFiles;
}
module.exports.getDistRelativePath = function getDistRelativePath(distFiles) {
return Object.keys(distFiles).map((path) => path.replace('./dist', '..'));
}
module.exports.getReadableThemeName = function getReadableThemeName(path) {
return path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf('.'))
.replace(/-|_/g, ' ')
.replace('cfonts', '+ fonts');
}
module.exports.getThemesMetadata = function getThemesMetadata(distFiles) {
const paths = this.getDistRelativePath(distFiles).reduce((result, current) => {
result[this.getReadableThemeName(current)] = current;
return result;
}, {});
const result = {}
Object.keys(paths).sort().forEach(function(key) {
result[key] = paths[key];
});
return JSON.stringify(
result
);
}
module.exports.getFile = function getFile(basepath, filename) {
if (basepath.endsWith('/'))
return getFileContents(`${basepath}${filename}`);
return getFileContents(`${basepath}/${filename}`);
}
module.exports.getFilenamesFromDirectory = function getFilenamesFromDirectory(path, extension) {
return getDirectoryContent(path)
.filter(file => file.includes(extension))
.map(file => `${path}/${file}`);
}
module.exports.getHtmlDistFiles = function getHtmlDistFiles(htmlFiles, srcpath, distpath) {
let distFiles = {};
return htmlFiles.reduce( (prev, file) => {
const dist = file.replace(srcpath, distpath);
const current = {};
current[dist] = file;
return {...prev, ...current};
}, distFiles);
}
module.exports.getRenderTags = function getRenderTags(src) {
return [...new Set(src.match(/<\s*Render\s*[^>]*>/g))];
}
module.exports.getSrcFromTag = function getSrcFromTag(tag) {
const src = tag.match(/src=".*"/)[0].match(/".*"/)[0];
return src.substring(1, src.length - 1);
}