UNPKG

create-banner

Version:
58 lines (55 loc) 1.57 kB
import changeCase from 'change-case'; import dotProp from 'dot-prop'; import deepMerge from 'deepmerge'; import readPkgUp from 'read-pkg-up'; const now = new Date(); const TEMPLATES = { normal: `/*! * @name v@version * @homepage * * Copyright @year @author.name * Released under the @license license * * Date: @date */ `, simple: `/*! * @name v@version * Copyright @year @author.name * Released under the @license license */ `, inline: '/*! @name v@version | (c) @year @author.name | @license */', }; const DEFAULTS = { case: '', data: { date: now.toISOString(), year: now.getFullYear(), }, template: 'normal', }; const REGEXP_SCOPE = /^.+\//; const REGEXP_PLACEHOLDER = /@(\w+(?:\.\w+)*)/gi; function createBanner(options) { const opts = deepMerge(DEFAULTS, options || {}); let { pkg } = opts; if (!pkg) { const result = readPkgUp.sync(); if (result) { pkg = result.packageJson; } } const data = deepMerge(pkg || {}, opts.data || {}); data.name = (data.name || '').replace(REGEXP_SCOPE, ''); if (typeof changeCase.camelCase === 'function') { const convertCase = changeCase[changeCase.camelCase(opts.case || '')]; if (typeof convertCase === 'function') { data.name = convertCase(data.name); } } const template = TEMPLATES[opts.template || ''] || String(opts.template); return template.replace(REGEXP_PLACEHOLDER, (placeholder, property) => dotProp.get(data, property)); } export default createBanner;