Skip to content

Commit

Permalink
feat: migrate code base to TypeScript (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jan 16, 2024
1 parent 0ae06f1 commit 4f5a345
Show file tree
Hide file tree
Showing 37 changed files with 13,231 additions and 4,696 deletions.
12 changes: 0 additions & 12 deletions .babelrc

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/angry-kiwis-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pretty-quick": minor
---

feat: migrate code base to TypeScript
15 changes: 0 additions & 15 deletions .eslintrc.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ jobs:
- name: Install Dependencies
run: yarn --immutable

- name: Build and Test
run: yarn run-s build test

- name: Lint
run: yarn lint
if: matrix.node == 18
env:
EFF_NO_LINK_RULES: true
PARSER_NO_WATCH: true

- name: Build and Test
run: yarn run-s build test

- name: Codecov
uses: codecov/codecov-action@v3
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dist
node_modules
test-dist
*.log
/.yarn/*
!/.yarn/plugins
Expand Down
1 change: 1 addition & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/lint-staged/tsc')
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
dist
test-dist
/.yarn
1 change: 1 addition & 0 deletions .simple-git-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/simple-git-hooks')
8 changes: 6 additions & 2 deletions __mocks__/execa.js → __mocks__/execa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ const mockStream = () => ({
const mockExeca = jest.fn().mockReturnValue({
stdout: mockStream(),
stderr: mockStream(),
// eslint-disable-next-line @typescript-eslint/no-empty-function
kill: () => {},
})

const mockExecaSync = jest.fn().mockReturnValue({
stdout: '',
stderr: '',
// eslint-disable-next-line @typescript-eslint/no-empty-function
kill: () => {},
})

module.exports = mockExeca
module.exports.sync = mockExecaSync
export = mockExeca

// @ts-expect-error -- intended
mockExeca.sync = mockExecaSync
20 changes: 0 additions & 20 deletions __mocks__/prettier.js

This file was deleted.

17 changes: 17 additions & 0 deletions __mocks__/prettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path'

export = {
format: jest.fn((input: string) => 'formatted:' + input),
resolveConfig: {
sync: jest.fn((file: string) => ({ file })),
},
getFileInfo: {
sync: jest.fn((file: string) => {
const ext = path.extname(file)
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' }
}
return { ignored: false, inferredParser: null }
}),
},
}
77 changes: 2 additions & 75 deletions bin/pretty-quick.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,76 +1,3 @@
#!/usr/bin/env node
// ! warning: this file is only here for compatibility, you should change to use `dist/cli.js` directly instead

'use strict'

const chalk = require('chalk')
const mri = require('mri')

const prettyQuick = require('..').default

const args = mri(process.argv.slice(2), {
alias: {
'resolve-config': 'resolveConfig',
'ignore-path': 'ignorePath',
},
})

const prettyQuickResult = prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm,
)} revision ${chalk.bold(revision)}.`,
)
},

onFoundChangedFiles: changedFiles => {
console.log(
`🎯 Found ${chalk.bold(changedFiles.length)} changed ${
changedFiles.length === 1 ? 'file' : 'files'
}.`,
)
},

onPartiallyStagedFile: file => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`)
},

onWriteFile: file => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`)
},

onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`)
}
},

onExamineFile: file => {
console.log(`🔍 Examining ${chalk.bold(file)}.`)
},
}),
)

if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!')
} else {
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
)
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
)
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
)
}
process.exit(1) // ensure git hooks abort
}
module.exports = require('../dist/cli')
79 changes: 62 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"engines": {
"node": ">=10.13"
},
"bin": "./bin/pretty-quick.js",
"main": "./dist",
"bin": "dist/cli.js",
"main": "dist/index.js",
"module": "dist/index.esm.mjs",
"types": "dist/index.d.ts",
"files": [
"bin",
"dist",
Expand All @@ -31,10 +33,13 @@
"precommit"
],
"scripts": {
"build": "babel src -d dist --copy-files --no-copy-ignored --ignore '**/__tests__/*.js'",
"lint": "eslint . --ignore-path=.gitignore",
"prepare": "simple-git-hooks",
"pretty-quick": "./bin/pretty-quick.js",
"build": "run-p 'build:*'",
"build:r": "r -f esm -o dist || exit 0",
"build:tsc": "tsc -b",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc --noEmit",
"prepare": "patch-package && simple-git-hooks",
"release": "yarn build && clean-pkg-json && changeset publish",
"test": "jest"
},
Expand All @@ -50,35 +55,75 @@
"multimatch": "^5.0.0"
},
"devDependencies": {
"@1stg/prettier-config": "3.9.2",
"@babel/cli": "^7.23.4",
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@1stg/lib-config": "^12.0.0",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@commitlint/cli": "^17.8.1",
"@pkgr/rollup": "^4.1.1",
"@total-typescript/ts-reset": "^0.5.1",
"@types/jest": "^29.5.11",
"@types/mock-fs": "^4.13.4",
"@unts/patch-package": "^8.0.0",
"clean-pkg-json": "^1.2.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^26.6.3",
"lint-staged": "^13.2.2",
"mock-fs": "^4.14.0",
"npm-run-all": "^4.1.5",
"prettier": "2.8.8",
"prettier": "^2.8.8",
"pretty-quick": "link:.",
"simple-git-hooks": "^2.9.0",
"size-limit": "^11.0.1",
"size-limit-preset-node-lib": "^0.3.0"
"size-limit-preset-node-lib": "^0.3.0",
"ts-jest": "^26.5.6",
"ts-node": "^10.9.2",
"typescript": "~4.4.4"
},
"resolutions": {
"typescript": "~4.4.4",
"yaml": "^1.10.2"
},
"commitlint": {
"extends": "@1stg"
},
"eslintConfig": {
"extends": "@1stg",
"rules": {
"unicorn/prefer-node-protocol": "off"
},
"overrides": [
{
"files": "__mocks__/*.*",
"env": {
"jest": true
}
}
]
},
"eslintIgnore": [
"coverage",
"dist",
"test-dist",
"!/.*.js"
],
"jest": {
"collectCoverage": true
"preset": "ts-jest",
"testMatch": [
"<rootDir>/test/*.spec.ts"
],
"collectCoverage": true,
"moduleNameMapper": {
"^pretty-quick$": "<rootDir>/src",
"^pretty-quick/(.+)$": "<rootDir>/src/$1"
}
},
"prettier": "@1stg/prettier-config",
"simple-git-hooks": {
"pre-commit": "./bin/pretty-quick.js --staged"
},
"size-limit": [
{
"path": "src/index.js",
"path": "src/index.ts",
"limit": "1KB"
}
]
Expand Down
1 change: 1 addition & 0 deletions shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@total-typescript/ts-reset'
Loading

0 comments on commit 4f5a345

Please sign in to comment.