Skip to content

Commit

Permalink
Add wireit plugin (#407)
Browse files Browse the repository at this point in the history
* Add wireit plugin

* Add tests for the wireit plugin
  • Loading branch information
mathieucaroff committed Dec 20, 2023
1 parent 27632ef commit 45ff78c
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 0 deletions.
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"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@fixtures/wireit-apps-missing",
"version": "*"
}
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"
}
}
}
1 change: 1 addition & 0 deletions packages/knip/src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const pluginsSchema = z.object({
vite: pluginSchema,
vitest: pluginSchema,
webpack: pluginSchema,
wireit: pluginSchema,
});

const baseWorkspaceConfigurationSchema = z.object({
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export * as typescript from './typescript/index.js';
export * as vite from './vite/index.js';
export * as vitest from './vitest/index.js';
export * as webpack from './webpack/index.js';
export * as wireit from './wireit/index.js';
44 changes: 44 additions & 0 deletions packages/knip/src/plugins/wireit/index.ts
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);
1 change: 1 addition & 0 deletions packages/knip/src/plugins/wireit/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type WireitConfig = Record<string, { command?: string }>;
36 changes: 36 additions & 0 deletions packages/knip/test/plugins/wireit.test.ts
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']);
});

0 comments on commit 45ff78c

Please sign in to comment.