1
1
mirror of https://github.com/primer/css.git synced 2024-11-11 15:16:03 +03:00

extract package metadata from */package.json

This commit is contained in:
Shawn Allen 2018-12-14 00:37:23 -05:00
parent 5b5dc4b361
commit c5a6cb2113
5 changed files with 58 additions and 7 deletions

View File

@ -12,7 +12,7 @@ module.exports = function addPackageMeta(options = {}) {
if (pkg) {
file[namespace].package = fields ? pluck(pkg, fields) : pkg
} else {
log('no package.json found relative to', fullPath)
log('no package.json found relative to', path)
}
}
done()

View File

@ -9,6 +9,8 @@ module.exports = function addSource(options = {}) {
}
const {branch, repo} = options
return each((file, source) => {
file[namespace].source = `https://github.com/${repo}/tree/${branch}/modules/${source}`
if (file[namespace]) {
file[namespace].source = `https://github.com/${repo}/tree/${branch}/modules/${source}`
}
})
}

View File

@ -0,0 +1,44 @@
module.exports = {extractPackages, writePackagesJSON}
function extractPackages(options = {}) {
return (files, metal, done) => {
const packages = {}
for (const key of Object.keys(files)) {
if (key.endsWith('package.json')) {
const file = files[key]
const pkg = JSON.parse(String(file.contents))
const plucked = pluck(pkg, [
'name',
'description',
'version'
])
plucked.dependencies = pkg.dependencies
? Object.keys(pkg.dependencies)
: []
packages[pkg.name] = plucked
}
}
const meta = metal.metadata()
Object.assign(meta, {packages})
done()
}
}
function writePackagesJSON(options = {}) {
const {path = 'packages.json'} = options
return (files, metal, done) => {
const {packages} = metal.metadata()
files[path] = {
contents: JSON.stringify(packages, null, 2)
}
done()
}
}
function pluck(obj, keys) {
const plucked = {}
for (const key of keys) {
plucked[key] = obj[key]
}
return plucked
}

View File

@ -2,13 +2,15 @@ module.exports = function rename(fn, options = {}) {
const {log = noop} = options
return (files, metal, done) => {
for (const [key, file] of Object.entries(files)) {
const dest = fn(file, key, files, metal)
if (dest && dest !== key) {
let dest = fn(file, key, files, metal)
if (dest === true) {
log(`[rename] keep: ${key}`)
} else if (dest && dest !== key) {
log(`[rename] ${key} -> ${dest}`)
file.path = dest
files[dest] = file
delete files[key]
} else if (!dest) {
} else if (dest === false) {
log(`[rename] delete ${key}`)
delete files[key]
}

View File

@ -5,6 +5,7 @@ const watch = require('metalsmith-watch')
const addChangelog = require('./changelog')
const addPackageMeta = require('./add-package-meta')
const {extractPackages, writePackagesJSON} = require('./extract-packages-json')
const addSource = require('./add-source')
const filterBy = require('./filter-by')
const parseDocComments = require('./parse-doc-comments')
@ -30,13 +31,15 @@ module.exports = function sync(options = {}) {
// ignore anything containing "node_modules" in its path
.ignore(path => path.includes('node_modules'))
// only match files that look like docs
.use(filter(['**/README.md', '**/docs/*.md']))
.use(filter(['*/README.md', '*/docs/*.md', '*/package.json']))
.use(extractPackages())
// convert <!-- %docs -->...<!-- %enddocs --> blocks into frontmatter
.use(parseDocComments({log}))
// parse frontmatter into "data" key of each file
.use(frontmatter(metaOptions))
// only match files that have a "path" key in their frontmatter
.use(filterBy(file => file[ns].path))
.use(writePackagesJSON({path: 'packages.json'}))
// write the source frontmatter key to the relative source path
.use(
addSource({
@ -53,7 +56,7 @@ module.exports = function sync(options = {}) {
})
)
// rename files with their "path" frontmatter key
.use(rename(file => `${file[ns].path}.md`), {log})
.use(rename(file => file[ns] ? `${file[ns].path}.md` : true), {log})
.use(addChangelog('../CHANGELOG.md', 'whats-new/changelog.md', file => {
file[ns] = {
title: 'Changelog'