-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add wireit plugin * Add tests for the wireit plugin
- Loading branch information
1 parent
27632ef
commit 45ff78c
Showing
10 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/knip/fixtures/plugins/wireit/apps/exampleconfiguration/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,21 @@ | ||
{ | ||
"name": "@fixtures/wireit-apps-exampleconfiguration", | ||
"version": "*", | ||
"scripts": { | ||
"build": "wireit", | ||
"bundle": "wireit" | ||
}, | ||
"wireit": { | ||
"build": { | ||
"command": "tsc", | ||
"files": ["src/**/*.ts", "tsconfig.json"], | ||
"output": ["lib/**"] | ||
}, | ||
"bundle": { | ||
"command": "rollup -c", | ||
"dependencies": ["build"], | ||
"files": ["rollup.config.json"], | ||
"output": ["dist/bundle.js"] | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/knip/fixtures/plugins/wireit/apps/exampleconfiguration/rollup.config.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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
packages/knip/fixtures/plugins/wireit/apps/exampleconfiguration/tsconfig.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 @@ | ||
{} |
4 changes: 4 additions & 0 deletions
4
packages/knip/fixtures/plugins/wireit/apps/missing/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,4 @@ | ||
{ | ||
"name": "@fixtures/wireit-apps-missing", | ||
"version": "*" | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/knip/fixtures/plugins/wireit/apps/withcommands/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,16 @@ | ||
{ | ||
"name": "@fixtures/wireit-apps-withcommands", | ||
"version": "*", | ||
"scripts": { | ||
"build": "wireit", | ||
"dev": "wireit" | ||
}, | ||
"wireit": { | ||
"build": { | ||
"command": "tsc" | ||
}, | ||
"dev": { | ||
"command": "tsc --watch" | ||
} | ||
} | ||
} |
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,44 @@ | ||
import { timerify } from '../../util/Performance.js'; | ||
import { getDependenciesFromScripts, hasDependency } from '../../util/plugin.js'; | ||
import type { WireitConfig } from './types.js'; | ||
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js'; | ||
|
||
// https://github.com/google/wireit | ||
|
||
export const NAME = 'Wireit'; | ||
|
||
/** @public */ | ||
export const ENABLERS = ['wireit']; | ||
|
||
export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS); | ||
|
||
export const CONFIG_FILE_PATTERNS = ['package.json']; | ||
|
||
/** @public */ | ||
export const ENTRY_FILE_PATTERNS = []; | ||
|
||
/** @public */ | ||
export const PRODUCTION_ENTRY_FILE_PATTERNS = []; | ||
|
||
export const PROJECT_FILE_PATTERNS = []; | ||
|
||
export const PACKAGE_JSON_PATH = 'wireit'; | ||
|
||
const findWireItDependencies: GenericPluginCallback = async (_configFilePath, options) => { | ||
const { cwd, manifest, isProduction } = options; | ||
|
||
if (isProduction) return []; | ||
|
||
const localConfig = manifest[PACKAGE_JSON_PATH] as WireitConfig; | ||
if (!localConfig) return []; | ||
|
||
const scriptArray = Object.values(localConfig) | ||
.map(({ command: script }) => script!) | ||
.filter(script => script !== undefined); | ||
|
||
const scriptDependencies = getDependenciesFromScripts(scriptArray, { cwd, manifest }); | ||
|
||
return scriptDependencies; | ||
}; | ||
|
||
export const findDependencies = timerify(findWireItDependencies); |
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 @@ | ||
export type WireitConfig = Record<string, { command?: string }>; |
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,36 @@ | ||
import assert from 'node:assert/strict'; | ||
import test from 'node:test'; | ||
import * as wireit from '../../src/plugins/wireit/index.js'; | ||
import { resolve, join } from '../../src/util/path.js'; | ||
import { getManifest, pluginConfig as config } from '../helpers/index.js'; | ||
|
||
const wireitPath = resolve('fixtures/plugins/wireit'); | ||
|
||
async function loadWireitConfig(pathSuffix: string): Promise<string[]> { | ||
const cwd = join(wireitPath, pathSuffix); | ||
const manifestFilePath = join(cwd, 'package.json'); | ||
const manifest = getManifest(cwd); | ||
|
||
return wireit.findDependencies(manifestFilePath, { | ||
manifest, | ||
config, | ||
cwd, | ||
isProduction: false, | ||
enabledPlugins: [], | ||
}); | ||
} | ||
|
||
test('Find no dependencies when the wireit configuration is missing', async () => { | ||
const dependencies = await loadWireitConfig('apps/missing'); | ||
assert.deepEqual(dependencies, []); | ||
}); | ||
|
||
test('Find dependencies when the wireit configuration has commands', async () => { | ||
const dependencies = await loadWireitConfig('apps/withcommands'); | ||
assert.deepEqual(dependencies, ['bin:tsc']); | ||
}); | ||
|
||
test('Find dependencies in the example wireit configuration', async () => { | ||
const dependencies = await loadWireitConfig('apps/exampleconfiguration'); | ||
assert.deepEqual(dependencies, ['bin:tsc', 'bin:rollup']); | ||
}); |