Skip to content

Commit 4f5a345

Browse files
authored
feat: migrate code base to TypeScript (#175)
1 parent 0ae06f1 commit 4f5a345

37 files changed

+13231
-4696
lines changed

.babelrc

-12
This file was deleted.

.changeset/angry-kiwis-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"pretty-quick": minor
3+
---
4+
5+
feat: migrate code base to TypeScript

.eslintrc.yml

-15
This file was deleted.

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ jobs:
3131
- name: Install Dependencies
3232
run: yarn --immutable
3333

34+
- name: Build and Test
35+
run: yarn run-s build test
36+
3437
- name: Lint
3538
run: yarn lint
3639
if: matrix.node == 18
3740
env:
3841
EFF_NO_LINK_RULES: true
3942
PARSER_NO_WATCH: true
4043

41-
- name: Build and Test
42-
run: yarn run-s build test
43-
4444
- name: Codecov
4545
uses: codecov/codecov-action@v3

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
dist
22
node_modules
3+
test-dist
34
*.log
45
/.yarn/*
56
!/.yarn/plugins

.lintstagedrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@1stg/lint-staged/tsc')

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage
22
dist
3+
test-dist
34
/.yarn

.simple-git-hooks.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@1stg/simple-git-hooks')

__mocks__/execa.js renamed to __mocks__/execa.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ const mockStream = () => ({
88
const mockExeca = jest.fn().mockReturnValue({
99
stdout: mockStream(),
1010
stderr: mockStream(),
11+
// eslint-disable-next-line @typescript-eslint/no-empty-function
1112
kill: () => {},
1213
})
1314

1415
const mockExecaSync = jest.fn().mockReturnValue({
1516
stdout: '',
1617
stderr: '',
18+
// eslint-disable-next-line @typescript-eslint/no-empty-function
1719
kill: () => {},
1820
})
1921

20-
module.exports = mockExeca
21-
module.exports.sync = mockExecaSync
22+
export = mockExeca
23+
24+
// @ts-expect-error -- intended
25+
mockExeca.sync = mockExecaSync

__mocks__/prettier.js

-20
This file was deleted.

__mocks__/prettier.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'path'
2+
3+
export = {
4+
format: jest.fn((input: string) => 'formatted:' + input),
5+
resolveConfig: {
6+
sync: jest.fn((file: string) => ({ file })),
7+
},
8+
getFileInfo: {
9+
sync: jest.fn((file: string) => {
10+
const ext = path.extname(file)
11+
if (ext === '.js' || ext === '.md') {
12+
return { ignored: false, inferredParser: 'babel' }
13+
}
14+
return { ignored: false, inferredParser: null }
15+
}),
16+
},
17+
}

bin/pretty-quick.js

100755100644
+2-75
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,3 @@
1-
#!/usr/bin/env node
1+
// ! warning: this file is only here for compatibility, you should change to use `dist/cli.js` directly instead
22

3-
'use strict'
4-
5-
const chalk = require('chalk')
6-
const mri = require('mri')
7-
8-
const prettyQuick = require('..').default
9-
10-
const args = mri(process.argv.slice(2), {
11-
alias: {
12-
'resolve-config': 'resolveConfig',
13-
'ignore-path': 'ignorePath',
14-
},
15-
})
16-
17-
const prettyQuickResult = prettyQuick(
18-
process.cwd(),
19-
Object.assign({}, args, {
20-
onFoundSinceRevision: (scm, revision) => {
21-
console.log(
22-
`🔍 Finding changed files since ${chalk.bold(
23-
scm,
24-
)} revision ${chalk.bold(revision)}.`,
25-
)
26-
},
27-
28-
onFoundChangedFiles: changedFiles => {
29-
console.log(
30-
`🎯 Found ${chalk.bold(changedFiles.length)} changed ${
31-
changedFiles.length === 1 ? 'file' : 'files'
32-
}.`,
33-
)
34-
},
35-
36-
onPartiallyStagedFile: file => {
37-
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`)
38-
},
39-
40-
onWriteFile: file => {
41-
console.log(`✍️ Fixing up ${chalk.bold(file)}.`)
42-
},
43-
44-
onCheckFile: (file, isFormatted) => {
45-
if (!isFormatted) {
46-
console.log(`⛔️ Check failed: ${chalk.bold(file)}`)
47-
}
48-
},
49-
50-
onExamineFile: file => {
51-
console.log(`🔍 Examining ${chalk.bold(file)}.`)
52-
},
53-
}),
54-
)
55-
56-
if (prettyQuickResult.success) {
57-
console.log('✅ Everything is awesome!')
58-
} else {
59-
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
60-
console.log(
61-
'✗ Partially staged files were fixed up.' +
62-
` ${chalk.bold('Please update stage before committing')}.`,
63-
)
64-
}
65-
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
66-
console.log(
67-
'✗ File had to be prettified and prettyQuick was set to bail mode.',
68-
)
69-
}
70-
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
71-
console.log(
72-
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
73-
)
74-
}
75-
process.exit(1) // ensure git hooks abort
76-
}
3+
module.exports = require('../dist/cli')

package.json

+62-17
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
"engines": {
1313
"node": ">=10.13"
1414
},
15-
"bin": "./bin/pretty-quick.js",
16-
"main": "./dist",
15+
"bin": "dist/cli.js",
16+
"main": "dist/index.js",
17+
"module": "dist/index.esm.mjs",
18+
"types": "dist/index.d.ts",
1719
"files": [
1820
"bin",
1921
"dist",
@@ -31,10 +33,13 @@
3133
"precommit"
3234
],
3335
"scripts": {
34-
"build": "babel src -d dist --copy-files --no-copy-ignored --ignore '**/__tests__/*.js'",
35-
"lint": "eslint . --ignore-path=.gitignore",
36-
"prepare": "simple-git-hooks",
37-
"pretty-quick": "./bin/pretty-quick.js",
36+
"build": "run-p 'build:*'",
37+
"build:r": "r -f esm -o dist || exit 0",
38+
"build:tsc": "tsc -b",
39+
"lint": "run-p 'lint:*'",
40+
"lint:es": "eslint . --cache",
41+
"lint:tsc": "tsc --noEmit",
42+
"prepare": "patch-package && simple-git-hooks",
3843
"release": "yarn build && clean-pkg-json && changeset publish",
3944
"test": "jest"
4045
},
@@ -50,35 +55,75 @@
5055
"multimatch": "^5.0.0"
5156
},
5257
"devDependencies": {
53-
"@1stg/prettier-config": "3.9.2",
54-
"@babel/cli": "^7.23.4",
55-
"@babel/core": "^7.23.7",
56-
"@babel/preset-env": "^7.23.8",
58+
"@1stg/lib-config": "^12.0.0",
5759
"@changesets/changelog-github": "^0.5.0",
5860
"@changesets/cli": "^2.27.1",
61+
"@commitlint/cli": "^17.8.1",
62+
"@pkgr/rollup": "^4.1.1",
63+
"@total-typescript/ts-reset": "^0.5.1",
64+
"@types/jest": "^29.5.11",
65+
"@types/mock-fs": "^4.13.4",
66+
"@unts/patch-package": "^8.0.0",
5967
"clean-pkg-json": "^1.2.0",
6068
"eslint": "^8.56.0",
6169
"eslint-config-prettier": "^9.1.0",
6270
"eslint-plugin-jest": "^27.6.3",
6371
"eslint-plugin-prettier": "^4.2.1",
6472
"jest": "^26.6.3",
73+
"lint-staged": "^13.2.2",
6574
"mock-fs": "^4.14.0",
6675
"npm-run-all": "^4.1.5",
67-
"prettier": "2.8.8",
76+
"prettier": "^2.8.8",
77+
"pretty-quick": "link:.",
6878
"simple-git-hooks": "^2.9.0",
6979
"size-limit": "^11.0.1",
70-
"size-limit-preset-node-lib": "^0.3.0"
80+
"size-limit-preset-node-lib": "^0.3.0",
81+
"ts-jest": "^26.5.6",
82+
"ts-node": "^10.9.2",
83+
"typescript": "~4.4.4"
7184
},
85+
"resolutions": {
86+
"typescript": "~4.4.4",
87+
"yaml": "^1.10.2"
88+
},
89+
"commitlint": {
90+
"extends": "@1stg"
91+
},
92+
"eslintConfig": {
93+
"extends": "@1stg",
94+
"rules": {
95+
"unicorn/prefer-node-protocol": "off"
96+
},
97+
"overrides": [
98+
{
99+
"files": "__mocks__/*.*",
100+
"env": {
101+
"jest": true
102+
}
103+
}
104+
]
105+
},
106+
"eslintIgnore": [
107+
"coverage",
108+
"dist",
109+
"test-dist",
110+
"!/.*.js"
111+
],
72112
"jest": {
73-
"collectCoverage": true
113+
"preset": "ts-jest",
114+
"testMatch": [
115+
"<rootDir>/test/*.spec.ts"
116+
],
117+
"collectCoverage": true,
118+
"moduleNameMapper": {
119+
"^pretty-quick$": "<rootDir>/src",
120+
"^pretty-quick/(.+)$": "<rootDir>/src/$1"
121+
}
74122
},
75123
"prettier": "@1stg/prettier-config",
76-
"simple-git-hooks": {
77-
"pre-commit": "./bin/pretty-quick.js --staged"
78-
},
79124
"size-limit": [
80125
{
81-
"path": "src/index.js",
126+
"path": "src/index.ts",
82127
"limit": "1KB"
83128
}
84129
]

shim.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@total-typescript/ts-reset'

0 commit comments

Comments
 (0)