Skip to content

Commit

Permalink
feat: support for config files (js and cjs) (#560)
Browse files Browse the repository at this point in the history
* feat: support for config files (`js` and `cjs`)

* docs: add config documentation and example
  • Loading branch information
wellwelwel committed Jul 20, 2024
1 parent 1ef2bac commit 368f396
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 69 deletions.
5 changes: 5 additions & 0 deletions fixtures/config-files/custom-js-file/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
debug: true,
};
3 changes: 0 additions & 3 deletions fixtures/config-files/json-no-ext/.pokurc

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/config-files/json/poku.json

This file was deleted.

18 changes: 0 additions & 18 deletions fixtures/config-files/jsonc-no-ext/.pokurc

This file was deleted.

10 changes: 0 additions & 10 deletions fixtures/config-files/jsonc/poku.jsonc

This file was deleted.

Empty file.
5 changes: 5 additions & 0 deletions fixtures/config-files/poku-config-cjs/poku.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
debug: true,
};
File renamed without changes.
5 changes: 5 additions & 0 deletions fixtures/config-files/poku-config-js/poku.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
debug: true,
};
5 changes: 5 additions & 0 deletions src/@types/poku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ export type ConfigFile = {
filter?: string;
exclude?: string;
} & Omit<Configs, 'beforeEach' | 'afterEach' | 'noExit' | 'filter' | 'exclude'>;

export type ConfigModuleFile = {
filter?: RegExp;
exclude?: RegExp;
} & Omit<ConfigFile, 'filter' | 'exclude'>;
6 changes: 4 additions & 2 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ import { getConfigs } from '../parsers/options.js';
: platformIsValid(defaultConfigs?.platform)
? defaultConfigs?.platform
: undefined,
filter: filter ? new RegExp(escapeRegExp(filter)) : undefined,
exclude: exclude ? new RegExp(escapeRegExp(exclude)) : undefined,
filter:
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : filter,
exclude:
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : exclude,
parallel,
concurrency,
quiet,
Expand Down
7 changes: 7 additions & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ConfigModuleFile } from '../@types/poku.js';

export { poku } from './essentials/poku.js';
export { assert } from './essentials/assert.js';
export { strict } from './essentials/strict.js';
Expand All @@ -19,6 +21,11 @@ export { getPIDs } from './helpers/get-pids.js';
export { exit } from './helpers/exit.js';
export { log } from './helpers/log.js';
export { listFiles } from './helpers/list-files.js';

/** 🐷 Auxiliary function to define the `poku` configurations */
export const defineConfig = (options: ConfigModuleFile): ConfigModuleFile =>
options;

export type { Code } from '../@types/code.js';
export type { Configs } from '../@types/poku.js';
export type {
Expand Down
21 changes: 16 additions & 5 deletions src/parsers/options.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
/* c8 ignore next */ // Types
import type { ConfigFile } from '../@types/poku.js';
import type { ConfigFile, ConfigModuleFile } from '../@types/poku.js';
import { cwd } from 'node:process';
import { join } from 'node:path';
import { normalize, join } from 'node:path';
import { readFile } from '../polyfills/fs.js';
import { JSONC } from '../polyfills/jsonc.js';

const processCWD = cwd();

/* c8 ignore next */ // ?
export const getConfigs = async (customPath?: string): Promise<ConfigFile> => {
export const getConfigs = async (
customPath?: string
): Promise<ConfigFile | ConfigModuleFile> => {
const expectedFiles = customPath
? [customPath]
: ['poku.json', 'poku.jsonc', '.pokurc.json', '.pokurc.jsonc', '.pokurc'];
: new Set([
'poku.config.js',
'poku.config.cjs',
'.pokurc.json',
'.pokurc.jsonc',
]);

for (const file of expectedFiles) {
const filePath = join(processCWD, file);

try {
const configsFile = await readFile(filePath, 'utf-8');
if (filePath.endsWith('.js') || filePath.endsWith('.cjs')) {
/* c8 ignore next */ // ?
return (await import(normalize(filePath))) as ConfigModuleFile;
}

const configsFile = await readFile(filePath, 'utf-8');
return JSONC.parse<ConfigFile>(configsFile);
} catch {}
}
Expand Down
43 changes: 18 additions & 25 deletions test/e2e/config-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,64 @@ if (isProduction) {
}

describe('Test Runtimes/Platforms + Extensions', async () => {
await it('JSONC', async () => {
await it('.pokurc.jsonc', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/jsonc',
});

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('JSONC (no extension)', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/jsonc-no-ext',
cwd: 'fixtures/config-files/jsonc-rc',
});

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('JSONC (RC)', async () => {
await it('.pokurc.json', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/jsonc-rc',
cwd: 'fixtures/config-files/json-rc',
});

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('JSON', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/json',
});
await it('Custom (JSON)', async () => {
const output = await inspectCLI(
'npx tsx ../../../src/bin/index.ts --config=custom.json',
{
cwd: 'fixtures/config-files/custom-file',
}
);

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('JSON (no extension)', async () => {
await it('poku.config.js', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/json-no-ext',
cwd: 'fixtures/config-files/poku-config-js',
});

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('JSON (RC)', async () => {
await it('poku.config.cjs', async () => {
const output = await inspectCLI('npx tsx ../../../src/bin/index.ts', {
cwd: 'fixtures/config-files/json-rc',
cwd: 'fixtures/config-files/poku-config-cjs',
});

assert.strictEqual(output.exitCode, 0, 'Exit Code needs to be 0');
assert(/PASS › 1/.test(output.stdout), 'CLI needs to pass 1');
assert(/debug/.test(output.stdout), 'CLI needs to pass able "debug"');
});

await it('Custom', async () => {
await it('Custom (JS)', async () => {
const output = await inspectCLI(
'npx tsx ../../../src/bin/index.ts --config=custom.json',
'npx tsx ../../../src/bin/index.ts --config=custom.js',
{
cwd: 'fixtures/config-files/custom-file',
cwd: 'fixtures/config-files/custom-js-file',
}
);

Expand Down
36 changes: 33 additions & 3 deletions website/docs/documentation/poku/config-files.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ sidebar_position: 2

By default, **Poku** comes with the most common usage pre-set, but you can configure it as you want.

:::danger
Unreleased _(work in progress)_.
:::

## JavaScript

Create a `poku.js` (or `poku.cjs` when using `"type": "module"` in your _package.json_) in your project's root directory, for example:

```js
const { defineConfig } = require('poku');

module.exports = defineConfig({
include: ['.'],
parallel: false,
debug: false,
filter: /\.(test.|.spec)\./,
exclude: /.bak$/,
failFast: false,
concurrency: 0, // No limit
quiet: false,
platform: 'node', // "node", "bun" and "deno"
deno: {
allow: ['run', 'env', 'read', 'hrtime', 'net'],
deny: [], // Same as allow
cjs: ['.js', '.cjs'], // specific extensions
// "cjs": true // all extensions
// "cjs": false // no polyfill
},
});
```

## JSON and JSONC

Create a `poku.json` (or `poku.jsonc`) in your project's root directory, for example:
Expand Down Expand Up @@ -46,11 +77,10 @@ Create a `poku.json` (or `poku.jsonc`) in your project's root directory, for exa

> In priority order.
- `poku.json`
- `poku.jsonc`
- `poku.config.js`,
- `poku.config.cjs`,
- `.pokurc.json`
- `.pokurc.jsonc`
- `.pokurc`

:::note

Expand Down

0 comments on commit 368f396

Please sign in to comment.