Skip to content

Commit 067a642

Browse files
committed
feat: update deps, strengthen types
BREAKING CHANGE: following the deps, requires nodejs v16
1 parent 38d678f commit 067a642

File tree

11 files changed

+1456
-1148
lines changed

11 files changed

+1456
-1148
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
strategy:
8282
matrix:
8383
os: [ ubuntu-20.04 ]
84-
node-version: [ 14, 18 ]
84+
node-version: [ 16, 18 ]
8585
name: Test (Node v${{ matrix.node-version }}, OS ${{ matrix.os }})
8686
runs-on: ${{ matrix.os }}
8787
steps:

jest.config.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"preset": "ts-jest",
33
"collectCoverage": true,
44
"transform": {
5-
"^.+\\.tsx?$": "ts-jest"
5+
"^.+\\.tsx?$": ["ts-jest", {
6+
"useESM": true,
7+
"tsconfig": "<rootDir>/tsconfig.test.json"
8+
}]
69
},
710
"extensionsToTreatAsEsm": [".ts"],
811
"coveragePathIgnorePatterns": [
@@ -34,12 +37,6 @@
3437
"mjs",
3538
"cjs"
3639
],
37-
"globals": {
38-
"ts-jest": {
39-
"useESM": true,
40-
"tsconfig": "<rootDir>/tsconfig.test.json"
41-
}
42-
},
4340
"snapshotResolver": "<rootDir>/jest.snapshot.cjs",
4441
"resolver": "<rootDir>/jest.resolver.cjs"
4542
}

package.json

+21-21
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test": "npm-run-all -p -l lint test:unit test:depcheck test:depaudit",
2828
"test:unit": "mkdirp src/test/temp && cross-env NODE_OPTIONS='--experimental-vm-modules' jest --config=jest.config.json --runInBand",
2929
"test:deps": "npm-run-all -p -l test:depcheck test:depauditfix",
30-
"test:depcheck": "cross-env npm_config_yes=true npx -p depcheck -p @babel/[email protected] depcheck --ignores typedoc,tslib,flowgen,@qiwi/dts-bundle,eslint-config-*",
30+
"test:depcheck": "cross-env npm_config_yes=true npx -p depcheck -p @babel/[email protected] depcheck --ignores typedoc,tslib,flowgen,@qiwi/dts-bundle,eslint-config-*,@types/jest",
3131
"test:depaudit": "yarn audit --level=moderate --groups=dependencies; [[ $? -ge 4 ]] && exit 1 || exit 0",
3232
"test:depauditfix": "npm_config_yes=true npx yarn-audit-fix --audit-level=moderate",
3333
"clean": "rimraf target typings flow-typed",
@@ -63,37 +63,37 @@
6363
},
6464
"dependencies": {
6565
"@qiwi/dts-bundle": "^0.7.5",
66-
"chalk": "^5.0.1",
66+
"chalk": "^5.2.0",
6767
"find-up": "^6.3.0",
68-
"flowgen": "1.19.0",
69-
"fs-extra": "^10.1.0",
70-
"globby": "^13.1.1",
71-
"meow": "^10.1.2",
72-
"nanoid": "^3.3.4",
73-
"pkg-dir": "^6.0.1",
68+
"flowgen": "1.20.1",
69+
"fs-extra": "^11.1.0",
70+
"globby": "^13.1.3",
71+
"meow": "^11.0.0",
72+
"nanoid": "^4.0.0",
73+
"pkg-dir": "^7.0.0",
7474
"tempy": "^3.0.0",
75-
"tslib": "^2.4.0"
75+
"tslib": "^2.4.1"
7676
},
7777
"devDependencies": {
78-
"@jest/globals": "^28.1.0",
78+
"@jest/globals": "^29.3.1",
7979
"@qiwi/npm-run-all": "^4.1.7",
8080
"@types/fs-extra": "^9.0.13",
81-
"@types/jest": "^27.5.1",
81+
"@types/jest": "^29.2.4",
8282
"@types/yargs-parser": "^21.0.0",
8383
"coveralls": "^3.1.1",
84-
"cpy-cli": "^4.1.0",
84+
"cpy-cli": "^4.2.0",
8585
"cross-env": "^7.0.3",
86-
"eslint": "^8.16.0",
86+
"eslint": "^8.30.0",
8787
"eslint-config-prettier": "^8.5.0",
88-
"eslint-config-qiwi": "^1.17.1",
89-
"jest": "^28.1.0",
90-
"prettier": "^2.6.2",
91-
"prettier-config-qiwi": "^1.7.1",
88+
"eslint-config-qiwi": "^2.0.6",
89+
"jest": "^29.3.1",
90+
"prettier": "^2.8.1",
91+
"prettier-config-qiwi": "^2.0.1",
9292
"rimraf": "^3.0.2",
93-
"ts-jest": "^28.0.3",
94-
"typedoc": "^0.22.15",
95-
"typescript": "^4.7.2",
96-
"yargs-parser": "^21.0.1"
93+
"ts-jest": "^29.0.3",
94+
"typedoc": "^0.23.22",
95+
"typescript": "^4.9.4",
96+
"yargs-parser": "^21.1.1"
9797
},
9898
"prettier": "prettier-config-qiwi",
9999
"author": "QIWI <[email protected]>",

src/main/ts/dts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import fse from 'fs-extra'
55
import { globbySync } from 'globby'
66
import { nanoid } from 'nanoid'
7-
import { join } from 'path'
7+
import { join } from 'node:path'
88

99
import { IContext, IExecPipe } from './interface'
1010
import { invoke } from './util'

src/main/ts/interface.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/** @module @qiwi/libdefkit */
22
/** */
33

4-
import { StdioOptions } from 'child_process'
4+
import { StdioOptions } from 'node:child_process'
55

66
export type ICmdInvokeOptions = {
77
cmd: string
8-
args?: string[] | Record<string, any>
8+
args?: string[] | TFlags
99
cwd?: string
1010
silent?: boolean
1111
stdio?: StdioOptions
@@ -26,7 +26,8 @@ export type IContext = ICliFlags & {
2626
cache: string
2727
cwd: string
2828
name: string
29-
[key: string]: any
30-
}
29+
} & TFlags
30+
31+
export type TFlags = Record<string, string | boolean | number | Array<string | boolean | number>>
3132

3233
export type IExecPipe = (ctx: IContext) => IContext | void

src/main/ts/libdefkit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** */
33

44
import fs from 'fs-extra'
5-
import { join } from 'path'
5+
import { join } from 'node:path'
66
import { temporaryDirectory } from 'tempy'
77

88
import { pipe as dtsgen } from './dts'

src/main/ts/util.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
/** */
33

44
import chalk from 'chalk'
5-
import cp, { StdioOptions } from 'child_process'
5+
import cp, { StdioOptions } from 'node:child_process'
66
import { findUpSync, pathExistsSync } from 'find-up'
77
import { fileURLToPath } from 'node:url'
8-
import { dirname, resolve } from 'path'
8+
import { dirname, resolve } from 'node:path'
99
import { packageDirectorySync } from 'pkg-dir'
1010

11-
import { ICmdInvokeOptions } from './interface'
11+
import {ICmdInvokeOptions, TFlags} from './interface'
1212

1313
const __dirname = dirname(fileURLToPath(import.meta.url))
1414

@@ -42,17 +42,17 @@ export const invoke = ({
4242
const result = cp.spawnSync(_cmd, _args, { cwd, stdio })
4343

4444
if (result.error || result.status) {
45-
throw result
45+
throw Object.assign(new Error(result.stderr.toString().trim()), result)
4646
}
4747

4848
return result.stdout?.toString().trim() ?? result.stdout
4949
}
5050

5151
const checkValue = (
5252
key: string,
53-
value: any,
54-
omitlist: any[],
55-
picklist: any[],
53+
value: TFlags[string],
54+
omitlist: string[],
55+
picklist: string[],
5656
): boolean =>
5757
value !== 'false' &&
5858
!omitlist.includes(key) &&
@@ -62,7 +62,7 @@ const formatFlag = (key: string): string =>
6262
(key.length === 1 ? '-' : '--') + key
6363

6464
export const formatFlags = (
65-
flags: Record<string, any> = {},
65+
flags: TFlags = {},
6666
picklist: string[] = [],
6767
): string[] =>
6868
Object.keys(flags).reduce<string[]>((memo, key: string) => {
@@ -71,18 +71,20 @@ export const formatFlags = (
7171
const flag = formatFlag(key)
7272

7373
if (checkValue(key, value, omitlist, picklist)) {
74-
memo.push(flag)
74+
[value].flat().forEach((v) => {
75+
memo.push(flag)
7576

76-
if (value !== true) {
77-
memo.push(value)
78-
}
77+
if (v !== true) {
78+
memo.push(v + '')
79+
}
80+
})
7981
}
8082

8183
return memo
8284
}, [])
8385

8486
export const formatArgs = (
85-
args: Record<string, any> | string[] = {},
87+
args: string[] | TFlags = {},
8688
picklist?: string[],
8789
): string[] => (Array.isArray(args) ? args : formatFlags(args, picklist))
8890

@@ -101,5 +103,5 @@ export const getClosestBin = (
101103
cwd: string = process.cwd(),
102104
): string =>
103105
findBin(cmd, cwd) ||
104-
findBin(cmd, packageDirectorySync({ cwd: __dirname })) ||
106+
findBin(cmd, packageDirectorySync({ cwd: __dirname }) as string) ||
105107
cmd

src/test/ts/libdefkit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@jest/globals'
22

33
import { fileURLToPath } from 'node:url'
4-
import { dirname, join, resolve } from 'path'
4+
import { dirname, join, resolve } from 'node:path'
55

66
import { execute } from '../../main/ts'
77

src/test/ts/libdefkit.ts.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`libdefkit #execute returns proper dts bundle 1`] = `
4-
Object {
4+
{
55
"cache": "<cache>",
6-
"customTypings": Array [
6+
"customTypings": [
77
"customTypings/**/*.d.ts",
88
],
99
"cwd": "<cwd>",
@@ -13,7 +13,7 @@ Object {
1313
}
1414
// Generated by dts-bundle v0.7.5
1515
// Dependencies for this module:
16-
// ../../../../../../../../child_process
16+
// ../../../../../../../../node:child_process
1717
1818
declare module '@qiwi/libdefkit/target/es6' {
1919
/** */
@@ -32,10 +32,10 @@ declare module '@qiwi/libdefkit/target/es6/libdefkit' {
3232
3333
declare module '@qiwi/libdefkit/target/es6/interface' {
3434
/** */
35-
import { StdioOptions } from 'child_process';
35+
import { StdioOptions } from 'node:child_process';
3636
export type ICmdInvokeOptions = {
3737
cmd: string;
38-
args?: string[] | Record<string, any>;
38+
args?: string[] | TFlags;
3939
cwd?: string;
4040
silent?: boolean;
4141
stdio?: StdioOptions;
@@ -54,8 +54,8 @@ declare module '@qiwi/libdefkit/target/es6/interface' {
5454
cache: string;
5555
cwd: string;
5656
name: string;
57-
[key: string]: any;
58-
};
57+
} & TFlags;
58+
export type TFlags = Record<string, string | boolean | number | Array<string | boolean | number>>;
5959
export type IExecPipe = (ctx: IContext) => IContext | void;
6060
}
6161
@@ -67,7 +67,7 @@ declare module '@qiwi/libdefkitfake' {
6767
"entry": "@qiwi/libdefkit/target/es6",
6868
"flowOut": "<cwd>/src/test/temp/index.flow.js",
6969
"name": "@qiwi/libdefkit",
70-
"tsconfig": Array [
70+
"tsconfig": [
7171
"tsconfig.es6.json",
7272
],
7373
}

src/test/ts/util.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { jest } from '@jest/globals'
2-
import cp from 'child_process'
2+
import cp from 'node:child_process'
33
import fse from 'fs-extra'
44
import { fileURLToPath } from 'node:url'
5-
import { dirname, resolve } from 'path'
5+
import { dirname, resolve } from 'node:path'
6+
import process from 'node:process'
67
import parseArguments from 'yargs-parser'
78

8-
import { ICmdInvokeOptions } from '../../main/ts/interface'
9+
import {ICmdInvokeOptions, TFlags} from '../../main/ts/interface'
910
import {
1011
formatArgs,
1112
getClosestBin,
@@ -17,12 +18,20 @@ import {
1718
const dotcmd = process.platform === 'win32' ? '.cmd' : ''
1819
const __filename = fileURLToPath(import.meta.url)
1920
const __dirname = dirname(__filename)
21+
const spawnResult = ({status = 0, stdout = '', stderr = ''} = {}): ReturnType<typeof cp.spawnSync> => ({
22+
status,
23+
stdout: Buffer.from(stdout),
24+
stderr: Buffer.from(stderr),
25+
pid: process.pid,
26+
signal: null,
27+
output: [],
28+
})
2029
const fakeExistsSync = jest.fn((cmd) => cmd !== 'not-found' + dotcmd)
2130
const fakeSpawnSync = jest.fn((cmd: string) => {
22-
const results: Record<string, any> = {
23-
error: { status: 1, stderr: 'some error' },
24-
def: { status: 0, stdout: 'foobar' },
25-
null: { status: 0, stdout: null },
31+
const results: Record<string, ReturnType<typeof cp.spawnSync>> = {
32+
error: spawnResult({ status: 1, stderr: 'some error' }),
33+
def: spawnResult({ stdout: 'foobar' }),
34+
empty: spawnResult({ stdout: '' }),
2635
}
2736

2837
return results[cmd.replace(dotcmd, '')] || results.def
@@ -31,10 +40,7 @@ const fakeSpawnSync = jest.fn((cmd: string) => {
3140
beforeAll(() => {
3241
jest.spyOn(cp, 'spawnSync').mockImplementation(fakeSpawnSync)
3342
jest.spyOn(fse, 'existsSync').mockImplementation(fakeExistsSync)
34-
// @ts-ignore
35-
jest.spyOn(process, 'exit').mockImplementation(() => {
36-
/* noop */
37-
})
43+
jest.spyOn(process, 'exit').mockImplementation(() => undefined as never)
3844
})
3945
afterEach(jest.clearAllMocks)
4046
afterAll(jest.resetAllMocks)
@@ -48,7 +54,7 @@ describe('util', () => {
4854
ICmdInvokeOptions,
4955
string?,
5056
(string | null)?,
51-
any?,
57+
(Parameters<typeof spawnResult>[0] | null)?,
5258
][] = [
5359
['uses global cmd ref', { cmd: 'tsc' }, 'tsc'],
5460
[
@@ -65,10 +71,10 @@ describe('util', () => {
6571
'foobar',
6672
],
6773
[
68-
'returns null if stdout is null',
69-
{ cmd: 'null', silent: true },
70-
'null',
71-
null,
74+
'returns "" if stdout is empty',
75+
{ cmd: 'empty', silent: true },
76+
'empty',
77+
'',
7278
],
7379
[
7480
'throws error if result.signal is not equal 0',
@@ -89,7 +95,7 @@ describe('util', () => {
8995
}
9096

9197
if (expectedError) {
92-
expect(() => invoke(opts)).toThrowError(new Error(expectedError))
98+
expect(() => invoke(opts)).toThrowError(new Error(expectedError.stderr?.toString()))
9399
} else {
94100
expect(invoke(opts)).toEqual(
95101
expectedResult === null ? expectedResult : expect.any(String),
@@ -109,7 +115,7 @@ describe('util', () => {
109115

110116
describe('#formatArgs', () => {
111117
it('return proper values', () => {
112-
const cases: [Record<string, any>, string[], string[]][] = [
118+
const cases: [TFlags, string[], string[]][] = [
113119
[{ _: [], '--': [] }, [], []],
114120
[{ foo: 'bar' }, [], ['--foo', 'bar']],
115121
[{ f: true }, [], ['-f']],

0 commit comments

Comments
 (0)