-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_CreamCSS.js
executable file
·129 lines (112 loc) · 3.15 KB
/
build_CreamCSS.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
// this script can be run directly if you have node installed
// but it can also be run as `node build_CreamCSS.js`
const srcDir = 'src/';
const dstDir = 'dist/';
const docsDir = 'docs/';
const srcFiles = [
srcDir + 'modern-normalize.css',
srcDir + 'cream.css',
srcDir + 'cream-extra.css'
];
// Note: if you modify the order of these elements you must modify the writeResult function !
const outFiles = [
'cream.min.css',
'cream-mn.css',
'cream-all.min.css',
'cream-all-mn.css'
];
const fs = require('node:fs/promises');
const CleanCSS = require('clean-css');
async function getVersion() {
try {
const jsonString = await fs.readFile('./package.json', { encoding: 'utf8' });
const jsonData = JSON.parse(jsonString);
return Promise.resolve(jsonData.version);
}
catch (err) {
console.error("Failed to parse version file");
console.error(err);
return Promise.reject();
}
}
function minifyCSS() {
return new CleanCSS(
{
returnPromise: true,
batch: true
}
).minify(srcFiles);
}
function displayMinimizeInfo(mFiles) {
srcFiles.forEach((sourceFile) => {
console.log(sourceFile);
console.log(mFiles[sourceFile].stats);
if (mFiles[sourceFile].errors.length > 0) {
console.error(mFiles[sourceFile].errors);
}
if (mFiles[sourceFile].warnings.length > 0) {
console.warn(mFiles[sourceFile].warnings);
}
});
}
function addCurrentVersionNumber(mFiles, currentVersion) {
srcFiles.forEach((sourceFile) => {
mFiles[sourceFile].styles = mFiles[sourceFile].styles.replace(
'{{ version }}',
currentVersion
);
});
}
async function writeResult(minimizedFiles, idx) {
if (idx < 0 || idx > outFiles.length) {
console.error('writeResult: idx value is wrong');
return;
}
const fd = await fs.open(dstDir + outFiles[idx], 'w');
switch (idx) {
case 0:
await fd.write(minimizedFiles[srcFiles[1]].styles);
break;
case 1:
await fd.write(minimizedFiles[srcFiles[0]].styles);
await fd.write('\n');
await fd.write(minimizedFiles[srcFiles[1]].styles);
break;
case 2:
await fd.write(minimizedFiles[srcFiles[1]].styles);
await fd.write('\n');
await fd.write(minimizedFiles[srcFiles[2]].styles);
break;
case 3:
await fd.write(minimizedFiles[srcFiles[0]].styles);
await fd.write('\n');
await fd.write(minimizedFiles[srcFiles[1]].styles);
await fd.write('\n');
await fd.write(minimizedFiles[srcFiles[2]].styles);
break;
}
await fd.close();
}
async function build() {
try {
let appVersion = await getVersion();
console.log(`App version: ${appVersion}`);
let minimizedFiles = await minifyCSS();
displayMinimizeInfo(minimizedFiles);
// replace version in file
addCurrentVersionNumber(minimizedFiles, appVersion);
// write destination files
for (let i = 0; i < outFiles.length; ++i) {
await writeResult(minimizedFiles, i);
}
await fs.copyFile(dstDir + 'cream-all-mn.css', docsDir + 'cream-all-mn.css');
}
catch (err) {
console.log('\nBuild error !');
if (err) {
console.log(err);
}
}
}
build();