Skip to content

Commit

Permalink
automatically insert current year
Browse files Browse the repository at this point in the history
  • Loading branch information
sadorlovsky committed Dec 17, 2016
1 parent 391fab0 commit 59ff228
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
},
"dependencies": {
"chalk": "^1.1.3",
"deep-assign": "^2.0.0",
"fullname": "^3.1.0",
"lodash": "^4.17.2",
"meow": "^3.7.0",
Expand Down
7 changes: 6 additions & 1 deletion src/lissie.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { readFile, readdir } from 'fs'
import path from 'path'
import pify from 'pify'
import assign from 'deep-assign'

const normalize = text => text.trim().toLowerCase().replace(' ', '-')

export const list = () => pify(readdir)('licenses')

export default (license = 'mit', options = {}) => {
const defaults = {
year: new Date().getFullYear()
}
const opts = assign({}, defaults, options)
return pify(readFile)(
path.join(__dirname, '..', 'licenses', normalize(license)),
{ encoding: 'utf8' }
)
.then(text => text.replace(
/\{year\}|\{author\}|\{project\}|\{email\}/gi,
matched => options[matched.replace(/\{|\}/gi, '')] || matched
matched => opts[matched.replace(/\{|\}/gi, '')] || matched
))
.catch(({ code }) => {
if (code === 'ENOENT') {
Expand Down
22 changes: 12 additions & 10 deletions src/lissie.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import fs from 'fs'
import path from 'path'
import pify from 'pify'
import test from 'ava'
import { take } from 'lodash'
import { stripIndent } from 'common-tags'
import license from './lissie'

const head = x => take(x.split('\n'), 3).join('\n')

test('returns license text', async t => {
const expected = await pify(fs).readFile(path.join('licenses', 'mit'), { encoding: 'utf8' })
t.is(await license(), expected)
t.is(await license('mit'), expected)
const expected = stripIndent`
The MIT License (MIT)
Copyright (c) 2016 {author}
`
t.is(head(await license()), expected)
t.is(head(await license('mit')), expected)
})

test('pass options', async t => {
const mit = await license('mit', {
year: 2016,
year: 2015,
author: 'Zach Orlovsky'
})
const expected = stripIndent`
The MIT License (MIT)
Copyright (c) 2016 Zach Orlovsky
Copyright (c) 2015 Zach Orlovsky
`
const head = take(mit.split('\n'), 3).join('\n')
t.is(head, expected)
t.is(head(mit), expected)
})

test('normalize input', async t => {
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,12 @@ decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"

deep-assign@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/deep-assign/-/deep-assign-2.0.0.tgz#ebe06b1f07f08dae597620e3dd1622f371a1c572"
dependencies:
is-obj "^1.0.0"

deep-equal@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
Expand Down

0 comments on commit 59ff228

Please sign in to comment.