Skip to content

Commit

Permalink
feat: support several license types
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 2, 2019
1 parent 461cb1c commit f008284
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ generate({
| `file` | License file name | `LICENSE` |
| `dir` | License target dir | Project root |
| `name` | Project name | `name` from `package.json` |
| `type` | License type | `qosl` |
21 changes: 21 additions & 0 deletions src/main/tpl/mit_en.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) <%= year %> QIWI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const cli = meow(`
--file, -f License file name
--dir, -d License target directory
--name, -n Software name
--type, -t License type
Examples
$ qiwilicense --year=2020 --dir=/Users/foo/bar
Expand All @@ -37,6 +38,10 @@ export const cli = meow(`
type: 'string',
alias: 'n',
},
type: {
type: 'string',
alias: 't',
},
},
})

Expand Down
3 changes: 2 additions & 1 deletion src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export type IRenderOpts = {
year?: number | string,
dir?: string,
file?: string,
name?: string
name?: string,
type?: string
}
5 changes: 3 additions & 2 deletions src/main/ts/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export const DEFAULT_OPTS: IRenderOpts = {
year: new Date().getFullYear(),
dir: ROOT,
name: readPkgSync().name,
type: 'qosl',
}

export const render = (opts: IRenderOpts): string => {
const {lang, year, name} = {...DEFAULT_OPTS, ...opts}
const tpl = loadTemplate(`license_${lang}.tpl`)
const {lang, year, name, type} = {...DEFAULT_OPTS, ...opts}
const tpl = loadTemplate(`${type}_${lang}.tpl`)

return template(tpl)({year, name})
}
Expand Down
14 changes: 8 additions & 6 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {resolve} from 'path'
import {readFileSync} from 'fs'
import {exec} from 'child_process'
import {render, generate} from '../../main/ts'
import {TLanguage} from '../../main/ts/interface'
import {render, generate, TLanguage} from '../../main/ts'

describe('index', () => {
describe('render', () => {
Expand All @@ -24,16 +23,21 @@ describe('index', () => {
const year = '2010-2019' + Math.random()
const dir = resolve(__dirname, '../tmp')
const file = 'lic'
const type = 'mit'
const filePath = resolve(dir, file)

generate({
lang: TLanguage.EN,
file,
dir,
year,
type,
})

expect(readFileSync(filePath, 'utf-8').includes(year)).toBeTruthy()
const result = readFileSync(filePath, 'utf-8')

expect(result.includes('MIT License')).toBeTruthy()
expect(result.includes(year)).toBeTruthy()
})
})
})
Expand All @@ -58,9 +62,7 @@ describe('bin', () => {

const cmd = `node ${args.join(' ')}`

exec(cmd, (err) => {
console.error(err)

exec(cmd, () => {
const result = readFileSync(filePath, 'utf-8')

expect(result.includes(year)).toBeTruthy()
Expand Down

0 comments on commit f008284

Please sign in to comment.