Skip to content
This repository was archived by the owner on Dec 3, 2021. It is now read-only.

Commit b4f600c

Browse files
committed
chore: updates build scripts
1 parent 5d7913e commit b4f600c

File tree

11 files changed

+1157
-488
lines changed

11 files changed

+1157
-488
lines changed

build/.eslintrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"browser": false,
4+
"node": true
5+
},
6+
"parserOptions": {
7+
"sourceType": "script"
8+
},
9+
"extends": "../.eslintrc.json"
10+
}

build/css-classes-migration.js

Lines changed: 0 additions & 160 deletions
This file was deleted.

build/postcss.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use strict'
22

3-
module.exports = (ctx) => ({
3+
module.exports = ctx => ({
44
map: ctx.file.dirname.includes('examples') ? false : {
55
inline: false,
66
annotation: true,
77
sourcesContent: true
88
},
99
plugins: {
10-
autoprefixer: { cascade: false }
10+
autoprefixer: {
11+
cascade: false
12+
},
13+
// 'cssnano': {},
14+
'postcss-combine-duplicated-selectors': {},
15+
'postcss-merge-rules': {}
1116
}
1217
})

build/pug.js

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,120 @@
11
#!/usr/bin/env node
2-
32
'use strict'
43

5-
const args = process.argv.slice(2)
6-
7-
const fs = require('fs')
8-
const path = require('path')
9-
const mkdirp = require('mkdirp')
10-
const pug = require('pug')
11-
const src = './pug/'
12-
const dest = './src/'
13-
const pkg = require(path.resolve(__dirname, '../package.json'))
14-
const beautify = require('js-beautify').html
4+
const fs = require('fs')
5+
const glob = require('glob')
6+
const path = require('path')
7+
const mkdirp = require('mkdirp')
8+
const pug = require('pug')
9+
const svgInjector = require('@coreui/svg-injector')
10+
const vendorsInjector = require('@coreui/vendors-injector')
11+
const beautify = require('js-beautify').html
1512
const jsbOptions = {
1613
indent_size: 2,
1714
indent_inner_html: true,
1815
unformatted: [''],
1916
content_unformatted: ['textarea'],
2017
extra_liners: ['']
2118
}
22-
const version = args[0];
19+
const argv = require('minimist')(process.argv.slice(2), {
20+
boolean: ['injectVendors', 'injectSvg']
21+
})
22+
console.dir(argv)
23+
const src = argv.src
24+
const dest = argv.dest
25+
const injectVendors = argv.injectVendors ? argv.injectVendors : false
26+
const injectSvg = argv.injectSvg ? argv.injectSvg : false
27+
const svgSelectors = 'img.c-icon, img.c-btn-icon, img.c-nav-icon'
2328

24-
const basename = path.basename
25-
const dirname = path.dirname
26-
const resolve = path.resolve
27-
const normalize = path.normalize
28-
const join = path.join
29-
const relative = path.relative
30-
const extension = path.extname
29+
const { basename, dirname, resolve } = path
30+
const extension = path.extname
3131

32-
const walkSync = (dir, filelist = []) => {
33-
fs.readdirSync(dir).forEach(file => {
34-
filelist = fs.statSync(path.join(dir, file)).isDirectory()
35-
? walkSync(path.join(dir, file), filelist)
36-
: filelist.concat(path.join(dir, file))
37-
})
38-
return filelist
32+
// Get all pug files
33+
const getAllFiles = src => {
34+
// const cwd = 'pug/'
35+
const pattern = `${src}/**/*.pug`
36+
const options = {
37+
ignore: `${src}/_*/**`
38+
}
39+
return new glob.sync(pattern, options)
3940
}
4041

41-
const isPug = (filename) => {
42-
return extension(filename) === '.pug' ? true : false
43-
}
42+
const isPug = filename => extension(filename) === '.pug'
4443

4544
const compile = (filename, basedir) => {
46-
const levels = filename.replace(`pug${path.sep}views${path.sep}`, '').replace(`pug${path.sep}pages${path.sep}`, '').split(`${path.sep}`).length
47-
const base = (levels) => {
45+
const levels = basedir.split(`${path.sep}`).filter(el => el !== '' ).length
46+
const base = levels => {
4847
let path = './'
49-
while (levels > 1) {
50-
levels = levels - 1;
51-
path = path + '../'
48+
while (levels > 0) {
49+
levels -= 1
50+
path += '../'
5251
}
52+
5353
return path
5454
}
5555

5656
const fn = pug.compileFile(filename, {
57-
basedir: basedir,
58-
pretty: true,
57+
basedir: './pug/',
58+
pretty: true
5959
})
6060
const html = fn({
6161
base: base(levels)
62-
});
62+
})
6363
return html
6464
}
6565

66-
// Build html files
67-
const compileHtml = () => {
68-
// Build index
69-
if (version === 'ajax') {
70-
const html = compile('./pug/layout/index.pug', './pug/layout/')
71-
fs.writeFile(resolve(dest, 'index.html'), beautify(html, jsbOptions), function(err) {
72-
if(err) {
73-
return console.log(err);
66+
const checkPath = (src, dest, injectVendors, injectSvg) => {
67+
// Check if path is file or directory
68+
if (fs.statSync(src).isDirectory()) {
69+
const files = getAllFiles(src)
70+
files.forEach(file => {
71+
if (isPug(file)) {
72+
compilePugToHtml(resolve(file), dest, injectVendors, injectSvg)
7473
}
75-
console.log('index.html file was saved!');
74+
// TODO: handle errors
7675
})
77-
}
78-
79-
// Build views
80-
const views = walkSync('./pug/views/')
81-
views.forEach((view) => {
82-
if (isPug(view)) {
83-
const html = compile(view, './pug/layout/')
84-
let file
85-
if (version === 'ajax') {
86-
file = view.replace(`pug${path.sep}`, '').replace('.pug', '.html')
87-
} else {
88-
file = view.replace(`pug${path.sep}views${path.sep}`, '').replace('.pug', '.html')
89-
}
90-
// Create tree
91-
mkdirp.sync(resolve(dest, dirname(file)))
92-
// Create HTML file
93-
fs.writeFile(resolve(dest, file), beautify(html, jsbOptions), function(err) {
94-
if(err) {
95-
return console.log(err)
76+
} else if (isPug(src)) {
77+
if (src.includes('_partial') || src.includes('_partial')) {
78+
const files = getAllFiles('pug/')
79+
files.forEach(file => {
80+
if (isPug(file)) {
81+
compilePugToHtml(resolve(file), dest, injectVendors, injectSvg)
9682
}
97-
console.log(file + ' file was saved!')
83+
// TODO: handle errors
9884
})
85+
} else {
86+
compilePugToHtml(resolve(src), dest, injectVendors, injectSvg)
9987
}
100-
})
101-
// Build pages
102-
const pages = walkSync('./pug/pages')
103-
pages.forEach((page) => {
104-
if (isPug(page)) {
105-
const html = compile(page, './pug/layout/')
106-
const file = page.replace(`pug${path.sep}pages${path.sep}`, '').replace('.pug', '.html')
107-
// Create tree
108-
mkdirp.sync(resolve(dest, dirname(file)))
109-
// Create HTML file
110-
fs.writeFile(resolve(dest, file), beautify(html, jsbOptions), function(err) {
111-
if(err) {
112-
return console.log(err)
113-
}
114-
console.log(file + ' file was saved!')
115-
})
88+
}
89+
// TODO: handle errors
90+
}
91+
92+
// Build html files
93+
const compilePugToHtml = (src, dest, injectVendors, injectSvg) => {
94+
const dir = dirname(src).replace('pug', '')
95+
const file = basename(src).replace('.pug', '.html')
96+
const relative = path.relative(resolve(__dirname, '..'), dir)
97+
let html = compile(src, `${relative}`)
98+
99+
// console.log(relative)
100+
// console.log('----------------')
101+
mkdirp.sync(resolve(__dirname, '..', dest, relative))
102+
103+
if (injectVendors === true) {
104+
html = vendorsInjector(html)
105+
}
106+
107+
if (injectSvg === true) {
108+
html = svgInjector(html, svgSelectors, 'src/')
109+
}
110+
111+
fs.writeFile(resolve(__dirname, '..', dest, relative, file), beautify(html, jsbOptions), err => {
112+
if (err) {
113+
throw err
116114
}
115+
116+
console.log(`${resolve(__dirname, '..', dest, relative, file)} file was saved!`)
117117
})
118118
}
119119

120-
compileHtml()
120+
checkPath(src, dest, injectVendors, injectSvg)

0 commit comments

Comments
 (0)