Skip to content

Commit

Permalink
feat: add listFiles method (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel authored Feb 18, 2024
1 parent f32d20a commit 0b78815
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ deno run npm:poku --include='./a,./b'

## Documentation

> Documentation in Progress 🧑🏻‍🔧
> Website in Progress 🧑🏻‍🔧
>
> Initially, the documentation is based on **Node.js** usage, but you can use all the options normally for both **Bun** and **Deno**.
### `poku(string | string[])`
### `poku(targetDirs: string | string[])`

#### Include directories

Expand All @@ -174,11 +174,11 @@ npx poku --include='./targetDirA,./targetDirB'

---

### `poku(string | string[], configs: Configs)`
### `poku(targetDirs: string | string[], configs?: Configs)`

#### `filter: RexExp`

By default, **Poku** searches for _`*.test.*`_ files, but you can customize it using the `filter` option.
By default, **Poku** searches for _`.test.`_ files, but you can customize it using the `filter` option.

> Filter by path using **Regex** to match only the files that should be performed.
Expand Down Expand Up @@ -375,6 +375,12 @@ npx poku --include='...' --exclude='some-file-or-dir|other-file-or-dir'

---

## Documentation in Progress...
### `listFiles(targetDir: string, configs?: ListFilesConfigs)`

> 🧑🏻‍🎓 Soon documenting all options and **Poku**'s usage variations.
Returns all files in a directory, independent of their depth.

```ts
listFiles('some-dir');
```

- You can use the `filter` and `exclude` options, as well as they are for **`poku`** method.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/@types/poku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Configs as GetFileOptions } from './get-files.ts';
import type { Configs as ListFilesConfigs } from './list-files.js';

export type Configs = {
/**
Expand Down Expand Up @@ -34,4 +34,4 @@ export type Configs = {
* @default false
*/
parallel?: boolean;
} & GetFileOptions;
} & ListFilesConfigs;
2 changes: 1 addition & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node

import { escapeRegExp } from '../modules/get-files.js';
import { escapeRegExp } from '../modules/list-files.js';
import { getArg, hasArg } from '../helpers/get-arg.js';
import { poku } from '../index.js';

Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { poku } from './modules/poku.js';
export { exit } from './modules/exit.js';
export type { Code } from './@types/code.js';
export type { Configs } from './@types/poku.js';
export { publicListFiles as listFiles } from './modules/list-files.js';
export type { Code } from './@types/code.ts';
export type { Configs } from './@types/poku.ts';
export type { Configs as ListFilesConfigs } from './@types/list-files.ts';
10 changes: 7 additions & 3 deletions src/modules/get-files.ts → src/modules/list-files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import type { Configs } from '../@types/get-files.ts';
import type { Configs } from '../@types/list-files.js';

export const escapeRegExp = (string: string) =>
string.replace(/[.*+?^${}()[\]\\]/g, '\\$&');
Expand All @@ -10,7 +10,7 @@ const envFilter = process.env.FILTER?.trim()
? new RegExp(escapeRegExp(process.env.FILTER), 'i')
: null;

export const getFiles = (
export const listFiles = (
dirPath: string,
files: string[] = [],
configs?: Configs
Expand All @@ -35,9 +35,13 @@ export const getFiles = (

if (exclude && exclude.some((regex) => regex.test(fullPath))) continue;

if (fs.statSync(fullPath).isDirectory()) getFiles(fullPath, files, configs);
if (fs.statSync(fullPath).isDirectory())
listFiles(fullPath, files, configs);
else if (filter.test(fullPath)) files.push(fullPath);
}

return files;
};

export const publicListFiles = (targetDir: string, configs?: Configs) =>
listFiles(targetDir, [], configs);
6 changes: 3 additions & 3 deletions src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EOL } from 'node:os';
import path from 'node:path';
import { runner } from '../helpers/runner.js';
import { indentation } from '../helpers/indentation.js';
import { getFiles } from '../modules/get-files.js';
import { listFiles } from '../modules/list-files.js';
import { hr } from '../helpers/hr.js';
import { format } from '../helpers/format.js';
import { runTestFile } from './run-test-file.js';
Expand All @@ -17,7 +17,7 @@ export const runTests = async (
const cwd = process.cwd();
const testDir = path.join(cwd, dir);
const currentDir = path.relative(cwd, testDir);
const files = getFiles(testDir, undefined, configs);
const files = listFiles(testDir, undefined, configs);
const totalTests = files.length;
const showLogs = !isQuiet(configs);

Expand Down Expand Up @@ -60,7 +60,7 @@ export const runTestsParallel = async (
): Promise<boolean> => {
const cwd = process.cwd();
const testDir = path.join(cwd, dir);
const files = getFiles(testDir, undefined, configs);
const files = listFiles(testDir, undefined, configs);
const showLogs = !isQuiet(configs);

const promises = files.map(async (filePath) => {
Expand Down
1 change: 1 addition & 0 deletions test/integration/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import * as index from '../../src/index.js';

assert.ok(index.poku);
assert.ok(index.exit);
assert.ok(index.listFiles);
4 changes: 2 additions & 2 deletions tools/compatibility/deno.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { promises as fs } from 'node:fs';
import { getFiles } from '../../src/modules/get-files.ts';
import { listFiles } from '../../src/modules/list-files.ts';

const ensureDenoCompatibility = async (path: string) => {
const files = getFiles(path, [], {
const files = listFiles(path, [], {
filter: /\.(m)?(t)?s$/,
});

Expand Down
4 changes: 2 additions & 2 deletions tools/compatibility/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { promises as fs } from 'node:fs';
import { getFiles } from '../../src/modules/get-files.js';
import { listFiles } from '../../src/modules/list-files.js';

const ensureNodeCompatibility = async (path: string) => {
const files = getFiles(path, [], {
const files = listFiles(path, [], {
filter: /\.(m)?(j|t)?s$/,
});

Expand Down

0 comments on commit 0b78815

Please sign in to comment.