diff --git a/README.md b/README.md index a65f012c4..407084d61 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,19 @@ const config: KnipConfig = { export default config; ``` +And if you need, you can also expose an (async) function if you need, like so: + +```ts +import type { KnipConfig } from 'knip'; + +const config = async (): Promise => ({ + entry: ['src/index.ts'], + project: ['src/**/*.ts'], +}); + +export default config; +``` + Use `--config path/to/knip.config.json` for a different path. ### Let's Go! diff --git a/fixtures/config/js-async/dangling.js b/fixtures/config/js-async/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/js-async/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/js-async/index.js b/fixtures/config/js-async/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/js-async/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/js-async/knip.js b/fixtures/config/js-async/knip.js new file mode 100644 index 000000000..977f2910e --- /dev/null +++ b/fixtures/config/js-async/knip.js @@ -0,0 +1,3 @@ +module.exports = async () => ({ + ignore: ['dangling.js'] +}); \ No newline at end of file diff --git a/fixtures/config/js-async/my-namespace.js b/fixtures/config/js-async/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/js-async/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/js-async/package.json b/fixtures/config/js-async/package.json new file mode 100644 index 000000000..bbc0ee528 --- /dev/null +++ b/fixtures/config/js-async/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-js-async" +} diff --git a/fixtures/config/js-flat/dangling.js b/fixtures/config/js-flat/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/js-flat/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/js-flat/index.js b/fixtures/config/js-flat/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/js-flat/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/js-flat/knip.js b/fixtures/config/js-flat/knip.js new file mode 100644 index 000000000..2255bd75d --- /dev/null +++ b/fixtures/config/js-flat/knip.js @@ -0,0 +1,3 @@ +module.exports = { + ignore: ['dangling.js'] +} \ No newline at end of file diff --git a/fixtures/config/js-flat/my-namespace.js b/fixtures/config/js-flat/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/js-flat/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/js-flat/package.json b/fixtures/config/js-flat/package.json new file mode 100644 index 000000000..bce57da7d --- /dev/null +++ b/fixtures/config/js-flat/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-js-flat" +} diff --git a/fixtures/config/json/dangling.js b/fixtures/config/json/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/json/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/json/index.js b/fixtures/config/json/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/json/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/json/knip.json b/fixtures/config/json/knip.json new file mode 100644 index 000000000..5ba6e3a51 --- /dev/null +++ b/fixtures/config/json/knip.json @@ -0,0 +1,3 @@ +{ + "ignore": ["dangling.js"] +} \ No newline at end of file diff --git a/fixtures/config/json/my-namespace.js b/fixtures/config/json/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/json/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/json/package.json b/fixtures/config/json/package.json new file mode 100644 index 000000000..3a7564325 --- /dev/null +++ b/fixtures/config/json/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-json" +} diff --git a/fixtures/config/mjs-async/dangling.js b/fixtures/config/mjs-async/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/mjs-async/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/mjs-async/index.js b/fixtures/config/mjs-async/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/mjs-async/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/mjs-async/knip.mjs b/fixtures/config/mjs-async/knip.mjs new file mode 100644 index 000000000..b655d548c --- /dev/null +++ b/fixtures/config/mjs-async/knip.mjs @@ -0,0 +1,5 @@ +const config = async () => ({ + ignore: ['dangling.js'], +}); + +export default config; diff --git a/fixtures/config/mjs-async/my-namespace.js b/fixtures/config/mjs-async/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/mjs-async/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/mjs-async/package.json b/fixtures/config/mjs-async/package.json new file mode 100644 index 000000000..3ed2711e6 --- /dev/null +++ b/fixtures/config/mjs-async/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-mjs-async" +} diff --git a/fixtures/config/mjs-flat/dangling.js b/fixtures/config/mjs-flat/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/mjs-flat/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/mjs-flat/index.js b/fixtures/config/mjs-flat/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/mjs-flat/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/mjs-flat/knip.mjs b/fixtures/config/mjs-flat/knip.mjs new file mode 100644 index 000000000..356e0177a --- /dev/null +++ b/fixtures/config/mjs-flat/knip.mjs @@ -0,0 +1,5 @@ +const config = { + ignore: ['dangling.js'], +}; + +export default config; diff --git a/fixtures/config/mjs-flat/my-namespace.js b/fixtures/config/mjs-flat/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/mjs-flat/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/mjs-flat/package.json b/fixtures/config/mjs-flat/package.json new file mode 100644 index 000000000..ee8bcdf52 --- /dev/null +++ b/fixtures/config/mjs-flat/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-mjs-flat" +} diff --git a/fixtures/config/package-json/dangling.js b/fixtures/config/package-json/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/package-json/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/package-json/index.js b/fixtures/config/package-json/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/package-json/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/package-json/my-namespace.js b/fixtures/config/package-json/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/package-json/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/package-json/package.json b/fixtures/config/package-json/package.json new file mode 100644 index 000000000..8f42c7683 --- /dev/null +++ b/fixtures/config/package-json/package.json @@ -0,0 +1,6 @@ +{ + "name": "@fixtures/config-package-json", + "knip": { + "ignore": ["dangling.js"] + } +} diff --git a/fixtures/config/ts-async/dangling.js b/fixtures/config/ts-async/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/ts-async/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/ts-async/index.js b/fixtures/config/ts-async/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/ts-async/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/ts-async/knip.ts b/fixtures/config/ts-async/knip.ts new file mode 100644 index 000000000..0119c954d --- /dev/null +++ b/fixtures/config/ts-async/knip.ts @@ -0,0 +1,7 @@ +import type { KnipConfig } from '../../../src/index'; + +const config = async (): Promise => ({ + ignore: ['dangling.js'], +}); + +export default config; diff --git a/fixtures/config/ts-async/my-namespace.js b/fixtures/config/ts-async/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/ts-async/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/ts-async/package.json b/fixtures/config/ts-async/package.json new file mode 100644 index 000000000..051cbc0ac --- /dev/null +++ b/fixtures/config/ts-async/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-ts-async" +} diff --git a/fixtures/config/ts-async/tsconfig.json b/fixtures/config/ts-async/tsconfig.json new file mode 100644 index 000000000..7812f071f --- /dev/null +++ b/fixtures/config/ts-async/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "types": ["node"] + } +} diff --git a/fixtures/config/ts-flat/dangling.js b/fixtures/config/ts-flat/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/ts-flat/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/ts-flat/index.js b/fixtures/config/ts-flat/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/ts-flat/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/ts-flat/knip.ts b/fixtures/config/ts-flat/knip.ts new file mode 100644 index 000000000..94c91c00f --- /dev/null +++ b/fixtures/config/ts-flat/knip.ts @@ -0,0 +1,7 @@ +import type { KnipConfig } from '../../../src/index'; + +const config: KnipConfig = { + ignore: ['dangling.js'], +}; + +export default config; diff --git a/fixtures/config/ts-flat/my-namespace.js b/fixtures/config/ts-flat/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/ts-flat/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/ts-flat/package.json b/fixtures/config/ts-flat/package.json new file mode 100644 index 000000000..3eeb63370 --- /dev/null +++ b/fixtures/config/ts-flat/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-ts-flat" +} diff --git a/fixtures/config/ts-flat/tsconfig.json b/fixtures/config/ts-flat/tsconfig.json new file mode 100644 index 000000000..7812f071f --- /dev/null +++ b/fixtures/config/ts-flat/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "types": ["node"] + } +} diff --git a/fixtures/config/ts-function/dangling.js b/fixtures/config/ts-function/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/ts-function/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/ts-function/index.js b/fixtures/config/ts-function/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/ts-function/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/ts-function/knip.ts b/fixtures/config/ts-function/knip.ts new file mode 100644 index 000000000..733a52e60 --- /dev/null +++ b/fixtures/config/ts-function/knip.ts @@ -0,0 +1,7 @@ +import type { KnipConfig } from '../../../src/index'; + +const config = (): KnipConfig => ({ + ignore: ['dangling.js'], +}); + +export default config; diff --git a/fixtures/config/ts-function/my-namespace.js b/fixtures/config/ts-function/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/ts-function/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/ts-function/package.json b/fixtures/config/ts-function/package.json new file mode 100644 index 000000000..900473fcf --- /dev/null +++ b/fixtures/config/ts-function/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-ts-function" +} diff --git a/fixtures/config/ts-function/tsconfig.json b/fixtures/config/ts-function/tsconfig.json new file mode 100644 index 000000000..7812f071f --- /dev/null +++ b/fixtures/config/ts-function/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "types": ["node"] + } +} diff --git a/fixtures/config/yaml/dangling.js b/fixtures/config/yaml/dangling.js new file mode 100644 index 000000000..da3e9a39d --- /dev/null +++ b/fixtures/config/yaml/dangling.js @@ -0,0 +1 @@ +export const unusedFile = true; diff --git a/fixtures/config/yaml/index.js b/fixtures/config/yaml/index.js new file mode 100644 index 000000000..b2da10dcc --- /dev/null +++ b/fixtures/config/yaml/index.js @@ -0,0 +1,3 @@ +import * as MyNamespace from './my-namespace'; + +export const b = MyNamespace.y; diff --git a/fixtures/config/yaml/knip.yaml b/fixtures/config/yaml/knip.yaml new file mode 100644 index 000000000..3adadc659 --- /dev/null +++ b/fixtures/config/yaml/knip.yaml @@ -0,0 +1,2 @@ +ignore: + - 'dangling.js' \ No newline at end of file diff --git a/fixtures/config/yaml/my-namespace.js b/fixtures/config/yaml/my-namespace.js new file mode 100644 index 000000000..185fbbd06 --- /dev/null +++ b/fixtures/config/yaml/my-namespace.js @@ -0,0 +1,2 @@ +const x = 1; +export const y = () => x; diff --git a/fixtures/config/yaml/package.json b/fixtures/config/yaml/package.json new file mode 100644 index 000000000..54523052e --- /dev/null +++ b/fixtures/config/yaml/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/config-yaml" +} diff --git a/src/ConfigurationChief.ts b/src/ConfigurationChief.ts index 109b6f565..ab0c99cab 100644 --- a/src/ConfigurationChief.ts +++ b/src/ConfigurationChief.ts @@ -18,6 +18,7 @@ import { getKeysByValue } from './util/object.js'; import { join, relative, toPosix } from './util/path.js'; import { normalizePluginConfig, toCamelCase } from './util/plugin.js'; import { _require } from './util/require.js'; +import { unwrapFunction } from './util/unwrapFunction.js'; import { byPathDepth } from './util/workspace.js'; import type { SyncCompilers, AsyncCompilers } from './types/compilers.js'; import type { @@ -150,7 +151,9 @@ export class ConfigurationChief { throw new ConfigurationError(`Unable to find ${rawConfigArg} or package.json#knip`); } - this.rawConfig = this.resolvedConfigFilePath ? await _load(this.resolvedConfigFilePath) : manifest.knip; + this.rawConfig = this.resolvedConfigFilePath + ? await this.loadResolvedConfigurationFile(this.resolvedConfigFilePath) + : manifest.knip; // Have to partition compiler functions before Zod touches them const parsedConfig = this.rawConfig ? ConfigurationValidator.parse(partitionCompilers(this.rawConfig)) : {}; @@ -159,6 +162,15 @@ export class ConfigurationChief { await this.setWorkspaces(); } + private async loadResolvedConfigurationFile(configPath: string) { + const loadedValue = await _load(configPath); + try { + return await unwrapFunction(loadedValue); + } catch (e) { + throw new ConfigurationError(`Error running the function from ${configPath}`); + } + } + public getCompilers(): [SyncCompilers, AsyncCompilers] { return [this.config.syncCompilers, this.config.asyncCompilers]; } diff --git a/src/util/unwrapFunction.ts b/src/util/unwrapFunction.ts new file mode 100644 index 000000000..7044613f4 --- /dev/null +++ b/src/util/unwrapFunction.ts @@ -0,0 +1,13 @@ +import { debugLogObject } from './debug.js'; + +export const unwrapFunction = async (possibleFunction: unknown) => { + if (typeof possibleFunction === 'function') { + try { + return await possibleFunction(); + } catch (error) { + debugLogObject('*', 'Error executing function:', error); + throw error; + } + } + return possibleFunction; +}; diff --git a/test/cli-preprocessor.test.ts b/test/cli-preprocessor.test.ts index 1af72ace8..b93bfc3d6 100644 --- a/test/cli-preprocessor.test.ts +++ b/test/cli-preprocessor.test.ts @@ -1,18 +1,11 @@ import assert from 'node:assert/strict'; -import { execSync } from 'node:child_process'; import test from 'node:test'; import { resolve } from '../src/util/path.js'; +import { execFactory } from './helpers/execKnip.js'; const cwd = resolve('fixtures/cli-preprocessor'); -const exec = (command: string) => { - try { - const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); - return output.toString().trim(); - } catch { - console.error(`Error during execution of command: ${command}`); - } -}; +const exec = execFactory(cwd); test('knip --preprocessor ./index.js', () => { assert.equal(exec('knip --preprocessor ./index.js'), 'hi from js preprocessor'); diff --git a/test/cli-reporter.test.ts b/test/cli-reporter.test.ts index 47351e11d..ac4b12aa7 100644 --- a/test/cli-reporter.test.ts +++ b/test/cli-reporter.test.ts @@ -1,14 +1,11 @@ import assert from 'node:assert/strict'; -import { execSync } from 'node:child_process'; import test from 'node:test'; import { resolve } from '../src/util/path.js'; +import { execFactory } from './helpers/execKnip.js'; const cwd = resolve('fixtures/cli-reporter'); -const exec = (command: string) => { - const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); - return output.toString().trim(); -}; +const exec = execFactory(cwd); test('knip --reporter ./index.js', () => { assert.equal(exec('knip --reporter ./index.js'), 'hi from js reporter'); diff --git a/test/cli.test.ts b/test/cli.test.ts index 49b19f89f..fa3bc3b18 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,16 +1,13 @@ import assert from 'node:assert/strict'; -import { execSync } from 'node:child_process'; import test from 'node:test'; import { helpText } from '../src/util/cli-arguments.js'; import { resolve } from '../src/util/path.js'; import { version } from '../src/version.js'; +import { execFactory } from './helpers/execKnip.js'; const cwd = resolve('fixtures/cli'); -const exec = (command: string) => { - const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); - return output.toString().trim(); -}; +const exec = execFactory(cwd); test('knip --version', () => { assert.equal(exec('knip --version'), version); diff --git a/test/config/js-async.test.ts b/test/config/js-async.test.ts new file mode 100644 index 000000000..c369e74ea --- /dev/null +++ b/test/config/js-async.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/js-async'); + +const exec = execFactory(cwd); + +test('Support loading js async function for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/js-flat.test.ts b/test/config/js-flat.test.ts new file mode 100644 index 000000000..f40bbb049 --- /dev/null +++ b/test/config/js-flat.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/js-flat'); + +const exec = execFactory(cwd); + +test('Support loading js object files for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/json.test.ts b/test/config/json.test.ts new file mode 100644 index 000000000..48a04d7a5 --- /dev/null +++ b/test/config/json.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/json'); + +const exec = execFactory(cwd); + +test('Support loading json files for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/mjs-async.test.ts b/test/config/mjs-async.test.ts new file mode 100644 index 000000000..1e519c68e --- /dev/null +++ b/test/config/mjs-async.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/mjs-async'); + +const exec = execFactory(cwd); + +test('Support loading mjs async function files for configuration', async () => { + assert.equal(exec('knip -c knip.mjs'), ''); +}); diff --git a/test/config/mjs-flat.test.ts b/test/config/mjs-flat.test.ts new file mode 100644 index 000000000..64ce3d259 --- /dev/null +++ b/test/config/mjs-flat.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/mjs-flat'); + +const exec = execFactory(cwd); + +test('Support loading mjs object files for configuration', async () => { + assert.equal(exec('knip -c knip.mjs'), ''); +}); diff --git a/test/config/package-json.test.ts b/test/config/package-json.test.ts new file mode 100644 index 000000000..68438d181 --- /dev/null +++ b/test/config/package-json.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/package-json'); + +const exec = execFactory(cwd); + +test('Support loading package.json for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/ts-async.test.ts b/test/config/ts-async.test.ts new file mode 100644 index 000000000..935d6dda6 --- /dev/null +++ b/test/config/ts-async.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/ts-async'); + +const exec = execFactory(cwd); + +test('Support loading ts async function for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/ts-flat.test.ts b/test/config/ts-flat.test.ts new file mode 100644 index 000000000..21cc9dddd --- /dev/null +++ b/test/config/ts-flat.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/ts-flat'); + +const exec = execFactory(cwd); + +test('Support loading ts object files for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/ts-function.test.ts b/test/config/ts-function.test.ts new file mode 100644 index 000000000..5af0096d2 --- /dev/null +++ b/test/config/ts-function.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/ts-function'); + +const exec = execFactory(cwd); + +test('Support loading ts function for configuration', async () => { + assert.equal(exec('knip'), ''); +}); diff --git a/test/config/yaml.test.ts b/test/config/yaml.test.ts new file mode 100644 index 000000000..28041c045 --- /dev/null +++ b/test/config/yaml.test.ts @@ -0,0 +1,12 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { resolve } from '../../src/util/path.js'; +import { execFactory } from '../helpers/execKnip.js'; + +const cwd = resolve('fixtures/config/yaml'); + +const exec = execFactory(cwd); + +test('Support loading yaml files for configuration', async () => { + assert.equal(exec('knip -c knip.yaml'), ''); +}); diff --git a/test/helpers/execKnip.ts b/test/helpers/execKnip.ts new file mode 100644 index 000000000..50ec04298 --- /dev/null +++ b/test/helpers/execKnip.ts @@ -0,0 +1,20 @@ +import { execSync } from 'node:child_process'; +// eslint-disable-next-line n/no-restricted-import +import { resolve } from 'node:path'; + +const cliPath = resolve('dist/cli.js'); + +export const execFactory = (cwd: string) => { + return (command: string) => { + try { + const output = execSync(command.replace(/^knip/, `node ${cliPath}`), { cwd }); + return output.toString().trim(); + } catch (error) { + if (error instanceof Error && 'stdout' in error && Buffer.isBuffer(error['stdout'])) { + return error['stdout'].toString(); + } + // Unknown error + throw error; + } + }; +};