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

Refactoring "patch.ts" #108

Open
GoToLoop opened this issue Jun 9, 2023 · 1 comment
Open

Refactoring "patch.ts" #108

GoToLoop opened this issue Jun 9, 2023 · 1 comment

Comments

@GoToLoop
Copy link

GoToLoop commented Jun 9, 2023

Just letting you know I'm doing lotsa refactoring on "patch.ts".

Here's a preview of it:

import { readFileSync, writeFileSync } from 'node:fs';

const
    UTF8 = { encoding: 'utf8' } as const,
    WHAT = '(event?: object): void;';

function replace(path: string | URL, what: string, swap: string) {
    var original = '', altered = '', gotModified = false;

    try {
        original = readFileSync(path, UTF8);
        altered = original.replace(what, swap);
        gotModified = altered != original;
        writeFileSync(path, altered);
    } catch (err) { console.error(err); }

    if (!gotModified) console.error(
        `"${ what }"\nnot found in "${ path }" for:\n"${ swap }"`
    );

    return gotModified;
}

replace(
    'types/p5/index.d.ts',
    'export = p5;',
    `export default p5;

type p5Class = typeof p5;

declare global {
    var p5: p5Class;
}

`);

replace(
    'types/p5/index.d.ts',
    'constructor(sketch: (...args: any[]) => any, node?: HTMLElement);',
    `constructor(sketch?: (p: any) => void, node?: string | HTMLElement);

    readonly __proto__: typeof p5.prototype;

    readonly static VERSION: string;

    static instance: p5;

    canvas: HTMLCanvasElement;

    _renderer: p5.Renderer;
    _curElement: p5.Renderer;

    registerMethod(
        register: 'init' | 'pre' | 'post' | 'remove',
        callback: () => void
    ): void;

    registerPreloadMethod(fnName: PropertyKey, proto: object): void;
`);

replace(
    'types/p5/index.d.ts',
    'disableFriendlyErrors: boolean;',
    'static disableFriendlyErrors: boolean;'
);

Lemme know what you think. ^_^

@GoToLoop
Copy link
Author

GoToLoop commented Jun 10, 2023

Function replace() has a problem that the same file can be loaded & saved multiple times.
As an example, path 'types/p5/src/events/mouse.d.ts' is loaded & saved 7x within its for loop:

{
    const
        path = 'types/p5/src/events/mouse.d.ts',
        swapL = '(event?: ',
        swapR = 'Event): false | void;';

    for (const [ func, evt ] of [
        [ 'mouseMoved',    'Mouse' ],
        [ 'mouseDragged',  'Drag'  ],
        [ 'mousePressed',  'Mouse' ],
        [ 'mouseReleased', 'Mouse' ],
        [ 'mouseClicked',  'Mouse' ],
        [ 'doubleClicked', 'Mouse' ],
        [ 'mouseWheel',    'Wheel' ]
    ]) replace(path, func + WHAT, func + swapL + evt + swapR);
}

For such cases where multiple change calls are made for the same file path, I've split helper function replace() into 3 parts: replaceOpen(), replaceData() & replaceClose()

import { readFileSync, writeFileSync } from 'node:fs';

const
    UTF8 = { encoding: 'utf8' } as const,
    WHAT = '(event?: object): void;';

function replaceOpen(path: string | URL) {
    try {
        return readFileSync(path, UTF8);
    } catch (err) { return console.error(err), ''; }
}

function replaceData(content: string, what: string, swap: string) {
    const altered = content.replace(what, swap);

    if (altered == content) console.error(
        `"${ what }"\nnot found when attempting:\n"${ swap }"\n`
    );

    return altered;
}

function replaceClose(path: string | URL, content: string) {
    try {
        return writeFileSync(path, content), true;
    } catch (err) { return console.error(err), false; }
}

Compare the previous loop example w/ this 1 using the split replace() functions instead:

{
    const
        path = 'types/p5/src/events/mouse.d.ts',
        swapL = '(event?: ',
        swapR = 'Event): false | void;';

    let content = replaceOpen(path);

    if (content) for (const [ func, evt ] of [
        [ 'mouseMoved',    'Mouse' ],
        [ 'mouseDragged',  'Drag'  ],
        [ 'mousePressed',  'Mouse' ],
        [ 'mouseReleased', 'Mouse' ],
        [ 'mouseClicked',  'Mouse' ],
        [ 'doubleClicked', 'Mouse' ],
        [ 'mouseWheel',    'Wheel' ]
    ]) content = replaceData(content, func + WHAT, func + swapL + evt + swapR);

    content && replaceClose(path, content);
}

Now file path 'types/p5/src/events/mouse.d.ts' is loaded once, its content is changed 7 times, and then saved once at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant