2018-12-07 10:40:06 +03:00
|
|
|
const Metalsmith = require('metalsmith')
|
|
|
|
const filter = require('metalsmith-filter')
|
|
|
|
const frontmatter = require('metalsmith-matters')
|
|
|
|
const watch = require('metalsmith-watch')
|
2018-12-01 03:17:36 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
const addPackageMeta = require('./add-package-meta')
|
2018-12-14 08:37:23 +03:00
|
|
|
const {extractPackages, writePackagesJSON} = require('./extract-packages-json')
|
2018-12-07 10:40:06 +03:00
|
|
|
const addSource = require('./add-source')
|
|
|
|
const filterBy = require('./filter-by')
|
|
|
|
const parseDocComments = require('./parse-doc-comments')
|
|
|
|
const rename = require('./rename')
|
|
|
|
const writeMeta = require('./write-meta')
|
|
|
|
const gitIgnore = require('./ignore')
|
2018-12-07 00:23:43 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
module.exports = function sync(options = {}) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
const {log = console.warn} = options
|
2018-12-01 03:17:36 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
const metaOptions = options.meta || {namespace: 'data', log}
|
|
|
|
const ns = metaOptions.namespace
|
2018-12-01 03:17:36 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
// this is what we'll resolve our Promise with later
|
|
|
|
let files
|
2018-12-01 03:17:36 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
const metal = Metalsmith(process.cwd())
|
2019-01-29 03:42:40 +03:00
|
|
|
.source('../modules/primer')
|
2018-12-07 10:40:06 +03:00
|
|
|
.destination('pages/css')
|
|
|
|
.clean(false)
|
|
|
|
.frontmatter(false)
|
|
|
|
// ignore anything containing "node_modules" in its path
|
|
|
|
.ignore(path => path.includes('node_modules'))
|
|
|
|
// only match files that look like docs
|
2019-01-29 03:42:40 +03:00
|
|
|
.use(filter(['**/*.md']))
|
2018-12-14 08:37:23 +03:00
|
|
|
.use(extractPackages())
|
2018-12-07 10:40:06 +03:00
|
|
|
// 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))
|
2018-12-14 08:37:23 +03:00
|
|
|
.use(writePackagesJSON({path: 'packages.json'}))
|
2018-12-07 10:40:06 +03:00
|
|
|
// write the source frontmatter key to the relative source path
|
|
|
|
.use(
|
|
|
|
addSource({
|
|
|
|
branch: 'master',
|
|
|
|
repo: 'primer/primer',
|
|
|
|
log
|
|
|
|
})
|
|
|
|
)
|
|
|
|
// copy a subset of fields from the nearest package.json
|
|
|
|
.use(
|
|
|
|
addPackageMeta({
|
|
|
|
fields: ['name', 'description', 'version'],
|
|
|
|
namespace: ns
|
|
|
|
})
|
|
|
|
)
|
|
|
|
// rename files with their "path" frontmatter key
|
2018-12-14 08:37:23 +03:00
|
|
|
.use(rename(file => file[ns] ? `${file[ns].path}.md` : true), {log})
|
2018-12-07 10:40:06 +03:00
|
|
|
.use((_files, metal, done) => {
|
|
|
|
files = _files
|
|
|
|
done()
|
|
|
|
})
|
2018-12-07 22:29:26 +03:00
|
|
|
// write frontmatter back out to the file
|
|
|
|
.use(writeMeta(metaOptions))
|
2018-12-07 10:40:06 +03:00
|
|
|
// keep .gitignore up-to-date with the list of generated files
|
|
|
|
.use(
|
|
|
|
gitIgnore({
|
|
|
|
header: '# DO NOT EDIT: automatically generated by ignore.js'
|
|
|
|
})
|
|
|
|
)
|
2018-12-01 03:17:36 +03:00
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
if (options.watch) {
|
|
|
|
metal.use(watch(typeof options.watch === 'object' ? options.watch : {}))
|
2018-12-01 03:17:36 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 10:40:06 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
metal.build(error => {
|
|
|
|
error ? reject(error) : resolve(files)
|
|
|
|
})
|
2018-12-07 01:00:24 +03:00
|
|
|
})
|
2018-12-01 03:17:36 +03:00
|
|
|
}
|