Skip to content

Commit

Permalink
feat: migrate code base to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 16, 2024
1 parent 0ae06f1 commit 2f57f1b
Show file tree
Hide file tree
Showing 35 changed files with 13,113 additions and 4,670 deletions.
12 changes: 0 additions & 12 deletions .babelrc

This file was deleted.

15 changes: 0 additions & 15 deletions .eslintrc.yml

This file was deleted.

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')
2 changes: 2 additions & 0 deletions __mocks__/execa.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const mockStream = () => ({
const mockExeca = jest.fn().mockReturnValue({
stdout: mockStream(),
stderr: mockStream(),
// eslint-disable-next-line no-empty-function
kill: () => {},
})

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

Expand Down
3 changes: 1 addition & 2 deletions __mocks__/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const prettierMock = {
const ext = path.extname(file)
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' }
} else {
return { ignored: false, inferredParser: null }
}
return { ignored: false, inferredParser: null }
}),
},
}
Expand Down
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')
73 changes: 58 additions & 15 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",
"build": "run-p 'build:*'",
"build:r": "r -f esm -o dist",
"build:tsc": "tsc -b",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc --noEmit",
"prepare": "simple-git-hooks",
"pretty-quick": "./bin/pretty-quick.js",
"release": "yarn build && clean-pkg-json && changeset publish",
"test": "jest"
},
Expand All @@ -50,32 +55,70 @@
"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",
"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"
},
"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",
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'
73 changes: 73 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env node

import chalk from 'chalk'
import mri from 'mri'

import prettyQuick from '.'

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

const prettyQuickResult = prettyQuick(process.cwd(), {
...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.includes('PARTIALLY_STAGED_FILE')) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
)
}
if (prettyQuickResult.errors.includes('BAIL_ON_WRITE')) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
)
}
if (prettyQuickResult.errors.includes('CHECK_FAILED')) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
)
}
// eslint-disable-next-line n/no-process-exit
process.exit(1) // ensure git hooks abort
}
14 changes: 0 additions & 14 deletions src/createIgnorer.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/createIgnorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable unicorn/filename-case */

import fs from 'fs'
import path from 'path'

import ignore from 'ignore'

export default (directory: string, filename = '.prettierignore') => {
const file = path.join(directory, filename)

if (fs.existsSync(file)) {
const text = fs.readFileSync(file, 'utf8')
const filter = ignore().add(text).createFilter()
return (filepath: string) => filter(path.join(filepath))
}

return () => true
}
9 changes: 6 additions & 3 deletions src/createMatcher.js → src/createMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable unicorn/filename-case */

import path from 'path'

import multimatch from 'multimatch'
const path = require('path')

export default pattern => {
export default (pattern: string[] | string | undefined) => {
// Match everything if no pattern was given
if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
return () => true
}
const patterns = Array.isArray(pattern) ? pattern : [pattern]
return file =>
return (file: string) =>
multimatch(path.normalize(file), patterns, { dot: true }).length > 0
}
Loading

0 comments on commit 2f57f1b

Please sign in to comment.