1
1
mirror of https://github.com/primer/css.git synced 2024-12-18 03:31:43 +03:00
css/docs/lib/add-package-meta.js

32 lines
783 B
JavaScript
Raw Normal View History

2018-10-20 02:02:39 +03:00
const each = require('./each')
const {existsSync} = require('fs')
const {dirname, join, resolve} = require('path')
const cache = {}
module.exports = function addPackageMeta(options) {
return (files, metal, done) => {
const root = metal.source()
for (const [path, file] of Object.entries(files)) {
file.package = getPackageRelativeTo(path, root)
}
done()
}
}
function getPackageRelativeTo(path, root) {
const pathDir = join(root, dirname(path))
if (pathDir in cache) {
return cache[pathDir]
}
let dir = pathDir
while (dir !== root) {
const pkgPath = join(dir, 'package.json')
if (existsSync(pkgPath)) {
return cache[dir] = require(pkgPath)
}
dir = resolve(dir, '..')
}
console.warn('no package.json found:', dir)
}