Skip to content

Commit

Permalink
feat: inject product name to license text
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 1, 2019
1 parent 9ad8e16 commit 297f038
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ install:

script:
- yarn run build
- chmod -R 777 ./target
- chmod -R 777 target
- yarn test:report

deploy:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@
"@qiwi/substrate": "^1.11.2",
"@types/lodash": "^4.14.141",
"@types/meow": "^5.0.0",
"@types/read-pkg": "^5.1.0",
"find-git-root": "^1.0.2",
"lodash": "^4.17.15",
"meow": "^5.0.0",
"read-pkg": "^5.2.0",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/main/tpl/license_en.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ QIWI JSC
<%= year %>

According to Art. 1286.1 of the Civil Code of the Russian Federation,
the Licensor provides an open license for the software product “__________
the Licensor provides an open license for the software product “<%= name %>
and related associated documentationfiles (hereinafter referred to as the “Software”).

Permission is hereby granted, free of charge, to any person obtaining
Expand Down
2 changes: 1 addition & 1 deletion src/main/tpl/license_ru.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%= year %>

Настоящим, согласно ст. 1286.1 Гражданского кодекс Российской Федерации,
Лицензиар предоставляет открытую лицензию на программный продукт «__________»
Лицензиар предоставляет открытую лицензию на программный продукт «<%= name %>»
и сопутствующую документацию (в дальнейшем именуемые «Программное обеспечение»).

Лица, получившие копию Программного обеспечения (далее – «Лицензиаты»), вправе
Expand Down
11 changes: 8 additions & 3 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export const cli = meow(`
Options
--year, -y Defines license year
--lang, -l Specifies license test language
--name, -n License file name
--dir, -d License file directory
--file, -f License file name
--dir, -d License directory
--name, -n Software name
Examples
$ qiwilicense --year=2020 --dir=/Users/foo/bar
$ qiwilicense -l en -n lic
$ qiwilicense -l en -f lic -n Pijma
`, {
flags: {
lang: {
Expand All @@ -28,6 +29,10 @@ export const cli = meow(`
type: 'string',
alias: 'd',
},
file: {
type: 'string',
alias: 'f',
},
name: {
type: 'string',
alias: 'n',
Expand Down
1 change: 1 addition & 0 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export type IRenderOpts = {
lang?: TLanguage | string,
year?: number | string,
dir?: string,
file?: string,
name?: string
}
16 changes: 10 additions & 6 deletions src/main/ts/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ import {readFileSync, writeFileSync, mkdirSync} from 'fs'
import {resolve} from 'path'
import {template} from 'lodash'
import findGitRoot from 'find-git-root'
import {sync as readPkgSync} from 'read-pkg'

import {
IRenderOpts,
TLanguage,
} from './interface'

const ROOT = resolve(findGitRoot(), '..')

export const DEFAULT_OPTS: IRenderOpts = {
lang: TLanguage.EN,
name: 'LICENSE',
file: 'LICENSE',
year: new Date().getFullYear(),
dir: resolve(findGitRoot(), '..'),
dir: ROOT,
name: readPkgSync().name,
}

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

return template(tpl)({year})
return template(tpl)({year, name})
}

export const loadTemplate = (name: string): string => {
Expand All @@ -29,9 +33,9 @@ export const loadTemplate = (name: string): string => {
}

export const generate = (opts: IRenderOpts) => {
const {dir, name} = {...DEFAULT_OPTS, ...opts}
const {dir, file} = {...DEFAULT_OPTS, ...opts}
const text = render(opts)
const target = resolve(dir + '', name + '')
const target = resolve(dir + '', file + '')

mkdirSync(dir + '', {recursive: true})
writeFileSync(target, text, 'utf-8')
Expand Down
25 changes: 17 additions & 8 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {resolve} from 'path'
import {readFileSync} from 'fs'
// import {exec} from 'child_process'
import {sync as execaSync} from 'execa'
import {render, generate} from '../../main/ts'
import {TLanguage} from '../../main/ts/interface'
Expand All @@ -23,12 +24,12 @@ describe('index', () => {
it('creates / updates target file with license', () => {
const year = '2010-2019' + Math.random()
const dir = resolve(__dirname, '../tmp')
const name = 'lic'
const filePath = resolve(dir, name)
const file = 'lic'
const filePath = resolve(dir, file)

generate({
lang: TLanguage.EN,
name,
file,
dir,
year,
})
Expand All @@ -43,20 +44,28 @@ describe('bin', () => {
const year = '2010-2019' + Math.random()
const dir = resolve(__dirname, '../tmp')
const lang = TLanguage.RU
const name = 'licFromCli'
const filePath = resolve(dir, name)
const file = 'licFromCli'
const filePath = resolve(dir, file)
const name = 'FOO'

execaSync(process.execPath,[
const args = [
'./target/es5/cli.js',
'-l', lang,
'--dir', dir,
`--name=${name}`,
`--file=${file}`,
'--year', year,
])
'-n', name,
]

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

execaSync(process.execPath, args)

const result = readFileSync(filePath, 'utf-8')

expect(result.includes(year)).toBeTruthy()
expect(result.includes('«КАК ЕСТЬ»')).toBeTruthy()
expect(result.includes('FOO')).toBeTruthy()

})
})
27 changes: 17 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,13 @@
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.5.3.tgz#1c3b71b091eaeaf5924538006b7f70603ce63d38"
integrity sha512-Jugo5V/1bS0fRhy2z8+cUAHEyWOATaz4rbyLVvcFs7+dXp5HfwpEwzF1Q11bB10ApUqHf+yTauxI0UXQDwGrbA==

"@types/read-pkg@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@types/read-pkg/-/read-pkg-5.1.0.tgz#5e0ba3323defbea679d645cd6f9985ea6ec8cb59"
integrity sha512-YOrcNByG1rPqIMhEMC1xU4SeFHcvQmHkl8yT6M4qPr+8eM1OBxRFJWog2uO+kx5Ept48Wdv+DuriUAVMC+LbQA==
dependencies:
read-pkg "*"

"@types/[email protected]":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
Expand Down Expand Up @@ -7284,16 +7291,7 @@ read-pkg-up@^6.0.0:
read-pkg "^5.1.1"
type-fest "^0.5.0"

read-pkg@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-3.0.0.tgz#9cbc686978fee65d16c00e2b19c237fcf6e38389"
integrity sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k=
dependencies:
load-json-file "^4.0.0"
normalize-package-data "^2.3.2"
path-type "^3.0.0"

read-pkg@^5.0.0, read-pkg@^5.1.1, read-pkg@^5.2.0:
read-pkg@*, read-pkg@^5.0.0, read-pkg@^5.1.1, read-pkg@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-5.2.0.tgz#7bf295438ca5a33e56cd30e053b34ee7250c93cc"
integrity sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg==
Expand All @@ -7303,6 +7301,15 @@ read-pkg@^5.0.0, read-pkg@^5.1.1, read-pkg@^5.2.0:
parse-json "^5.0.0"
type-fest "^0.6.0"

read-pkg@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-3.0.0.tgz#9cbc686978fee65d16c00e2b19c237fcf6e38389"
integrity sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k=
dependencies:
load-json-file "^4.0.0"
normalize-package-data "^2.3.2"
path-type "^3.0.0"

read@1, read@~1.0.1, read@~1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/read/-/read-1.0.7.tgz#b3da19bd052431a97671d44a42634adf710b40c4"
Expand Down

0 comments on commit 297f038

Please sign in to comment.