Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (92 loc) · 3.26 KB
/
Copy pathindex.js
File metadata and controls
109 lines (92 loc) · 3.26 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
import fs from 'fs';
import path from 'path';
import chokidar from 'chokidar';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
function findCssFilesOfFonts(fileName, fileList = []) {
const fontImports = extractImportsFromFile(fileName);
fontImports.forEach(fontImport => {
const css = require.resolve(fontImport);
fileList.push(css);
});
return fileList;
}
export function extractImports(source) {
return [...source.matchAll(/^\s*import\s+(['"])([^'"]+)\1\s*;?\s*$/gm)]
.map(([, , fontImport]) => fontImport);
}
function extractImportsFromFile(fileName) {
return extractImports(fs.readFileSync(fileName, 'utf8'));
}
function copyFontFolders(fontImport, destDir) {
const fontPath = require.resolve(fontImport);
const fontDir = path.join(path.dirname(fontPath), 'files');
const fontFiles = fs.readdirSync(fontDir);
const fontDestDir = path.join(destDir, path.basename(fontDir));
fs.mkdirSync(fontDestDir, { recursive: true });
fontFiles.forEach(file => {
const filePath = path.join(fontDir, file);
const destPath = path.join(fontDestDir, file);
console.log("Copying:", filePath, destPath);
fs.copyFileSync(filePath, destPath);
});
}
function combineSassFiles(declarationFiles, combinedContent) {
declarationFiles.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
combinedContent += content + '\n';
});
return combinedContent;
}
function generateFontsFile(destDir, fontsFile, generatedFontsFile) {
const fontModules = extractImportsFromFile(fontsFile);
fontModules.forEach((fontImport) => copyFontFolders(fontImport, destDir));
const cssFiles = findCssFilesOfFonts(fontsFile);
const combinedContent = combineSassFiles(cssFiles, '');
fs.writeFileSync(generatedFontsFile, combinedContent);
}
export function copyFonts(source, destination, watch, verbose) {
///
const viewsDir = path.join(source, 'styles');
const destDir = path.join(destination, 'styles');
const watchFlag = process.argv.includes('--watch');
const fontsFile = path.join(viewsDir, 'fonts.ts');
const generatedFontsFolder = path.join(viewsDir, 'generated');
const generatedFontsFile = path.join(generatedFontsFolder, '_fonts.scss');
fs.mkdirSync(generatedFontsFolder, { recursive: true });
console.log("Generated folder created:", generatedFontsFolder);
///
const isSourceExists = fs.existsSync(viewsDir);
if (!watch && !isSourceExists) {
console.error(`Source path "${source}" does not exist.`);
process.exit(1);
}
if (isSourceExists) {
try {
generateFontsFile(destDir, fontsFile, generatedFontsFile);
} catch (error) {
console.error('Error compiling SCSS:', error);
}
}
if (watchFlag) {
const watcher = chokidar.watch(fontsFile, {
ignored: /(^|[\/\\])\../,
persistent: true
});
watcher.on('add', () => {
try {
generateFontsFile(destDir, fontsFile, generatedFontsFile);
} catch (error) {
console.error('Error compiling SCSS:', error);
}
});
watcher.on('change', () => {
try {
generateFontsFile(destDir, fontsFile, generatedFontsFile);
} catch (error) {
console.error('Error compiling SCSS:', error);
}
});
console.log('Watching for file changes...');
}
}