-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add license render and generator
- Loading branch information
1 parent
1c45c7c
commit 9c3845d
Showing
7 changed files
with
102 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ typings | |
|
||
# Temp | ||
temp | ||
tmp | ||
*.tmp | ||
|
||
# Typescript | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters