Skip to content

Commit

Permalink
feat(cli): show paths and options when using --debug (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel authored Jun 10, 2024
1 parent a6facf0 commit 1c0da5c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 93 deletions.
90 changes: 52 additions & 38 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,53 @@

import { escapeRegExp } from '../modules/list-files.js';
import {
// getAllArgs,
getArg,
getLastParam,
hasArg,
getSubArg,
argToArray,
} from '../helpers/get-arg.js';
import { kill, poku } from '../index.js';
import { poku } from '../modules/poku.js';
import { kill } from '../modules/processes.js';
import { platformIsValid } from '../helpers/get-runtime.js';
import { format } from '../helpers/format.js';
import { write } from '../helpers/logs.js';

// Argument with values
const dirs =
(hasArg('include')
? getArg('include')?.split(',')
: getLastParam()?.split(',')) || [];
const platform = getArg('platform');
const filter = getArg('filter');
const exclude = getArg('exclude');
const killPort = getArg('kill-port');
const killRange = getArg('kill-range');
const killPID = getArg('kill-pid');
const concurrency = Number(getArg('concurrency')) || undefined;
const denoAllow = getSubArg('deno-allow');
const denoDeny = getSubArg('deno-deny');
const denoCJS = getArg('deno-cjs')?.split(',') || hasArg('deno-cjs');

// Multiple arguments with values or not
// TODO (Custom Args)
// const args = getAllArgs('arg');

// Argument exists
const parallel = hasArg('parallel');
const quiet = hasArg('quiet');
const debug = hasArg('debug');
const failFast = hasArg('fail-fast');

if (hasArg('log-success'))
write(
`The flag ${format.bold('--log-success')} is deprecated. Use ${format.bold('--debug')} instead.`
);
import type { Configs } from '../@types/poku.js';
import { hr } from '../helpers/hr.js';

(async () => {
const dirs = (() => {
const includeArg = getArg('include');
if (includeArg !== undefined) return includeArg.split(',');

const lastParam = getLastParam();
if (lastParam !== undefined) return lastParam.split(',');

return ['.'];
})();

const platform = getArg('platform');
const filter = getArg('filter');
const exclude = getArg('exclude');
const killPort = getArg('kill-port');
const killRange = getArg('kill-range');
const killPID = getArg('kill-pid');
const denoAllow = argToArray('deno-allow');
const denoDeny = argToArray('deno-deny');
const denoCJS =
getArg('deno-cjs')
?.split(',')
.map((a) => a.trim())
.filter((a) => a) || hasArg('deno-cjs');

const parallel = hasArg('parallel');
const quiet = hasArg('quiet');
const debug = hasArg('debug');
const failFast = hasArg('fail-fast');

const concurrency = parallel
? Number(getArg('concurrency')) || undefined
: undefined;

if (killPort) {
const ports = killPort.split(',').map(Number);

Expand All @@ -72,7 +76,7 @@ if (hasArg('log-success'))
await kill.pid(PIDs);
}

await poku(dirs, {
const options: Configs = {
platform: platformIsValid(platform) ? platform : undefined,
filter: filter ? new RegExp(escapeRegExp(filter)) : undefined,
exclude: exclude ? new RegExp(escapeRegExp(exclude)) : undefined,
Expand All @@ -81,14 +85,24 @@ if (hasArg('log-success'))
debug,
failFast,
concurrency,
// TODO (Custom Args)
// arguments: args.length > 0 ? args : undefined,
deno: {
allow: denoAllow,
deny: denoDeny,
cjs: denoCJS,
},
});
};

if (debug) {
hr();
write(`${format.bg(104, 'Debug Enabled')}\n`);
write(`${format.italic(format.info('…'))} ${format.bold('Paths')}`);
console.table(dirs);
write('\n');
write(`${format.italic(format.info('…'))} ${format.bold('Options')}`);
console.dir(options, { depth: null, colors: true });
}

poku(dirs, options);
})();

/* c8 ignore stop */
75 changes: 20 additions & 55 deletions src/helpers/get-arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,12 @@ const [, , ...processArgs] = process.argv;
* ```
*/
export const getArg = (arg: string, prefix = '--'): string | undefined => {
const mountArg = processArgs.find((a) => a.startsWith(`${prefix}${arg}=`));
if (!mountArg) return undefined;
const argPattern = `${prefix}${arg}=`;
const argValue = processArgs.find((a) => a.startsWith(argPattern));

return mountArg.split('=')?.[1].replace(/''|""/, '');
};
if (!argValue) return undefined;

/**
* Parses all arguments of an argument value.
*
* ---
*
* CLI arguments examples:
*
* ```sh
* command --arg='--sub=some' # ['--sub=some']
* command --arg='--sub=some, --sub2' # ['--sub=some', '--sub2']
* ```
*/
export const getSubArg = (arg: string, prefix = '--') => {
if (hasArg(arg) && !getArg(arg)?.[1]) return [];

return processArgs
.find((a) => a.startsWith(`${prefix}${arg}=`))
?.split(`--${arg}=`)[1]
.split(',')
.map((a) => a.trim())
.filter((a) => a && !/''|""/.test(a));
return argValue.slice(argPattern.length).replace(/''|""/, '');
};

/**
Expand All @@ -58,8 +37,11 @@ export const getSubArg = (arg: string, prefix = '--') => {
* command # false
* ```
*/
export const hasArg = (arg: string, prefix = '--'): boolean =>
processArgs.some((a) => a.startsWith(`${prefix}${arg}`));
export const hasArg = (arg: string, prefix = '--'): boolean => {
const argPattern = `${prefix}${arg}`;

return processArgs.some((a) => a.startsWith(argPattern));
};

/**
* Gets the last param/value.
Expand All @@ -81,36 +63,19 @@ export const getLastParam = (prefix = '--'): string | undefined => {
return lastArg;
};

// TODO (Custom Args)
// export const getAllArgs = (arg: string, prefix = '--'): string[] => {
// return processArgs
// .filter((a) => a.startsWith(`${prefix}${arg}=`) || a === `${prefix}${arg}`)
// .map((a) => {
// const [key, ...value] = a.split('=');
// return value.length > 0 ? value.join('=') : key;
// });
// };
export const argToArray = (arg: string, prefix = '--') => {
const hasArgument = hasArg(arg);
if (!hasArgument) return undefined;

// TODO (Custom Args)
// export const setArgs = (
// args: (string | Record<string, string>)[],
// options?: { prefix: string }
// ): string[] => {
// const customArgs: string[] = [];
// const prefix = options?.prefix || '';
const argValue = getArg(arg, prefix);

// args.forEach((arg) => {
// if (!Array.isArray(arg) && typeof arg === 'object') {
// for (const key in arg) {
// customArgs.push(`${prefix}${key}=${arg[key]}`);
// }
if (hasArgument && !argValue) return [];
if (!argValue) return undefined;

// return;
// }

// customArgs.push(`${prefix}${arg}`);
// });
return argValue
.split(',')
.map((a) => a.trim())
.filter((a) => a);
};

// return customArgs;
// };
/* c8 ignore stop */

0 comments on commit 1c0da5c

Please sign in to comment.