Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to node parse args #847

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json",
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"files": {
"include": ["**/**"],
"ignore": [
Expand Down
104 changes: 51 additions & 53 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { Configs } from '../@types/poku.js';
import { escapeRegExp } from '../modules/helpers/list-files.js';
import { getArg, getPaths, hasArg, argToArray } from '../parsers/get-arg.js';
import { positionals, values } from '../parsers/get-arg.js';
import { states } from '../configs/files.js';
import { platformIsValid } from '../parsers/get-runtime.js';
import { format } from '../services/format.js';
Expand All @@ -12,72 +12,73 @@ import { poku } from '../modules/essentials/poku.js';
import { Write } from '../services/write.js';
import { getConfigs } from '../parsers/options.js';

const argToArray = (
argValue: string | undefined,
): string[] | undefined => {
if (argValue === undefined) return undefined;
if (!argValue) return [];

return argValue
.split(',')
.map((a) => a.trim())
.filter((a) => a);
};

(async () => {
if (hasArg('version') || hasArg('v', '-')) {
if (values.version) {
const { VERSION } = require('../configs/poku.js');

Write.log(VERSION);
return;
}

if (hasArg('help') || hasArg('h', '-')) {
if (values.help) {
const { help } = require('./help.js');

help();

return;
}

const enforce = hasArg('enforce') || hasArg('x', '-');
const configFile = getArg('config') || getArg('c', '-');
const enforce = values.enforce;
const configFile = typeof values.config === 'boolean' ? undefined : values.config;
const defaultConfigs = await getConfigs(configFile);
const dirs: string[] = (() => {
const dirs = ((): string[] => {
/* c8 ignore next 2 */ // Deprecated
const includeArg = getArg('include');
if (includeArg !== undefined) return includeArg.split(',');

return (
getPaths('-') ??
(defaultConfigs?.include
? Array.prototype.concat(defaultConfigs?.include)
: ['.'])
);
const includeArg = values.include;
if (typeof includeArg === 'string') return includeArg.split(',');
if (positionals.length > 0) return positionals;
if (defaultConfigs?.include) return Array.prototype.concat(defaultConfigs?.include);
return ['.'];
})();
const platform = getArg('platform');
const filter = getArg('filter') ?? defaultConfigs?.filter;
const exclude = getArg('exclude') ?? defaultConfigs?.exclude;
const killPort = getArg('killport');
const killRange = getArg('killrange');
const killPID = getArg('killpid');
const platform = values.platform;
const filter = values.filter ?? defaultConfigs?.filter;
const exclude = values.exclude ?? defaultConfigs?.exclude;
const killPort = typeof values.killport === 'boolean' ? undefined : values.killport;
const killRange = typeof values.killrange === 'boolean' ? undefined : values.killrange;
const killPID = typeof values.killpid === 'boolean' ? undefined : values.killpid;
/* c8 ignore start */ // Deno
const denoAllow = argToArray('denoallow') ?? defaultConfigs?.deno?.allow;
const denoDeny = argToArray('denodeny') ?? defaultConfigs?.deno?.deny;
const denoCJS =
getArg('denocjs')
?.split(',')
.map((a) => a.trim())
.filter((a) => a) ||
hasArg('denocjs') ||
defaultConfigs?.deno?.cjs;
const denoAllow = argToArray(typeof values.denoallow === 'boolean' ? undefined : values.denoallow) ?? defaultConfigs?.deno?.allow;
const denoDeny = argToArray(typeof values.denodeny === 'boolean' ? undefined : values.denodeny) ?? defaultConfigs?.deno?.deny;
const denoCJS = argToArray(typeof values.denocjs === 'boolean' ? undefined : values.denocjs) ?? defaultConfigs?.deno?.cjs;
/* c8 ignore stop */
const parallel =
hasArg('parallel') || hasArg('p', '-') || defaultConfigs?.parallel;
const quiet = hasArg('quiet') || hasArg('q', '-') || defaultConfigs?.quiet;
const debug = hasArg('debug') || hasArg('d', '-') || defaultConfigs?.debug;
const failFast = hasArg('failfast') || defaultConfigs?.failFast;
const watchMode = hasArg('watch') || hasArg('w', '-');
const hasEnvFile = hasArg('envfile');
const parallel = !!values.parallel || defaultConfigs?.parallel;
const quiet = !!values.quiet || defaultConfigs?.quiet;
const debug = !!values.debug || defaultConfigs?.debug;
const failFast = !!values.failfast || defaultConfigs?.failFast;
const watchMode = !!values.watch;
const hasEnvFile = values.envfile;
const concurrency = (() => {
if (!(parallel || defaultConfigs?.parallel)) return;

const value = Number(getArg('concurrency'));
const value = Number(values.concurrency);

return Number.isNaN(value) ? defaultConfigs?.concurrency : value;
})();

if (dirs.length === 1) states.isSinglePath = true;

if (hasArg('listfiles')) {
if (values.listfiles) {
const { listFiles } = require('../modules/helpers/list-files.js');

const files: string[] = [];
Expand Down Expand Up @@ -149,28 +150,25 @@ import { getConfigs } from '../parsers/options.js';
/* c8 ignore stop */

if (hasEnvFile || defaultConfigs?.envFile) {
const envFilePath = getArg('envfile') ?? defaultConfigs?.envFile;
const envFilePath = (typeof values.envfile === 'boolean' ? null : values.envfile) ?? defaultConfigs?.envFile;

tasks.push(envFile(envFilePath));
}

const options: Configs = {
/* c8 ignore next 11 */ // Varies Platform
platform: platformIsValid(platform)
? platform
: hasArg('node')
? 'node'
: hasArg('bun')
? 'bun'
: hasArg('deno')
? 'deno'
: platformIsValid(defaultConfigs?.platform)
? defaultConfigs?.platform
: undefined,
platform: (() => {
if (platformIsValid(platform)) return platform;
if (values.node) return 'node';
if (values.bun) return 'bun';
if (values.deno) return 'deno';
if (platformIsValid(defaultConfigs?.platform)) return defaultConfigs?.platform;
return undefined;
})(),
filter:
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : filter,
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : undefined,
exclude:
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : exclude,
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : undefined,
parallel,
concurrency,
quiet,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Write } from '../services/write.js';
import process from 'node:process';
import type { Configs } from '../@types/poku.js';
import { format } from '../services/format.js';
import { getArg } from '../parsers/get-arg.js';
import { values } from '../parsers/get-arg.js';
import { fileResults } from '../configs/files.js';
import { availableParallelism } from '../polyfills/os.js';

Expand All @@ -14,7 +14,7 @@ export const startWatch = async (dirs: string[], options: Configs) => {

const watchers: Set<Watcher> = new Set();
const executing = new Set<string>();
const interval = Number(getArg('watchinterval')) || 1500;
const interval = Number(values.watchinterval) || 1500;

const setIsRunning = (value: boolean) => {
isRunning = value;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/helpers/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
import { DockerCompose, DockerContainer } from '../../services/container.js';

/** A minimal API to assist tests that require containers or tests that run inside containers using a **Dockerfile**. */
const dockerfile = (configs: DockerfileConfigs) => new DockerContainer(configs);
const dockerfile = (configs: DockerfileConfigs): DockerContainer => new DockerContainer(configs);

/** A minimal API to assist tests that require containers or tests that run inside containers using a **docker-compose.yml**. */
const compose = (configs: DockerComposeConfigs) => new DockerCompose(configs);
const compose = (configs: DockerComposeConfigs): DockerCompose => new DockerCompose(configs);

export const docker = { dockerfile, compose };
16 changes: 8 additions & 8 deletions src/modules/helpers/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ export const beforeEach = (
): Control => {
options?.immediate && callback();

each.before.cb = () => {
each.before.cb = (): unknown => {
if (each.before.status) return callback();
};

const pause = () => {
const pause = (): void => {
each.before.status = false;
};

const continueFunc = () => {
const continueFunc = (): void => {
each.before.status = true;
};

const reset = () => {
const reset = (): void => {
each.before.cb = undefined;
};

Expand All @@ -61,19 +61,19 @@ export const beforeEach = (
* ```
*/
export const afterEach = (callback: () => unknown): Control => {
each.after.cb = () => {
each.after.cb = (): unknown => {
if (each.after.status) return callback();
};

const pause = () => {
const pause = (): void => {
each.after.status = false;
};

const continueFunc = () => {
const continueFunc = (): void => {
each.after.status = true;
};

const reset = () => {
const reset = (): void => {
each.after.cb = undefined;
};

Expand Down
4 changes: 2 additions & 2 deletions src/modules/helpers/list-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const sanitizePath = (input: string, ensureTarget?: boolean): string => {
: sanitizedPath;
};

export const isFile = async (fullPath: string) =>
export const isFile = async (fullPath: string): Promise<boolean> =>
(await fsStat(fullPath)).isFile();

export const escapeRegExp = (string: string) =>
export const escapeRegExp = (string: string): string =>
string.replace(regex.safeRegExp, '\\$&');

const envFilter = env.FILTER?.trim()
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseResultType } from '../../parsers/assert.js';
import { Write } from '../../services/write.js';

/** By default **Poku** only shows outputs generated from itself. This helper allows you to use an alternative to `console.log` with **Poku**. */
export const log = (...args: unknown[]) => {
export const log = (...args: unknown[]): void => {
const parsedMessages = args
.map((arg) => parseResultType(arg))
.join(' ')
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exit, env } from 'node:process';
import { Write } from '../../services/write.js';
import { format } from '../../services/format.js';

export const skip = (message = 'Skipping') => {
export const skip = (message = 'Skipping'): void => {
const isPoku = typeof env?.FILE === 'string' && env?.FILE.length > 0;
const FILE = env.FILE;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ export type {
export type { Configs as ListFilesConfigs } from '../@types/list-files.js';

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