Skip to content

Commit

Permalink
feat: introduce cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 30, 2022
1 parent a527952 commit 01e00b3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ Insert script to `package.json`
|`--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
|`--cache` | Dir for temporary assets | `tempy.directory()`
|`--dtsOut` | **TS** typings output | `typings/index.d.ts`
|`--flowOut` | **Flow** libdef output | `flow-typed/index.flow.js`
|`--flowOut` | **Flow** libdef output | `flow-typed/index.flow.js`

## License
[MIT](./LICENSE)
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"description": "Util toolset to produce single-file TS and Flow libdefs",
"source": "src/main/ts/index.ts",
"bin": {
"libdefkit": "./target/es5/cli.js"
"libdefkit": "./target/es6/cli.js"
},
"main": "./target/es5/index.js",
"main": "./target/es6/index.js",
"type": "module",
"types": "typings/index.d.ts",
"typescript": {
Expand Down Expand Up @@ -62,11 +62,7 @@
"**/glob-parent": "^6.0.1"
},
"dependencies": {
"@qiwi/dts-bundle": "^0.7.5",
"@types/find-cache-dir": "^3.2.1",
"@types/fs-extra": "^9.0.13",
"chalk": "^5.0.0",
"find-cache-dir": "^3.3.2",
"find-up": "^6.2.0",
"flowgen": "1.17.0",
"fs-extra": "^10.0.0",
Expand All @@ -77,6 +73,9 @@
"tslib": "^2.3.1"
},
"devDependencies": {
"@qiwi/dts-bundle": "^0.7.5",
"@types/find-cache-dir": "^3.2.1",
"@types/fs-extra": "^9.0.13",
"@jest/globals": "^27.4.6",
"@qiwi/npm-run-all": "^4.1.7",
"@types/jest": "^27.4.0",
Expand Down
3 changes: 3 additions & 0 deletions src/main/ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const cli = meow(
type: 'string',
isMultiple: true,
},
cache: {
type: 'string'
}
},
},
)
Expand Down
23 changes: 12 additions & 11 deletions src/main/ts/dts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @module @qiwi/libdefkit */
/** */

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

Expand All @@ -13,14 +13,15 @@ export const generate = (
tempDir: string,
name: string,
): void => {
const cfg = fs.readJsonSync(tsconfig)
const cfg = fse.readJsonSync(tsconfig)
const targetDir = cfg?.compilerOptions?.outDir
const genDir = join(tempDir, 'gen', targetDir)
const bundleDir = join(tempDir, 'bundle')
const rand = Math.random().toString(16).slice(2, 10)
const genDir = join(tempDir, `libdefkit-${rand}/gen`, targetDir)
const bundleDir = join(tempDir, `libdefkit-${rand}/bundle`)
const bundlePath = join(bundleDir, cfg?.compilerOptions?.target + '.d.ts')

fs.ensureDirSync(genDir)
fs.ensureDirSync(bundleDir)
console.log('!!!bundlePath', bundlePath)
fse.ensureDirSync(genDir)
fse.ensureDirSync(bundleDir)

invoke({
cmd: 'tsc',
Expand Down Expand Up @@ -50,11 +51,11 @@ export const alias = (from: string, to: string, tempDir: string): void => {
export * from '${from}';
}
`
fs.outputFileSync(bundlePath, contents)
fse.outputFileSync(bundlePath, contents)
}

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

export const pipe: IExecPipe = (ctx): IContext => {
Expand All @@ -67,14 +68,14 @@ export const pipe: IExecPipe = (ctx): IContext => {
}

const files = [
...globbySync(['bundle/**/*.ts'], {onlyFiles: true, absolute: true, cwd: cache}),
...globbySync(['**/bundle/**/*.ts'], {onlyFiles: true, absolute: true, cwd: cache}),
...globbySync(customTypings, {onlyFiles: true, absolute: true, cwd})
]

const dts = merge(files)

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

return { ...ctx, dts }
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 @@ -19,6 +19,7 @@ export type ICliFlags = {
flowOut?: string
tsconfig?: string[]
customTypings?: string[]
cache?: string
}

export type IContext = ICliFlags & {
Expand Down
8 changes: 4 additions & 4 deletions src/main/ts/libdefkit.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/** @module @qiwi/libdefkit */
/** */

import findCacheDir from 'find-cache-dir'
import fs from 'fs-extra'
import { join } from 'path'
import tempy from 'tempy'

import { pipe as dtsgen } from './dts'
import { ICliFlags, IContext, IExecPipe } from './interface'
import { invoke } from './util'

export const normalize: IExecPipe = (flags: ICliFlags): IContext => {
const cwd = flags.cwd || process.cwd()
const cache = findCacheDir({ name: '@qiwi/libdefkit' }) + ''
const cache = tempy.directory()
const name = fs.readJsonSync(join(cwd, 'package.json')).name
const entry = flags.entry ?? `${name}/target/es6`
const dtsOut = flags.dtsOut ?? join(cwd, 'typings', 'index.d.ts')
const flowOut = flags.flowOut ?? join(cwd, 'flow-typed', 'index.flow.js')

return { ...flags, cache, cwd, name, entry, dtsOut, flowOut }
return { cache, ...flags, cwd, name, entry, dtsOut, flowOut }
}

export const clear: IExecPipe = (ctx) => {
Expand All @@ -30,7 +30,7 @@ export const flowgen: IExecPipe = ({ dtsOut, flowOut }): void => {
}
}

export const pipeline: IExecPipe[] = [normalize, clear, dtsgen, flowgen, clear]
export const pipeline: IExecPipe[] = [normalize, clear, dtsgen, flowgen]

export const execute = (flags: ICliFlags): IContext =>
pipeline.reduce((ctx, pipe) => pipe(ctx) || ctx, flags as IContext)
10 changes: 8 additions & 2 deletions src/test/ts/libdefkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ describe('libdefkit', () => {
describe('#execute', () => {
it('returns proper dts bundle', () => {
const temp = resolve(__dirname, '..', 'temp')
const cache = join(temp, 'cache')
const dtsOut = join(temp, 'index.d.ts')
const flowOut = join(temp, 'index.flow.js')
const result = execute({
tsconfig: ['tsconfig.es6.json'],
dtsOut,
flowOut,
customTypings: ['customTypings/**/*.d.ts']
customTypings: ['customTypings/**/*.d.ts'],
cache
})

const cwd = JSON.stringify(process.cwd()).slice(1, -1)
const snap = JSON.parse(
JSON.stringify(result).split(cwd).join('<cwd>').replace(/\\\\/g, '/'),
JSON.stringify(result)
.replaceAll(cwd,'<cwd>')
.replace(/\\\\/g, '/'),
)
snap.cwd = '<cwd>'
snap.cache = '<cache>'

expect(snap).toMatchSnapshot()
})
Expand Down
9 changes: 4 additions & 5 deletions src/test/ts/libdefkit.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`libdefkit #execute returns proper dts bundle 1`] = `
Object {
"cache": "<cwd>/node_modules/.cache/@qiwi/libdefkit",
"cache": "<cache>",
"customTypings": Array [
"customTypings/**/*.d.ts",
],
Expand All @@ -11,10 +11,9 @@ Object {
/** */
export * from '@qiwi/libdefkit/target/es6';
}
// Generated by dts-bundle v0.7.5
// Dependencies for this module:
// ../../../../../../../child_process
// ../../../../../../../../child_process
declare module '@qiwi/libdefkit/target/es6' {
/** */
Expand Down Expand Up @@ -49,6 +48,7 @@ declare module '@qiwi/libdefkit/target/es6/interface' {
flowOut?: string;
tsconfig?: string[];
customTypings?: string[];
cache?: string;
};
export type IContext = ICliFlags & {
cache: string;
Expand All @@ -64,11 +64,10 @@ declare module '@qiwi/libdefkitfake' {
}
",
"dtsOut": "<cwd>/src/test/temp/index.d.ts",
"entry": "@qiwi/libdefkit/target/es5",
"entry": "@qiwi/libdefkit/target/es6",
"flowOut": "<cwd>/src/test/temp/index.flow.js",
"name": "@qiwi/libdefkit",
"tsconfig": Array [
"tsconfig.es5.json",
"tsconfig.es6.json",
],
}
Expand Down

0 comments on commit 01e00b3

Please sign in to comment.