Skip to content

Commit

Permalink
Add wireit to schema.json, improve consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Dec 27, 2023
1 parent 18dea2a commit 37e58a8
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@fixtures/wireit-apps-example-configuration",
"version": "1.0.0",
"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 @@
{}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@fixtures/wireit-apps-missing",
"version": "*"
"version": "1.0.0"
}
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "@fixtures/wireit-apps-withcommands",
"version": "*",
"name": "@fixtures/wireit",
"version": "1.0.0",
"workspaces": [
"apps/*"
],
"scripts": {
"build": "wireit",
"dev": "wireit"
},
"devDependencies": {
"wireit": "*"
},
"wireit": {
"build": {
"command": "tsc"
Expand Down
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@
"webpack": {
"title": "Webpack plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/webpack/README.md)",
"$ref": "#/definitions/plugin"
},
"wireit": {
"title": "Wireit plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/wireit/README.md)",
"$ref": "#/definitions/plugin"
}
}
},
Expand Down
6 changes: 2 additions & 4 deletions packages/knip/src/plugins/wireit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ const findWireItDependencies: GenericPluginCallback = async (_configFilePath, op
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 scripts = Object.values(localConfig).flatMap(({ command: script }) => (script ? [script] : []));

const scriptDependencies = getDependenciesFromScripts(scriptArray, { cwd, manifest });
const scriptDependencies = getDependenciesFromScripts(scripts, { cwd, manifest });

return scriptDependencies;
};
Expand Down
53 changes: 35 additions & 18 deletions packages/knip/test/plugins/wireit.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.js';
import * as wireit from '../../src/plugins/wireit/index.js';
import { resolve, join } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.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: [],
});
}
const cwd = resolve('fixtures/plugins/wireit');
const options = { cwd, config, isProduction: false, enabledPlugins: [] };

test('Find no dependencies when the wireit configuration is missing', async () => {
const dependencies = await loadWireitConfig('apps/missing');
const dir = join(cwd, 'apps/missing');
const configFilePath = join(dir, 'package.json');
const manifest = getManifest(dir);
const dependencies = await wireit.findDependencies(configFilePath, { ...options, manifest, cwd: dir });
assert.deepEqual(dependencies, []);
});

test('Find dependencies when the wireit configuration has commands', async () => {
const dependencies = await loadWireitConfig('apps/withcommands');
const configFilePath = join(cwd, 'package.json');
const manifest = getManifest(cwd);
const dependencies = await wireit.findDependencies(configFilePath, { ...options, manifest });
assert.deepEqual(dependencies, ['bin:tsc']);
});

test('Find dependencies in the example wireit configuration', async () => {
const dependencies = await loadWireitConfig('apps/exampleconfiguration');
const dir = join(cwd, 'apps/example-configuration');
const configFilePath = join(dir, 'package.json');
const manifest = getManifest(dir);
const dependencies = await wireit.findDependencies(configFilePath, { ...options, manifest, cwd: dir });
assert.deepEqual(dependencies, ['bin:tsc', 'bin:rollup']);
});

test('Find dependencies in wireit configuration', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.binaries['package.json']['tsc']);
assert(issues.binaries['apps/example-configuration/package.json']['rollup']);
assert(issues.binaries['apps/example-configuration/package.json']['tsc']);

assert.deepEqual(counters, {
...baseCounters,
binaries: 3,
devDependencies: 0,
processed: 0,
total: 0,
});
});

0 comments on commit 37e58a8

Please sign in to comment.