Skip to content

Commit

Permalink
Merge pull request #952 from primer/mkt/asyncify
Browse files Browse the repository at this point in the history
Async/awaitify script/dist.js
  • Loading branch information
Michelle Tilley authored Oct 22, 2019
2 parents 23e21a7 + 0daf23f commit 7194e2d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
version: 11
node-version: 11
- name: install
run: npm install
- name: lint
Expand Down
95 changes: 47 additions & 48 deletions script/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +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(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 = []
Expand Down Expand Up @@ -102,3 +97,7 @@ function writeDeprecationData() {
}
return writeFile(join(outDir, 'deprecations.json'), JSON.stringify(data, null, 2))
}

if (require.main === module) {
dist()
}

0 comments on commit 7194e2d

Please sign in to comment.