Skip to content

Commit

Permalink
feat: introduce customTypings injection
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
antongolub committed Jul 28, 2021
1 parent 8f1b2a8 commit 68005d0
Show file tree
Hide file tree
Showing 10 changed files with 966 additions and 941 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# @qiwi/libdefkit
Util toolset to produce single-file TS and Flow libdefs for Qiwi OSS projects
Util toolset to produce single-file TS and Flow libdefs

## Requirements
Node.js >= 14
Expand All @@ -25,6 +25,7 @@ Insert script to `package.json`
|`--cwd` | Set current working dir | `process.cwd()`
|`--entry` | Define pkg entry point | `<pkgName>/target/es5`
|`--tsconfig` | Define path(s) to project's TS config
|`--customTypings` | Attach custom libdefs to d.ts bundle
|`--dtsOut` | **TS** typings output | `typings/index.d.ts`
|`--flowOut` | **Flow** libdef output | `flow-typed/index.flow.js`

Expand Down
3 changes: 3 additions & 0 deletions customTypings/@qiwi__libdefkitfake/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@qiwi/libdefkitfake' {
export const foo: string
}
2 changes: 1 addition & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"preset": "ts-jest",
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"types.ts"
"<rootDir>/src/main/ts/cli.ts"
],
"collectCoverageFrom": [
"<rootDir>/src/main/ts/**/*.ts"
Expand Down
43 changes: 22 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@qiwi/libdefkit",
"version": "3.0.0",
"private": false,
"description": "Util toolset to produce single-file TS and Flow libdefs for Qiwi OSS projects",
"description": "Util toolset to produce single-file TS and Flow libdefs",
"source": "src/main/ts/index.ts",
"bin": {
"libdefkit": "./target/es5/cli.js"
Expand All @@ -28,7 +28,7 @@
"test:unit": "mkdirp src/test/temp && jest --config=jest.config.json --runInBand",
"test:deps": "npm-run-all -p -l test:depcheck test:depauditfix",
"test:depcheck": "npm_config_yes=true npx depcheck --ignores tslib,@swissquote/crafty-preset-jest,@types/jest,flowgen,@qiwi/dts-bundle,esm,eslint-config-*",
"test:depaudit": "yarn audit --level=moderate; [[ $? -ge 4 ]] && exit 1 || exit 0",
"test:depaudit": "yarn audit --level=moderate --groups=dependencies; [[ $? -ge 4 ]] && exit 1 || exit 0",
"test:depauditfix": "npm_config_yes=true npx yarn-audit-fix --audit-level=moderate",
"clean": "rimraf target typings flow-typed",
"build": "npm-run-all -p -l build:es5 build:es6 build:ts docs build:fix && yarn build:libdef",
Expand Down Expand Up @@ -59,38 +59,39 @@
},
"homepage": "https://github.com/qiwi/libdefkit#readme",
"dependencies": {
"@types/find-cache-dir": "^3.2.0",
"@types/fs-extra": "^9.0.11",
"chalk": "^4.1.1",
"@qiwi/dts-bundle": "^0.7.5",
"@types/find-cache-dir": "^3.2.1",
"@types/fs-extra": "^9.0.12",
"chalk": "^4.1.1",
"find-cache-dir": "^3.3.1",
"find-up": "^5.0.0",
"flowgen": "1.14.1",
"fs-extra": "^10.0.0",
"tslib": "^2.2.0",
"meow": "^10.0.0",
"pkg-dir": "^5.0.0"
"globby": "^11.0.4",
"meow": "^10.1.0",
"pkg-dir": "^5.0.0",
"tslib": "^2.3.0"
},
"devDependencies": {
"@types/yargs-parser": "^20.2.0",
"yargs-parser": "^20.2.7",
"@qiwi/npm-run-all": "^4.1.7",
"@types/jest": "^26.0.23",
"coveralls": "^3.1.0",
"@types/jest": "^26.0.24",
"@types/yargs-parser": "^20.2.1",
"coveralls": "^3.1.1",
"cpy-cli": "^3.1.1",
"eslint": "^7.28.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-qiwi": "^1.13.0",
"jest": "^27.0.4",
"eslint-config-qiwi": "^1.13.2",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"prettier-config-qiwi": "^1.4.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"typedoc": "^0.20.36",
"typescript": "^4.3.2",
"prettier": "^2.3.1",
"prettier-config-qiwi": "^1.4.1"
"ts-jest": "^27.0.4",
"typedoc": "^0.21.4",
"typescript": "^4.3.5",
"yargs-parser": "^20.2.9"
},
"prettier": "prettier-config-qiwi",
"author": "QIWI <orensource@qiwi.com>",
"author": "QIWI <opensource@qiwi.com>",
"contributors": [
"Anton Golub <[email protected]>"
],
Expand Down
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 @@ const cli = meow(
Options
--cwd, working dir path
--tsconfig
--customTypings, attach custom libdefs to d.ts bundle
--entry, alias entry (<pkgName>/target/es5/index by default)
--dtsOut, dts output (typings/index.d.ts by default)
--flowOut, flow output (flow-typed/index.flow.js by default)
Expand All @@ -31,6 +32,10 @@ const cli = meow(
type: 'string',
isMultiple: true,
},
customTypings: {
type: 'string',
isMultiple: true,
},
},
},
)
Expand Down
30 changes: 15 additions & 15 deletions src/main/ts/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/** */

import fs from 'fs-extra'
import globby from 'globby'
import { join } from 'path'

import { IContext, IExecPipe } from './interface'
Expand Down Expand Up @@ -52,30 +53,29 @@ export const alias = (from: string, to: string, tempDir: string): void => {
fs.outputFileSync(bundlePath, contents)
}

export const merge = (tempDir: string, dtsOut?: string): string => {
const bundleDir = join(tempDir, 'bundle')
const contents = fs
.readdirSync(bundleDir)
.reduce(
(m, f) => fs.readFileSync(join(bundleDir, f), { encoding: 'utf-8' }) + m,
'',
)

dtsOut && fs.outputFileSync(dtsOut, contents, {})

return contents
}
export const merge = (files: string[], memo = ''): string => files.reduce((m: string, f: string) =>
m + fs.readFileSync(f, { encoding: 'utf-8' })
, memo)

export const pipe: IExecPipe = (ctx): IContext => {
const { name, entry, cache, tsconfig = [], dtsOut } = ctx
const { name, entry, cache, tsconfig = [], customTypings = [], dtsOut, cwd } = ctx

tsconfig.forEach((cfg) => generate(cfg, cache, name))

if (entry) {
alias(entry, name, cache)
}

const dts = merge(cache, dtsOut)
const files = [
...globby.sync(['bundle/**/*.ts'], {onlyFiles: true, absolute: true, cwd: cache}),
...globby.sync(customTypings, {onlyFiles: true, absolute: true, cwd})
]

const dts = merge(files)

if (dtsOut) {
fs.outputFileSync(dtsOut, dts, {})
}

return { ...ctx, dts }
}
1 change: 1 addition & 0 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ICliFlags = {
dtsOut?: string
flowOut?: string
tsconfig?: string[]
customTypings?: string[]
}

export type IContext = ICliFlags & {
Expand Down
1 change: 1 addition & 0 deletions src/test/ts/libdefkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('libdefkit', () => {
tsconfig: ['tsconfig.es5.json', 'tsconfig.es6.json'],
dtsOut,
flowOut,
customTypings: ['customTypings/**/*.d.ts']
})

const cwd = JSON.stringify(process.cwd()).slice(1, -1)
Expand Down
36 changes: 22 additions & 14 deletions src/test/ts/libdefkit.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
exports[`libdefkit #execute returns proper dts bundle 1`] = `
Object {
"cache": "<cwd>/node_modules/.cache/@qiwi/libdefkit",
"customTypings": Array [
"customTypings/**/*.d.ts",
],
"cwd": "<cwd>",
"dts": "// Generated by dts-bundle v0.7.5
"dts": "declare module '@qiwi/libdefkit' {
/** */
export * from '@qiwi/libdefkit/target/es5';
}
// Generated by dts-bundle v0.7.5
// Dependencies for this module:
// ../../../../../../../child_process
declare module '@qiwi/libdefkit/target/es6' {
declare module '@qiwi/libdefkit/target/es5' {
/** */
export * from '@qiwi/libdefkit/target/es6/libdefkit';
export * from '@qiwi/libdefkit/target/es5/libdefkit';
}
declare module '@qiwi/libdefkit/target/es6/libdefkit' {
declare module '@qiwi/libdefkit/target/es5/libdefkit' {
/** */
import { ICliFlags, IContext, IExecPipe } from '@qiwi/libdefkit/target/es6/interface';
import { ICliFlags, IContext, IExecPipe } from '@qiwi/libdefkit/target/es5/interface';
export const normalize: IExecPipe;
export const clear: IExecPipe;
export const flowgen: IExecPipe;
export const pipeline: IExecPipe[];
export const execute: (flags: ICliFlags) => IContext;
}
declare module '@qiwi/libdefkit/target/es6/interface' {
declare module '@qiwi/libdefkit/target/es5/interface' {
/** */
import { StdioOptions } from 'child_process';
export type ICmdInvokeOptions = {
Expand All @@ -40,6 +47,7 @@ declare module '@qiwi/libdefkit/target/es6/interface' {
dtsOut?: string;
flowOut?: string;
tsconfig?: string[];
customTypings?: string[];
};
export type IContext = ICliFlags & {
cache: string;
Expand All @@ -54,22 +62,22 @@ declare module '@qiwi/libdefkit/target/es6/interface' {
// Dependencies for this module:
// ../../../../../../../child_process
declare module '@qiwi/libdefkit/target/es5' {
declare module '@qiwi/libdefkit/target/es6' {
/** */
export * from '@qiwi/libdefkit/target/es5/libdefkit';
export * from '@qiwi/libdefkit/target/es6/libdefkit';
}
declare module '@qiwi/libdefkit/target/es5/libdefkit' {
declare module '@qiwi/libdefkit/target/es6/libdefkit' {
/** */
import { ICliFlags, IContext, IExecPipe } from '@qiwi/libdefkit/target/es5/interface';
import { ICliFlags, IContext, IExecPipe } from '@qiwi/libdefkit/target/es6/interface';
export const normalize: IExecPipe;
export const clear: IExecPipe;
export const flowgen: IExecPipe;
export const pipeline: IExecPipe[];
export const execute: (flags: ICliFlags) => IContext;
}
declare module '@qiwi/libdefkit/target/es5/interface' {
declare module '@qiwi/libdefkit/target/es6/interface' {
/** */
import { StdioOptions } from 'child_process';
export type ICmdInvokeOptions = {
Expand All @@ -86,6 +94,7 @@ declare module '@qiwi/libdefkit/target/es5/interface' {
dtsOut?: string;
flowOut?: string;
tsconfig?: string[];
customTypings?: string[];
};
export type IContext = ICliFlags & {
cache: string;
Expand All @@ -96,9 +105,8 @@ declare module '@qiwi/libdefkit/target/es5/interface' {
export type IExecPipe = (ctx: IContext) => IContext | void;
}
declare module '@qiwi/libdefkit' {
/** */
export * from '@qiwi/libdefkit/target/es5';
declare module '@qiwi/libdefkitfake' {
export const foo: string
}
",
"dtsOut": "<cwd>/src/test/temp/index.d.ts",
Expand Down
Loading

0 comments on commit 68005d0

Please sign in to comment.