1
1
mirror of https://github.com/primer/css.git synced 2024-12-25 07:03:35 +03:00
css/script/dist

64 lines
2.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
const globby = require('globby')
const cssstats = require('cssstats')
const postcss = require('postcss')
const loadConfig = require('postcss-load-config')
const {remove, mkdirp, readFile, writeFile} = require('fs-extra')
const {dirname, basename, join} = require('path')
const {promisify} = require('util')
const inDir = 'src'
const outDir = 'dist'
const statsDir = join(outDir, 'stats')
const encoding = 'utf8'
remove(outDir)
.then(() => mkdirp(statsDir))
.then(() => globby([`${inDir}/**/index.scss`]))
.then(files => {
return loadConfig().then(({plugins, options}) => {
const processor = postcss(plugins)
const inPattern = new RegExp(`^${inDir}/`)
const names = {
'index.scss': 'primer'
}
const tasks = files.map(from => {
const path = from.replace(inPattern, '')
const name = names[path] || dirname(path).replace(/\//g, '-')
const to = join(outDir, `${name}.css`)
const info = {
name,
source: from,
sass: `@primer/css/${path}`,
css: to,
map: `${to}.map`,
js: join(outDir, `${name}.js`),
stats: join(statsDir, `${name}.json`),
legacy: `primer-${name}/index.scss`
}
return readFile(from)
.then(scss => processor.process(scss, Object.assign({from, to}, options)))
.then(result => Promise.all([
writeFile(to, result.css, encoding),
writeFile(info.stats, JSON.stringify(cssstats(result.css)), encoding),
writeFile(info.js, `module.exports = {cssstats: require('./stats/${name}.json')}`, encoding),
result.map ? writeFile(info.map, result.map, encoding) : null
]))
.then(() => info)
})
return Promise.all(tasks)
})
.then(files => {
const meta = {files}
return writeFile(join(outDir, 'meta.json'), JSON.stringify(meta, null, 2), encoding)
})
})
.catch(error => {
console.error(error)
process.exitCode = 1
})