Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template argument #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ if (argv.h || argv.help) {
-a, --author Define author
-y, --year Define year
-l, --license Define license
--template Define template
-h, --help Display help information
-v, --version Output version

Examples:

$ banner-cli dist/**/*.js
$ banner-cli dist/**/*.css --author 'CJ Patoilo' --license MIT --site https://milligram.io
$ banner-cli dist/**/*.css --template '/*<br> [name]<br> [tag]<br> [site]<br> [author]<br> [year]<br> [license]<br> [time] */'
`)
process.exit(0)
}
Expand Down Expand Up @@ -86,6 +88,10 @@ if (argv.y && argv.y !== true) {
options.year = argv.y
}

if (argv.template && argv.template !== true) {
options.template = argv.template
}

if (argv._[0]) {
options.source = argv._[0]
banner(options)
Expand Down
35 changes: 27 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,48 @@ const getAuthorName = value => {
}

const unlicense = 'This is free and unencumbered software'
const defaultTemplate =
`/*!
* [name] v[tag]
* [homepage]
*
* Copyright (c) [year] [author]
* [license]
*/
`

function getBanner(template, options) {
return template
.replace('[name]', options.name.charAt(0).toUpperCase() + options.name.slice(1))
.replace('[tag]', options.tag)
.replace('[homepage]', options.homepage)
.replace('[license]', options.license)
.replace('[author]', options.author)
.replace('[year]', options.year)
.replace('[time]', Date.now())
.split('<br>')
.join("\n")

}

function banner (options = {}) {
options.name = options.name || pkg.name || 'unknown'
options.tag = options.tag || pkg.version || '0.0.0'
options.homepage = options.homepage || pkg.homepage || `https://npm.com/${options.name}`
options.license = options.license || pkg.license
options.license = options.license ? `Licensed under the ${options.license} license` : unlicense
options.author = options.author || getAuthorName(pkg.author)
options.year = options.year || new Date().getFullYear()

const template = options.template || `/*!
* ${options.name.charAt(0).toUpperCase() + options.name.slice(1)} v${options.tag}
* ${options.homepage}
*
* Copyright (c) ${options.year} ${options.author}
* ${options.license ? `Licensed under the ${options.license} license\n *` : unlicense}/\n
`
let template = options.template || defaultTemplate;
let bannerText = getBanner(template, options)

if (!options.source) {
throw new Error(`File not found!`)
} else {
glob(options.source, (err, files) => {
if (err) throw err
files.map(file => prependFile.sync(file, template))
files.map(file => prependFile.sync(file, bannerText))
process.exit(0)
})
}
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ $ banner-cli --help
-a, --author Define author
-y, --year Define year
-l, --license Define license
--template Define template
-h, --help Display help information
-v, --version Output version

Examples:

$ banner-cli dist/**/*.js
$ banner-cli dist/**/*.css --author 'CJ Patoilo' --license MIT --site https://milligram.io
$ banner-cli dist/**/*.css --template '/*<br> [name]<br> [tag]<br> [site]<br> [author]<br> [year]<br> [license]<br> [time] */'

```

Expand Down