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

feat: stable enforce values validation #905

Merged
merged 6 commits into from
Dec 23, 2024
Merged
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
51 changes: 0 additions & 51 deletions src/bin/enforce.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/bin/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const summary: [string, string][] = [
['--watchInterval', 'Set an interval for watch events.'],
];
const sortedSummary = summary.sort(([a], [b]) => a.localeCompare(b));
const largeEndPad = (() =>
Math.max(...summary.map(([start]) => start.length)))();
const largeEndPad = Math.max(...summary.map(([start]) => start.length));

const header = `
🐷 ${format(' Poku — CLI Usage ').bg('brightMagenta')}
Expand Down
84 changes: 37 additions & 47 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#! /usr/bin/env node

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 { states } from '../configs/files.js';
Expand All @@ -10,26 +8,25 @@ import { envFile } from '../modules/helpers/env.js';
import { poku } from '../modules/essentials/poku.js';
import { log, hr } from '../services/write.js';
import { getConfigs } from '../parsers/options.js';
import { GLOBAL, VERSION } from '../configs/poku.js';

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

log(VERSION);
return;
}

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

help();

require('./help.js').help();
return;
}

const enforce = hasArg('enforce') || hasArg('x', '-');
const configFile = getArg('config') || getArg('c', '-');
const defaultConfigs = await getConfigs(configFile);

GLOBAL.defaultConfigs = await getConfigs(configFile);
const { defaultConfigs } = GLOBAL;

const dirs: string[] =
getPaths('-') ??
(defaultConfigs?.include
Expand Down Expand Up @@ -98,13 +95,36 @@ import { getConfigs } from '../parsers/options.js';
return;
}

if (enforce) {
const { checkFlags } = require('./enforce.js');
GLOBAL.configFile = configFile;
GLOBAL.options = {
filter:
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : filter,
exclude:
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : exclude,
concurrency,
sequential,
quiet,
debug,
failFast,
deno: {
allow: denoAllow,
deny: denoDeny,
cjs: denoCJS,
},
noExit: watchMode,
beforeEach:
'beforeEach' in defaultConfigs ? defaultConfigs.beforeEach : undefined,
afterEach:
'afterEach' in defaultConfigs ? defaultConfigs.afterEach : undefined,
};

const tasks: Promise<unknown>[] = [];

checkFlags();
if (hasEnvFile || defaultConfigs?.envFile) {
GLOBAL.envFile = getArg('envFile') ?? defaultConfigs?.envFile ?? '.env';
}

const tasks: Promise<unknown>[] = [];
if (enforce) require('../services/enforce.js').enforce();

/* c8 ignore start */ // Process-based
if (killPort || defaultConfigs?.kill?.port) {
Expand Down Expand Up @@ -135,33 +155,7 @@ import { getConfigs } from '../parsers/options.js';
}
/* c8 ignore stop */

if (hasEnvFile || defaultConfigs?.envFile) {
const envFilePath = getArg('envFile') ?? defaultConfigs?.envFile;

tasks.push(envFile(envFilePath));
}

const options: Configs = {
filter:
typeof filter === 'string' ? new RegExp(escapeRegExp(filter)) : filter,
exclude:
typeof exclude === 'string' ? new RegExp(escapeRegExp(exclude)) : exclude,
concurrency,
sequential,
quiet,
debug,
failFast,
deno: {
allow: denoAllow,
deny: denoDeny,
cjs: denoCJS,
},
noExit: watchMode,
beforeEach:
'beforeEach' in defaultConfigs ? defaultConfigs.beforeEach : undefined,
afterEach:
'afterEach' in defaultConfigs ? defaultConfigs.afterEach : undefined,
};
GLOBAL.envFile && tasks.push(envFile(GLOBAL.envFile));

if (debug || defaultConfigs?.debug) {
hr();
Expand All @@ -170,15 +164,11 @@ import { getConfigs } from '../parsers/options.js';
console.table(dirs);
log('\n');
log(`${format('…').info().italic()} ${format('Options').bold()}`);
console.dir(options, { depth: null, colors: true });
console.dir(GLOBAL.options, { depth: null, colors: true });
}

await Promise.all(tasks);
await poku(dirs, options);

if (watchMode) {
const { startWatch } = require('./watch.js');
await poku(dirs, GLOBAL.options);

await startWatch(dirs, options);
}
if (watchMode) await require('./watch.js').startWatch(dirs, GLOBAL.options);
})();
4 changes: 2 additions & 2 deletions src/configs/files.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { FileResults } from '../@types/list-files.js';
import type { FinalResults, States } from '../@types/poku.js';

export const states = {} as States;
export const states = Object.create(null) as States;

export const fileResults: FileResults = {
success: new Map(),
fail: new Map(),
};

export const finalResults = {} as FinalResults;
export const finalResults = Object.create(null) as FinalResults;
5 changes: 5 additions & 0 deletions src/configs/poku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { env, cwd } from 'node:process';
import type { ConfigFile, ConfigJSONFile, Configs } from '../@types/poku.js';

export const results = {
success: 0,
Expand All @@ -16,4 +17,8 @@ export const GLOBAL = {
runAsOnly: false,
isPoku: typeof env?.POKU_FILE === 'string' && env?.POKU_FILE.length > 0,
FILE: env.POKU_FILE,
options: Object.create(null) as Configs,
configFile: undefined as string | undefined,
defaultConfigs: Object.create(null) as ConfigFile | ConfigJSONFile,
envFile: undefined as string | undefined,
};
3 changes: 2 additions & 1 deletion src/modules/helpers/describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export async function describeBase(
if (title) {
indentation.hasDescribe = true;

const { background, icon } = options ?? {};
const { background, icon } =
options ?? (Object.create(null) as DescribeOptions);
const message = `${cb ? format('◌').dim() : (icon ?? '☰')} ${cb ? format(title).dim() : format(title).bold()}`;
const noBackground = !background;

Expand Down
9 changes: 7 additions & 2 deletions src/parsers/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { normalize, join } from 'node:path';
import { readFile } from 'node:fs/promises';
import { JSONC } from '../polyfills/jsonc.js';
import { GLOBAL } from '../configs/poku.js';
import { isWindows } from './get-runner.js';

export const getConfigs = async (
customPath?: string
Expand All @@ -18,16 +19,20 @@ export const getConfigs = async (

for (const file of expectedFiles) {
const filePath = join(GLOBAL.cwd, file);
let path = '';

if (isWindows) path += 'file://';
path += normalize(filePath);

try {
if (filePath.endsWith('.js') || filePath.endsWith('.cjs'))
return require(`file://${normalize(filePath)}`);
return require(path);

const configsFile = await readFile(filePath, 'utf8');

return JSONC.parse<ConfigJSONFile>(configsFile);
} catch {}
}

return {};
return Object.create(null);
};
Loading
Loading