mirror of
https://github.com/primer/css.git
synced 2024-12-18 03:31:43 +03:00
32 lines
783 B
JavaScript
32 lines
783 B
JavaScript
|
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)
|
||
|
}
|