Skip to content

Commit

Permalink
Add template argument
Browse files Browse the repository at this point in the history
  • Loading branch information
borracciaBlu committed Dec 18, 2019
1 parent 9750dd8 commit 3f55e39
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
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

0 comments on commit 3f55e39

Please sign in to comment.