UNPKG

quasar-json-api

Version:

Normalizes and validates JSON API for 3rd-party Quasar Components, Directives, Mixins and Plugins using the Quasar UI Kit

163 lines (135 loc) 3.66 kB
const fs = require('fs') const path = require('path') const zlib = require('zlib') const { green, blue, red, magenta, grey, underline } = require('chalk') const kebabRegex = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g const tableData = [] const { version, name } = require('../package.json') process.on('exit', code => { if (code === 0 && tableData.length > 0) { const { table } = require('table') tableData.sort((a, b) => { return a[ 0 ] === b[ 0 ] ? a[ 1 ] < b[ 1 ] ? -1 : 1 : a[ 0 ] < b[ 0 ] ? -1 : 1 }) tableData.unshift([ underline('Ext'), underline('Filename'), underline('Size'), underline('Gzipped') ]) const output = table(tableData, { columns: { 0: { alignment: 'right' }, 1: { alignment: 'left' }, 2: { alignment: 'right' }, 3: { alignment: 'right' } } }) console.log() console.log(` Summary of ${name} v${version}:`) console.log(output) } }) function getSize (code) { return (code.length / 1024).toFixed(2) + 'kb' } module.exports.createFolder = function (folder) { const dir = path.join(__dirname, '..', folder) if (!fs.existsSync(dir)) { fs.mkdirSync(dir) } } function getDestinationInfo (dest) { if (dest.endsWith('.json')) { return { banner: grey('[json]'), tableEntryType: grey('json'), toTable: true } } if (dest.endsWith('.js')) { return { banner: green('[js] '), tableEntryType: green('js'), toTable: dest.indexOf('dist/') > -1 } } if (dest.endsWith('.css') || dest.endsWith('.styl') || dest.endsWith('.sass')) { return { banner: blue('[css] '), tableEntryType: blue('css'), toTable: true } } if (dest.endsWith('.ts')) { return { banner: magenta('[ts] '), tableEntryType: magenta('ts'), toTable: true } } logError(`Unknown file type using buildUtils.writeFile: ${ dest }`) process.exit(1) } module.exports.writeFile = function (dest, code, zip) { const { banner, tableEntryType, toTable } = getDestinationInfo(dest) const fileSize = getSize(code) const filePath = path.relative(process.cwd(), dest) return new Promise((resolve, reject) => { function report (gzippedString, gzippedSize) { console.log(`${ banner } ${ filePath.padEnd(49) } ${ fileSize.padStart(8) }${ gzippedString || '' }`) if (toTable) { tableData.push([ tableEntryType, filePath, fileSize, gzippedSize || '-' ]) } resolve(code) } fs.writeFile(dest, code, err => { if (err) return reject(err) if (zip) { zlib.gzip(code, (err, zipped) => { if (err) return reject(err) const size = getSize(zipped) report(` (gzipped: ${ size.padStart(8) })`, size) }) } else { report() } }) }) } module.exports.readFile = function (file) { return fs.readFileSync(file, 'utf-8') } function logError (err) { console.error('\n' + red('[Error]'), err) console.log() } module.exports.logError = logError function uppercase (string) { return string[0].toUpperCase() + string.substring(1) } module.exports.uppercase = uppercase module.exports.rollupUMD = function (config = {}) { return { name: `${name}-umd`, transform (code) { return { code: uppercase(name) + `.${ config.type }.set(${ code.replace('export default ', '') })` } } } } module.exports.kebabCase = function (str) { return str.replace( kebabRegex, match => '-' + match.toLowerCase() ).substring(1) }