-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
packages/knip/fixtures/plugins/simple-git-hooks/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@fixtures/simple-git-hooks", | ||
"devDependencies": { | ||
"simple-git-hooks": "*", | ||
"lint-staged": "*" | ||
}, | ||
"simple-git-hooks": { | ||
"pre-commit": "pnpm lint-staged" | ||
}, | ||
"lint-staged": { | ||
"*": "eslint --fix" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { EnablerPatterns } from '#p/types/config.js'; | ||
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js'; | ||
import { getDependenciesFromScripts, hasDependency } from '#p/util/plugin.js'; | ||
import type { PluginConfig } from './types.js'; | ||
|
||
// https://github.com/toplenboren/simple-git-hooks | ||
|
||
const title = 'simple-git-hooks'; | ||
|
||
const enablers: EnablerPatterns = ['simple-git-hooks']; | ||
|
||
const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers); | ||
|
||
const packageJsonPath = 'simple-git-hooks'; | ||
|
||
const config: string[] = [ | ||
'.simple-git-hooks.{js,cjs}', | ||
'simple-git-hooks.{js,cjs}', | ||
'.simple-git-hooks.json', | ||
'simple-git-hooks.json', | ||
'package.json', | ||
]; | ||
|
||
const resolveConfig: ResolveConfig<PluginConfig> = async (config, options) => { | ||
if (typeof config === 'function') config = config(); | ||
|
||
if (!config) return []; | ||
|
||
const dependencies = new Set<string>(); | ||
|
||
for (const hook of Object.values(config)) { | ||
for (const id of getDependenciesFromScripts(hook, options)) dependencies.add(id); | ||
} | ||
|
||
return Array.from(dependencies); | ||
}; | ||
|
||
export default { | ||
title, | ||
enablers, | ||
isEnabled, | ||
packageJsonPath, | ||
config, | ||
resolveConfig, | ||
} satisfies Plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// https://github.com/toplenboren/simple-git-hooks?tab=readme-ov-file#additional-configuration-options | ||
// { | ||
// "pre-commit": "npx lint-staged", | ||
// "pre-push": "npm run format" | ||
// } | ||
type Config = Record<string, string>; | ||
|
||
export type PluginConfig = Config | (() => Config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { test } from 'bun:test'; | ||
import assert from 'node:assert/strict'; | ||
import { main } from '../../src/index.js'; | ||
import { resolve } from '../../src/util/path.js'; | ||
import baseArguments from '../helpers/baseArguments.js'; | ||
import baseCounters from '../helpers/baseCounters.js'; | ||
|
||
const cwd = resolve('fixtures/plugins/simple-git-hooks'); | ||
|
||
test('Find dependencies with the simple-git-hooks plugin', async () => { | ||
const { issues, counters } = await main({ | ||
...baseArguments, | ||
cwd, | ||
}); | ||
|
||
assert(issues.binaries['package.json']['eslint']); | ||
assert(issues.binaries['package.json']['lint-staged']); | ||
assert(issues.devDependencies['package.json']['simple-git-hooks']); | ||
assert(issues.devDependencies['package.json']['lint-staged']); | ||
|
||
assert.deepEqual(counters, { | ||
...baseCounters, | ||
binaries: 2, | ||
devDependencies: 2, | ||
}); | ||
}); |