better-firebase-functions icon indicating copy to clipboard operation
better-firebase-functions copied to clipboard

Completed funcNameFromRelPathDefault

Open LeadDreamer opened this issue 4 years ago • 5 comments

One issue, one style; both resolved by simplifying code: Issue: the last replace, because it was a value not a regex, only replaced the first instance

Style: the string was already split; why .join(sep) only to .replace(`/${sep}/g, "-") later?

SO: split, then join("-"), then no need to replace.

Ran what tests I could (apparently don't have the full sample/test environment), but also tested "in the field" (emulator and deploy).

export function funcNameFromRelPathDefault(relPath: string): string {
  const relPathArray = relPath.split(sep); /* ? */
  const fileName = relPathArray.pop(); /* ? */
  const relDirPathFunctionNameChunk = relPathArray
    .map((pathFragment) => camelCase(pathFragment))
    .join("-");
  const fileNameFunctionNameChunk = camelCase(fileName!.split(".")[0]);
  const funcName = relDirPathFunctionNameChunk
    ? `${relDirPathFunctionNameChunk}-${fileNameFunctionNameChunk}`
    : fileNameFunctionNameChunk;
  return funcName;
}
```

LeadDreamer avatar Nov 23 '20 01:11 LeadDreamer

Runs successfully in emulation; have yet to deploy properly. Mis-read logs ealier.

LeadDreamer avatar Nov 23 '20 02:11 LeadDreamer

Deploy successful. Issue was unrelated (exact format of firebase-admin initializeApp - I use a centralized wrapper module for all firebase operations). Pull request valid.

LeadDreamer avatar Nov 24 '20 20:11 LeadDreamer

This is the only PR which I havent been able to merge yet. Please check if this change still applies to the latest version.

george43g avatar May 04 '22 04:05 george43g

So: I ended up converting to JS, since this was the only TS portion of my codebase. BUT I also both simplified it, and detected if the actual filename was "index.{wev}" - tossing that part if it was. I also did NOT reassemble with {sep} only to split it and rejoin it again later. If you never use "index.js" under a named directory, then my addition is not relevant. So I ended up:

function funcNameFromRelPathDefault(relPath) {
  if (!relPath) return ""; //short circuit

  let relPathArray = relPath.split(sep); /* ? */
  //check the last element for "index", and remove it if it is
  let filename = relPathArray.pop().toLowerCase().split(".").shift();
  if (filename === "index") {
    filename = relPathArray.pop().toLowerCase().split(".").shift();
  }
  relPathArray.push(filename);
  //camelcase all the elements, and join with "-"
  const funcName = relPathArray.map((element) => camelCase(element)).join("-");

  return funcName;
}

LeadDreamer avatar May 09 '22 18:05 LeadDreamer

This still appears to be an issue for me using the latest release. Here's what I need to do in order to get it working:

const { exportFunctions } = require('better-firebase-functions')
const camelCase = require('camelcase')
const { sep } = require('path')

exportFunctions({
  __filename,
  exports,
  funcNameFromRelPath: relPath => {
    const relPathArray = relPath.split(sep)
    const fileName = relPathArray.pop()
    const relDirPathFunctionNameChunk = relPathArray
      .map(pathFragment => camelCase(pathFragment))
      .join(sep)
    const fileNameFunctionNameChunk = camelCase((fileName || '').split('.')[0])
    const funcName = relDirPathFunctionNameChunk
      ? `${relDirPathFunctionNameChunk}${sep}${fileNameFunctionNameChunk}`
      : fileNameFunctionNameChunk

    return funcName.split(sep).join('-')
  }
})

Without that, I receive the following error:

... function name(s) can only contain letters, numbers, hyphens, and not exceed 62 characters in length

hursey013 avatar May 13 '22 14:05 hursey013