Skip to content

Commit

Permalink
feat: assert required opts
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 5, 2020
1 parent d02ec0e commit a0c4f77
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Util toolset to produce TS and Flow libdefs for Qiwi OSS projects",
"source": "src/main/ts/index.ts",
"bin": {
"libdef": "target/es5/cli.js"
"libdeffix": "target/es5/cli.js"
},
"main": "target/bundle/libdefkit.js",
"module": "target/bundle/libdefkit.mjs",
Expand Down
27 changes: 27 additions & 0 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import meow from 'meow'
import {rinf} from '.'

const cli = meow(`
Usage
$ libdeffix --dts {dts file path} --prefix {entry point prefix}
Options
--dts Target *.d.ts file path
--prefix Entry point path prefix
Examples
$ libdeffix --dts ./typings/index.d.ts --prefix @qiwi/decorator-utils/target/es5
$ libdeffix --version
$ libdeffix --help
`, {
flags: {
dts: {
type: 'string'
},
prefix: {
type: 'string'
}
}
})

rinf(cli.flags)
2 changes: 1 addition & 1 deletion src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './fix'
export * from './rinf'
5 changes: 5 additions & 0 deletions src/main/ts/rinf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {resolve} from 'path'
import assert from 'assert'
import {readFileSync} from 'fs'
import {
ReplaceInFileConfig,
Expand All @@ -21,6 +22,10 @@ import {logger} from './logger'
import {IReplacer, IRunnerOpts} from './interface'

export const rinf = (opts: IRunnerOpts) => {
const {dts, prefix} = opts
assert(!!dts, '`--dts` file path should be specified')
assert(!!prefix, 'entry point `--prefix` should be specified')

const cfg = getRinfConfig(opts)
const result: ReplaceResult[] = replaceInFileSync(cfg)

Expand Down
31 changes: 22 additions & 9 deletions src/test/ts/rinf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {resolve} from 'path'
import {copyFileSync, readFileSync} from 'fs'
import {IReplacer} from '../../main/ts/interface'
import {IReplacer, IRunnerOpts} from '../../main/ts/interface'
import {
rinf,
getRinfConfig,
Expand Down Expand Up @@ -35,17 +35,30 @@ describe('rinf', () => {
})
})

it('#rinf modifies target file', () => {
copyFileSync(inputDtsPath, tempDtsPath)
describe('#rinf', () => {
it('asserts `prefix` option', () => {
expect(() => rinf({dts: tempDtsPath} as IRunnerOpts))
.toThrow('entry point `--prefix` should be specified')
})

expect(readFileSync(tempDtsPath, 'utf-8')).toBe(readFileSync(inputDtsPath, 'utf-8'))
it('asserts `dts` option', () => {
expect(() => rinf({prefix: 'test'} as IRunnerOpts))
.toThrow('`--dts` file path should be specified')
})

const dts = tempDtsPath
const prefix = '@qiwi/decorator-utils/target/es5'
const cxt = {dts, prefix}
it('modifies target file', () => {
copyFileSync(inputDtsPath, tempDtsPath)

rinf(cxt)
expect(readFileSync(tempDtsPath, 'utf-8')).toBe(readFileSync(inputDtsPath, 'utf-8'))

expect(readFileSync(tempDtsPath, 'utf-8')).toBe(readFileSync(outputDtsPath, 'utf-8'))
const dts = tempDtsPath
const prefix = '@qiwi/decorator-utils/target/es5'
const cxt = {dts, prefix}

rinf(cxt)

expect(readFileSync(tempDtsPath, 'utf-8')).toBe(readFileSync(outputDtsPath, 'utf-8'))
})
})

})

0 comments on commit a0c4f77

Please sign in to comment.