-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
283 lines (260 loc) · 9.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Meshki v3.0.0 (https://borderliner.github.io/Meshki/)
* Copyright 2016-2024 Mohammadreza Hajianpour <hajianpour.mr@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Disable no-console error, since this file runs on Node.js platform
/* eslint no-console: off */
/* eslint no-unused-expressions: off */
import fs from 'fs'
import fsExtra from 'fs-extra'
import path from 'path'
import chalk from 'chalk'
import * as sass from 'sass'
import uglifyJs from 'uglify-js'
import watch from 'node-watch'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const isWatch = process.argv[2]
const options = {
main_scss: 'src/scss/main.scss',
main_js: 'src/js/meshki.js',
main_fonts: 'src/fonts/',
output_dir: 'dist/',
output_css: 'dist/css/meshki.css',
output_css_min: 'dist/css/meshki.min.css',
output_js: 'dist/js/meshki.js',
output_js_min: 'dist/js/meshki.min.js',
output_fonts: 'dist/fonts/',
plugins_dir: 'src/scss/plugins',
source_map: true,
licensed: [
'dist/js/meshki.min.js',
'dist/css/meshki.min.css',
'dist/plugins/meshki-extra-button-colors.min.css',
'dist/plugins/meshki-rtl.min.css',
'dist/plugins/meshki-light-mode.css'
]
}
function listDirectory (srcpath) {
return fs.readdirSync(srcpath)
.filter((file) => fs.lstatSync(path.join(srcpath, file)).isDirectory())
}
function removeDirectory (srcpath) {
if (fs.existsSync(srcpath)) {
fs.readdirSync(srcpath).forEach((file) => {
const currentPath = `${srcpath}/${file}`
if (fs.lstatSync(currentPath).isDirectory()) {
removeDirectory(currentPath)
} else {
fs.unlinkSync(currentPath)
}
})
fs.rmdirSync(srcpath)
}
}
function createDist (log) {
if (!fs.existsSync('dist/')) {
log ? console.log(chalk.yellow('No ') + chalk.blue.bold('"dist"') + chalk.yellow(' folder exists. Creating folders...')) : undefined
fs.mkdirSync('dist/')
fs.mkdirSync('dist/plugins/')
// fs.mkdirSync('dist/fonts/')
fs.mkdirSync('dist/css/')
fs.mkdirSync('dist/js/')
log ? console.log(chalk.green('=> ') + chalk.blue('"dist"') + chalk.green(' folder was created successfully!')) : undefined
} else {
log ? console.log(chalk.magenta('Cleaning previously compiled files...')) : undefined
removeDirectory('dist/')
createDist()
}
}
const plugins = listDirectory(options.plugins_dir)
function sassifyMeshki (log) {
log ? console.log(chalk.yellow('Compiling ') + chalk.blue.bold('meshki.scss') + chalk.yellow('...')) : undefined
const result = sass.renderSync({
file: options.main_scss,
outFile: options.output_css
})
// Write CSS file
try {
fs.writeFileSync(options.output_css, result.css)
log ? console.log(chalk.green('=> Successfully compiled ') + chalk.blue.bold('meshki.scss')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
}
}
function minifyMeshki (log) {
log ? console.log(chalk.yellow('Minifying ') + chalk.blue.bold('meshki.css') + chalk.yellow('...')) : undefined
const result = sass.renderSync({
file: options.main_scss,
outputStyle: 'compressed',
outFile: options.output_css_min,
sourceMap: options.source_map
})
// Write Minified CSS file
try {
fs.writeFileSync(options.output_css_min, result.css)
log ? console.log(chalk.green('=> Successfully minified ') + chalk.blue.bold('meshki.css')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
}
if (options.source_map) {
// Write SourceMaps
try {
fs.writeFileSync(`${options.output_css}.map`, result.map)
log ? console.log(chalk.green('=> Successfully generated source maps for ') + chalk.blue.bold('meshki.min.css')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
}
}
}
function sassifyPlugins (log) {
log ? console.log(chalk.magenta('Starting to compile plugins...')) : undefined
plugins.forEach((name) => {
log ? console.log(chalk.yellow('Compiling ') + chalk.blue.bold(`${name}`)) : undefined
const result = sass.renderSync({
file: `${options.plugins_dir}/${name}/main.scss`,
outFile: `${options.output_dir}/plugins/`
})
// Write CSS file
try {
fs.writeFileSync(`${options.output_dir}plugins/meshki-${name}.css`, result.css)
log ? console.log(chalk.green('=> Successfully compiled ') + chalk.blue.bold(name)) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
})
}
function minifyPlugins (log) {
log ? console.log(chalk.magenta('Starting to minify plugins...')) : undefined
plugins.forEach((name) => {
log ? console.log(chalk.yellow('Minifying ') + chalk.blue.bold(`${name}`)) : undefined
const result = sass.renderSync({
file: `${options.plugins_dir}/${name}/main.scss`,
outFile: `${options.output_dir}/plugins/`,
outputStyle: 'compressed',
sourceMap: options.source_map
})
// Write CSS file
try {
fs.writeFileSync(`${options.output_dir}plugins/meshki-${name}.min.css`, result.css)
log ? console.log(chalk.green('=> Successfully minified ') + chalk.blue.bold(name)) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
if (options.source_map) {
// Write source map file
try {
fs.writeFileSync(`${options.output_dir}plugins/meshki-${name}.map`, result.map)
log ? console.log(chalk.green('=> Successfully generated source map for ') + chalk.blue.bold(name)) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
}
})
}
function uglifyJavaScript (log) {
let code = ''
let uglified = {}
try {
code = fs.readFileSync(options.main_js, 'utf-8')
uglified = uglifyJs.minify(code, {
warnings: true
})
if (uglified.warnings) console.log(uglified.warnings)
} catch (error) {
console.log(chalk.red('Could not read the input file from the disk. Check if you have read permissions.'))
console.log(error)
}
try {
fs.writeFileSync(options.output_js_min, uglified.code)
log ? console.log(chalk.green('=> Successfully uglified ') + chalk.blue.bold('meshki.js')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
}
function copyJavaScript (log) {
log ? console.log(chalk.yellow('Copying ') + chalk.blue.bold('meshki.js') + chalk.yellow(' to dist/js')) : undefined
try {
fsExtra.copySync(path.resolve(__dirname, options.main_js), options.output_js)
log ? console.log(chalk.green('=> Successfully copied ') + chalk.blue.bold('meshki.js') + chalk.green(' to dist/js')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
}
function copyFonts (log) {
log ? console.log(chalk.yellow('Copying ') + chalk.blue.bold('fonts') + chalk.yellow('to dist/fonts')) : undefined
fs.readdirSync(options.main_fonts).forEach((file) => {
try {
fsExtra.copySync(path.join(__dirname, options.main_fonts, file), path.join(options.output_fonts, file))
log ? console.log(chalk.green('=> Successfully copied ') + chalk.blue.bold(file) + chalk.green(' to dist/fonts')) : undefined
} catch (error) {
console.log(chalk.red('Could not write the output to the disk. Check if you have write permissions.'))
console.log(error)
}
})
}
function copyLicense (log) {
let copying = ''
try {
copying = fs.readFileSync('COPYING', 'utf-8')
if (process.platform === 'darwin' || process.platform === 'linux') {
log ? console.log(chalk.cyan.bold('✅ Meshki was compiled succesfully! Rock on! 🤘')) : undefined
} else {
log ? console.log(chalk.cyan.bold('--Meshki was compiled succesfully! Rock on!')) : undefined
}
} catch (error) {
console.log(error)
}
options.licensed.forEach((file) => {
try {
const fileContent = fs.readFileSync(file, 'utf-8')
fs.writeFileSync(file, `${copying}\n${fileContent}`)
} catch (error) {
console.log(error)
}
})
}
function compileAll (log = true) {
createDist(log)
sassifyMeshki(log)
minifyMeshki(log)
sassifyPlugins(log)
minifyPlugins(log)
uglifyJavaScript(log)
log ? console.log(chalk.magenta('Copying assets...')) : undefined
copyJavaScript(log)
// copyFonts(log)
copyLicense(log)
return true
}
// If --watch has been passed as an argument
if ((isWatch !== undefined) && (isWatch === '--watch')) {
console.log(chalk.greenBright.italic('Compiling project...'))
compileAll()
console.log(chalk.magenta.italic('Watching "src" folder...'))
watch('src', { recursive: true }, (evt, name) => {
console.log('File ' + chalk.magenta('%s') + ' changed.', name)
compileAll(false) ? console.log(chalk.green('Re-compiled Meshki successfully!')) : undefined
console.log(chalk.magenta.italic('Watching "src" folder...'))
})
} else {
compileAll(true)
}