Skip to content

Commit

Permalink
fix(n4s): make enforce chaining work
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 73b28a1 commit 4751584
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages/*/index.js
packages/*/*.js
packages/*/umd/
packages/*/es/
packages/*/cjs/
packages/*/*.tgz

Expand Down
68 changes: 30 additions & 38 deletions packages/n4s/src/n4s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ import throwError from 'throwError';
import { DropFirst } from 'utilityTypes';

import { isEmpty } from 'isEmpty';
import type {
TRuleReturn,
TRuleDetailedResult,
TLazyRuleMethods,
} from 'ruleReturn';
import { baseRules, compounds, TRule, TRuleValue, TArgs } from 'runtimeRules';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import {
baseRules,
compounds,
getRule,
TRule,
TRuleValue,
TArgs,
} from 'runtimeRules';
import { transformResult } from 'transformResult';

const rules: typeof baseRules &
Record<string, (...args: TArgs) => TRuleReturn> = {
...baseRules,
};

function EnforceBase(value: TRuleValue): TEaegerRules {
const proxy = new Proxy({} as TEaegerRules, {
get: (target, ruleName: string) => {
// @ts-ignore - this is actually fine
const rule = rules[ruleName] || compounds[ruleName];
get: (_, ruleName: string) => {
const rule = getRule(ruleName);
if (rule) {
return (...args: TArgs) => {
const transformedResult = transformResult(
Expand All @@ -38,44 +35,42 @@ function EnforceBase(value: TRuleValue): TEaegerRules {
throw transformedResult.message;
}
}
return proxy;
};
}

return target[ruleName];
},
});

return proxy;
}

const enforce = new Proxy(EnforceBase as TEnforce, {
get: (target: TEnforce, key: string) => {
const registeredRules: Array<(value: TRuleValue) => TRuleDetailedResult> =
[];
get: (_: TEnforce, key: string) => {
const registeredRules: TRegisteredRules = [];

if (key === 'extend') {
return function extend(customRules: TRule) {
Object.assign(rules, customRules);
Object.assign(baseRules, customRules);
};
}

// @ts-ignore - this is actually fine
if (!rules[key] || !compounds[key]) {
return target[key];
if (!getRule(key)) {
return;
}

return addRegisteredRule(key);

function addRegisteredRule(ruleName: string) {
return (...args: TArgs) => {
// @ts-ignore - this is actually fine
const rule = rules[ruleName] || compounds[ruleName];
const rule = getRule(ruleName);

registeredRules.push((value: TRuleValue) =>
transformResult(rule(value, ...args), ruleName, value, ...args)
);

const proxy: TEnforce = new Proxy({} as TEnforce, {
get: (target, key: string) => {
if (rules[key]) {
get: (_, key: string) => {
if (getRule(key)) {
return addRegisteredRule(key);
}

Expand All @@ -90,44 +85,41 @@ const enforce = new Proxy(EnforceBase as TEnforce, {
}

if (key === 'test') {
// @ts-ignore need to fix this
return (value: TRuleValue) => proxy.run(value).pass;
}

return target[key];
},
});
return proxy;
};
}

return addRegisteredRule(key);
},
});

export default enforce;

type TRegisteredRules = Array<(value: TRuleValue) => TRuleDetailedResult>;

type TEaegerRules = {
[P in keyof typeof compounds]: (
...args: DropFirst<Parameters<typeof compounds[P]>>
) => TEaegerRules;
} &
{
[P in keyof typeof rules]: (
...args: DropFirst<Parameters<typeof rules[P]>>
[P in keyof typeof baseRules]: (
...args: DropFirst<Parameters<typeof baseRules[P]>>
) => TEaegerRules;
};

type TLazyRules = {
[P in keyof typeof compounds]: (
...args: DropFirst<Parameters<typeof compounds[P]>>
...args: DropFirst<Parameters<typeof compounds[P]>> | TArgs
) => TLazyRules & TLazyRuleMethods;
} &
{
[P in keyof typeof rules]: (
...args: DropFirst<Parameters<typeof rules[P]>>
[P in keyof typeof baseRules]: (
...args: DropFirst<Parameters<typeof baseRules[P]>> | TArgs
) => TLazyRules & TLazyRuleMethods;
};

type TEnforce = typeof EnforceBase &
TLazyRules & { extend: (customRules: TRule) => void };
TLazyRules & { extend: (customRules: TRule) => void } & TLazyRuleMethods;
7 changes: 6 additions & 1 deletion packages/n4s/src/runtime/runtimeRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ export type TRule = Record<string, TRuleBase>;

const baseRules = rules();

export { baseRules, compounds };
function getRule(ruleName: string) {
// @ts-ignore - this should actually be fine
return baseRules[ruleName] || compounds[ruleName];
}

export { baseRules, compounds, getRule };
33 changes: 10 additions & 23 deletions packages/vest/types/vest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,21 @@ declare const baseRules: {
shorterThanOrEquals: typeof shorterThanOrEquals;
startsWith: typeof startsWith;
};
declare const rules: typeof baseRules &
Record<string, (...args: TArgs) => TRuleReturn>;
declare function EnforceBase(value: TRuleValue): TEaegerRules;
declare const enforce: TEnforce;
type TEaegerRules = {
[P in keyof typeof compounds]: (
...args: DropFirst<Parameters<typeof compounds[P]>>
) => TEaegerRules;
} &
{
[P in keyof typeof rules]: (
...args: DropFirst<Parameters<typeof rules[P]>>
) => TEaegerRules;
};
[P in keyof typeof compounds]: (...args: DropFirst<Parameters<typeof compounds[P]>>) => TEaegerRules;
} & {
[P in keyof typeof baseRules]: (...args: DropFirst<Parameters<typeof baseRules[P]>>) => TEaegerRules;
};
type TLazyRules = {
[P in keyof typeof compounds]: (
...args: DropFirst<Parameters<typeof compounds[P]>>
) => TLazyRules & TLazyRuleMethods;
} &
{
[P in keyof typeof rules]: (
...args: DropFirst<Parameters<typeof rules[P]>>
) => TLazyRules & TLazyRuleMethods;
};
type TEnforce = typeof EnforceBase &
TLazyRules & {
[P in keyof typeof compounds]: (...args: DropFirst<Parameters<typeof compounds[P]>> | TArgs) => TLazyRules & TLazyRuleMethods;
} & {
[P in keyof typeof baseRules]: (...args: DropFirst<Parameters<typeof baseRules[P]>> | TArgs) => TLazyRules & TLazyRuleMethods;
};
type TEnforce = typeof EnforceBase & TLazyRules & {
extend: (customRules: TRule) => void;
};
} & TLazyRuleMethods;
/**
* Reads the testObjects list and gets full validation result from it.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/types/vest.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vx/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const { package, command, dry = false } = cli;
if (!commands[command]) {
throw new Error(`Command ${command} not found.`);
}

if (package) {
packageName.setPackageName(package);
}

// FIXME: is there a better way of doing this?
const options = argv.slice(
namedOptions.reduce((count, [option, increment]) => {
Expand Down
4 changes: 2 additions & 2 deletions vx/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const exec = require('vx/exec');

function build(packageName, { options }) {
if (packageName) {
exec([`yarn workspace ${packageName} vx buildPackage`, options]);
exec([`yarn workspace ${packageName} vx pack`, options]);
} else {
exec([`yarn workspaces run vx buildPackage`, options]);
exec([`yarn workspaces run vx pack`, options]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions vx/commands/buildPackage.js → vx/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const buildPackage = require('../scripts/build/buildPackage');

const packageName = require('vx/packageName');

function build(name = packageName(), { options }) {
function pack(name = packageName(), { options }) {
if (!name) {
throw new Error('buildPackage must be called with a package name!');
throw new Error('pack must be called with a package name!');
}

buildPackage(name, { options });
}

module.exports = build;
module.exports = pack;
1 change: 1 addition & 0 deletions vx/config/rollup/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = () => {
if (fs.existsSync(customConfigPath)) {
customConfig = require(customConfigPath);
}

return cleanupConfig(
customConfig?.(configs, {
getInputFile,
Expand Down
6 changes: 4 additions & 2 deletions vx/packageName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function packageName() {
const name = process.env.npm_package_name;
return process.env.VX_PACKAGE_NAME ?? process.env.npm_package_name;
};

return name;
module.exports.setPackageName = function (name) {
process.env.VX_PACKAGE_NAME = name;
};
3 changes: 3 additions & 0 deletions vx/scripts/build/buildPackage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fse = require('fs-extra');

const copyDist = require('./../release/steps/copyDist');
const writeMainTemplate = require('./writeMainTemplate');

const exec = require('vx/exec');
Expand All @@ -15,6 +16,8 @@ function buildPackage(name = packageName(), { options } = {}) {
exec([`rollup -c`, vxPath.ROLLUP_CONFIG_PATH, options]);

writeMainTemplate(packageName(), vxPath.packageDist(packageName()));

copyDist();
}

module.exports = buildPackage;
3 changes: 0 additions & 3 deletions vx/scripts/release/releasePackage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const build = require('./../build/buildPackage');
const genDiffData = require('./../release/genDiffData');
const getDiff = require('./../release/github/getDiff');
const copyDist = require('./../release/steps/copyDist');
const publishPackage = require('./../release/steps/publishPackage');
const setNextVersion = require('./../release/steps/setNextVersion');
const updateChangelog = require('./../release/steps/updateChangelog');
Expand Down Expand Up @@ -30,8 +29,6 @@ function releasePackage(packageName) {
updateChangelog(diffData);

publishPackage(diffData);

copyDist();
}

module.exports = releasePackage;
3 changes: 2 additions & 1 deletion vx/util/dryRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function setDryRun(dry) {
}

function isDryRun() {
return !!process.env[DRY_RUN_KEY];
// FIXME: Check why this is sometimes gets set to a string
return [true, 'true'].includes(process.env[DRY_RUN_KEY]);
}

function cliOpt() {
Expand Down

0 comments on commit 4751584

Please sign in to comment.