1
1
mirror of https://github.com/primer/css.git synced 2024-11-29 14:14:26 +03:00

Async/awaitify script/dist.js

This commit is contained in:
Michelle Tilley 2019-10-21 13:24:26 -07:00 committed by simurai
parent 769729d438
commit 851a70a468

View File

@ -21,61 +21,55 @@ const bundleNames = {
'index.scss': 'primer'
}
remove(outDir)
.then(() => mkdirp(statsDir))
.then(() => globby([`${inDir}/**/index.scss`]))
.then(files => {
return loadConfig()
.then(({plugins, options}) => {
const processor = postcss(plugins)
const bundles = {}
async function dist() {
try {
const bundles = {}
const {plugins, options} = await loadConfig()
const processor = postcss(plugins)
const inPattern = new RegExp(`^${inDir}/`)
const tasks = files.map(from => {
const path = from.replace(inPattern, '')
const name = bundleNames[path] || getPathName(dirname(path))
await remove(outDir)
await mkdirp(statsDir)
const files = await globby([`${inDir}/**/index.scss`])
const to = join(outDir, `${name}.css`)
const meta = {
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`
}
const inPattern = new RegExp(`^${inDir}/`)
const tasks = files.map(async from => {
const path = from.replace(inPattern, '')
const name = bundleNames[path] || getPathName(dirname(path))
return readFile(from, encoding)
.then(scss => {
meta.imports = getExternalImports(scss, path).map(getPathName)
return processor.process(scss, Object.assign({from, to}, options))
})
.then(result =>
Promise.all([
writeFile(to, result.css, encoding),
writeFile(meta.stats, JSON.stringify(cssstats(result.css)), encoding),
writeFile(meta.js, `module.exports = {cssstats: require('./stats/${name}.json')}`, encoding),
result.map ? writeFile(meta.map, result.map, encoding) : null
])
)
.then(() => (bundles[name] = meta))
})
const to = join(outDir, `${name}.css`)
const meta = {
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 Promise.all(tasks).then(() => bundles)
})
.then(bundles => {
const meta = {bundles}
return writeFile(join(outDir, 'meta.json'), JSON.stringify(meta, null, 2), encoding)
})
.then(writeVariableData)
.then(writeDeprecationData)
})
.catch(error => {
const scss = await readFile(from, encoding)
meta.imports = getExternalImports(scss, path).map(getPathName)
const result = await processor.process(scss, Object.assign({from, to}, options))
await Promise.all([
writeFile(to, result.css, encoding),
writeFile(meta.stats, JSON.stringify(cssstats(result.css)), encoding),
writeFile(meta.js, `module.exports = {cssstats: require('./stats/${name}.json')}`, encoding),
result.map ? writeFile(meta.map, result.map, encoding) : null
])
bundles[name] = meta
})
await Promise.all(tasks)
const meta = {bundles}
await writeFile(join(outDir, 'meta.json'), JSON.stringify(meta, null, 2), encoding)
await writeDeprecationData()
} catch (error) {
console.error(error)
process.exitCode = 1
})
}
}
function getExternalImports(scss, relativeTo) {
const imports = []
@ -104,9 +98,6 @@ function writeDeprecationData() {
return writeFile(join(outDir, 'deprecations.json'), JSON.stringify(data, null, 2))
}
function writeVariableData() {
const analyzeVariables = require('./analyze-variables')
return analyzeVariables('src/support/index.scss').then(data =>
writeFile(join(outDir, 'variables.json'), JSON.stringify(data, null, 2))
)
if (require.main === module) {
dist()
}