-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
103 lines (93 loc) · 2.88 KB
/
build.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
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
require('colors');
const fs = require('fs');
const { createStyle } = require('./utils/manager.js')
const { loadObject } = require('./utils/ObjectCreator');
function getFirstComponent(text) {
if (text.includes('{%')) {
let result = text.substring(0, text.indexOf('{%'));
const component = text.substring(text.indexOf('{%') + 2, text.indexOf('%}'));
const endResult = text.substring(text.indexOf('%}') + 2, text.length);
const componentToSearch = component.split('"')[1];
const contentComponent = fs.readFileSync(`./components/${componentToSearch}`, 'utf8');
result += contentComponent;
result += endResult;
return getFirstComponent(result);
}
return text;
}
function searchComponents() {
const text = fs.readFileSync('./index.html', 'utf8');
const result = getFirstComponent(text);
return result;
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
const curPath = `${path}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function importStyle(style) {
return `<link rel="stylesheet" href="./${style}">\n`
}
function importScript(script) {
return `<script src="./scripts/${script}"></script>\n`
}
async function createHierarchy(path, dirs) {
await fs.mkdirSync(`build/${path}`);
for (let i = 0; i < dirs.length; i += 1) {
const directory = dirs[i];
const directoryName = typeof (directory) === 'string'
? directory
: directory.name;
const newPath = `${path}/${directoryName}`;
const newPathSource = `./${newPath}`;
const newPathDestination = `./build/${newPath}`;
fs.copyFile(newPathSource, newPathDestination, async (err) => {
if (err) throw err;
return true;
});
}
}
function importStyleScript() {
const scripts = fs.readdirSync('./scripts', { withFileTypes: true });
createHierarchy('scripts', scripts);
let generatedImport = '';
generatedImport += importStyle('style.css');
for (let i = 0; i < scripts.length; i += 1) {
const script = scripts[i];
generatedImport += importScript(
typeof (script) === 'string'
? script
: script.name
);
}
generatedImport += importScript('creator.js');
const images = fs.readdirSync('./images', { withFileTypes: true });
createHierarchy('images', images);
return generatedImport;
}
async function build() {
await deleteFolderRecursive('./build');
await fs.mkdirSync('build');
const content = await searchComponents();
const generatedImport = await importStyleScript();
const result = content.split('<head>')
const finalContent = `${result[0]}<head>\n${generatedImport}\n${result[1]}`
createStyle();
loadObject();
fs.appendFile('./build/index.html', finalContent, (err) => {
if (err) throw err;
console.log('\n✅ Build Completed!\n'.green);
return Promise.resolve();
});
}
module.exports = {
build
};