Skip to content

Commit

Permalink
feat: support prettier v3
Browse files Browse the repository at this point in the history
close #164
  • Loading branch information
JounQin committed Jan 17, 2024
1 parent ed34de4 commit 1b041f9
Show file tree
Hide file tree
Showing 22 changed files with 4,763 additions and 5,114 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist
lib
node_modules
*.log
/.yarn/*
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage
dist
lib
/.yarn
541 changes: 541 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

55 changes: 0 additions & 55 deletions .yarn/releases/yarn-2.4.3.cjs

This file was deleted.

874 changes: 874 additions & 0 deletions .yarn/releases/yarn-3.6.4.cjs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ nodeLinker: node-modules

plugins:
- path: .yarn/plugins/plugin-prepare-lifecycle.cjs
spec: 'https://github.com/un-es/yarn-plugin-prepare-lifecycle/releases/download/v0.0.1/index.js'
spec: "https://github.com/un-es/yarn-plugin-prepare-lifecycle/releases/download/v0.0.1/index.js"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-2.4.3.cjs
yarnPath: .yarn/releases/yarn-3.6.4.cjs
28 changes: 13 additions & 15 deletions __mocks__/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/* eslint-disable @typescript-eslint/require-await */

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 }
}),
},
}
export const format = jest.fn(async (input: string) => 'formatted:' + input)

export const resolveConfig = jest.fn(async (file: string) => ({ file }))

export const getFileInfo = jest.fn(async (file: string) => {
const ext = path.extname(file)
return {
ignored: false,
inferredParser: ext === '.js' || ext === '.md' ? 'babel' : null,
}
})
3 changes: 0 additions & 3 deletions bin/pretty-quick.js

This file was deleted.

53 changes: 31 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@
"JounQin (https://www.1stG.me) <[email protected]>"
],
"license": "MIT",
"packageManager": "yarn@2.4.3",
"packageManager": "yarn@3.6.4",
"engines": {
"node": ">=10.13"
"node": ">=14.8.0"
},
"bin": "dist/cli.js",
"main": "dist/index.js",
"module": "dist/index.esm.mjs",
"types": "dist/index.d.ts",
"bin": "lib/cli.mjs",
"main": "lib/index.js",
"module": "lib/index.esm.mjs",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.js",
"default": "./lib/index.esm.mjs"
},
"./*": "./lib/*.js",
"./package.json": "./package.json"
},
"types": "lib/index.d.ts",
"files": [
"bin",
"dist",
"img"
"img",
"lib"
],
"keywords": [
"git",
Expand All @@ -34,7 +43,7 @@
],
"scripts": {
"build": "run-p 'build:*'",
"build:r": "r -f esm -o dist || exit 0",
"build:r": "r -f esm || exit 0",
"build:tsc": "tsc -b",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
Expand All @@ -44,12 +53,12 @@
"test": "jest"
},
"peerDependencies": {
"prettier": "^2.0.0"
"prettier": "^3.0.0"
},
"dependencies": {
"chalk": "^4.1.2",
"execa": "^4.1.0",
"find-up": "^4.1.0",
"execa": "^5.1.1",
"find-up": "^5.0.0",
"ignore": "^5.3.0",
"mri": "^1.2.0",
"multimatch": "^5.0.0",
Expand All @@ -67,25 +76,24 @@
"@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",
"jest": "^29.7.0",
"lint-staged": "^13.2.2",
"mock-fs": "^4.14.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"prettier": "^3.2.2",
"pretty-quick": "link:.",
"simple-git-hooks": "^2.9.0",
"size-limit": "^11.0.1",
"size-limit-preset-node-lib": "^0.3.0",
"ts-jest": "^26.5.6",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"typescript": "~4.4.4"
"typescript": "^5.3.3"
},
"resolutions": {
"typescript": "~4.4.4",
"yaml": "^1.10.2"
"@1stg/prettier-config": "^4.0.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.0"
},
"commitlint": {
"extends": "@1stg"
Expand All @@ -106,7 +114,7 @@
},
"eslintIgnore": [
"coverage",
"dist",
"lib",
"!/.*.js"
],
"jest": {
Expand All @@ -116,6 +124,7 @@
],
"collectCoverage": true,
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1",
"^pretty-quick$": "<rootDir>/src",
"^pretty-quick/(.+)$": "<rootDir>/src/$1"
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts → src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import chalk from 'chalk'
import mri from 'mri'

import prettyQuick from '.'
import prettyQuick from './index.js'

const args = mri(process.argv.slice(2), {
alias: {
Expand All @@ -12,7 +12,7 @@ const args = mri(process.argv.slice(2), {
},
})

const prettyQuickResult = prettyQuick(process.cwd(), {
const prettyQuickResult = await prettyQuick(process.cwd(), {
...args,
onFoundSinceRevision: (scm, revision) => {
console.log(
Expand Down
57 changes: 31 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import createIgnorer from './createIgnorer'
import createMatcher from './createMatcher'
import isSupportedExtension from './isSupportedExtension'
import processFiles from './processFiles'
import scms from './scms'
import { PrettyQuickOptions } from './types'
import createIgnorer from './createIgnorer.js'
import createMatcher from './createMatcher.js'
import isSupportedExtension from './isSupportedExtension.js'
import processFiles from './processFiles.js'
import scms from './scms/index.js'
import { PrettyQuickOptions } from './types.js'
import { filterAsync } from './utils.js'

export = (
export = async (
currentDirectory: string,
{
config,
Expand Down Expand Up @@ -44,33 +45,37 @@ export = (
? () => true
: createIgnorer(currentDirectory, ignorePath)

const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer)
.filter(isSupportedExtension(resolveConfig))
const patternMatcher = createMatcher(pattern)

const isFileSupportedExtension = isSupportedExtension(resolveConfig)

const changedFiles = await filterAsync(
scm
.getChangedFiles(directory, revision, staged)
.filter(patternMatcher)
.filter(rootIgnorer)
.filter(cwdIgnorer),
isFileSupportedExtension,
)

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(
directory,
// @ts-expect-error -- TODO: check
revision,
)
.filter(isSupportedExtension(resolveConfig))
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer)
? await filterAsync(
scm
.getUnstagedChangedFiles(directory)
.filter(patternMatcher)
.filter(rootIgnorer)
.filter(cwdIgnorer),
isFileSupportedExtension,
)
: []

const wasFullyStaged = (f: string) => !unstagedFiles.includes(f)
const wasFullyStaged = (file: string) => !unstagedFiles.includes(file)

onFoundChangedFiles?.(changedFiles)

const failReasons = new Set()
const failReasons = new Set<string>()

processFiles(directory, changedFiles, {
await processFiles(directory, changedFiles, {
check,
config,
onWriteFile(file: string) {
Expand Down
14 changes: 9 additions & 5 deletions src/isSupportedExtension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-disable unicorn/filename-case */

import * as prettier from 'prettier'
import {
type FileInfoOptions,
getFileInfo,
resolveConfig as prettierResolveConfig,
} from 'prettier'

export default (resolveConfig?: boolean) => (file: string) => {
const config = prettier.resolveConfig.sync(file, {
export default (resolveConfig?: boolean) => async (file: string) => {
const config = (await prettierResolveConfig(file, {
editorconfig: true,
}) as prettier.FileInfoOptions
const fileInfo = prettier.getFileInfo.sync(file, {
})) as FileInfoOptions
const fileInfo = await getFileInfo(file, {
resolveConfig,
...config,
})
Expand Down
14 changes: 7 additions & 7 deletions src/processFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import fs from 'fs'
import path from 'path'

import prettier from 'prettier'
import { format, check as prettierCheck, resolveConfig } from 'prettier'

import { PrettyQuickOptions } from './types'
import type { PrettyQuickOptions } from './types.js'

export default (
export default async (
directory: string,
files: string[],
{
Expand All @@ -22,21 +22,21 @@ export default (
onExamineFile?.(relative)
const file = path.join(directory, relative)
const options = {
...prettier.resolveConfig.sync(file, {
...(await resolveConfig(file, {
config,
editorconfig: true,
}),
})),
filepath: file,
}
const input = fs.readFileSync(file, 'utf8')

if (check) {
const isFormatted = prettier.check(input, options)
const isFormatted = await prettierCheck(input, options)
onCheckFile?.(relative, isFormatted)
continue
}

const output = prettier.format(input, options)
const output = await format(input, options)

if (output !== input) {
fs.writeFileSync(file, output)
Expand Down
8 changes: 4 additions & 4 deletions src/scms/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as gitScm from './git'
import * as hgScm from './hg'
import * as gitScm from './git.js'
import * as hgScm from './hg.js'

const scms = [gitScm, hgScm]
const scmList = [gitScm, hgScm]

export default (directory: string) => {
for (const scm of scms) {
for (const scm of scmList) {
const rootDirectory = scm.detect(directory)
if (rootDirectory) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../tsconfig.lib",
"compilerOptions": {
"composite": true,
"outDir": "../dist"
"outDir": "../lib"
},
"include": ["../shim.d.ts", "."]
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const filterAsync = async <T>(
items: T[],
predicate: (item: T) => Promise<boolean>,
) => {
const boolItems = await Promise.all(items.map(predicate))
return items.filter((_, i) => boolItems[i])
}
Loading

0 comments on commit 1b041f9

Please sign in to comment.