favicons icon indicating copy to clipboard operation
favicons copied to clipboard

feat: allow defining cwd paths

Open paul-vd opened this issue 2 years ago • 3 comments

Why

When you programmatically try to run the favicons inside a node application, you might have the favicons script in subfolders, this will allow you to pass the process.cwd() to the options and prefix any source paths with the cwd folder. for example

import favicons from "favions"

favicons("/public/favicon.png", {
   path:"/favicons",
   manifestMaskable: "/public/images/favicon-mask.png",
   cwd: process.cwd()
})

Here is an example of running this from a different folder with node:

image

paul-vd avatar Feb 14 '23 18:02 paul-vd

why not to join before the invocation?

andy128k avatar Feb 14 '23 20:02 andy128k

So in our use case, we have a framework which is consumed by other developers, they can configure the settings from their relative directory, and we then consume it under our installed package. We are currently doing that manually, but it could lead to issues, as we can not know which icons they have configured, here is an example of what we are currently doing

const withRootPath = (src: string) => {
  return path.join(process.cwd(), src);
};

// path might be a URL or Buffer, which we don't want to modify.
const isRelativePath = (input: any): input is string => {
  return typeof input === "string" && input.startsWith("/");
};

const extractFaviconsOptions = (
  { source, ...options } = {} as Required<PWAConfig>
) => {
  if (options?.manifestMaskable && isRelativePath(options.manifestMaskable)) {
    options.manifestMaskable = withRootPath(options.manifestMaskable);
  }

  if (options.shortcuts) {
    options.shortcuts = options.shortcuts.map((shortcut) => {
      if (isRelativePath(shortcut.icon)) {
        return {
          ...shortcut,
          icon: withRootPath(shortcut.icon),
        };
      }
      return shortcut;
    });
  }

  return options;
};

let source = userConfig.source
if(isRelativePath(source)){
  source = withRootPath(source);
}
const faviconOptions = extractFaviconsOptions(userConfig);

paul-vd avatar Feb 15 '23 06:02 paul-vd

Honestly, I don't see anything wrong with your current approach.

andy128k avatar Feb 15 '23 18:02 andy128k