UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

1 lines 4.77 kB
{"version":3,"file":"setup-init.cjs","sources":["../../../src/cli/commands/setup-init.ts"],"sourcesContent":["import fs from \"fs/promises\";\nimport type { namedTypes } from \"ast-types\";\nimport { parse } from \"recast\";\nimport { initFilePath, initJsxFilePath } from \"../consts\";\nimport { compareNodes } from \"../utils/compare-nodes\";\nimport type { Config } from \"../utils/create-config\";\n\n/**\n * Sets up the `.flowbite-react/init.tsx` file in the project.\n *\n * This function ensures the init.tsx file exists and is up-to-date with the current configuration.\n * It will create or update the file if needed.\n */\nexport async function setupInit(config: Config) {\n const content = `\n/* eslint-disable */\n// @ts-nocheck\n// biome-ignore-all lint: auto-generated file\n\n// This file is auto-generated by the flowbite-react CLI.\n// Do not edit this file directly.\n// Instead, edit the .flowbite-react/config.json file.\n\nimport { StoreInit } from \"flowbite-react/store/init\";\nimport React from \"react\";\n\nexport const CONFIG = {\n dark: ${config.dark},\n prefix: \"${config.prefix}\",\n version: ${config.version},\n};\n\nexport function ThemeInit() {\n return <StoreInit {...CONFIG} />;\n}\n\nThemeInit.displayName = \"ThemeInit\";\n`.trim();\n\n const targetPath = config.tsx ? initFilePath : initJsxFilePath;\n const oldPath = config.tsx ? initJsxFilePath : initFilePath;\n\n try {\n let currentContent = \"\";\n try {\n currentContent = await fs.readFile(targetPath, \"utf-8\");\n } catch {\n console.log(`Creating ${targetPath} file...`);\n setTimeout(() => fs.writeFile(targetPath, content), 10);\n }\n\n if (currentContent) {\n const currentAst = removeReactImport(parse(currentContent));\n const newAst = removeReactImport(parse(content));\n\n if (!compareNodes(currentAst.program, newAst.program)) {\n console.log(`Updating ${targetPath} file...`);\n setTimeout(() => fs.writeFile(targetPath, content), 10);\n }\n }\n\n try {\n await fs.access(oldPath);\n console.log(`Removing ${oldPath} file...`);\n await fs.unlink(oldPath);\n } catch {\n // noop\n }\n } catch (error) {\n console.error(`Failed to update ${targetPath}:`, error);\n }\n}\n\n/**\n * Removes `import React from \"react\"` from the AST\n */\nfunction removeReactImport(ast: namedTypes.File): namedTypes.File {\n if (ast?.program?.body) {\n ast.program.body = ast.program.body.filter(\n (node) =>\n !(\n node.type === \"ImportDeclaration\" &&\n \"value\" in node.source &&\n typeof node.source.value === \"string\" &&\n node.source.value === \"react\" &&\n node.specifiers?.[0]?.local?.name === \"React\"\n ),\n );\n }\n return ast;\n}\n"],"names":["initFilePath","initJsxFilePath","parse","compareNodes"],"mappings":";;;;;;;AAKO,eAAe,SAAS,CAAC,MAAM,EAAE;AACxC,EAAE,MAAM,OAAO,GAAG;AAClB;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC;AACtB,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC;AAC3B,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC;AAC5B;;AAEA;AACA;AACA;;AAEA;AACA,CAAC,CAAC,IAAI,EAAE;AACR,EAAE,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,GAAGA,mBAAY,GAAGC,sBAAe;AAChE,EAAE,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,GAAGA,sBAAe,GAAGD,mBAAY;AAC7D,EAAE,IAAI;AACN,IAAI,IAAI,cAAc,GAAG,EAAE;AAC3B,IAAI,IAAI;AACR,MAAM,cAAc,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;AAC7D,KAAK,CAAC,MAAM;AACZ,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;AACnD,MAAM,UAAU,CAAC,MAAM,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC;AAC7D;AACA,IAAI,IAAI,cAAc,EAAE;AACxB,MAAM,MAAM,UAAU,GAAG,iBAAiB,CAACE,YAAK,CAAC,cAAc,CAAC,CAAC;AACj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