Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async/awaitify script/dist.js #952

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}