Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

1.2 KB
Raw Blame
import fs from 'fs/promises';

export default async function combineFiles(files: string[], output: string) {
  const contents = await Promise.all(
    files.map(async (file) => {
      if (file.toLowerCase().endsWith('.css')) {
        const fileContent = await fs.readFile(file, 'utf-8');
        return `window.addEventListener('DOMContentLoaded', (_event) => {
        const css = ${JSON.stringify(fileContent)};
        if (typeof window.__PAKE_INJECT_STYLE__ === 'function') {
          window.__PAKE_INJECT_STYLE__(css);
        } else {
          const style = document.createElement('style');
          style.textContent = css;
          document.head.appendChild(style);
        }
      });`;
      }

      const fileContent = await fs.readFile(file);
      // Keep the closing `});` on its own line. If the injected file ends in a
      // line comment without a trailing newline, appending ` });` on the same
      // line would comment it out and break the wrapper (mirrors the .css
      // branch above, which already closes on a separate line).
      return (
        "window.addEventListener('DOMContentLoaded', (_event) => {\n" +
        fileContent +
        '\n});'
      );
    }),
  );
  await fs.writeFile(output, contents.join('\n'));
  return files;
}