Skip to content

Commit

Permalink
feat: add license render and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 1, 2019
1 parent 1c45c7c commit 9c3845d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typings

# Temp
temp
tmp
*.tmp

# Typescript
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"lint:fix": "yarn lint --fix",
"test": "yarn lint && yarn jest",
"clean": "rimraf target typings",
"build": "yarn clean && yarn build:es5 && yarn build:es6 && yarn build:ts && yarn libdef && yarn docs && yarn uglify && yarn build:bundle",
"build": "yarn clean && yarn build:tpl && yarn build:es5 && yarn build:es6 && yarn build:ts && yarn libdef && yarn docs && yarn uglify && yarn build:bundle",
"build:tpl": "mkdir -p target/tpl && cp -r src/main/tpl/ \"$_\"",
"build:es5": "mkdir -p target/es5 && tsc -p tsconfig.es5.json",
"build:es6": "mkdir -p target/es6 && tsc -p tsconfig.es6.json",
"build:ts": "cp -r src/main/ts/ target/ts/",
Expand Down Expand Up @@ -54,6 +55,8 @@
"homepage": "https://github.com/qiwi/license#readme",
"dependencies": {
"@qiwi/substrate": "^1.11.2",
"@types/lodash": "^4.14.141",
"find-git-root": "^1.0.2",
"lodash": "^4.17.15",
"tslib": "^1.10.0"
},
Expand Down
39 changes: 38 additions & 1 deletion src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
export const render = () => { /*noop*/ }
import {readFileSync, writeFileSync, mkdirSync} from 'fs'
import {resolve} from 'path'
import {template} from 'lodash'
import findGitRoot from 'find-git-root'

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

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

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

return template(tpl)({year})
}

export const loadTemplate = (name: string): string => {
const templatePath = resolve(__dirname,'../tpl', name)

return readFileSync(templatePath, 'utf-8')
}

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

mkdirSync(dir + '', {recursive: true})
writeFileSync(target, text, 'utf-8')
}
11 changes: 11 additions & 0 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum TLanguage {
RU = 'ru',
EN = 'en',
}

export type IRenderOpts = {
lang?: TLanguage | string,
year?: number | string,
dir?: string,
name?: string
}
37 changes: 35 additions & 2 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import {render, generate} from '../../main/ts'
import {resolve} from 'path'
import {readFileSync} from 'fs'
import {TLanguage} from '../../main/ts/interface'

describe('index', () => {
it('', () => {
expect(true).toBeTruthy()
describe('render', () => {
it('returns license text for the specified language (if exists)', () => {
expect(render({
lang: TLanguage.EN,
})).toEqual(expect.any(String))
})

it('throws error otherwise', () => {
expect(() => render({
lang: 'foo',
})).toThrowError()
})
})

describe('generate', () => {
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)

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

expect(readFileSync(filePath, 'utf-8').includes(year)).toBeTruthy()
})
})
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"rootDir": "./src/main/ts/",
"baseUrl": "./src/main/ts/",
"moduleResolution": "node",
Expand Down
46 changes: 12 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,11 @@
dependencies:
"@types/jest-diff" "*"

"@types/lodash@^4.14.141":
version "4.14.141"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.141.tgz#d81f4d0c562abe28713406b571ffb27692a82ae6"
integrity sha512-v5NYIi9qEbFEUpCyikmnOYe4YlP8BMUdTcNCAquAKzu+FA7rZ1onj9x80mbnDdOW/K5bFf3Tv5kJplP33+gAbQ==

"@types/minimatch@*", "@types/minimatch@3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -2588,7 +2593,7 @@ debug@^4.0.0, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3254,6 +3259,11 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

find-git-root@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/find-git-root/-/find-git-root-1.0.2.tgz#1b22195cef4bde0a0133d426baf424809da44e57"
integrity sha512-4RvQEPt1yDFrom3rEG30DN8L8dhNbxq2iKBwGUQlYNUPVDuS/Kmsz0O/xkSOCPEGFeNZYLKZ7XQku2GfhUa4Pw==

find-npm-prefix@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/find-npm-prefix/-/find-npm-prefix-1.0.2.tgz#8d8ce2c78b3b4b9e66c8acc6a37c231eb841cfdf"
Expand Down Expand Up @@ -3933,7 +3943,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5172,11 +5182,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5185,33 +5190,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -5267,11 +5250,6 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit 9c3845d

Please sign in to comment.