2018-01-14 02:41:15 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const sass = require('node-sass')
|
|
|
|
const chokidar = require('chokidar')
|
|
|
|
const path = require('path')
|
|
|
|
const debounce = require('lodash/debounce')
|
|
|
|
const fs = require('fs')
|
|
|
|
const pify = require('pify')
|
|
|
|
const writeFile = pify(fs.writeFile.bind(fs))
|
|
|
|
const readdir = pify(fs.readdir.bind(fs))
|
2018-01-14 23:29:32 +01:00
|
|
|
const readFile = pify(fs.readFile.bind(fs))
|
2018-01-14 02:41:15 +01:00
|
|
|
const render = pify(sass.render.bind(sass))
|
2018-01-14 03:59:49 +01:00
|
|
|
const now = require('performance-now')
|
2018-01-14 02:41:15 +01:00
|
|
|
|
|
|
|
const globalScss = path.join(__dirname, '../scss/global.scss')
|
|
|
|
const defaultThemeScss = path.join(__dirname, '../scss/themes/_default.scss')
|
2018-01-19 05:57:15 +01:00
|
|
|
const offlineThemeScss = path.join(__dirname, '../scss/themes/_offline.scss')
|
2018-11-28 04:17:18 +01:00
|
|
|
const customScrollbarScss = path.join(__dirname, '../scss/custom-scrollbars.scss')
|
2018-01-14 23:29:32 +01:00
|
|
|
const html2xxFile = path.join(__dirname, '../templates/2xx.html')
|
2018-01-14 02:41:15 +01:00
|
|
|
const scssDir = path.join(__dirname, '../scss')
|
|
|
|
const themesScssDir = path.join(__dirname, '../scss/themes')
|
|
|
|
const assetsDir = path.join(__dirname, '../assets')
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
function doWatch () {
|
2018-04-11 05:12:41 +02:00
|
|
|
let start = now()
|
2018-01-14 02:41:15 +01:00
|
|
|
chokidar.watch(scssDir).on('change', debounce(() => {
|
2018-01-14 03:59:49 +01:00
|
|
|
console.log('Recompiling SCSS...')
|
|
|
|
Promise.all([
|
|
|
|
compileGlobalSass(),
|
|
|
|
compileThemesSass()
|
|
|
|
]).then(() => {
|
|
|
|
console.log('Recompiled SCSS in ' + (now() - start) + 'ms')
|
|
|
|
})
|
2018-01-14 02:41:15 +01:00
|
|
|
}, 500))
|
|
|
|
chokidar.watch()
|
|
|
|
}
|
|
|
|
|
2018-11-24 09:41:36 +01:00
|
|
|
async function renderCss (file) {
|
|
|
|
return (await render({ file, outputStyle: 'compressed' })).css
|
|
|
|
}
|
2018-01-14 02:41:15 +01:00
|
|
|
|
2018-11-24 09:41:36 +01:00
|
|
|
async function compileGlobalSass () {
|
|
|
|
let mainStyle = (await Promise.all([defaultThemeScss, globalScss].map(renderCss))).join('')
|
|
|
|
let offlineStyle = (await renderCss(offlineThemeScss))
|
2018-11-28 04:17:18 +01:00
|
|
|
let scrollbarStyle = (await renderCss(customScrollbarScss))
|
2018-01-14 02:41:15 +01:00
|
|
|
|
2018-01-14 23:29:32 +01:00
|
|
|
let html = await readFile(html2xxFile, 'utf8')
|
2018-11-24 09:41:36 +01:00
|
|
|
html = html.replace(/<!-- begin inline CSS -->[\s\S]+<!-- end inline CSS -->/,
|
|
|
|
`<!-- begin inline CSS -->\n` +
|
|
|
|
`<style>\n${mainStyle}</style>\n` +
|
|
|
|
`<style media="only x" id="theOfflineStyle">\n${offlineStyle}</style>\n` +
|
2018-11-28 04:17:18 +01:00
|
|
|
`<style media="all" id="theScrollbarStyle">\n${scrollbarStyle}</style>\n` +
|
2018-11-24 09:41:36 +01:00
|
|
|
`<!-- end inline CSS -->`
|
|
|
|
)
|
2018-01-14 23:29:32 +01:00
|
|
|
|
|
|
|
await writeFile(html2xxFile, html, 'utf8')
|
2018-01-14 02:41:15 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function compileThemesSass () {
|
2018-01-14 02:41:15 +01:00
|
|
|
let files = (await readdir(themesScssDir)).filter(file => !path.basename(file).startsWith('_'))
|
|
|
|
await Promise.all(files.map(async file => {
|
2018-08-30 06:42:57 +02:00
|
|
|
let res = await render({ file: path.join(themesScssDir, file), outputStyle: 'compressed' })
|
2018-01-14 03:59:49 +01:00
|
|
|
let outputFilename = 'theme-' + path.basename(file).replace(/\.scss$/, '.css')
|
|
|
|
await writeFile(path.join(assetsDir, outputFilename), res.css, 'utf8')
|
2018-01-14 02:41:15 +01:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function main () {
|
2018-01-14 02:41:15 +01:00
|
|
|
await Promise.all([compileGlobalSass(), compileThemesSass()])
|
2018-09-23 16:57:07 +02:00
|
|
|
if (process.argv.includes('--watch')) {
|
2018-01-14 02:41:15 +01:00
|
|
|
doWatch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.resolve().then(main).catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
2018-02-09 07:29:29 +01:00
|
|
|
})
|