1
1
mirror of https://github.com/primer/css.git synced 2024-11-27 09:45:45 +03:00

move copy.js -> script/copy, add watch()

This commit is contained in:
Shawn Allen 2018-12-06 13:03:41 -08:00
parent 0479a6c329
commit 29ef33801a
3 changed files with 87 additions and 56 deletions

View File

@ -1,54 +0,0 @@
#!/usr/bin/env node
if (process.env.NOW_URL) {
console.warn('copy.js should not run in Now!')
return
}
const {green, red, yellow} = require('colorette')
const {basename, join} = require('path')
const {removeSync} = require('fs-extra')
const {getLinks, sync} = require('./lib/sync')
const {getIgnored, setIgnored} = require('./lib/ignore')
const sourceDir = join(__dirname, '../modules')
const destDir = join(__dirname, 'pages/css')
const ignoreFile = join(destDir, '.gitignore')
const ignored = getIgnored(ignoreFile)
for (const file of ignored) {
console.warn(`${yellow('x')} removing: ${file}`)
removeSync(file)
}
const links = getLinks(sourceDir, destDir, {
'../CHANGELOG.md': 'whats-new/changelog.md',
'primer/README.md': false, // 'packages/primer.md',
'primer-base/README.md': false, // 'support/base.md',
'primer-core/README.md': false, // 'packages/primer-core.md',
'primer-layout/README.md': 'objects/layout.md',
'primer-layout/docs/*.md': path => `objects/${basename(path)}`,
'primer-marketing-support/README.md': 'support/marketing-variables.md',
'primer-marketing-type/README.md': 'utilities/marketing-type.md',
'primer-marketing-utilities/README.md': 'utilities/marketing.md',
'primer-marketing-utilities/docs/*.md': path => `utilities/marketing-${basename(path)}`,
'primer-marketing/README.md': false, // 'packages/primer-marketing.md',
'primer-product/README.md': false, // 'packages/primer-product.md',
'primer-support/README.md': false, // 'support/index.md',
'primer-support/docs/*.md': path => `support/${basename(path)}`,
'primer-table-object/README.md': 'objects/table-object.md',
'primer-utilities/README.md': false, // 'utilities/index.md',
'primer-utilities/docs/*.md': path => `utilities/${basename(path)}`,
// this is a catch-all rule that needs to go last so that it doesn't override others
'primer-*/README.md': path => `components/${shortName(path)}.md`,
})
console.warn(yellow(`linking ${links.length} files...`))
sync(links)
const toBeIgnored = links.map(link => link.dest.substr(destDir.length + 1))
console.warn(yellow(`adding ${toBeIgnored.length} files to ${ignoreFile}...`))
setIgnored(ignoreFile, toBeIgnored)
console.warn(green('done!'))
function shortName(path) {
return path.match(/primer-([-\w]+)/)[1]
}

View File

@ -1,12 +1,49 @@
const chokidar = require('chokidar')
const klaw = require('klaw-sync')
const minimatch = require('minimatch')
const {green, red, yellow} = require('colorette')
const {basename, dirname, join} = require('path')
const {copySync, ensureDirSync, removeSync, writeFileSync} = require('fs-extra')
const {getIgnored, setIgnored} = require('./ignore')
module.exports = {getLinks, sync}
module.exports = {sync, watch}
function sync(links) {
function sync({sourceDir, destDir, map, debug = false}) {
const log = debug ? console.warn : noop
const ignoreFile = join(destDir, '.gitignore')
const ignored = getIgnored(ignoreFile)
for (const file of ignored) {
log(`${yellow('x')} removing: ${file}`)
removeSync(file)
}
const links = getLinks(sourceDir, destDir, map)
log(yellow(`linking ${links.length} files...`))
syncLinks(links)
const toBeIgnored = links.map(link => link.dest.substr(destDir.length + 1))
log(yellow(`adding ${toBeIgnored.length} files to ${ignoreFile}...`))
setIgnored(ignoreFile, toBeIgnored)
log(green('done!'))
}
function watch(options) {
const globs = Object.keys(map).map(path => join(sourceDir, path))
let timeout
const update = path => {
if (timeout) return
timeout = setTimeout(() => {
copySync(options)
clearTimeout(timeout)
timeout = null
}, 50)
}
console.warn(`watching: ${globs.map(g => yellow(g)).join(', ')}`)
return chokidar.watch(globs)
.on('add', update)
.on('change', update)
.on('unlink', update)
}
function syncLinks(links) {
for (const {source, dest} of links) {
const destDir = dirname(dest)
removeSync(dest)
@ -66,3 +103,6 @@ function getLinks(sourceDir, destDir, map) {
dest: join(destDir, dest)
}))
}
function noop() {
}

45
docs/script/copy Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env node
const {basename, join} = require('path')
const {sync, watch} = require('../lib/sync')
const args = process.argv.slice(2)
const sourceDir = join(__dirname, '../../modules')
const destDir = join(__dirname, '../pages/css')
const options = {
sourceDir,
destDir,
debug: !args.includes('--quiet'),
map: {
'../CHANGELOG.md': 'whats-new/changelog.md',
'primer/README.md': false, // 'packages/primer.md',
'primer-base/README.md': false, // 'support/base.md',
'primer-core/README.md': false, // 'packages/primer-core.md',
'primer-layout/README.md': 'objects/layout.md',
'primer-layout/docs/*.md': path => `objects/${basename(path)}`,
'primer-marketing-support/README.md': 'support/marketing-variables.md',
'primer-marketing-type/README.md': 'utilities/marketing-type.md',
'primer-marketing-utilities/README.md': false, // 'utilities/marketing.md',
'primer-marketing-utilities/docs/*.md': path => `utilities/marketing-${basename(path)}`,
'primer-marketing/README.md': false, // 'packages/primer-marketing.md',
'primer-product/README.md': false, // 'packages/primer-product.md',
'primer-support/README.md': false, // 'support/index.md',
'primer-support/docs/*.md': path => `support/${basename(path)}`,
'primer-table-object/README.md': 'objects/table-object.md',
'primer-utilities/README.md': false, // 'utilities/index.md',
'primer-utilities/docs/*.md': path => `utilities/${basename(path)}`,
// this is a catch-all rule that needs to go last so that it doesn't override others
'primer-*/README.md': path => `components/${shortName(path)}.md`,
}
}
if (args.includes('--watch')) {
const watcher = watch(options)
process.on('exit', () => watcher.close())
} else {
sync(options)
}
function shortName(path) {
return path.match(/primer-([-\w]+)/)[1]
}