src/index.js
/**
 * Implement server methods
 * @param {string} dir The directory to implement methods from
 */
Implement server methods
@param {string} dir The directory to implement methods from
Implement server methods @param { string } dir The directory to implement methods from
async function implementMethods(dir) {
    const files = readdirSync(dir)
        .filter((fileName) => !fileName.startsWith("_")) 
    const methodsToImplement = files.filter((fileName) => statSync(dir + fileName).isFile())
    const folders = files.filter((fileName) => !methodsToImplement.includes(fileName))
    for (const folder of folders) implementMethods(join(dir, `${folder}/`))
    for (const methodName of methodsToImplement) {
        try {
            const method = require(join(dir, methodName))
            if (method !== undefined && typeof method.path === "string" && typeof method.handler === "function") {
                methods.push({
                    name: methodName,
                    path: new RegExp(method.path), 
                    handler: method.handler
                })
            } else throw new Error("Method does not export path or handler properties")
        } catch (err) {
            console.error(`Failed to implement method from ${join(dir, methodName)} due to an error:`, err)
        }
    }
}
async function implementMethods ( dir ) { const files = readdirSync ( dir ) . filter ( ( fileName ) => ! fileName . startsWith ( "_" ) ) const methodsToImplement = files . filter ( ( fileName ) => statSync ( dir + fileName ) . isFile ( ) ) const folders = files . filter ( ( fileName ) => ! methodsToImplement . includes ( fileName ) ) for ( const folder of folders ) implementMethods ( join ( dir , ` ${ folder } ` ) ) for ( const methodName of methodsToImplement ) { try { const method = require ( join ( dir , methodName ) ) if ( method !== undefined && typeof method . path === "string" && typeof method . handler === "function" ) { methods . push ( { name : methodName , path : new RegExp ( method . path ) , handler : method . handler } ) } else throw new Error ( "Method does not export path or handler properties" ) } catch ( err ) { console . error ( ` ${ join ( dir , methodName ) } ` , err ) } } }
Implement server methods
 @param {string} dir The directory to implement methods from
Implement server methods @param { string } dir The directory to implement methods from
// Get all files in the directory and filter out folders // Skip files that start with _ // Implement methods from folders recursively // Make sure requirements are met // eslint-disable-next-line global-require, import/no-dynamic-require // Should we handle normal strings?