1
1
mirror of https://github.com/primer/css.git synced 2024-12-24 06:32:00 +03:00

docs: rework package metadata helper to set "bundle" metadata

This commit is contained in:
Shawn Allen 2019-02-12 11:20:28 -08:00
parent 9fac57a3a6
commit e8dd915ea6

View File

@ -8,37 +8,29 @@ module.exports = function addPackageMeta(options = {}) {
return (files, metal, done) => {
const root = metal.source()
for (const [path, file] of Object.entries(files)) {
const pkg = getPackageRelativeTo(path, root)
if (pkg) {
file[namespace].package = fields ? pluck(pkg, fields) : pkg
} else {
log('no package.json found relative to', path)
}
file[namespace].bundle = getBundleRelativeTo(path, root)
}
done()
}
}
function getPackageRelativeTo(file, root) {
function getBundleRelativeTo(file, root) {
let dir = join(root, dirname(file))
if (dir in cache) {
return cache[dir]
}
while (dir !== root) {
const pkgPath = join(dir, 'package.json')
if (existsSync(pkgPath)) {
return (cache[dir] = require(pkgPath))
const indexPath = join(dir, 'index.scss')
if (existsSync(indexPath)) {
return (cache[dir] = getPathName(indexPath.substr(root.length + 1)))
}
dir = resolve(dir, '..')
}
return false
}
function pluck(data, fields) {
return fields.reduce((out, field) => {
out[field] = data[field]
return out
}, {})
function getPathName(path) {
return dirname(path).replace(/\//g, '-')
}
function noop() {}