main.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import "./style.css";
  2. import typescriptLogo from "./typescript.svg";
  3. import viteLogo from "/vite.svg";
  4. import { setupCounter } from "./counter.ts";
  5. // document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
  6. // <div>
  7. // <a href="https://vitejs.dev" target="_blank">
  8. // <img src="${viteLogo}" class="logo" alt="Vite logo" />
  9. // </a>
  10. // <a href="https://www.typescriptlang.org/" target="_blank">
  11. // <img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
  12. // </a>
  13. // <h1>Vite + TypeScript</h1>
  14. // <div class="card">
  15. // <button id="counter" type="button"></button>
  16. // </div>
  17. // <p class="read-the-docs">
  18. // Click on the Vite and TypeScript logos to learn more
  19. // </p>
  20. // </div>
  21. // `
  22. // setupCounter(document.querySelector<HTMLButtonElement>('#counter')!)
  23. // index.ts
  24. import fs from "fs/promises";
  25. import { log } from "console";
  26. const assetsPath = "./assets";
  27. const tsPath = "./tsFiles";
  28. async function main() {
  29. console.log(1111);
  30. const files = await fs.readdir(assetsPath);
  31. for (const file of files) {
  32. const filePath = path.join(assetsPath, file);
  33. const stat = await fs.stat(filePath);
  34. if (stat.isDirectory()) {
  35. console.log(`dir: ${filePath}`);
  36. } else if (stat.isFile() && ["svg", "png"].includes(path.extname(file))) {
  37. // 生成 ts files
  38. generateTsFile(filePath);
  39. }
  40. }
  41. }
  42. async function generateTsFile(filePath) {
  43. const fileContent = fs.readFile(filePath, "utf8");
  44. if (fileContent.includes("<svg")) {
  45. // svg文件内容
  46. const match = fileContent.match(/<svg[^>]*>(.*?)<\/svg>/s);
  47. if (match) {
  48. console.log(`icon ${filePath} created!`);
  49. }
  50. } else if (fileContent.includes(".png")) {
  51. // png文件内容
  52. const match = fileContent.match(/data:image\/png;,\s*base64\((.*?)\)/);
  53. if (match) {
  54. console.log(`icon ${filePath} created!`);
  55. }
  56. }
  57. }
  58. main().catch((error) => {
  59. console.error(error);
  60. });